Deep Learning

Build Your Own Generative Adversarial Network (GAN) Using Pytorch

A simple step-by-step guide for generating faces with DCGANS by using PyTorch.

Muhammad Ahmad Bilal
Towards AI
Published in
7 min readJul 10, 2021

--

Generative Adversarial Networks is the most interesting idea in machine learning in last ten years

— Yann Lecun (Facebook AI Director)

GANs was first introduced in 2014 by Ian Goodfellow, a Ph.D. Student at the University of Montreal. The most common example of GANs is generating images. There is a website that contains faces of people that do not exist. This is one of the examples of what GANs can do. That is what we are going to build in this lesson.

Generative Adversarial Networks consists of two neural networks Generator and Discriminator competing with each other. I would be explaining each step in detail later in this lesson. If you’re completely unfamiliar with this topic. I would suggest you go through the following lessons first.

At the end of this lesson, you’ll be able to train and build your own generative adversarial network from scratch. So without further ado let’s dive in.

I have also built this generative network in Google Colab here. You can easily open Google Colabatory and easily follow along.

The Road Ahead

  • Step 0: Import Datasets
  • Step 1: Loading and Preprocessing of Images
  • Step 2: Define the Discriminator Algorithm
  • Step 3: Define the Generator Algorithm
  • Step 4: Write the training Algorithm
  • Step 5: Train the Model
  • Step 6: Test the Model

So Are you excited??? Let’s dive in!

Step 0: Import Datasets

The first step is to download and load the data into memory. So we’ll do that here. We’ll be using the CelebFaces Attributes Dataset (CelebA) to train your adversarial networks.

  • Download the dataset from here.
  • Unzip the dataset.
  • Clone this Github repository.

After doing that you can either open it in a colab environment or you can use your own pc to train the model.

Import the necessary libraries

It is always considered a good practice to import all the libraries you’d be using in the first block of the notebook.

Step 1: Loading And Preprocessing of Images

In this step, we’re going to preprocess the image data that we’ve downloaded in the previous section.

The following step would be taken

  1. Resize the images
  2. Convert it into tensors
  3. Load it into PyTorch Dataset
  4. Load it into PyTorch DataLoader

The size of images should be sufficiently small which would help in training the model faster. Tensors are basically NumPy array we’re just converting our images into NumPy array that is necessary for working in PyTorch.

Then we’re loading this transformed into a PyTorch Dataset. After that as we’ll be training our data into small batches. This data loader would provide the image data to our model at every iteration.

Then we would check the loaded image data by using NumPy and pyplot. This displaying helper function is provided in the notebook. After calling the function you’d get output like this.

Loaded from the Dataset

As the data is loaded. Now, we can preprocess the images.

Preprocessing of images

We would be using tanh activated generator in the training. The output of this generator is in the range of -1 to 1. We would need to rescale our images in that range too. So you’ll do just that.

This function would rescale any input image. we would be using this function later in the training.

Now we’re done with the boring preprocessing step.

Here comes the exciting part. Now we need to write code for our generator and discriminator neural networks.

Step 2: Define the Discriminator Algorithm

Discriminator Model Visuals. Courtesy

A discriminator is a neural network that would differentiate between the real and fake images. Both real images and the images generated by the generator would be given to it.

We will first define a helper function that is quite handy in creating the convolutional network layers.

This helper function is receiving the required arguments necessary to create any convolutional layer and returning a sequential container. Now we will use this helper function to create our own discriminator network.

Step 3: Define the Generator Algorithm

Generator Model Visuals. Courtesy

As you can see from the diagram, We give a gaussian or noise vector into the network and it outputs something in S. The “z” on the figure the noise, and G(z) on the right is the generated sample.

Same as the discriminator, we will first create a helper function for building our generator network as follows:

Now, it’s time to build the generator network!!

To help the model to converge faster we would initialize the weights of linear and convolutional layers. According to the research paper.

All weights were initialized from a zero-centered Normal distribution with standard deviation 0.02.

We will be defining a function for this purpose as follows:

Now we would initialize the hyperparameter and both networks as follows:

The output would be something like this:

Discriminator Loss:

For the discriminator, the total loss is the sum of the losses for real and fake images, d_loss = d_real_loss + d_fake_loss.

Remember that we want the discriminator to output 1 for real images and 0 for fake images, so we need to set up the losses to reflect that.

Source: DCGAN Research Paper

We would be defining two-loss functions. One will be a real loss and the other will be a fake loss as follows:

Generator Loss:

The generator loss will look similar only with flipped labels. The generator’s goal is to get the discriminator to think its generated images are real.

Source: DCGAN Research Paper

Now, it’s time to set the optimizers for our networks

I will be using Adam optimizer for our training. As it’s considered to be good for GAN’s. You can choose your own by reading this. The values of the hyperparameters are set according to this research paper. They have experimented with it and these are turned out to be the best!

Step 4: Write the training Algorithm

We have to write our training algorithm for our two neural networks. First, we need to initialize the noise vector. We would keep it fixed throughout the training process.

For discriminator:

We would first pass the real images through the discriminator network then we would calculate the real loss on it. Then, we would generate fake images and pass them through the discriminator network in order to calculate the fake loss.

After calculating both real and fake losses we would add them and take the optimizer step for training.

For Generator:

We would do the same for the training of the generator network. Just now after passing fake images through the discriminator, we would calculate the real loss on it. And then optimize our generator network.

Step 5: Train the Model

Now we would start training on 100 epochs :D

After training the graph of losses would look something like this.

We can see that the discriminator loss is quite smooth and converging to some specific number even after the 100 epoch. But for the Generator loss, the loss spiked up.

And we can see from the results below that after 60 epochs the generated images are distorted. So we can conclude that the epoch of 60 could be considered as an optimal training epoch.

As both losses are minimum at that point and the generator is generating some pretty good image!

Step 6: Test the Model

After 10 epochs:

After 20 epochs:

After 30 epochs:

After 40 epochs:

After 50 epochs:

After 60 epochs:

Conclusion:

We can see that training a Generative Adversarial Network doesn’t mean it would generate good images.

We can see from the results that from 40–60 epochs the generator has generated the images relatively better than the others.

You can try to change the optimizers, learning rate, and other hyperparameters to make it generate better images! Kudos to you for making it so far!

This entire notebook can be found here.

Here are my Github and Linkedin accounts. Feel Free to connect.

References

DCGAN Orignal Paper

Research Paper by Alec Radford & Luke Metz.

Research Paper by Hugo Berard

Udacity Deep Learning Nanodegree

--

--

Microsoft Learn Student Ambassador, Deep Learning Enthusiast, Non-fiction books lover, Nustian.