Friendly Introduction to Generative Adversarial Networks GANs

Can you believe that those people do not exist?

Image of four photorealistic faces created by a generative adversarial
network.
Figure 1: Images generated by a GAN created by NVIDIA.

This website by Phillip Wang: thispersondoesnotexist.com shows a new image of a person each time you reload the page, and guess what! none of those people is actually real!

Or even more, you can visit the website thisxdoesnotexist.com to see horses, cats, and other objects that do not exist in reality. All those images are created by Generative Adversarial Networks (GANs).

This promising innovation in machine learning can achieve a high level of realism by generating high-quality fake images of people, animals, objects, synthetic speech, and text.

GANs are one type of generative model that creates new data examples that resemble the training data.

What is a generative model?

Generative models aim to learn the true data distribution of the training set in order to generate new data points with some variations.

[source] The generative model takes a random data vector and tries to modify it by learning the true distribution of the training dataset.

Different types of Generative models are used such as Autoregressive Models, Bayesian Network, Gaussian Mixture Model, Hidden Markov Model, Variational Autoencoders (VAE), and Generative Adversarial Networks (GAN).

What are GANs?

GANs are just one kind of generative model. It introduces a new concept called the discriminator.

A generative adversarial network (GAN) architecture consists of two pairs of neural networks competing against each other: the generator and the discriminator.

The generator model generates new instances and the discriminator model classifies whether generated examples are real, from the domain, or artificial, created by the generator model.

[source]

How do GANs work?

Both the generator and discriminator are neural networks.

The generator: The generator model takes as input a fixed-length random vector (noise) from a Gaussian distribution and creates a sample in the domain. These produced samples are utilized to train the discriminator with the positive instances from the training set.

The discriminator:  The discriminator learns to determine whether the data is real or fake from the generator.

The Training process of GANs:

The general process:

The generator and discriminator models are trained together. The generator creates a batch of samples, which, along with actual instances from the training set (the domain), are sent to the discriminator, which classifies them as real or artificial(fake).

The discriminator is then updated in the following round to improve its ability to distinguish between real and fake samples, and the generator is updated based on how effectively, or not, the produced samples deceived the discriminator.

When the discriminator correctly distinguishes between real and fake samples, no changes to model parameters are required (rewarded), but the generator is punished with substantial updates to model parameters.

Otherwise, When the generator tricks the discriminator, it is rewarded (no changes to the model parameters are required), but the discriminator is punished and its model parameters are modified.

In simpler words:

At the beginning of the training phase, the generator produces obvious fake images that the discriminator can detect easily.

After each iteration, the generator improves on generating better images and the discriminator becomes better at image classification.

Finally, the generator will succeed in fooling the discriminator that will classify fake images as real.

Example:

[source] The generator produces fake images of money and improves each time. Finally, it will completely fool the discriminator.

Loss function

A GAN has two loss functions: One for the generator training and one for the discriminator training.

The discriminator connects to two loss functions.

During the training of the discriminator, the generator does not train, Its weights remain constant while it produces examples for the discriminator to train on. The discriminator ignores the generator loss and just uses the discriminator loss.

During the generator training, we don’t want the discriminator to change; that’s why we use only the generator loss.

Training step by step:

GAN training proceeds in alternating periods:

  1. The discriminator trains for one or more epochs:
    1. The discriminator classifies both real data and fake data from the generator.
    2. The discriminator loss penalizes the discriminator for misclassifying a real instance as fake or a fake instance as real.
    3. The discriminator updates its weights through backpropagation from the discriminator loss through the discriminator network.
  2. The generator trains for one or more epochs.
    1. Sample random noise.
    2. generator network produces output from sampled random noise.
    3. discriminator network classifies the generated data as “Real” or “Fake”.
    4. generator loss punishes the generator for failing to fool the discriminator (Calculate loss from discriminator classification).
    5. Backpropagate through both the discriminator and generator to obtain gradients.
    6. Use gradients to change only the generator weights.
  3. Repeat steps 1 and 2 to continue to train the generator and discriminator networks.

GANs applications:

Some of GANs applications are:

  • Data augmentation (in case of insufficient labeled training data)
  • Image-to-image translation
  • Text-to-image translation
  • Super-resolution
  • Domain transfer

Evolution of GANs:

GANs architecture got evolved through the years; some of those new architectures are:

  • DC GAN 2015
  • Conditional GAN and Unconditional GAN (CGAN)
  • Least Square GAN(LSGAN) 
  • Auxiliary Classifier GAN(ACGAN)
  • Dual Video Discriminator GAN 
  • SRGAN 
  • Cycle GAN
  • Info GAN
  • Progressive Growing of GANs (PG GANS) 2017
  • large scale GAN

Here is a GAN application using python Google collab:

https://colab.research.google.com/github/zaidalyafeai/Notebooks/blob/master/BigGanEx.ipynb

Conclusion

In this post, we introduce the Generative Adversarial Networks model, its architecture, and training phase steps in simple words.

References:

https://developers.google.com/machine-learning/gan/training