Reader small image

You're reading from  Generative Adversarial Networks Cookbook

Product typeBook
Published inDec 2018
PublisherPackt
ISBN-139781789139907
Edition1st Edition
Right arrow
Author (1)
Josh Kalin
Josh Kalin
author image
Josh Kalin

Josh Kalin is a Physicist and Technologist focused on the intersection of robotics and machine learning. Josh works on advanced sensors, industrial robotics, machine learning, and automated vehicle research projects. Josh holds degrees in Physics, Mechanical Engineering, and Computer Science. In his free time, he enjoys working on cars (has owned 36 vehicles and counting), building computers, and learning new techniques in robotics and machine learning (like writing this book).
Read more about Josh Kalin

Right arrow

Chapter 6. Style Transfering Your Image Using CycleGAN

The following recipes will be covered in this chapter:

  • Pseudocode – how does it work?
  • Parsing the CycleGAN datasets
  • Code implementation – generator
  • Code implementation – discriminator
  • Code implementation – GAN
  • On to training

Introduction


CycleGAN is one of the most well-known architectures in the GAN community for good reason. It doesn't require paired training data to produce stunning style transfer results. As you'll see in this chapter, we're going to go over the basic structure of the model and the results you can expect when you use it. 

Pseudocode – how does it work?


This recipe will focus on dissecting the internal pieces of the CycleGAN paper (https://arxiv.org/pdf/1703.10593.pdf)—the structure they propose, simple tips they suggest throughout their development, and any potential metrics that we may want to use in our development for this chapter.

Getting ready

For this recipe, you will simply need to create a folder for this chapter's code in your home directory. As a reminder, ensure that you've completed all of the prerequisite installation steps such as installing Docker, Nvidia-Docker, and the Nvidia drivers. Last, grab the CycleGAN paper (https://arxiv.org/pdf/1703.10593.pdf) and make sure to read it before you go on to the next section.

How to do it...

As with every chapter, I'd like to encourage you to begin by reading the paper that this particular algorithm was derived from. The paper provides a foundation for implementing the paper and grounds assumptions that we make during the development. For instance, we won...

Parsing the CycleGAN dataset


You'll get tired of hearing how important data is to us—but honestly, it can make or break your development. In our case, we are going to simply use the same datasets that the original CycleGAN authors used in their development. This has two use cases: we can compare our results to theirs and we can take advantage of their small curated datasets.

Getting ready

So far, we've focused on just reviewing the structure of how we will solve the problem. As with every one of these chapters, we need to spend a few minutes collecting training data for our experiments. Replicate the directory structure with files, as seen as follows:

├── data
│   ├── 
├── docker
│   ├── build.sh
│   ├── clean.sh
│   └── Dockerfile
├── README.md
├── run.sh
├── scripts
│   └── create_data.sh
├── src
│   ├── 

We'll go and introduce the files you'll need to build so you can have a development environment and data to work with on CycleGAN.

How to do it...

This should start to become a habit by now...

Code implementation – generator


It might seem obvious by now but each of the generators we've built until this point has been an incremental improvement on the last GAN to Deep Convolutional Generative Adversarial Network (DCGAN) to CycleGAN will represent a similar incremental change in the generator code. In this case, we'll downsample for a few blocks then upsample. We'll also introduce a new layer called InstanceNormalization that the authors used to enforce better training for style transfer.

Getting ready

Every recipe is going to demonstrate the structure that you should have in your directory. This ensures that you've got the right files at each step of the way:

├── data
│   ├── 
├── docker
│   ├── build.sh
│   ├── clean.sh
│   └── Dockerfile
├── README.md
├── run.sh
├── scripts
│   └── create_data.sh
├── src
│   ├── generator.py

How to do it....

With the generator, we will replicate the paper with the number of filters and the block style.

These are the steps for this:

  1. Imports will match...

Code implementation – discriminator


Discriminators are the bread and butter of the discriminative modeling world—it's funny that we use them in such a unique way. Each discriminator that're designed is built to understand the difference between real and fake data but not too well. Why? If the discriminator could always tell the difference between the two types of data then the generator would never improve consistently. The next discriminator, based on the CycleGAN paper, will use a structure heavily based on their original implementation.

Getting ready

Your directory structure should look like the following tree:

├── data
│   ├── 
├── docker
│   ├── build.sh
│   ├── clean.sh
│   └── Dockerfile
├── README.md
├── run.sh
├── scripts
│   └── create_data.sh
├── src
│   ├── discriminator.py
│   ├── generator.py

How to do it...

The discriminator takes the image as input and outputs a decision (real or fake). We'll cover the general construction of the discriminator class (hint: it'll look pretty similar...

Code implementation – GAN


Building the GAN is a core step with every one of these architectures—we have to be somewhat careful with CycleGAN because it's one of the first times we are going to develop a multilevel model. The GAN model will have six models in adversarial training mode—let's build it!

Getting ready

Every recipe is going to demonstrate the structure that you should have in your directory. This ensures that you've got the right files at each step of the way:

├── data
│   ├── 
├── docker
│   ├── build.sh
│   ├── clean.sh
│   └── Dockerfile
├── README.md
├── run.sh
├── scripts
│   └── create_data.sh
├── src
│   ├── generator.py
│   ├── discriminator.py
│   ├── gan.py

How to do it...

The code is quite simple but the power of Keras really shines here—we are able to place six separate models into adversarial training in under 50 lines of code.

These are the steps for this:

  1. Make sure to get your imports for the implementation phase of the code:
#!/usr/bin/env python3
import sys
import numpy...

On to training


Here we are again—our ole friend training. Training for CycleGAN has its own idiosyncratic components but you'll notice quite a bit of similarities with our previous chapters. You should be on the lookout for additional training steps—because we are training multiple generators and discriminators, we are increasing the time per batch and consequently per epoch significantly. The only advantage is that our batch in this base is only a single image. 

Getting ready

Your directory should match the following tree—if you don't have the Python files beneath src, simply make sure to add the blank files for run.py and train.py and we will fill in the code throughout this recipe:

├── data
│   ├── 
├── docker
│   ├── build.sh
│   ├── clean.sh
│   └── Dockerfile
├── README.md
├── run.sh
├── scripts
│   └── create_data.sh
├── src
│   ├── discriminator.py
│   ├── gan.py
│   ├── generator.py
│   ├── run.py
│   ├── save_to_npy.py
│   └── train.py

Training can be broken into a few key components...

Exercise


  1. Can you rewrite the discriminator and generator in more compact methods?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Generative Adversarial Networks Cookbook
Published in: Dec 2018Publisher: PacktISBN-13: 9781789139907
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
Josh Kalin

Josh Kalin is a Physicist and Technologist focused on the intersection of robotics and machine learning. Josh works on advanced sensors, industrial robotics, machine learning, and automated vehicle research projects. Josh holds degrees in Physics, Mechanical Engineering, and Computer Science. In his free time, he enjoys working on cars (has owned 36 vehicles and counting), building computers, and learning new techniques in robotics and machine learning (like writing this book).
Read more about Josh Kalin