Title: Intro to Generative Adversarial Networks (GANs) - PyImageSearch

URL Source: https://pyimagesearch.com/2021/09/13/intro-to-generative-adversarial-networks-gans/

Published Time: 2021-09-13T14:00:00+00:00

Markdown Content:
This post covers the intuition of Generative Adversarial Networks (GANs) at a high level, the various GAN variants, and applications for solving real-world problems.

![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20350'%3E%3C/svg%3E)

This is the first post of a GAN tutorial series:

1.  _Intro to Generative Adversarial Networks (GANs)_ (this post)
2.  [_Get Started: DCGAN for Fashion-MNIST_](https://pyimagesearch.com/2021/11/11/get-started-dcgan-for-fashion-mnist/)
3.  [_GAN Training Challenges: DCGAN for Color Images_](https://pyimagesearch.com/2021/12/13/gan-training-challenges-dcgan-for-color-images/)

**How GANs work**
-----------------

GANs are a type of **generative** models, which observe many sample distributions and generate more samples of the same distribution. Other generative models include variational autoencoders ([VAE](https://arxiv.org/abs/1906.02691)) and [Autoregressive](https://arxiv.org/abs/1601.06759) models.

### **The GAN architecture**

There are two networks in a basic GAN architecture: the generator model and the discriminator model. GANs get the word **“adversarial”** in its name because the two networks are trained simultaneously and competing against each other, like in a zero-sum game such as chess.

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20626%20416'%3E%3C/svg%3E)

**Figure 1:** Chess pieces on a board.

The **generator** model generates new images. The goal of the generator is to generate images that look so real that it fools the discriminator. In the simplest GAN architecture for image synthesis, the input is typically random noise, and its output is a generated image.

![Image 3](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20650%20308'%3E%3C/svg%3E)

**Figure 2:** Generator input and output (image by the author).

The **discriminator** is just a binary image classifier which you should already be familiar with. Its job is to classify whether an image is real or fake.

**_Note:_** _In more complex GANs, we could condition the Discriminator with image or text for Image-to-Image translation or Text-to-Image generation)._

![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20730%20399'%3E%3C/svg%3E)

**Figure 3:** Discriminator input and output (image by the author).

Putting it all together, here is what a basic GAN architecture looks like: the generator makes fake images; we feed both the real images (training dataset) and the fake images into the discriminator in separate batches. The discriminator then tells whether an image is real or fake.

![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20243'%3E%3C/svg%3E)

**Figure 4:** GAN architecture (image by the author).

### **Training GANs**

#### **The Minimax game: G vs. D**

Most deep learning models (for example, image classification) are based on optimization: finding the low value of the cost function. GANs are different because the two networks: the generator and discriminator, each has its own cost with opposite objectives:

*   The generator tries to fool the discriminator into thinking the fake images as real
*   The discriminator tries to classify real and fake images correctly

The minimax game math function below illustrates this adversarial dynamic during training. Don’t worry too much if you don’t understand the math, which I will explain in more detail when coding the G loss and D loss in a future DCGAN post.

![Image 6](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20650%20276'%3E%3C/svg%3E)

**Figure 5:** GANs Minimax Game (image by the author).

Both the generator and discriminator improve over time during training. The generator gets better and better at producing images that resemble the training data, while the discriminator gets better at telling the real and fake images apart.

Training GANs is to find an **equilibrium** in the game when:

*   The generator makes data that looks almost identical to the training data.
*   The discriminator can no longer tell the difference between the fake images from the real images.

#### **The artist vs. the critic**

Mimicking masterpieces is a great way to learn art — “[How Artists Are Copying Masterpieces at World-Renowned Museums](https://www.artsy.net/article/artsy-editorial-artists-allowed-copy-masterpieces-worlds-prestigious-museums).” As a human artist mimicking a masterpiece, I’d find the artwork I like as an inspiration and try to copy it as much as possible: the contours, the colors, the compositions and the brushstrokes, and so on. Then a critic takes a look at the copy and tells me whether it looks like the real masterpiece.

![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20500%20362'%3E%3C/svg%3E)

**Figure 6:** An artist copies another painting.

GANs training is similar to that process. We can think of the generator as the artist and the discriminator as the critic. Note the difference in this analogy between the human artist and the machine (GANs) artist, though: the generator doesn’t have access or visibility to the masterpiece that it’s trying to copy. Instead, it only relies on the discriminator’s feedback to improve the images it’s generating.

### **Evaluation metrics**

A good GAN model should have good image **quality** — for example, not blurry and resembles the training image; and **diversity**: a good variety of images get generated that approximate the distribution of the training dataset.

To evaluate the GAN model, you can visually inspect the generated images during training or by inference with the generator model. If you’d like to evaluate your GANs quantitatively, here are two popular evaluation metrics:

1.  **Inception Score,** which captures both the **_quality_** and **_diversity_** of the generated images
2.  **Fréchet Inception Distance** which compares the real vs. fake images and doesn’t just evaluate the generated images in isolation

**GAN variants**
----------------

Since [Ian Goodfellow et al.’s original GANs paper in 2014](https://arxiv.org/abs/1406.2661), there have been many GAN variants. They tend to build upon each other, either to solve a particular training issue or to create new GANs architectures for finer control of the GANs or better images.

Here are a few of these variants with breakthroughs that provided the foundation for future GAN advances. This is by all means not a complete list of all the GAN variants.

![Image 8](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20438'%3E%3C/svg%3E)

**Figure 7:** GAN variants timeline (image by the author).

[**DCGAN**](https://arxiv.org/abs/1511.06434) (Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks) was the first GAN proposal using Convolutional Neural Network (CNN) in its network architecture. Most of the GAN variations today are somewhat based on DCGAN. Thus, DCGAN is most likely your first GAN tutorial, the “Hello-World” of learning GANs.

[**WGAN**](https://arxiv.org/abs/1701.07875) (Wasserstein GAN) and [**WGAN-GP**](https://arxiv.org/abs/1704.00028v3) (were created to solve GAN training challenges such as mode collapse — when the generator produces the same images or a small subset (of the training images) repeatedly. WGAN-GP improves upon WGAN by using gradient penalty instead of weight clipping for training stability.

[**cGAN**](https://arxiv.org/abs/1411.1784) (Conditional Generative Adversarial Nets) first introduced the concept of generating images based on a condition, which could be an image class label, image, or text, as in more complex GANs. **Pix2Pix** and **CycleGAN** are both conditional GANs, using images as conditions for image-to-image translation.

[**Pix2PixHD**](https://github.com/NVIDIA/pix2pixHD) (High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs) disentangles the effects of multiple input conditions and, as in the paper example: control color, texture, and shape of a generated garment image for fashion design. In addition, it can generate realistic 2k high-resolution images.

[**SAGAN**](https://arxiv.org/abs/1805.08318) (Self-Attention Generative Adversarial Networks) improves image synthesis quality: generating details using cues from all feature locations by applying the self-attention module (a concept from the NLP models) to CNNs. Google DeepMind scaled up SAGAN to make BigGAN.

[**BigGAN**](https://arxiv.org/abs/1809.11096) (Large Scale GAN Training for High Fidelity Natural Image Synthesis) can create high-resolution and high-fidelity images.

ProGAN, StyleGAN, and StyleGAN2 all create high-resolution images.

[**ProGAN**](https://arxiv.org/abs/1710.10196) (Progressive Growing of GANs for Improved Quality, Stability, and Variation) grows the network progressively.

[**StyleGAN**](https://arxiv.org/abs/1812.04948) (A Style-Based Generator Architecture for Generative Adversarial Networks), introduced by NVIDIA Research, uses the progress growing ProGAN plus image style transfer with adaptive instance normalization (AdaIN) and was able to have control over the style of generated images.

[**StyleGAN2**](https://arxiv.org/abs/1912.04958) (Analyzing and Improving the Image Quality of StyleGAN) improves upon the original StyleGAN by making several improvements in areas such as normalization, progressively growing and regularization techniques, etc.

**GAN applications**
--------------------

GANs are versatile and can be used in a variety of applications.

### **Image synthesis**

Image synthesis can be fun and provide practical use, such as image augmentation in machine learning (ML) training or help with creating artwork and design assets.

GANs can be used to create images **that never existed before**, which is perhaps what GANs are best known for. They can create unseen new faces, cat images and artwork, and more. I’ve included a few high-fidelity images below, which I generated from the websites powered by StyleGAN2. Go to these links, experiment yourself, and see what images you get from your experiments.

Zalando Research uses GANs to generate fashion designs based on color, shape, and texture ([Disentangling Multiple Conditional Inputs in GANs](https://arxiv.org/abs/1806.07819)).

[Fashion++](https://research.fb.com/wp-content/uploads/2019/08/Fashion-Minimal-Edits-for-Outfit-Improvement-v2.pdf) by Facebook Research goes beyond generating fashion into recommending fashion change recommendations: “what is fashionable?”

GANs can also help train reinforcement agents. For example, NVIDIA’s [GameGAN](https://arxiv.org/abs/2005.12126) simulates the game environments.

### **Image-to-image translation**

Image-to-image translation is a computer vision task that translates the input image to another domain (e.g., color or style) while preserving the original image content. This is perhaps one of the most important tasks to use GANs in art and design.

[Pix2Pix](https://arxiv.org/abs/1611.07004) (Image-to-Image Translation with Conditional Adversarial Networks) is a conditional GAN that was perhaps the most famous image-to-image translation GAN. However, one major drawback of Pix2Pix is that it requires paired training image datasets.

![Image 9](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20650%20413'%3E%3C/svg%3E)

****Figure 10:**** Inputs and outputs of Pix2Pix GANs (image source: [Pix2Pix paper](https://arxiv.org/abs/1611.07004)).

CycleGAN was built upon Pix2Pix and only needs unpaired images, much easier to come by in the real world. It can convert images of apples to oranges, day to night, horses to zebras … ok. These may not be real-world use cases to start with; there are so many other image-to-image GANs developed since then for art and design.

Now you can translate your selfie to comics, painting, cartoons, or any other styles you can imagine. For example, I can use [White-box CartoonGAN](https://github.com/SystemErrorWang/White-box-Cartoonization/blob/master/paper/06791.pdf) to turn my selfie into a cartoonized version:

![Image 10](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20650%20300'%3E%3C/svg%3E)

**Figure 12:** Input and output of the White-box CartoonGAN (images by the author).

**Colorization** can be applied to not only black and white photos but also artwork or design assets. In the artwork making or UI/UX design process, we start with outlines or contours and then coloring. Automatic colorization could help provide inspiration for artists and designers.

### **Text-to-Image**

We’ve seen a lot of Image-to-Image translation examples by GANs. We could also use words as the condition to generate images, which is much more flexible and intuitive than using class labels as the condition.

Combining NLP and computer vision has become a popular research area in recent years. Here are a few examples: [StyleCLIP](https://github.com/orpatashnik/StyleCLIP) and [Taming Transformers for High-Resolution Image Synthesis](https://arxiv.org/abs/2012.09841v3).

![Image 11](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20894%20378'%3E%3C/svg%3E)

****Figure 13:**** A GAN transforms NLP and computer vision (image source: [StyleCLIP paper](https://arxiv.org/abs/2103.17249)).

### **Beyond images**

GANs can be used for not only images but also music and video. For example, [GANSynth](https://magenta.tensorflow.org/gansynth) from the Magenta project can make music. Here is a fun example of GANs on video motion transfer called “Everybody Dance Now” ([YouTube](https://youtu.be/PCBTZh41Ris) | [Paper](https://arxiv.org/abs/1808.07371)). I’ve always loved watching this charming video where the dance moves by professional dancers get transferred to the amateurs.

### **Other GAN applications**

Here are a few other GAN applications:

*   [**Image inpainting**](https://www.nvidia.com/research/inpainting/index.html)**:** replace the missing portion of the image.
*   [**Image uncropping** **or extension**](https://arxiv.org/abs/1908.07007)**:** this could be useful in simulating camera parameters in virtual reality.
*   **Super-resolution** ([SRGAN](https://arxiv.org/abs/1609.04802) & [ESRGAN](https://arxiv.org/abs/1809.00219)): enhance an image from lower-resolution to high resolution. This could be very helpful in photo editing or medical image enhancements.

Here is an example of how GANs can be used for climate change. [Earth Intelligent Engine](http://trillium.tech/eie/), an [FDL](https://frontierdevelopmentlab.org/) (Frontier Development Lab) 2020 project, uses Pix2PixHD to simulate what an area would look like after flooding.

We have seen GAN demos from papers, research labs. and open source projects. These days we are starting to see real commercial applications using GANs. Designers are familiar with using design assets from [icons8](https://icons8.com/). Take a look at their website, and you will notice the GAN applications: from the Smart Upscaler, Generated Photos to Face Generator.

Summary
-------

In this post, you learned a high-level overview of GANs, their variants, and fun applications. While most of the examples in this post are about using GANs for art and design, the same techniques can be easily adapted and applied to many other fields: medicine, agriculture, and climate change. As you see in the post, GANs are powerful and versatile. I hope you are excited to dive deeper into GANs: follow along with the upcoming posts as we explore GANs in depth with code examples!

![Image 12](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.