Skip to content

Latest commit

 

History

History

cGAN_PyTorch

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Pytorch Implementation of Conditional GAN

Usage

$ python3 main.py --ndata 'cifar10' --epochs 100

NOTE: on Colab Notebook use following command:

!git clone link-to-repo
%run main.py --ndata 'cifar10' --epochs 100 

Contributed by:

References

  • Title: Conditional Generative Adversarial Nets
  • Authors: Mehdi Mirza, Simon Osindero
  • Link: https://arxiv.org/pdf/1411.1784.pdf
  • Tags: Neural Network, Generative Networks, GANs
  • Year: 2014

Summary

Introduction

Generative adversarial nets were recently introduced as a novel way to train a generative model. They consists of two ‘adversarial’ models: a generative model G that captures the data distribution, and a discriminative model D that estimates the probability that a sample came from the training data rather than G. Both G and D could be a non-linear mapping function, such as a multi-layer perceptron.

Minimax

Minimax is a decision rule for minimizing the possible loss for a worst case (maximum loss) scenario. The maximizer tries to get the highest score possible while the minimizer tries to do the opposite and get the lowest score possible.

Under the game theory perspective, GAN can be viewed as a game of two players: the discriminator D and the generator G. The discriminator tries to discriminate the generated (or fake) data and the real data, while the generator attempts to make the discriminator confusing by gradually generating the fake data that break into the real data.

Conditional GAN

Generative adversarial nets can be extended to a conditional model if both the generator and discriminator are conditioned on some extra information y. We can perform the conditioning by feeding y into the both the discriminator and generator as additional input layer. In cGAN (Conditional GAN), labels act as an extension to the latent space z to generate and discriminate images better. Its structure can be shown as:

GAN

To learn a generator distribution Pg over data data x conditoned on label y, the generator builds a mapping function from a prior noise distribution Pz(z) to data space as G(Z|Y). And the discriminator, D(X|Y), outputs a single scalar representing the probability that x came form training data rather than Pg.

G and D are both trained simultaneously: we adjust parameters for G to minimize log(1 − D(G(Z|Y)) and adjust parameters for D to minimize log D(X|Y), as if they are following the two-player min-max game with value function V(G, D):

Equation

One the intuition behing Condional GANs is that, a typical GAN would operate with a generator G(z) where z is a random vector and a discriminator D(G(z)), where as a conditional GAN would add additional information to both discriminator and generator eventually leading to a better performance.

Implementaion

We simply give conditioning input and prior noise as inputs to the MLP architecture. We use Sigmoid, Relu and Maxout activations in the layers of our MLP.

  • We train our model for 100 epoch
  • We use BCE loss(Binary Crossentropy loss) with a learning rate of 0.0002
  • We test the model by generating images for a fixed randomly generated noise and label

Results

On FastionMNIST Dataset

Here is the loss graph

NOTE: In this graph blue plot corresponds to generator loss and orange to discriminator loss

graph

Here is the final image generated by the generator for a randomly generated noise and label.

img

On CIFAR10 Dataset

Here is the loss graph

NOTE: In this graph blue plot corresponds to generator loss and orange to discriminator loss

graph

Here is the final image generated by the generator for a randomly generated noise and label.

img

On MNIST Dataset

Here is the loss graph

NOTE: In this graph blue plot corresponds to generator loss and orange to discriminator loss

graph

Here is the final image generated by the generator for a randomly generated noise and label.

img