Create Any Image with C# And A Generative Adversarial Network
In this article I’m going to build a specialized neural network architecture called a Generative Adversarial Network (GAN).
GANs are weird. Here’s what they look like:
The Generator is a convolutional neural network (CNN) laid out in reverse.
A normal CNN reads in an image and outputs a list of class probabilities which usually indicate if the image contains a person, animal, or object.
But a reverse CNN does the opposite: we create a 1-dimensional class vector (just a list of numbers) and the network will convert this information to a fully realized machine-generated color image. And by tweaking the class vector, we can make the network generate any kind of image.
Of course, these images might not be particularly good. The generator might try to generate a picture of a horse, but how would it know if the output image looks anything like a real horse?
This is where the Discriminator comes in.
The discriminator is a regular CNN that is trained to identify images of people, animals, objects, or landscapes. Its job is to look at the images created by the generator, compare them to a dataset of real images, and correctly identify every real and generated image.
We can now run the generator and the discriminator against each other. We have the generator create a stream of fake images and then feed these images into the discriminator. Then we ask the discriminator to classify all the fakes produced by the generator:
At first, this will be easy because the generator will not be very good at creating fakes. But after each training epoch, the generator gets a little better at producing fakes, and the discriminator becomes a little better at spotting the fakes.
We continue to train, until the fakes produced by the generator have become so good that we humans can no longer tell the difference between the fakes and the real images.
This specific architecture is called a Generative Adversarial Network (GAN), and it’s a very cool and active area of research right now.
We can use GANs to create a wide range of computer-generated images: