Reader small image

You're reading from  Hands-On Generative Adversarial Networks with Keras

Product typeBook
Published inMay 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789538205
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Rafael Valle
Rafael Valle
author image
Rafael Valle

Rafael Valle is a research scientist at NVIDIA focusing on audio applications. He has years of experience developing high performance machine learning models for data/audio analysis, synthesis and machine improvisation with formal specifications. Dr. Valle was the first to generate speech samples from scratch with GANs and to show that simple yet efficient techniques can be used to identify GAN samples. He holds an Interdisciplinary PhD in Machine Listening and Improvisation from UC Berkeley, a Masters degree in Computer Music from the MH-Stuttgart in Germany and a Bachelors degree in Orchestral Conducting from UFRJ in Brazil.
Read more about Rafael Valle

Right arrow

Implementing Your First GAN

In this chapter, you will learn the basics about implementing and training Generative Adversarial Network (GAN) models for image synthesis. You will learn to implement a Generator and Discriminator a GAN. You will then learn to implement your loss function and use it to train your GAN framework. You will also learn to visualize the samples from your first GAN. We will focus on the well-known CIFAR-10 dataset with 60,000 32 by 32 colour images in 10 classes.

The following topics will be covered in this chapter:

  • Imports
  • Implementing the Generator and the Discriminator
  • Auxiliary functions
  • Training your GAN

Technical requirements

Imports

In this section, we provide a list of libraries and methods that will be used in our first GAN implementation. The following code block is the list of libraries to be used:

import matplotlib
matplotlib.use("Agg")
import matplotlib.pylab as plt
from math import ceil
import numpy as np
from keras.models import Sequential, Model
from keras.layers import Input, ReLU, LeakyReLU, Dense
from keras.layers.core import Activation, Reshape
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.core import Flatten
from keras.optimizers import SGD, Adam
from keras.datasets import cifar10
from keras import initializers

Lines one through four import libraries and methods that are necessary for plotting.

Line five imports numpy, which is used for overall data operations, including generation, manipulation...

Implementing a Generator and Discriminator

In this section, we will learn how to implement a Generator and Discriminator that is commonly used in the GAN framework. Within the possible network architectures, our research community has focused on network architectures similar to DCGAN and Resnet. The DCGAN architecture was first described in the paper Unsupervised Representation Learning with Deep Convolutional by A. Radford et al. Resnet-like GAN architectures were probably first used in the Wasserstein GAN paper by Martin Arjovsky et al and first described in Deep Residual Learning for Image Recognition by K. He et al.

At the time Resnets were proposed, Resnets and their residual connections were essential for achieving state-of-the-art results in computer vision tasks. The concept of residual connections extended to other architectures and domains, and has been extensively used...

Auxiliary functions

We are going to use three auxiliary functions to get the data, plot the images, and plot the losses.

The function to get the data uses the Keras cifar10 class, converts it to fp32, 32-bit floating point, and scales it to [-1, 1]. Scaling images to [-1, 1] is a common practice when training GANs on image data that bounds the range of the output, possibly avoiding explosions:

def get_data():
# load cifar10 data
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

# convert train and test data to float32
X_train = X_train.astype(np.float32)
X_test = X_test.astype(np.float32)

# scale train and test data to [-1, 1]
X_train = (X_train / 255) * 2 - 1
X_test = (X_train / 255) * 2 - 1

return X_train, X_test

The method below is used to plot a grid with images. It takes a tensor with the images and the full path where the image...

Training your GAN

In Chapter 2, Introduction to Generative Models, we described GANs as a two-player min-max game, in which the Discriminator and Generator take turns. Informally speaking, the Discriminator learns to identify whether a sample is real or fake while the Generator tries to produce samples that the Discriminator believes to be real.

The implementation of this procedure is, indeed, similar to the informal description. Although, in our first implementation, each model will take one turn at a time, it is possible, and sometimes desirable, to have one model taking more turns than the other.

Our training procedure starts with sampling fake data produced by the Generator and real data. Note that, at this stage, we are not updating the Generator and no gradient is flowing through. Let's start with our method header:

def train(ndf=64, ngf=64, z_dim=100, lr_d=2e-4, lr_g...

Summary

In this chapter, we also learned how to implement our first GAN. We covered some basic theory related to the GAN framework and how it relates to architecture design, especially focusing on the similar capacity of the Discriminator and Generator. We also covered, in detail, the theory behind upsampling layers, weight normalizations, and loss functions seen in GANs.

We learned how to implement the DCGAN Discriminator and Generator architecture, including their optimizers and loss functions. We learned how to implement the training procedure in GANs, wherein the Discriminator and Generator take turns at optimizing their parameters. Finally, we learned how to sample the Generator to get image outputs, and how to visualize those outputs for our amusement and to train the models.

In the next chapter, you will learn how to evaluate your first GAN.

...

Further reading

The following are topics for further reading:

  • The DCGAN Paper
  • Image upsampling
  • Batch normalization paper or weight normalization summary
  • The Convolution Arithmetic Paper
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Generative Adversarial Networks with Keras
Published in: May 2019Publisher: PacktISBN-13: 9781789538205
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Rafael Valle

Rafael Valle is a research scientist at NVIDIA focusing on audio applications. He has years of experience developing high performance machine learning models for data/audio analysis, synthesis and machine improvisation with formal specifications. Dr. Valle was the first to generate speech samples from scratch with GANs and to show that simple yet efficient techniques can be used to identify GAN samples. He holds an Interdisciplinary PhD in Machine Listening and Improvisation from UC Berkeley, a Masters degree in Computer Music from the MH-Stuttgart in Germany and a Bachelors degree in Orchestral Conducting from UFRJ in Brazil.
Read more about Rafael Valle