create a machine learning model to generate images of cats in python

To create a machine learning model to generate images of cats, we can use a generative adversarial network (GAN). GAN is a class of deep learning neural network that can generate new data that is similar to the training data.

Here are the steps to create a GAN model to generate images of cats:

  1. Import the required libraries:
main.py
import tensorflow as tf
from tensorflow.keras.layers import Dense, Reshape, Flatten, Conv2D, Conv2DTranspose
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt
296 chars
8 lines
  1. Load the dataset:
main.py
(x_train, y_train), (_, _) = tf.keras.datasets.cifar10.load_data()
cat_images = x_train[y_train[:, 0] == 3]
cat_images = cat_images.reshape(cat_images.shape[0], 32, 32, 3).astype('float32')
cat_images = (cat_images - 127.5) / 127.5
232 chars
5 lines
  1. Define the generator model:
main.py
def build_generator(latent_dim):
    generator = Sequential()
    generator.add(Dense(4 * 4 * 256, input_dim=latent_dim))
    generator.add(Reshape((4, 4, 256)))
    generator.add(Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same', activation='relu'))
    generator.add(Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same', activation='relu'))
    generator.add(Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same', activation='relu'))
    generator.add(Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same', activation='relu'))
    generator.add(Conv2D(3, (3, 3), activation='tanh', padding='same'))
    generator.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))
    return generator
740 chars
12 lines
  1. Define the discriminator model:
main.py
def build_discriminator():
    discriminator = Sequential()
    discriminator.add(Conv2D(64, (3, 3), strides=(2, 2), padding='same', input_shape=(32, 32, 3), activation='relu'))
    discriminator.add(Conv2D(128, (3, 3), strides=(2, 2), padding='same', activation='relu'))
    discriminator.add(Conv2D(256, (3, 3), strides=(2, 2), padding='same', activation='relu'))
    discriminator.add(Flatten())
    discriminator.add(Dense(1, activation='sigmoid'))
    discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))
    return discriminator
571 chars
10 lines
  1. Combine the generator and discriminator to create the GAN model:
main.py
def build_gan(generator, discriminator):
    discriminator.trainable = False
    gan = Sequential()
    gan.add(generator)
    gan.add(discriminator)
    gan.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))
    return gan
248 chars
8 lines
  1. Train the GAN model:
main.py
latent_dim = 100
epochs = 10000
batch_size = 128
sample_period = 100

generator = build_generator(latent_dim)
discriminator = build_discriminator()
gan = build_gan(generator, discriminator)

for epoch in range(epochs):
    noise = np.random.normal(0, 1, size=(batch_size, latent_dim))
    fake_images = generator.predict(noise)
    real_images = cat_images[np.random.choice(cat_images.shape[0], size=batch_size, replace=False)]
    x = np.concatenate([real_images, fake_images])
    y = np.zeros(2 * batch_size)
    y[:batch_size] = 0.9  # one-sided label smoothing

    discriminator.trainable = True
    discriminator.train_on_batch(x, y)

    noise = np.random.normal(0, 1, size=(batch_size, latent_dim))
    y2 = np.ones(batch_size)
    discriminator.trainable = False
    gan.train_on_batch(noise, y2)

    if epoch % sample_period == 0:
        plt.imshow(fake_images[0])
        plt.title(f"Epoch: {epoch}")
        plt.axis('off')
        plt.show()
958 chars
31 lines

After the GAN model is trained, you can use the generator to generate new images of cats by providing random noise as input to the generator. The output of the generator will be a new image of a cat.

gistlibby LogSnag