A complete guide for building a Generative Adversarial Network (GAN) to make your very own anime characters with Keras
In this post, we will learn to develop a Generative Adversarial Network (GAN) for generating realistic manga or anime characters.
I’ve always been amazed by vivid animations, especially Manga and their bold looks and strokes. Wouldn’t it be awesome to be able to draw a few ourselves, to experience the thrill of creating them with the help of a self-developed Neural Network?!
The best way to master a skill is to practice and improvise it until you’re satisfied with yourself and your efforts. For a machine or a neural network, the best output it can generate is the one that matches human-generated outputs—or even fool a human to believe that a human actually produced the output. That’s exactly what a GAN does—well, at least figuratively ;)
Generative adversarial networks have lately been a hot topic in Deep Learning.
In Generative Adversarial Networks (GANs), two networks train and compete against each other, resulting in mutual improvisation. The generator misleads the discriminator by creating compelling fake inputs and tries to fool the discriminator into thinking of these as real inputs . The discriminator tells if an input is real or fake.
There are 3 major steps in the training of a GAN:
An important point to note is that the discriminator’s weights are frozen during the last step.
The reason for combining both networks is that there is no feedback on the generator’s outputs. The ONLY guide is if the discriminator accepts the generator’s output.
the minimax objective function
You can say that they are rivals destined for each other. The main character is the generator who strives to be better and better to make our purpose realized by learning from the fight with its rival, the discriminator
For the task at hand, we use a DCGAN (deep convolutional generative adversarial network)
A few guidelines to follow with DCGANs:
The dataset for anime faces can be generated by curling through various manga websites and downloading images, cropping faces out of them, and resizing them to a standard size. Given below is the link for A Python code depicting the same: Anime-Face-GAN-Keras
A Glimpse of the Dataset
glimpse of the dataset
Now let’s have a look at the architecture of our neural network! Do remember the points we discussed earlier about DCGANs.
This implementation of GAN uses deconv layers in Keras. I’ve tried various combinations of layers such as:
The generator consists of convolution transpose layers followed by batch normlization and a leaky ReLU activation function for upsampling. We’ll use a strides parameter in the convolution layer. This is done to avoid unstable training. Leaky ReLUs are one attempt to fix the “dying ReLU” problem. Instead of the function being zero when x < 0, a leaky ReLU will instead have a small negative slope (of 0.01, or so).
The discriminator also consists of convolution layers where we use strides to do downsampling and batch normalization for stability.
To conduct back propagation for the generator, in order to keep a check on it’s outputs, we compile a network in Keras —generator followed by discriminator.
In this network, the input is the random noise for the generator, and the output would is the generator’s output fed to the discriminator, keeping the discriminator’s weights frozen to avoid the adversarial collapse. Sounds cool, right? Look it up!
Training the Model
minimax objective realized during training
Basic Configurations of the model
I trained this code on my Acer-Predator helios 300, which took almost half an hour for 10,000 steps and around 32,000 images, with an Nvidia GTX GeForce 1050Ti GPU.
After training for 10,000 steps, the results came out looking pretty cool and satisfying. Have a look!
Transition of Images throughout Training
Final Output Images
In terms of improving the model, I think training for a longer duration and on a bigger dataset would improve the results further (Some of the faces were scary weird! Not the conventional Manga, I must say :D)
The task of generating manga-style faces was certainly interesting.
But there is still quite a bit of room for improvement with better training, models, and dataset. Our model cannot make a human wonder if these faces generated were real of fake; even so, it does an appreciably good job of generating manga-style images. Go ahead and try this with complete manga postures, too.
Most popular Data Science and Machine Learning courses — August 2020. This list was last updated in August 2020 — and will be updated regularly so as to keep it relevant
PyTorch for Deep Learning | Data Science | Machine Learning | Python. PyTorch is a library in Python which provides tools to build deep learning models. What python does for programming PyTorch does for deep learning. Python is a very flexible language for programming and just like python, the PyTorch library provides flexible tools for deep learning.
Data Augmentation is a technique in Deep Learning which helps in adding value to our base dataset by adding the gathered information from various sources to improve the quality of data of an organisation.
In this article, I clarify the various roles of the data scientist, and how data science compares and overlaps with related fields such as machine learning, deep learning, AI, statistics, IoT, operations research, and applied mathematics.
PyTorch is a library in Python which provides tools to build deep learning models. What python does for programming PyTorch does for deep learning.