How I used AI to make fake people (GANs)

— And why it matters

Aadil Pickle
Towards Data Science

--

Beating humans at chess? Check.

Classifying malignant and benign tumours better than a doctor? Check.

Modelling millions of drugs that could potentially cure cancer? Che- wait, AI can actually do this??

What about our beautiful human complexions? Surely AI can’t imitate the sheer gorgeousness that is the human face, right?

The opposite of right.

Those last 2 mind-blowing applications of AI can be done through GANs. But first, before we get to the Hows, Whats, Whys and the “Oh my god I think I just shit myself ”s, let’s contextualize the intuition behind this special type of AI with a story.

Are these paintings even real or did AI make them too? Is it safe to trust your eyes?

Say there’s an art gallery opening in a couple of months, looking to display authentic, verified paintings from famous artists so they hire a detective to inspect each of the paintings. Meanwhile, there’s a forger in the area trying to sell off some of his fakes. But he’s not that great of a painter yet so his art kind of just looks like random shapes and scribbles right now (he calls them abstract).

At first, the detective is able to tell the real paintings from the fake ones quite easily. Keep in mind that this detective is also new to his job but it doesn’t take much skill to tell that the forger’s paintings right now are pretty terrible and nothing like real art.

But as the months go by, the forger figures out what actually makes real art look… well, real. He studies the style, patterns and shapes used by real artists to make their work look world class and begins to copy them as well as he can. He does this so well that the detective now has a much harder time distinguishing between the knockoff paintings and authentic ones. Even though the detective also gained a lot more experience at his job over time, the forger just outpaced him.

When the gallery actually opens, about half the paintings are real and half of the paintings were made by the forger. The detective was so confused that he just ended up guessing on if a piece was real or fake.

Now let’s take it one step further; let’s see if you can tell which faces are real and which ones are fake (if any).

The scary part is that all the faces on the right side were generated by an AI system; specifically, a special type of neural network called a Generative Adversarial Network (GAN).

Overview of GANs

GANs are composed of 2 neural networks, the Generator and the Discriminator, that compete against each other in a game to minimize or maximize a certain function. The Discriminator takes in real training data as well as generated (fake) data from the Generator and has to output the probability that each image is real. Its goal is to maximize the number of times correctly classifies the type of data it is given while the Generator is trying to minimize that number; it tries to get Discriminator into classifying the generated output as real, making the Discriminator less correct.

To put it simply, the generator is like our forger; its goal is to make fake images in order to fool the detective into thinking they’re real. The discriminator is like our detective; its job is to tell the real training images apart from the ones made by the forger.

Wait, why are there 2 neural networks here?

The function that both networks are trying to minimize/maximize (the rules of the game)

What the complicated function above actually means is that both the Generator (G) and the Discriminator (D) are playing a game against each other, competing to see who’s better at achieving their certain goal. When given 2 pictures to choose from, one from a real dataset(artist) and another which was drawn by G, D wants to figure out how to tell which one is real. It wants to pick the real one accurately every single time.

G thinks it’s smarter than D and can fool it easily so its job is to work towards making sure D thinks G’s images are real and make the number of times D gets the answer right as low as possible. In reality, the Discriminator will output a 1 when it’s convinced an image is real and a 0 when it spots a fake. Ideally, we’d like the average output of D to be 0.5 which confirms that G has gotten so good at making fakes that they look almost identical to real images, meaning D just has to guess and is right only about half the time.

What the Discriminator (left) and Generator (right) look like in code (PyTorch Implementation)

The special type of GAN that I built is a Deep Convolutional Generative Adversarial Network (DCGAN) which is what generated the fake fakes seen above. The basic idea is both networks D and G are built using a convolutional layer structure where feature maps or kernels that scan a certain portion of the image looking for key features.

This is actually done through matrix multiplication since each pixel in an image can be represented as a number. The values of groups of pixels are multiplied by the matrix, allowing networks to learn important features like edges, shapes and lines that are present in that group of pixels. This is a lot to take in at first; you’ll be able to see a more in-depth explanation and visualization here.

The Generator scans the real input images to look for patterns to mimic the features of the real training images as closely as possible. The Discriminator scans both the training data and generated images to find the important differences between their features so it can tell them apart. This important data is transferred through each layer, resulting in a final representation of the most important features in each image.

Repeating the training process over and over to get the Generator and Discriminator to better learn what real faces look like, we can go from having something like the images on left to the super realistic looking results on the right. I was actually kind of awestruck; some faces on the right actually look like real people!

Results over time look wayyyyyyyy more realistic

What can we actually do with GANs in real life?

One of my personal favourite and arguably the most fun application is the pix2pix implementation of conditional GANs. An unskilled artist only has to draw the outline of a drawing and a GAN can fill in the rest of the features based on the images it’s been trained to mimic (the future of forgery).

Edges2cats draws a great cat, not that great of a hat

Music

GANs aren’t only good for just images; they can make music too! Instead of identifying features in pictures like edges and shapes to forge paintings, we can identify aspects like chords and melodies to compose songs.

Drug Discovery

The most exciting and practical use for GANs is in drug discovery. The Generator can continuously generate the molecular structure of random drugs, receiving feedback from the Discriminator about if the drug looks real or not to continue iterating. Ideally, we’d eventually like the Generator to be able to mimic the patterns found in the structure of real drugs. This would be insane; machines could be able to create new drugs that have never existed before, potentially subduing the world’s most harmful diseases.

Key Takeaways

  • GANs are 2 neural networks competing against each other
  • The Generator aims to minimize a certain function; the Discriminator tries to maximize the same function
  • The Generator makes fake images that mimic real ones to fool the Discriminator which tries to accurately tell fake images apart from real ones
  • Over time the network is able to generate super realistic looking data
  • There are multiple variants, 2 of which are Conditional and Deep Convolutional GANs
  • GANs can be used to make fake people, music, drugs and draw cats
  • Trust no one; anything can be forged

Thank you so much for reading! Follow me on Medium for more and LinkedIn. Feel free to reach out if you want to learn more or let me know what you’re working on!

--

--