O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generative Adversarial Networks Cookbook

Book Description

Simplify next-generation deep learning by implementing powerful generative models using Python, TensorFlow and Keras

Key Features

  • Understand the common architecture of different types of GANs
  • Train, optimize, and deploy GAN applications using TensorFlow and Keras
  • Build generative models with real-world data sets, including 2D and 3D data

Book Description

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.

What you will learn

  • Structure a GAN architecture in pseudocode
  • Understand the common architecture for each of the GAN models you will build
  • Implement different GAN architectures in TensorFlow and Keras
  • Use different datasets to enable neural network functionality in GAN models
  • Combine different GAN models and learn how to fine-tune them
  • Produce a model that can take 2D images and produce 3D models
  • Develop a GAN to do style transfer with Pix2Pix

Who this book is for

This book is for data scientists, machine learning developers, and deep learning practitioners looking for a quick reference to tackle challenges and tasks in the GAN domain. Familiarity with machine learning concepts and working knowledge of Python programming language will help you get the most out of the book.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Generative Adversarial Networks Cookbook
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Dedication
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Get in touch
      1. Reviews
  7. Dedication2
  8. What Is a Generative Adversarial Network?
    1. Introduction
    2. Generative and discriminative models
      1. How to do it...
      2. How it works...
    3. A neural network love story
      1. How to do it...
      2. How it works...
    4. Deep neural networks
      1. How to do it...
      2. How it works...
    5. Architecture structure basics
      1. How to do it...
      2. How it works...
    6. Basic building block – generator
      1. How to do it...
      2. How it works...
    7. Basic building block – discriminator
      1. How to do it...
      2. How it works...
    8. Basic building block – loss functions
      1. How to do it...
      2. How it works...
    9. Training
      1. How to do it...
      2. How it works...
    10. GAN pieces come together in different ways
      1. How to do it...
      2. How it works...
    11. What does a GAN output?
      1. How to do it...
      2. How it works...
        1. Working with limited data – style transfer
        2. Dreaming new scenes – DCGAN
        3. Enhancing simulated data – simGAN
    12. Understanding the benefits of a GAN structure
      1. How to do it...
      2. How it works...
    13. Exercise
  9. Data First, Easy Environment, and Data Prep
    1. Introduction
    2. Is data that important?
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. But first, set up your development environment
      1. Getting ready
      2. How to do it...
        1. Installing the NVIDIA driver for your GPU
        2. Installing Nvidia-Docker
          1. Purging all older versions of Docker 
          2. Adding package repositories
          3. Installing NVIDIA-Docker2 and reloading the daemon
          4. Testing nvidia-smi through the Docker container
        3. Building a container for development
      3. There's more...
    4. Data types
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Running this code in the Docker container
      4. There's more...
    5. Data preprocessing
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Anomalous data
      1. Getting ready
      2. How to do it...
        1. Univariate method
      3. There's more...
    7. Balancing data
      1. Getting ready
      2. How to do it...
        1. Sampling techniques
          1. Random undersampling
          2. Random oversampling
          3. Synthetic minority oversampling technique
        2. Ensemble techniques
          1. Bagging
          2. Boosting
          3. AdaBoost
      3. There's more...
    8. Data augmentation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    9. Exercise
  10. My First GAN in Under 100 Lines
    1. Introduction
    2. From theory to code – a simple example
      1. Getting ready
      2. How to do it...
        1. Discriminator base class
        2. Generator base class
        3. GAN base class
      3. See also
    3. Building a neural network in Keras and TensorFlow
      1. Getting ready
      2. How to do it...
        1. Building the Docker containers
        2. The Docker container
        3. The run file
      3. See also
    4. Explaining your first GAN component – discriminator
      1. Getting ready
      2. How to do it...
        1. Imports
        2. Initialization variables (init in the Discriminator class)
        3. Model definition for the discriminator
        4. Helper methods in the Discriminator class
    5. Explaining your second GAN component – generator
      1. Getting ready
      2. How to do it...
        1. Imports
        2. Generator initialization
        3. Model definition of the generator
        4. Helper methods of the generator
    6. Putting all the GAN pieces together
      1. Getting ready
      2. How it works...
        1. Step 1 – GAN class initialization
        2. Step 2 – model definition
        3. Step 3 – helper functions
    7. Training your first GAN
      1. Getting ready
      2. How to do it...
        1. Training class definition
          1. Imports
          2. init method in class
          3. Load data method
          4. Training method
          5. Helper functions
        2. Run script definition
    8. Training the model and understanding the GAN output
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Exercise
  11. Dreaming of New Outdoor Structures Using DCGAN
    1. Introduction
    2. What is DCGAN? A simple pseudocode example
      1. Getting ready
      2. How to do it...
        1. Generator
        2. Discriminator
      3. See also
    3. Tools – do I need any unique tools?
      1. Getting ready
      2. How to do it...
        1. The development environment for DCGAN
        2. Downloading and unpacking LSUN data
      3. There's more...
      4. See also
    4. Parsing the data – is our data unique?
      1. Getting ready
      2. How to do it...
    5. Code implementation – generator
      1. Getting ready
      2. How to do it...
        1. Initializing generator – the DCGAN update
        2. Building the DCGAN structure
      3. See also
    6. Code implementation – discriminator
      1. Getting ready
      2. How to do it...
        1. Initializing the Discriminator class
        2. Building the model structure
      3. See also
    7. Training
      1. Getting ready
      2. How to do it...
        1. Changes to class initialization
        2. Understanding the changes in pseudocode
        3. The new and improved training script
        4. Python run script
        5. Shell run script
    8. Evaluation – how do we know it worked?
      1. Getting ready
      2. How it works...
    9. Adjusting parameters for better performance
      1. How to do it...
        1. Training parameters
        2. Discriminator and generator architecture parameters
    10. Exercise
  12. Pix2Pix Image-to-Image Translation
    1. Introduction
    2. Introducing Pix2Pix with pseudocode
      1. Getting ready
      2. How to do it...
        1. Discriminator
        2. Generator
    3. Parsing our dataset
      1. Getting ready
      2. How to do it...
        1. Building the Docker container with a new Dockerfile
        2. Building the auxiliary scripts
    4. Code implementation – generator
      1. Getting ready
      2. How to do it...
    5. Code – the GAN network
      1. Getting ready
      2. How to do it...
    6. Code implementation – discriminator
      1. Getting ready
      2. How it works...
    7. Training
      1. Getting ready
      2. How to do it...
        1. Setting up the class
        2. Training method
        3. Plotting the results
        4. Helper functions
        5. Running the Training Script
    8. Exercise
  13. Style Transfering Your Image Using CycleGAN
    1. Introduction
    2. Pseudocode – how does it work?
      1. Getting ready
      2. How to do it...
        1. What is so powerful about CycleGAN?
    3. Parsing the CycleGAN dataset
      1. Getting ready
      2. How to do it...
        1. Docker implementation
        2. The data download script
        3. What does the data actually look like?
    4. Code implementation – generator
      1. Getting ready
      2. How to do it....
    5. Code implementation – discriminator
      1. Getting ready
      2. How to do it...
    6. Code implementation – GAN
      1. Getting ready
      2. How to do it...
    7. On to training
      1. Getting ready
      2. How to do it...
        1. Initialization
        2. Training  method
        3. Helper method
    8. Exercise
  14. Using Simulated Images To Create Photo-Realistic Eyeballs with SimGAN
    1. Introduction
    2. How SimGAN architecture works
      1. Getting ready
      2. How to do it...
    3. Pseudocode – how does it work?
      1. Getting ready
      2. How to do it...
    4. How to work with training data
      1. Getting ready
      2. How to do it...
        1. Kaggle and its API
        2. Building the Docker image
        3. Running the Docker image
    5. Code implementation – loss functions
      1. Getting ready
      2. How to do it...
    6. Code implementation – generator
      1. Getting ready
      2. How to do it...
        1. Boilerplate items
        2. Model development
        3. Helper functions
    7. Code implementation – discriminator
      1. Getting ready
      2. How to do it...
        1. Boilerplate
        2. Model architecture
        3. Helper functions
    8. Code implementation – GAN
      1. Getting ready
      2. How to do it...
    9. Training the simGAN network
      1. Getting ready
      2. How to do it...
        1. Initialization
        2. Training function
        3. Helper functions
        4. Python run script
        5. Shell run script
    10. Exercise
  15. From Image to 3D Models Using GANs
    1. Introduction
    2. Introduction to using GANs in order to produce 3D models
      1. Getting ready
      2. How to do it...
        1. For a 2D image – learning an encoding space for an image
        2. Training a model using 3D convolutions
    3. Environment preparation
      1. Getting ready
      2. How to do it...
        1. Creating the Docker container
        2. Building the Docker container
    4. Encoding 2D data and matching to 3D objects
      1. Getting ready
      2. How to do it...
        1. Code to run a simple encoder
        2. The shell script to run the encoder with our Docker container
    5. Code implementation – generator
      1. Getting ready
      2. How to do it...
        1. Generator class preparation
        2. Building the generator model
    6. Code implementation – discriminator
      1. Getting ready
      2. How to do it...
        1. Discriminator class preparation
        2. Building the discriminator model
    7. Code implementation – GAN
      1. Getting ready
      2. How to do it...
    8. Training this model
      1. Getting ready
      2. How to do it...
        1. Training class preparation
        2. Helper functions
        3. The training method
        4. Plotting the output of the network
        5. Running the training script
    9. Exercise
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think