Introduction to Generative Adversarial Networks

December 23, 2021
Introduction to Generative Adversarial Networks

Generative Adversarial Networks, also called GANs, are usually described as algorithmic architectures that use two neural networks, pitting one against the other (that’s why they are called “adversarial”) to create new, synthetic instances of data that can pass for real data. In other words, this is an approach to generative modeling using deep learning methods, such as convolutional neural networks.

Read our introduction to generative adversarial networks to learn more about how GANs work and what they are used for.

GAN Explained: How do They Work

To better understand the concept of generative models machine learning, let’s see how they work. In simple words, a generator (which is a neural network) generates new data instances when the discriminator checks them for authenticity. It means that the discriminator reviews the instances and determines whether each instance of this data belongs to a certain training dataset.

Generative adversarial networks are often used for image generation, video generation, generative modeling, and voice generation. Let’s go straight to the cases to understand generative adversarial networks meaning better.

In the original paper by Ian Goodfellow, “Generative Adversarial Networks,” GANs were used to generate new plausible examples for the MNIST handwritten digit dataset, the CIFAR-10 small object photograph dataset, and the Toronto Face Database.

The generator creates new images and generative models and passes them to the discriminator. The generator's goal is to produce passable hand-written digits when the task of the discriminator is to identify images created by the generator as fake.

Here is how the whole process looks like:

  • The generator takes in random numbers and returns an image.
  • This generated image is fed into the discriminator with different images taken from the actual, ground-truth dataset.
  • The discriminator takes in both real and fake images and returns probabilities, a number between 0 and 1, with 1 representing a prediction of authenticity and 0 representing fake.
Image by Proxet, Custom Software Development Company. How GANs Work: The Steps a GAN Takes.
class="wp-image-4943"/>How GANs Work: The Steps a GAN Takes

Entry into GAN Architecture

When the discriminator receives a fake sample, it wants to call it out as fake. But the Generator wants to generate samples so that the Discriminator will call it a real one. So the Generator aims to fool the Discriminator.

Let’s take a look at the following picture with a conditional GAN:

Image by Proxet, Custom Software Development Company. Objective Function in GAN Formulation.
class="wp-image-4944"/>Objective Function in GAN Formulation

It shows the optimized objective function.  The Discriminator function is symbolized as D. The Generator function is termed as G. Pz shows the probability distribution of the latent space called a random Gaussian distribution.

Data is the probability distribution of the training dataset. When x is sampled from Pdata , the Discriminator wants to classify it as a real sample. G(z) is a generated sample when G(z) is given as input to the Discriminator, and it wants to classify it as a fake data set.

CycleGAN is one of the types of GAN architecture used to lean transformation and changes between images of different styles.

Here is what this formulation can determine:

  • a map between artistic and realistic images
  • a transformation between images of horse and zebra
  • a transformation between winter image and summer image

One of the most popular examples of CycleGAN GAN datasets is FaceApp that shows how human faces will look at different ages.

“GANs can generate datasets that follow all of the rules of "natural" datasets and so, in theory, can be used for training of deep learning models. A great example of where this would be useful might be medical images, that can be expensive and time-consuming to collect for real – requiring both patient consent and medical expertise to label them.”

Bernard Marr, an internationally best-selling author, popular keynote speaker, futurist, and a strategic business & technologist.

Generative Adversarial Networks with Python

You can program a simple GAN in Python, using the Keras library to teach it how to draw a specific class of curves. Here’s what you should do first for developing GAN Python:

  • Generate a dataset of sinusoids;
  • Set up the discriminator and generator networks;
  • Use these to build up the GAN;
  • Train the GAN, showing how to combine the training of its components, and;
  • Contemplate a somewhat skewed and distorted sinusoid drawn by the program from pure noise.

Your main goal is to make GAN generate curves with this form. To keep things simple, we consider a=1 and let b∈[1/2,2] and c∈[0,π] generative models.

There is a need to define some constants and produce a dataset of such curves. To describe a curve, do not use the symbolic form employing the sine function, but turn to some points in the curve, sampled over the same x values, and represent the curve y = f(x) by the vector (y1,…,yN) where yi = f(xi) for the fixed xs.

The y values are generated using the previous formula for random b and c within the prescribed intervals. Having defined the training set, some of these curves can be plotted.

After that, you need to define our discriminator, namely the neural network used to distinguish a sinusoidal curve from any other set of sampled points. The discriminator accepts an input vector (y1, …, yN) and returns 1 if it corresponds to a sinusoidal curve, otherwise 0.

With the help of the Keras library, you can create a Sequence object in which to stack the different layers of the network. Then you need to create three layers: the input layer with N neurons, N being the size of the input vectors, a second layer with the same number of hidden neurons, and a third with just one neuron, the output layer.

Understanding Generative Adversarial Networks

GANs can create new information based on the rules established by existing ones. That’s why it has caused a lot of boom in AI development. Let’s take writing instruction manuals as an example. If you train a GAN on thousands of instruction manuals, one day it will be possible to create a system that could look at any tool, device, or software and create instructions on how to use it.

Here is how it works: the “forger” network that creates fake data is called a generative network, and its main goal is to read and understand the properties of the training data. Then it tries to replicate it by producing “candidate” datasets that follow the same rules.

The “detective” network determines whether the generative network is outputting false (artificially generated) data or real (training) data is known as the discriminative network. Because it competes against the generative network, the system as a whole is described as "adversarial."

A great example of a GAN architecture in action is a popular demonstration, “This Person Does Not Exist.” The network powering the website has learned to produce realistic images of human faces that, while they follow all of the rules regarding how a human face should look, do not exist outside of the computer program.

While you may think that the program builds images of faces by putting together pieces from a database of eyes, ears, mouths, and hair, this isn't true. The "input" data for the generative network consists of a string of numbers – only the discriminative network sees the training data. The generative network improves its output based entirely on the output of the discriminative network.

“Inventions like this could change the way people interact with nearly all media. For instance, when people see images in the news, they will try to determine whether what they are looking at is real or fake. This is the future of AI we all should know about.”

George Serebrennikov, COO at Proxet (ex - Rails Reactor) – a custom software development solutions company.

Gan Datasets for Training GANs

Let’s get into some examples of GAN datasets.

  • Abstract art data

This data set consists of 2782 abstract art images scraped from This data can be used to build a GAN to generate synthetic abstract art images. The data set contains images of real abstract art by Van Gogh, Dali, Picasso, and more.

  • High-content screening with C. Elegans

This data contains images that correspond to screens to find novel antibiotics using roundworm C. Elegans. The data consists of the images of roundworms infected with a pathogen called Enterococcus. Some of the images are of roundworms that have not been treated with the antibiotic, ampicillin, and others are of infected roundworms treated with ampicillin.

  • Glasses or no glasses

This data set contains images of faces with glasses and images of faces without glasses. The images were generated using GANs and can serve as training data for generating additional synthetic images.

Deep learning has changed our lives and made them much easier.

“We have taught machines to figure things out themselves. There are many existing deep learning techniques that can be ascribed to its prolific success.”

Andrej Karpathy, Director of Artificial Intelligence at Tesla.

However, to build complex software for image analysis custom-tailored to your needs, you will likely need help from a team of professionals. Proxet has worked with many clients in the healthcare industry and equipped their businesses with AI-based solutions, for example, triage chatbots. Our team has profound machine learning expertise, which includes image recognition. We will do our best to provide you with the most suitable software solution as fast as we can.

Related Posts