Reader small image

You're reading from  Generative Adversarial Networks Projects

Product typeBook
Published inJan 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789136678
Edition1st Edition
Languages
Right arrow
Author (1)
Kailash Ahirwar
Kailash Ahirwar
author image
Kailash Ahirwar

Kailash Ahirwar is a machine learning and deep learning enthusiast. He has worked in many areas of Artificial Intelligence (AI), ranging from natural language processing and computer vision to generative modeling using GANs. He is a co-founder and CTO of Mate Labs. He uses GANs to build different models, such as turning paintings into photos and controlling deep image synthesis with texture patches. He is super optimistic about AGI and believes that AI is going to be the workhorse of human evolution.
Read more about Kailash Ahirwar

Right arrow

Generating Anime Characters Using DCGANs

As we know, convolution layers are really good at processing images. They are capable of learning important features, such as edges, shapes, and complex objects, effectively, as shown in neural networks, such as Inception, AlexNet, Visual Geometry Group (VGG), and ResNet. Ian Goodfellow and others proposed a Generative Adversarial Network (GAN) with dense layers in their paper titled Generative Adversarial Nets, which can be found at the following link: https://arxiv.org/pdf/1406.2661.pdf. Complex neural networks, such as Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), and Long Short-Term Memory (LSTM) were not initially tested in GANs. The development of Deep Convolutional Generative Adversarial Networks (DCGANs) was an important step toward using CNNs for image generation. A DCGAN uses convolutional layers instead...

Introducing to DCGANs

CNNs have been phenomenal in computer vision tasks, be it for classifying images or detecting objects in images. CNNs were so good at understanding images that they inspired researchers to use CNNs in a GAN network. Initially, authors of the official GAN paper introduced Deep Neural Networks (DNNs) with dense layers only. Convolutional layers were not used in the original implementation of the GAN network. In the previous GANs, the generator and the discriminator network used dense hidden layers only. Instead, authors suggested that different neural network architectures can be used in a GAN setup.

DCGANs extend the idea of using convolutional layers in the discriminator and the generator network. The setup of a DCGAN is similar to a vanilla GAN. It consists of two networks: a generator and a discriminator. The generator is a DNN with convolutional layers...

Setting up the project

We have already cloned/downloaded the complete code for all the chapters. The downloaded code contains a directory called Chapter04, which contains the entire code for this chapter. Execute the following commands to set up the project:

  1. Start by navigating to the parent directory, as follows:
cd Generative-Adversarial-Networks-Projects
  1. Now, change the directory from the current directory to Chapter04:
cd Chapter04
  1. Next, create a Python virtual environment for this project:
virtualenv venv
virtualenv venv -p python3 # Create a virtual environment using
python3 interpreter
virtualenv venv -p python2 # Create a virtual environment using
python2 interpreter

We will be using this newly created virtual environment for this project. Each chapter has its own separate virtual environment.

  1. Next, activate...

Downloading and preparing the anime characters dataset

To train a DCGAN network, we need a dataset of anime characters containing cropped faces of the characters. There are multiple ways to collect a dataset. We can either use a publicly available dataset, or we can scrape one, as long as we don't violate the website's scraping policies. In this chapter, we will be scraping images for educational and demonstration purposes only. We have scraped images from pixiv.net using a crawler tool called gallery-dl. This is a command-line tool that can be used to download image collections from websites, such as pixiv.net, exhentai.org, danbooru.donmai.us, and more. It is available at the following link: https://github.com/mikf/gallery-dl.

Downloading the dataset

...

Implementing a DCGAN using Keras

In this section, we will write an implementation of a DCGAN in the Keras framework. Keras is a meta-framework that uses TensorFlow or Teano as a backend. It provides high-level APIs for working with neural networks. It also has pre-built neural network layers, optimizers, regularizers, initializers, and data-preprocessing layers for easy prototyping compared to low-level frameworks, such as TensorFlow. Let's start by writing the implementation of the generator network.

Generator

As mentioned in the Architecture of DCGAN section, the generator network consists of some 2D convolutional layers, upsampling layers, a reshape layer, and a batch normalization layer. In Keras, every operation...

Training the DCGAN

Again, training a DCGAN is similar to training a Vanilla GAN network. It is a four-step process:

  1. Load the dataset.
  2. Build and compile the networks.
  3. Train the discriminator network.
  4. Train the generator network.

We will work on these steps one by one in this section.

Let's start by defining the variables and the hyperparameters:

dataset_dir = "/Path/to/dataset/directory/*.*"
batch_size = 128
z_shape = 100
epochs = 10000
dis_learning_rate = 0.0005
gen_learning_rate = 0.0005
dis_momentum = 0.9
gen_momentum = 0.9
dis_nesterov = True
gen_nesterov = True

Here, we have specified different hyperparameters for the training. We will now see how to load the dataset for the training.

Loading the samples

To train...

Practical applications of DCGAN

DCGANs can be customized for different use cases. The various practical applications of DCGANs include the following:

  • The generation of anime characters: Currently, animators manually draw characters with computer software and sometimes on paper as well. This is a manual process that usually takes a lot of time. With DCGANs, new anime characters can be generated in much less time, hence improving the creative process.
  • The augmentation of datasets: If you want to train a supervised machine learning model, to train a good model, you would require a large dataset. DCGANs can help by augmenting the existing dataset, therefore increasing the size of the dataset required for supervised model training.

  • The generation of MNIST characters: The MNIST dataset contains 60,000 images of handwritten digits. To train a complex supervised learning model, the...

Summary

In this chapter, we have introduced deep convolutional generative adversarial networks. We started with a basic introduction to DCGANs and then explored the architecture of the DCGAN network in depth. After that, we set up the project and installed the necessary dependencies. Then, we looked at the different steps required to download and prepare the dataset. We then prepared a Keras implementation of the network and trained it on our dataset. Once it was trained, we used it to generate new anime characters. We also explored different applications of DCGAN for real-world use cases.

In the next chapter, we will work on SRGANs for high-resolution image generation.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Generative Adversarial Networks Projects
Published in: Jan 2019Publisher: PacktISBN-13: 9781789136678
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
Kailash Ahirwar

Kailash Ahirwar is a machine learning and deep learning enthusiast. He has worked in many areas of Artificial Intelligence (AI), ranging from natural language processing and computer vision to generative modeling using GANs. He is a co-founder and CTO of Mate Labs. He uses GANs to build different models, such as turning paintings into photos and controlling deep image synthesis with texture patches. He is super optimistic about AGI and believes that AI is going to be the workhorse of human evolution.
Read more about Kailash Ahirwar