Intuitive Explanation of GAN

Shobhit Srivastava
Nov 3 · 5 min read
Dumb architecture of Generative Adversarial Network; Source : Google Images

In this blog post we are going to learn about Generative Adversarial Network. This explanation is about to be short as to not bore you all with a lot of explanation and I can bet you of finding it intuitive in grasping the concept.

Introduction

  • Generative Adversarial Networks (GANs)
  • How GANs Work
  • GANs Process
  • Examples

Generative Adversarial Networks (GANs)

Generative Adversarial Networks are used to generate images that never existed before. They learn about the world (objects, animals and so forth) and create new versions of those images that never existed.

They have two components:

  • A Generator — this creates the images.
  • A Discriminator — this assesses the images and tells the generator if they are similar to what it has been trained on. These are based off real world examples.

When training the network, both the generator and discriminator start from scratch and learn together.

How GANs Work

G for Generative — this is a model that takes an input as a random noise singal and then outputs an image.

A for Adversarial — this is the discriminator, the opponent of the generator. This is capable of learning about objects, animals or other features specified. For example: if you supply it with pictures of dogs and non-dogs, it would be able to identify the difference between the two.

Using this example, once the discriminator has been trained, showing the discriminator a picture that isn’t a dog it will return a 0. Whereas, if you show it a dog it will return a 1.

N for Network — meaning the generator and discriminator are both neural networks.

How GAN’s Process run :

Step 1 — we input a random noise signal into the generator. The generator creates some images which is used for training the discriminator. We provide the discriminator with some features/images we want it to learn and the discriminator outputs probabilities. These probabilities can be rather high as the discriminator has only just started being trained. The values are then assessed and identified. The error is calculated and these are backpropagated through the discriminator, where the weights are updated.

Next we train the generator. We take the batch of images that it created and put them through the discriminator again. We do not include the feature images. The generator learns by tricking the discriminator into it outputting false positives.

The discriminator will provide an output of probabilities. The values are then assessed and compared to what they should have been. The error is calculated and backpropagated through the generator and the weights are updated.

Step 2 — This is the same as step 1 but the generator and discriminator are trained a little more. Through backpropagation the generator understands its mistakes and starts to make them more like the feature.

This is created through a Deconvolutional Neural Network.

Loss Function Employed in GAN :

As we can see, whole training algorithm for GAN is mentioned above.

We will get more of it with the following steps :

  1. First a sample of real image(according to the batch size) is selected form the bunch of the real images, taken as [REAL].
  2. Also, some random noised images are produced in the same number as of batch size as in step 1 as well, is taken as [FAKE].
  3. Now, we define labels for each of the above data[Real+ Fake] separately. Setting 1 for real images and 0 for fake images.
  4. Now the data from step 1 and step 2 are concatenated, along with labels in 3.
  5. Finally, using the above datasets we train the Discriminator for classification.
  6. Now Freezing the Discriminator’s weights, we pass a sample of previously fake generated data with labels as 1(as we want Discriminator to predict them as real) to the GAN model(Generator+ Discriminator) and error is calculated with loss function as in down image.
  7. Now the question arise, what values are there in loss function, okk.., let me clarify, as we have provided labels as 1 in step 6 but Discriminator will predict them as fake(0), Expected : 1, Obtained : 0. Hope you got the point.
  8. Now the same process is continued again from step 1, depending to the number of epochs.

Some interesting thing happens in the way, Discriminator tried to maximize the below loss function and at the same time Generator tried to minimize it. So this tail continues till the whole training process gets completed.

Ultimately, we see Generator goes on to produce some realistic images as that of Real one.

So, this is how GAN gets trained.

Examples

GANs can be used for the following:

  • Generating Images
  • Image Modification
  • Super Resolution
  • Assisting Artists
  • Photo-Realistic Images
  • Speech Generation
  • Face Ageing

This is the first article in continued series of GAN, next one will come with bunch of codes in Keras, so get tuned for the same.

That’s all for now. I hope that you have found this post of help. If you all have any doubts please do write about it in the comments section down below. Your response will encourage me to come up with the next one asap.

Thank you.

[Image Source : Google Images]

Shobhit Srivastava

Written by

Data Analyst

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade