Path: blob/master/notebooks/misc/dcgan_fashion_tf.ipynb
1192 views
Deep convolutional generative adversarial networks (DCGAN)
This tutorial fits a DC-GAN to Fashion-MNIST. The code is based on https://www.tensorflow.org/beta/tutorials/generative/dcgan
Load and prepare the dataset
You will use the MNIST dataset to train the generator and the discriminator. The generator will generate handwritten digits resembling the MNIST data.
Create the models
Both the generator and discriminator are defined using the Keras Sequential API.
The Generator
The generator uses tf.keras.layers.Conv2DTranspose
(upsampling) layers to produce an image from a seed (random noise). Start with a Dense
layer that takes this seed as input, then upsample several times until you reach the desired image size of 28x28x1. Notice the tf.keras.layers.LeakyReLU
activation for each layer, except the output layer which uses tanh.
Use the (as yet untrained) generator to create an image.
The Discriminator
The discriminator is a CNN-based image classifier.
Use the (as yet untrained) discriminator to classify the generated images as real or fake. The model will be trained to output positive values for real images, and negative values for fake images.
Define the loss and optimizers
Define loss functions and optimizers for both models.
Discriminator loss
This method quantifies how well the discriminator is able to distinguish real images from fakes. It compares the discriminator's predictions on real images to an array of 1s, and the discriminator's predictions on fake (generated) images to an array of 0s.
Generator loss
The generator's loss quantifies how well it was able to trick the discriminator. Intuitively, if the generator is performing well, the discriminator will classify the fake images as real (or 1). Here, we will compare the discriminators decisions on the generated images to an array of 1s.
The discriminator and the generator optimizers are different since we will train two networks separately.
Save checkpoints
This notebook also demonstrates how to save and restore models, which can be helpful in case a long running training task is interrupted.
Define the training loop
The training loop begins with generator receiving a random seed as input. That seed is used to produce an image. The discriminator is then used to classify real images (drawn from the training set) and fakes images (produced by the generator). The loss is calculated for each of these models, and the gradients are used to update the generator and discriminator.
Train the model
Call the train()
method defined above to train the generator and discriminator simultaneously. Note, training GANs can be tricky. It's important that the generator and discriminator do not overpower each other (e.g., that they train at a similar rate).
At the beginning of the training, the generated images look like random noise. As training progresses, the generated digits will look increasingly real. After about 50 epochs, they resemble MNIST digits. This may take about one minute / epoch with the default settings on Colab.
Restore the latest checkpoint.