DCGAN Tutorial — Generate Fake Celebrity image

A beginner-friendly tutorial on DCGAN with PyTorch to generate Fake celebrity images with CelebA dataset.

Aniket Maurya
Analytics Vidhya
5 min readNov 16, 2020

--

What is DCGAN?

DCGAN (Deep Convolutional Generative Adversarial Network) created by Alec Radford, Luke Metz and Soumith Chintala in 2016 to train Deep Generative Adversarial Networks. In the DCGAN paper, the authors trained the network that can generate fake images of celebrities and fake bedrooms.

The architecture consists of two networks — Generator and Discriminator. The Generator is the heart of GANs. It produces real looking fake images from random noise.

This article can be opened as Jupyter Notebook to train DCGAN on CelebA dataset to generate fake celebrity images.

Discriminator wants the real and fake image distributions to be as far as possible while the Generator wants to reduce the distance between the real and fake image distribution. In simple words, the Generator tries to fool the Discriminator by producing real looking images while the Discriminator tries to catch the fake images from the real ones.

Vector arithmetic for visual concepts. Source: DCGAN Paper

Training details from the paper

Preprocessing: Images are scaled to be in the range of tanh activation, [-1, 1]. The training was done with a mini-batch size of 128 and Adam optimizer with a learning rate of 0.0002. All the weights initialised with Normal distribution μ(0,0.02).

Authors guidelines:

  • All the pooling layers are replaced with strided convolutions in the discriminator and fractional strided convolution in the discriminator.
  • No fully-connected or pooling layers are used.
  • Batchnorm used in both Generator and Discriminator
  • ReLu activation is used for the generator for all the layers except the last layer which uses tanh
  • Discriminator uses LeakyReLu for all the layers

In this post, I will train a GAN to generate celebrity faces.

Generator

A Generator consists of Transposed Convolution, Batch Normalisation and activation function layer.

  • First, the random noise of size 100 will be reshaped to 100x1x1 (channel first in PyTorch).
  • It is passed through a Transposed CNN layer which upsamples the input Tensor.
  • Batch Normalisation is applied.
  • If the layer is not the last layer then ReLu activation is applied else Tanh.

First channel size is 1024 which is then decreased block by block to 3 for RGB image. Finally, we will get a 3x64x64 Tensor which will be our image.

Generator architecture from the DCGAN Paper

Discriminator

The architecture of a Discriminator is same as that of a normal image classification model. It contains Convolution layers, Activation layer and BatchNormalisation. In the DCGAN paper, strides are used instead of pooling to reduce the size of a kernel. Also, there is no Fully Connected layer in the network. Leaky ReLU with leak slope 0.2 is used.

The Discriminator wants to predict the fake images as fake and real images as real. On the other hand, the Generator wants to fool Discriminator into predicting the fake images produced by the Generator as real.

Source: deeplearning.ai GANs Specialisation

Source: deeplearning.ai GANs Specialisation

Define learning rate, z_dim (noise dimension), batch size and other configuration based on the paper.

Discriminator Loss

As the discriminator wants to increase the distance between Generated and Real distribution, we will train it to give high loss when the generated images are classified as real or when real images are classified as fake.

Generator Loss

The BCE loss for Generator will be high when it fails to fool the Discriminator. It will give high loss when the generated image is classified as fake by the discriminator. Note that the Generator never knows about real images.

References

[1.] Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks

[2.] Generative Adversarial Networks (GANs) Specialization

[3.] DCGAN Tutorial — PyTorch Official

I would highly recommend GANs Specialization on Coursera if you want to learn GANs in depth.

If you got some feedback please reach out to me at https://twitter.com/aniketmaurya

--

--

Aniket Maurya
Analytics Vidhya

ML Engineer at Lightning AI (PyTorch Lightning) ⚡️ ; Creator of Gradsflow ; https://aniketmaurya.com