Generative Adversarial Networks

In the previous section, we learned about generative models: models that can generate new images similar to the ones in the training dataset. VAE was a good example of a generative model.

Pre-lecture quiz

However, if we try to generate something really meaningful, like a painting at reasonable resolution, with VAE, we will see that training does not converge well. For this use case, we should learn about another architecture specifically targeted at generative models - Generative Adversarial Networks, or GANs.

The main idea of a GAN is to have two neural networks that will be trained against each other:

<img src="images/gan_architecture.png" width="70%"/>

Image by Dmitry Soshnikov

✅ A little vocabulary:

  • Generator is a network that takes some random vector, and produces the image as a result
  • Discriminator is a network that takes an image, and it should tell whether it is a real image (from training dataset), or it was generated by a generator. It is essentially an image classifier.

Discriminator

The architecture of discriminator does not differ from an ordinary image classification network. In the simplest case it can be fully-connected classifier, but most probably it will be a convolutional network.

✅ A GAN based on convolutional networks is called a DCGAN

A CNN discriminator consists of the following layers: several convolutions+poolings (with decreasing spatial size) and, one-or-more fully-connected layers to get "feature vector", final binary classifier.

✅ A 'pooling' in this context is a technique that reduces the size of the image. "Pooling layers reduce the dimensions of data by combining the outputs of neuron clusters at one layer into a single neuron in the next layer." - source

Generator

A Generator is slightly more tricky. You can consider it to be a reversed discriminator. Starting from a latent vector (in place of a feature vector), it has a fully-connected layer to convert it into the required size/shape, followed by deconvolutions+upscaling. This is similar to decoder part of autoencoder.

✅ Because the convolution layer is implemented as a linear filter traversing the image, deconvolution is essentially similar to convolution, and can be implemented using the same layer logic.

<img src="images/gan_arch_detail.png" width="70%"/>

Image by Dmitry Soshnikov

Training the GAN

GANs are called adversarial because there is a constant competition between the generator and the discriminator. During this competition, both generator and discriminator improve, thus the network learns to produce better and better pictures.

The training happens in two stages:

During this process, both the generator and the discriminator losses are not going down significantly. In the ideal situation, they should oscillate, corresponding to both networks improving their performance.

✍️ Exercises: GANs

Problems with GAN training

GANs are known to be especially difficult to train. Here are a few problems:

Style Transfer

GANs is a great way to generate artistic images. Another interesting technique is so-called style transfer, which takes one content image, and re-draws it in a different style, applying filters from style image.

The way it works is the following:

✍️ Example: Style Transfer

Post-lecture quiz

Conclusion

In this lesson, you learned about GANS and how to train them. You also learned about the special challenges that this type of Neural Network can face, and some strategies on how to move past them.

🚀 Challenge

Run through the Style Transfer notebook using your own images.

Review & Self Study

For reference, read more about GANs in these resources:

Assignment

Revisit one of the two notebooks associated to this lesson and retrain the GAN on your own images. What can you create?