Image for post
Image for post

An Introduction To Conditional GANs (CGANs)

Manish Nayak
May 9, 2019 · 5 min read

Introduction

Conditional GANs (CGANs) are an extension of the GANs model. You can read about a variant of GANs called DCGANs in my previous post here. CGANs are allowed to generate images that have certain conditions or attributes.

Like DCGANs, Conditional GANs also has two components.

  • A Generator(An artist) neural network.

Conditional GANs (CGANs): The Generator and Discriminator both receive some additional conditioning input information. This could be the class of the current image or some other property.

For example, if we train a DCGANs to generate new MNIST images, There is no control over which specific digits will be produced by the Generator. There is no mechanism for how to request a particular digit from the Generator. This problem can be addressed by a variation of GAN called Conditional GAN (CGAN). we could add an additional input layer with values of one-hot-encoded image labels.

In conditional GANs (CGANs)

  • Adding a vector of features controls the output and guide Generator figure out what to do.

NOTE: CGANs have one disadvantage. CGANs are not strictly unsupervised and we need some kind of labels for them to work.

High-Level CGAN’s Architecture Diagram

Image for post
Image for post
Conditional GAN

The Discriminator’s Network

The CGAN Discriminator’s model is similar to DCGAN Discriminator’s model except for the one-hot vector, which is used to condition Discriminator outputs. You can read about Discriminator’s Network in my previous post here

The Generator’s Network

The CGAN Generator’s model is similar to DCGAN Generator’s model except for the one-hot vector, which is used to condition Generator outputs. You can read about Generator’s Network in my previous post here

Image for post
Image for post
The DCGAN architecture of the generator https://arxiv.org/pdf/1511.06434.pdf

CGAN’s Architecture Diagram

Image for post
Image for post
CGAN’s Architecture

Loss Functions

The Discriminator has two task

  • Discriminator has to correctly label real images which are coming from training data set as “real”.

We need to calculate two losses for the Discriminator. The sum of the “fake” image and “real” image loss is the overall Discriminator loss. So the loss function of the Discriminator is aiming at minimizing the error of predicting real images coming from the dataset and fake images coming from the Generator given their one-hot labels.

Image for post
Image for post
Discriminator’s loss function

The Generator network has one task

  • To create an image that looks as “real” as possible to fool the Discriminator.

The loss function of the Generator minimizes the correct prediction of the Discriminator on fake images conditioned on the specified one-hot labels.

Image for post
Image for post
Generator’s loss function

Training of DCGANs

The following steps are repeated in training

  • The Discriminator is trained using real and fake data and generated data.

Discriminator’s Training Flow

Image for post
Image for post
Discriminator’s training process

Generator’s Training Flow

Image for post
Image for post
Generator’s training process

Accompanied jupyter notebook for this post can be found here.

Conclusion

CGANs can be used to build a model which can generate an image of an imaginary actor of given class like male or female. It can also use to build Face Aging system, Age synthesis and age progression have many practical industrial and consumer applications like cross-age face recognition, finding lost children, entertainment, visual effects in movies.

I hope this article helped you get started building your own CGANs. I think it will at least provides a good explanation and understanding about CGANs.

Data Driven Investor

empower you with data, knowledge, and expertise

Sign up for DDIntel

By Data Driven Investor

In each issue we share the best stories from the Data-Driven Investor's expert community. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Manish Nayak

Written by

Machine Learning, AI & Deep Learning Enthusiasts

Data Driven Investor

empower you with data, knowledge, and expertise

Manish Nayak

Written by

Machine Learning, AI & Deep Learning Enthusiasts

Data Driven Investor

empower you with data, knowledge, and expertise

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app