How it works...

In step 1, we defined the shape of the input images and the number of channels. Since the images that were used were colourful, we specified the number of channels as 3, meaning RGB mode. We also specified the latent space dimension. In step 2, we constructed a generator network. The job of this generator is to map random normal vectors of shape latent_dim to images of shape (32, 32, 3). 

Use normal distribution to generate points from latent space rather than a uniform distribution for robust results.

We used a deep convolutional network as the generator network in our example. The layer_conv_2d_transpose() function is used to upsample the image data. We used tanh as the activation function in the last layer of the generator and Leaky Relu as the activation function for the hidden layers.

It is recommended to use strided convolutions rather than max-pooling while downsampling to avoid the risk of sparse gradients.

In the next step, we defined and compiled the discriminator network. It mapped images of shape (32, 32, 3) that were produced by the generator to a probability that indicates whether the generated image is real or fake. Since our generator network was a convnet, the discriminator was also a convolutional network. To induce randomness and make our GAN robust, we added dropout layers and random noise to the labels of the discriminator. 

In step 4, we froze the weights of the discriminator to make it non-trainable. In step 5, we configured and compiled the GAN network. This GAN network maps the images that have been generated by a generator to the discriminator's assessment of real and fake images. In the last step, we trained the GAN network. When training GANs, we need to train the discriminator so that it identifies the real and fake images accurately. The generator uses feedback from the discriminator to update its weights. We use the gradients of the generator's weights with respect to the loss of the gan model to train the generator. Finally, we saved the models and the generated images for a few batches.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset