Professional Documents
Culture Documents
Image Generator - Drawing Cartoons With Generative Adversarial Networks PDF
Image Generator - Drawing Cartoons With Generative Adversarial Networks PDF
Image Generator - Drawing Cartoons With Generative Adversarial Networks PDF
In today’s article, we are going to implement a machine learning model that can
generate an infinite number of alike image samples based on a given dataset. In order to
do so, we are going to demystify Generative Adversarial Networks (GANs) and feed it
with a dataset containing
Get one characters
more story from ‘The
in your member Simspons’.
preview whenByyou
thesign
endup.
of this article, you
It’s free.
will be familiar with the basics behind the GANs and you will be able to build a
generative model on your own! Sign up with Google
To get a better idea about the GANs’ capabilities, take a look at the following example of
Sign up with Facebook
the Homer Simpson evolution during the training process.
Already have an account? Sign in
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 1/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
Fascinating, right?
Let’s dive into some theory to get a better understanding of how it actually works.
We would like to provide a set of images as an input, and generate samples based on
them as an output.
With the following problem definition, GANs fall into the Unsupervised Learning
bucket because we are not going to feed the model with any expert knowledge (like for
example labels in the classification task).
The idea of generating samples based on a given dataset without any human supervision
sounds very promising.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 2/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
Generator
The Generator takes random noise as an input and generates samples as an output. It’s
goal is to generate such samples that will fool the Discriminator to think that it is seeing
real images while actually seeing fakes. We can think of the Generator as a counterfeit.
Discriminator
Discriminator takes both real images from the input dataset and fake images from the
Generator and outputs a verdict whether a given image is legit or not. We can think of
the Discriminator as a policeman trying to catch the bad guys while letting the good guys
free.
Minimax Representation
If we think once again about Discriminator’s and Generator’s goals, we can see that they
are opposing each other. Discriminator’s success is a Generator’s failure and vice-versa.
That is why we can represent GANs framework more like Minimax game framework
rather than an optimization problem.
Get one more story in your member preview when you sign up. It’s free.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 3/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
(source: http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture13.pdf)
towardsdatascience.com
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 4/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
It gets both real images and fake ones and tries to tell whether they are legit or not. We,
as the system designers know whether they came from a dataset (reals) or from a
generator (fakes). We can use this information to label them accordingly and perform a
classic backpropagation allowing the Discriminator to learn over time and get better in
distinguishing images. If the Discriminator correctly classifies fakes as fakes and reals as
reals, we can reward it with positive feedback in the form of a loss gradient. If it fails at
its job, it gets negative feedback. This mechanism allows it to learn and get better.
. . .
It takes random noise as input and samples the output in order to fool the Discriminator
Getreal
that it’s the oneimage.
more story
Oncein your
the member output
Generator’s previewgoes
when you sign
through theup. It’s free.
Discriminator, we
know the Discriminator’s verdict whether it thinks that it was a real image or a fake one.
Sign up with Google
We can use this information to feed the Generator and perform backpropagation again.
If the Discriminator identifies the Generator’s output as real, it means that the Generator
Sign up with Facebook
did a good job and it should be rewarded. On the other hand, if the Discriminator
recognized that it was given aAlready
fake, ithave
means that the Generator
an account? Sign in failed and it should be
punished with negative feedback.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 5/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
. . .
If you think about it for a while, you’ll realize that with the above approach we’ve tackled
the Unsupervised Learning problem with combining Game Theory, Supervised
Learning and a bit of Reinforcement Learning.
. . .
(source: https://www.oreilly.com/ideas/deep-convolutional-generative-adversarial-networks-with-
tensor ow)
Get one more story in your member preview when you sign up. It’s free.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 6/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
(source: https://medium.com/@jonathan_hui/gan-whats-generative-adversarial-networks-and-its-
application-f39ed278ef09)
I hope you are not scared by the above equations, they will definitely get more
comprehensible as we will move on to the actual GAN implementation.
gsurma/image_generator
DCGAN image generator 🖼. Contribute to gsurma/image_generator
development by creating an account on GitHub.
github.com
. . .
Since we are going to deal with image data, we have to find a way of how to represent it
effectively. It can be achieved with Deep Convolutional Neural Networks, thus the
name - DCGAN.
Model
In our project, we are going to use a well-tested model architecture by Radford et al.,
2015 that can be seen below.
Get one more story in your member preview when you sign up. It’s free.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 7/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
You can find my TensorFlow implementation of this model here in the discriminator and
generator functions.
As you can see in the above visualization. Generator and Discriminator have almost the
same architectures, but reflected. We won’t dive deeper into the CNN aspect of this topic
but if you are more curious about the underlying aspects, feel free to check the following
article.
Loss Functions
In order for our Discriminator and Generator to learn over time, we need to provide loss
functions that will allow backpropagation to take place.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 8/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
While the above loss declarations are consistent with the theoretic explanations from the
previous chapter, you may notice two extra things:
2. One-sided label smoothening for the real images recognized by the Discriminator in
line 12.
You’ll notice that training GANs is notoriously hard because of the two loss functions
(for the Generator and Discriminator) and getting a balance between them is a key to
the good results.
Because of the fact that it’s very common for the Discriminator to get too strong over the
Generator, sometimes we need to weaken the Discriminator and we are doing it with the
above modifications. We’ll cover other techniques of achieving the balance later.
Optimizers
We are going to optimize our models with the following Adam optimizers.
LR_D = 0.00004
LR_G = 0.0004
BETA1 = 0.5
As the above hyperparameters are very use-case specific, don’t hesitate to tweak them
but also remember that GANs are very sensitive to the learning rates modifications so
tune them carefully.
Training
Finally, we can begin training.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 10/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
Above function contains a standard machine learning training protocol. We are dividing
our dataset into batches of a specific size and performing training for a given number of
epochs.
The core training part is in lines 20–23 where we are training Discriminator and
Generator. Same as with the loss functions and learning rates, it’s also a possible place to
balance the Discriminator and the Generator. Some researchers found that modifying
the ratio between Discriminator and Generator training runs may benefit the results. In
my case 1:1 ratio performed the best but feel free to play with it as well.
Moreover, I have used the following hyperparameters but they are not written in stone,
so don’t hesitate to modify them.
IMAGE_SIZE = 128
NOISE_SIZE = 100
BATCH_SIZE = 64
EPOCHS = 300
It’s very important to regularly monitor model’s loss functions and its performance. I
recommend to do it every epoch, like in the code snippet above. Let’s see some samples
that were generated during training.
We can clearly see that our model gets better and learns how to generate more real-
looking Simpsons.
Get one more story in your member preview when you sign up. It’s free.
Let’s focus on the main character, the man of the house, Homer Simpson.
Sign up with Google
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 11/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
Get one more story in your member preview when you sign up. It’s free.
Sign up
Epoch with
200 EyeFacebook
balls
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 12/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
Final Results
Ultimately, after 300 epochs of training that took about 8 hours on NVIDIA P100
(Google Cloud), we can see that our artificially generated Simpsons actually started
looking like the real ones! Take a look at the following cherry-picked samples.
Get one more story in your member preview when you sign up. It’s free.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 13/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
What’s next?
While GAN image generation proved to be very successful, it’s not the only possible
application of the Generative Adversarial Networks. For example, take a look at the
following Image-to-Image translation with CycleGAN.
Get one more story in your member preview when you sign up. It’s free.
Already
(source: have an account? Sign in
https://junyanz.github.io/CycleGAN/)
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 14/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
Amazing, right?
I encourage you to dive deeper into the GANs field as there is still more to explore!
. . .
gsurma/image_generator
DCGAN image generator 🖼. Contribute to gsurma/image_generator
development by creating an account on GitHub.
github.com
. . .
Questions? Comments? Feel free to leave your feedback in the comments section or
contact me directly at https://gsurma.github.io.
Get one more story in your member preview when you sign up. It’s free.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 15/16
11/11/2019 Image Generator - Drawing Cartoons with Generative Adversarial Networks
Get one more story in your member preview when you sign up. It’s free.
https://towardsdatascience.com/image-generator-drawing-cartoons-with-generative-adversarial-networks-45e814ca9b6b 16/16