Generative adversarial networks (GANs) are a powerful tool in the field of artificial intelligence (AI) and widely used in various applications, such as image generation, video synthesis, data augmentation, and text-to-image translation.
In terms of history, GANs were first proposed in the paper, Generative Adversarial Nets, by Ian Goodfellow and colleagues in 2014. Since then, they have received significant attention due to their ability to generate realistic and professional-looking artwork and their potential to generate convincing deep fakes. This article explores GANs in detail, including their internal operations, how they generate image and video outputs, and more.
Table of contents
- What is a generative adversarial network?
- How do GANs work?
- Training data andgenerated data
- Loss function
- Applications of GANs
- Applications and future directions
What is a generative adversarial network?
Generative adversarial networks are a type of AI model that consists of two neural networks: a generator and an adversary referred to as a discriminator. The generator creates new data while the discriminator evaluates it for authenticity. In a generative adversarial network, the goal of the generator is to create realistic images that the discriminator can't distinguish from real data. If the generator model generates images that the discriminator model can easily detect as implausible, such as an input image that is clearly not a face, the generator is penalized for the initial training data. Over time, the generator learns to generate more plausible examples.
At this point, you may wonder if a GAN differs from the popular generative pre-trained transformer (GPT) at all. The difference between GPT and GAN lies in their structure and purpose. GPT is a type of transformer model that uses a transformer architecture to generate text. It is pre-trained on a large corpus of text and then fine-tuned for specific tasks on neural networks. The key difference between GPT and GAN is the adversarial generator-discriminator process inherent in the latter.
Another important distinction between GANs and GPT is the network architecture used to create the models. GANs typically utilize convolutional neural networks (CNNs), and GPTs primarily use transformers.
How do GANs work?
The generator and discriminator collaborate to create data closely resembling the training data. The generator produces synthetic data, such as images, from random noise or latent vectors, while the discriminator evaluates the generated samples, distinguishing them from real data.
The generator's fake examples and the training set of real examples are both fed randomly into the discriminator network. The discriminator does not know whether a particular input originated from the generator or the training set. In other words, the generative network generates candidates while the discriminative network evaluates them. The contest operates in terms of data distribution.
Typically, the generative network learns to map from a latent space to a data distribution of interest, while the discriminative network distinguishes candidates produced by the generator from the true sample distribution. The generator network transforms random noise or latent vectors into data instances, aiming to generate high quality, realistic samples. Simultaneously, the discriminator network acts as a binary classifier, distinguishing between real and generated data. Through the adversarial process, these networks continually improve their performance.
Initially, before training has begun, the generator’s fake images are very easy for the discriminator to recognize. Over time, during the process of generating images, the generator aims to deliver more realistic images that resemble real data so that the generator gets better at fooling the discriminator. Eventually, the generator’s outputs are so realistic that the discriminator cannot distinguish them from the real examples.
A GAN aims to train both the generator and discriminator simultaneously through a back-and-forth competition. As the generator attempts to produce increasingly high quality samples over time, the discriminator's ability to differentiate between real and fake examples becomes more challenging. This competition drives the training for the generator network, ultimately leading to the generator producing high quality, synthetic data that closely resembles the real data.
Training data and generated data
The training process of GANs relies on an iterative optimization algorithm, typically based on stochastic gradient descent (SGD). The defining parameters of a neural network are important to produce samples or create images. The key idea is that the generator's objective is to create data that minimizes the discriminator's ability to discriminate, while the discriminator tries to maximize its ability to distinguish between real data and generator output.
Machine learning models are used over progressive GAN stages to train the discriminator for one or more epochs—or one complete pass through the entire training dataset—keeping the generator weights constant. They then train the generator for one or more epochs, keeping the discriminator weights constant. They repeat these steps until they determine that two neural networks have converged.
Input data
GANs often rely on input data to learn the underlying patterns and characteristics in neural networks. The initial training data serves as the foundation upon which the generator attempts to deliver high quality output. Data augmentation techniques may also be employed to diversify the training dataset for low-resolution images, facilitating the generation of a broader range of synthetic data in a neural network.
Generated images
Producing realistic samples is the main aim of the GAN model. The generator creates images that ideally mimic the true data distribution. These synthetic data or generated instances can be used for various applications, including image synthesis and anomaly detection as the generator's output for producing realistic images. The goal is to generate data (or to generate images) that is indistinguishable from real examples.
Due to its dual-network structure, the training algorithm for a GAN must contend with two specific challenges:
- Dual training types: GANs involve the simultaneous training of two networks—the generator and the discriminator. This introduces the complexity of managing distinct training processes for each.
- Convergence ambiguity: Determining when a GAN has converged poses a challenging task. The convergence status is not easily identifiable.
Alternating training
To address the disparate training procedures for the generator and discriminator, the training for GAN follows an alternating pattern:
- The discriminator undergoes training for one or more epochs.
- The generator undergoes training for one or more epochs.
- Steps 1 and 2 are iteratively repeated to refine both the generator and discriminator networks.
This iterative process allows GANs to grapple with challenging generative problems by initially framing them as simpler classification tasks. Conversely, if a classifier cannot distinguish if data is real or not, the training process faces hurdles.
Convergence challenges
As the generator refines its capabilities through training, the discriminator's performance diminishes. The discriminator struggles to differentiate between real and generated data, resulting in 50% accuracy—essentially, a random prediction.
This dynamic poses a convergence dilemma for the entire GAN system. The feedback from the discriminator becomes less meaningful over time. If training persists beyond the point where the discriminator offers entirely random feedback, the generator may inadvertently train on unreliable information, leading to a potential deterioration in its own quality.
Consequently, achieving convergence in a GAN is often a transient rather than a stable state, requiring careful management to balance the training dynamics of the generator and discriminator.
Challenges and techniques
Several challenges arise during GAN training, such as mode collapse and training instability. GANs can collapse or fail to train properly due to several reasons, including the difficulty of training two networks in tandem, mode collapse (where the generator produces a limited variety of outputs), and the difficulty of distinguishing between real and fake data. These issues can be mitigated through careful architecture design, appropriate loss function design, and careful training strategies.
To address these issues, researchers have developed advanced GAN variants such as convolutional neural networks, including conditional GAN (cGAN) and deep convolutional GAN (DCGAN), a type of deep convolutional neural network. Deep convolutional GAN uses only deep networks consisting entirely of convolution-deconvolution layers—that is, fully convolutional networks.
Batch normalization and other techniques are employed to stabilize training and enhance the quality of generated outputs.
Loss function
Generative adversarial networks aim to emulate a probability distribution, necessitating the utilization of loss functions that quantify the dissimilarity between the distribution of GAN-generated data instances and that of real images.
Addressing the divergence between two distributions in GAN loss functions is an actively researched area, yielding various proposed approaches. This discussion focuses on two prevalent GAN loss functions: the minimax loss, originating from the seminal GAN paper, and the Wasserstein loss, the default choice in TF-GAN (tensor-flow GAN).
One loss function or two?
A GAN may employ two distinct loss functions—one for generator training and another for discriminator training. The challenge lies in harmonizing these functions to express a comprehensive measure of distribution dissimilarity.
Both generator and discriminator losses emanate from a unified measure of distribution distance. However, during the training of GAN, one term (reflecting the distribution of fake data) dominates, while the other (representing the distribution of real data) is omitted.
Although derived from the same formula, the generator and discriminator losses exhibit dissimilar appearances.
Minimax loss
In the original GAN paper, the generator seeks to minimize a function while the discriminator endeavors to maximize it. The formula involves probabilities estimated by the discriminator for real and fake data instances, and the expected values over these instances and random inputs to the generator.
The generator's influence is confined to the term representing fake data, leading to the minimization of log(1 - D(G(z))) during the training.
Acknowledging issues in early GAN training, the original paper suggests modifying the generator loss to maximize log D(G(z)). This modification is implemented in TF-GAN as modified_generator_loss.
Wasserstein loss
TF-GAN defaults to Wasserstein loss, reliant on a GAN modification known as Wasserstein GAN or WGAN. Here, the discriminator functions as a "critic," outputting numbers without adherence to a probability range. The critic maximizes the difference between its outputs for real and fake instances, while the generator maximizes the critic's output for fake instances.
The simplicity of the critic and generator loss functions is deceptive, involving the output differences for real and fake instances.
In TF-GAN, Wasserstein loss is implemented through wasserstein_generator_loss and wasserstein_discriminator_loss.
Benefits
Wasserstein GANs exhibit greater resilience against training challenges compared to minimax-based GANs. Moreover, they mitigate issues related to vanishing gradients. The earth mover distance, used in Wasserstein loss, has the advantage of being a true metric—representing distance in a probability distribution space—unlike cross-entropy.
Applications of GANs
Generative models have various applications, including generating synthetic training data for machine learning applications where training data is scarce. They can also be used for style transfer, where images can be transformed from one style to another to produce realistic, high-resolution images from a low-resolution one.
In addition, GANs can be applied to audio data to morph sound from one speaker’s voice to another, or to “transfer” a piece of music from one style to another. GANs are also used in the phenomenon of deepfakes, where hyper-realistic fake videos of celebrities and politicians speaking are created.
Today, the versatility and generative power of GANs have led to their adoption in various domains and applications. Here are some notable examples:
- Image generation: GANs can generate realistic images that resemble real photographs, enabling applications like artistic style transfer, image synthesis, image-to-image translation, and data augmentation for training deep learning models.
- Text generation: GANs have been utilized to generate coherent and contextually relevant text, contributing to applications such as chatbot responses, natural language generation, and text-based data augmentation.
- Anomaly detection: GANs can learn the normal patterns from a dataset and detect anomalies that deviate from this learned distribution, making them useful for identifying fraudulent activities, detecting errors in complex systems, or diagnosing medical conditions.
- Video generation: GANs have been used to generate synthetic videos by extending their generative capabilities over time, enabling applications such as video synthesis and video prediction.
Applications and future directions
GANs have applications in diverse fields, from creating photorealistic data and high-resolution image synthesis to machine learning models and artificial intelligence. As the field progresses, GANs continue to evolve. They represent a powerful paradigm in machine learning, enabling the generation of realistic data through the collaboration of two neural networks. As GANs continue to push the boundaries of what is achievable in artificial intelligence, their impact on various domains is likely to grow, ushering in new possibilities for data generation and synthesis.