Book Image

Generative Adversarial Networks Cookbook

By : Josh Kalin
Book Image

Generative Adversarial Networks Cookbook

By: Josh Kalin

Overview of this book

Developing Generative Adversarial Networks (GANs) is a complex task, and it is often hard to find code that is easy to understand. This book leads you through eight different examples of modern GAN implementations, including CycleGAN, simGAN, DCGAN, and 2D image to 3D model generation. Each chapter contains useful recipes to build on a common architecture in Python, TensorFlow and Keras to explore increasingly difficult GAN architectures in an easy-to-read format. The book starts by covering the different types of GAN architecture to help you understand how the model works. This book also contains intuitive recipes to help you work with use cases involving DCGAN, Pix2Pix, and so on. To understand these complex applications, you will take different real-world data sets and put them to use. By the end of this book, you will be equipped to deal with the challenges and issues that you may face while working with GAN models, thanks to easy-to-follow code solutions that you can implement right away.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
About Packt
Dedication
Contributors
Preface
Dedication2
Index

Code – the GAN network


The GAN network combines the discriminator and generator from previous recipes into a conditional adversarial configuration for training.

Getting ready

Keep track of the fact that you remembered to add gan.py to your working directory:

├── docker
│   ├── build.sh
│   ├── clean.sh
│   └── Dockerfile
├── README.md
├── run.sh
└── src
|   ├── generator.py
|   ├── gan.py

How to do it...

The GAN network in this case is arguably the easiest part to implement—we're simply going to link up our networks so they can train together:

  1. Import all of the libraries we need to use for this class:
#!/usr/bin/env python3
import sys
import numpy as np
from keras.models import Sequential, Model
from keras.layers import Input
from keras.optimizers import Adam, SGD
from keras.utils import plot_model

 

  1. Implement the init class with the Adam optimizer and then an array of model_inputs and model_outputs:
class GAN(object):
    def __init__(self, model_inputs=[],model_outputs=[]):
        self.inputs ...