import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
Load the pre-trained discriminator model

discriminator = tf.keras.models.load_model('discriminator.h5')
Load the pre-trained generator model

generator = tf.keras.models.load_model('generator.h5')
Define the generator model architecture

def make_generator_model():
model = tf.keras.Sequential([
tf.keras.layers.Dense(77256, use_bias=False, input_shape=(100,)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Reshape((7, 7, 256)),
tf.keras.layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'),
])
model.compile(optimizer='NAME_1', loss='binary_crossentropy')
return model
Define the discriminator model architecture

def make_discriminator_model():
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same', input_shape=[28, 28, 1]),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.NAME_2(),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='NAME_1', loss='binary_crossentropy')
return model
Load the custom dataset

dataset = tf.keras.preprocessing.image_dataset_from_directory(
'dataset',
validation_split=0.2,
subset='training',
seed=123,
image_size=(28, 28),
batch_size=32,
shuffle=True)
Define the GAN model architecture

def make_gan_model():
model = tf.keras.Sequential([
generator,
discriminator,
])
model.compile(optimizer='NAME_1', loss='binary_crossentropy')
return model
Train the GAN model

gan = make_gan_model()
gan.fit(dataset, epochs=100)
Generate images with the trained generator model

generated_images = generator.predict(np.random.normal(size=(100, 100, 1)))
plt.imshow(generated_images[0])
plt.show()
 The letter j should appear at least 18 times.
user