Generating handwritten digits with a Generative Adversarial Network
Ladies and gentlemen, behold:
A number eight! Generated by a computer! Are you impressed?
I guess not. And that’s ok.
As you may know, I’m super enthusiastic about Generative Adversarial Networks or GANs. This is the third and final post in a series of three blogs, and previously we have looked at deepfakes and how GANs work. In this post I will share the results of a GAN network I ran locally. It is able to generate handwritten digits with a quality high enough that the discriminator will not be able to tell the difference between the real life samples and the generated samples. This is to test out how the network behaves on a very controlled and relatively simple dataset.
The sample data is retrieved from the MNIST dataset, which contains a training set of 60,000 samples. The Python code I have used is originally written by Diego Gomez Mosquera and can be found here.
My code alterations are insignificant and just to make it run in my own Jupyter Notebook. I have not tried to run it on SAP Cloud Platform Cloud Foundry – but if you see a purpose to do so please report your findings.
I ran the GAN for 100 epochs with other values as suggested by the original code – and with random noise as input to the generator.
The first epoch shows the random noise:
Already after a two epochs, we can see something is forming:
After 20 epochs something resembling numbers are quite visible in the output:
The final result after 100 turns shows quite good results:
Results after 100 epochs. The discriminator loss is higher than the generator’s, as the generator is now good at it’s job and the discriminator is being fooled more often.
So it worked! We create more or less believable numbers images. Running the training for another 100 epochs would probably have fixed the remaining defects. Going from simple, handwritten digits to pieces of art or complex photographs will require a different layout of the network – but the concept is the same: A forger, an expert and the competition between them.
So far, this is not put into an SAP context – or even related to a SAP business area or process. Maybe you have the idea of how this technique can be applied to solve an existing problem or to create new opportunities. If so, please comment, share and contribute!
- Diego Gomez Mosquera, “GANs from Scratch 1: A deep introduction. With code in PyTorch and TensorFlow” URL: https://medium.com/ai-society/gans-from-scratch-1-a-deep-introduction-with-code-in-pytorch-and-tensorflow-cb03cdcdba0f
- Goodfellow et al 2016, “Generative Adversarial Nets” URL: https://arxiv.org/pdf/1406.2661.pdf