Generative Adversarial Networks Cookbook - Josh Kalin - E-Book

Generative Adversarial Networks Cookbook E-Book

Josh Kalin

0,0
40,81 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 242

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Generative Adversarial Networks Cookbook

 

Over 100 recipes to build generative models using Python, TensorFlow, and Keras

 

 

 

 

 

 

 

 

Josh Kalin

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Generative Adversarial Networks Cookbook

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Sunith ShettyAcquisition Editor: Devika BattikeContent Development Editor: Unnati GuhaTechnical Editor: Sayli NikaljeCopy Editor: Safis EditingProject Coordinator: Manthan PatelProofreader: Safis EditingIndexer: Tejal Daruwale SoniGraphics: Jisha ChirayilProduction Coordinator: Shraddha Falebhai

First published: December 2018

Production reference: 1311218

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78913-990-7

www.packtpub.com

 
mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

Packt.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. 

To my wife Lara who is 90% inspiration and 90% patience. No, it doesn't add up to 180%. She's just a great multitasker.

Contributors

About the author

 

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).

I thank my mother, father, step-mom, in-laws, grandparents, and friends who supported me in this crazy idea; also, my kids for understanding when dad's pulling his hair out over GANs. Hope one day they understand what the book is about. Special thanks to Jeremiah for listening to me drone on about this book. Finally, I’d thank my amazing wife—without her, nothing could be possible. I can’t thank her enough for pushing me to finish this book.

About the reviewer

Mayur Ravindra Narkhede has a good blend of experience in data science and industrial domain. He is a researcher with a B.Tech in computer science and an M.Tech in CSE with a specialization in Artificial Intelligence.

A data scientist whose core experience lies in building automated end-to-end solutions, he is proficient at applying technology, AI, ML, data mining, and design thinking to better understand and predict improvements in business functions and desirable requirements with growth profitability.

He has worked on multiple advanced solutions, such as ML and predictive model development for the oil and gas industry, financial services, road traffic and transport, life sciences, and the big data platform for asset-intensive industries.

 

 

 

 

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Generative Adversarial Networks Cookbook

About Packt

Why subscribe?

Packt.com

Dedication

Contributors

About the author

About the reviewer

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Sections

Getting ready

How to do it…

How it works…

There's more…

See also

Get in touch

Reviews

Dedication2

What Is a Generative Adversarial Network?

Introduction

Generative and discriminative models

How to do it...

How it works...

A neural network love story

How to do it...

How it works...

Deep neural networks

How to do it...

How it works...

Architecture structure basics

How to do it...

How it works...

Basic building block – generator

How to do it...

How it works...

Basic building block – discriminator

How to do it...

How it works...

Basic building block – loss functions

How to do it...

How it works...

Training

How to do it...

How it works...

GAN pieces come together in different ways

How to do it...

How it works...

What does a GAN output?

How to do it...

How it works...

Working with limited data – style transfer

Dreaming new scenes – DCGAN

Enhancing simulated data – simGAN

Understanding the benefits of a GAN structure

How to do it...

How it works...

Exercise

Data First, Easy Environment, and Data Prep

Introduction

Is data that important?

Getting ready

How to do it...

How it works...

There's more...

But first, set up your development environment

Getting ready

How to do it...

Installing the NVIDIA driver for your GPU

Installing Nvidia-Docker

Purging all older versions of Docker 

Adding package repositories

Installing NVIDIA-Docker2 and reloading the daemon

Testing nvidia-smi through the Docker container

Building a container for development

There's more...

Data types

Getting ready

How to do it...

How it works...

Running this code in the Docker container

There's more...

Data preprocessing

Getting ready

How to do it...

How it works...

There's more...

Anomalous data

Getting ready

How to do it...

Univariate method

There's more...

Balancing data

Getting ready

How to do it...

Sampling techniques

Random undersampling

Random oversampling

Synthetic minority oversampling technique

Ensemble techniques

Bagging

Boosting

AdaBoost

There's more...

Data augmentation

Getting ready

How to do it...

How it works...

There's more...

Exercise

My First GAN in Under 100 Lines

Introduction

From theory to code – a simple example

Getting ready

How to do it...

Discriminator base class

Generator base class

GAN base class

See also

Building a neural network in Keras and TensorFlow

Getting ready

How to do it...

Building the Docker containers

The Docker container

The run file

See also

Explaining your first GAN component – discriminator

Getting ready

How to do it...

Imports

Initialization variables (init in the Discriminator class)

Model definition for the discriminator

Helper methods in the Discriminator class

Explaining your second GAN component – generator

Getting ready

How to do it...

Imports

Generator initialization

Model definition of the generator

Helper methods of the generator

Putting all the GAN pieces together

Getting ready

How it works...

Step 1 – GAN class initialization

Step 2 – model definition

Step 3 – helper functions

Training your first GAN

Getting ready

How to do it...

Training class definition

Imports

init method in class

Load data method

Training method

Helper functions

Run script definition

Training the model and understanding the GAN output

Getting ready

How to do it...

How it works...

Exercise

Dreaming of New Outdoor Structures Using DCGAN

Introduction

What is DCGAN? A simple pseudocode example

Getting ready

How to do it...

Generator

Discriminator

See also

Tools – do I need any unique tools?

Getting ready

How to do it...

The development environment for DCGAN

Downloading and unpacking LSUN data

There's more...

See also

Parsing the data – is our data unique?

Getting ready

How to do it...

Code implementation – generator

Getting ready

How to do it...

Initializing generator – the DCGAN update

Building the DCGAN structure

See also

Code implementation – discriminator

Getting ready

How to do it...

Initializing the Discriminator class

Building the model structure

See also

Training

Getting ready

How to do it...

Changes to class initialization

Understanding the changes in pseudocode

The new and improved training script

Python run script

Shell run script

Evaluation – how do we know it worked?

Getting ready

How it works...

Adjusting parameters for better performance

How to do it...

Training parameters

Discriminator and generator architecture parameters

Exercise

Pix2Pix Image-to-Image Translation

Introduction

Introducing Pix2Pix with pseudocode

Getting ready

How to do it...

Discriminator

Generator

Parsing our dataset

Getting ready

How to do it...

Building the Docker container with a new Dockerfile

Building the auxiliary scripts

Code implementation – generator

Getting ready

How to do it...

Code – the GAN network

Getting ready

How to do it...

Code implementation – discriminator

Getting ready

How it works...

Training

Getting ready

How to do it...

Setting up the class

Training method

Plotting the results

Helper functions

Running the Training Script

Exercise

Style Transfering Your Image Using CycleGAN

Introduction

Pseudocode – how does it work?

Getting ready

How to do it...

What is so powerful about CycleGAN?

Parsing the CycleGAN dataset

Getting ready

How to do it...

Docker implementation

The data download script

What does the data actually look like?

Code implementation – generator

Getting ready

How to do it....

Code implementation – discriminator

Getting ready

How to do it...

Code implementation – GAN

Getting ready

How to do it...

On to training

Getting ready

How to do it...

Initialization

Training  method

Helper method

Exercise

Using Simulated Images To Create Photo-Realistic Eyeballs with SimGAN

Introduction

How SimGAN architecture works

Getting ready

How to do it...

Pseudocode – how does it work?

Getting ready

How to do it...

How to work with training data

Getting ready

How to do it...

Kaggle and its API

Building the Docker image

Running the Docker image

Code implementation – loss functions

Getting ready

How to do it...

Code implementation – generator

Getting ready

How to do it...

Boilerplate items

Model development

Helper functions

Code implementation – discriminator

Getting ready

How to do it...

Boilerplate

Model architecture

Helper functions

Code implementation – GAN

Getting ready

How to do it...

Training the simGAN network

Getting ready

How to do it...

Initialization

Training function

Helper functions

Python run script

Shell run script

Exercise

From Image to 3D Models Using GANs

Introduction

Introduction to using GANs in order to produce 3D models

Getting ready

How to do it...

For a 2D image – learning an encoding space for an image

Training a model using 3D convolutions

Environment preparation

Getting ready

How to do it...

Creating the Docker container

Building the Docker container

Encoding 2D data and matching to 3D objects

Getting ready

How to do it...

Code to run a simple encoder

The shell script to run the encoder with our Docker container

Code implementation – generator

Getting ready

How to do it...

Generator class preparation

Building the generator model

Code implementation – discriminator

Getting ready

How to do it...

Discriminator class preparation

Building the discriminator model

Code implementation – GAN

Getting ready

How to do it...

Training this model

Getting ready

How to do it...

Training class preparation

Helper functions

The training method

Plotting the output of the network

Running the training script

Exercise

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

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 implementation, including CycleGAN, SimGAN, DCGAN, and imitation learning with GANs. Each chapter builds on a common architecture in Python and Keras to explore increasingly difficult GAN architectures in an easy-to-read format.

The Generative Adversarial Networks Cookbook starts by covering the different types of GAN architecture to help you understand how the model works. You will learn how to perform key tasks and operations, such as creating false and high-resolution images, text-to-image synthesis, and generating videos with this recipe-based guide. You will also work with use cases such as DCGAN and deepGAN. To become well versed in the working of complex applications, you will take different real-world datasets 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.

Who this book is for

This book is for data scientists, machine learning (ML) 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 a working knowledge of the Python programming language will help you get the most out of the book.

What this book covers

Chapter 1, What is a Generative Adversarial Network?, introduces you to GAN architectures and looks at the implementation of each of them.

Chapter 2, Data First – Easy Environment and Data Preparation, lays down the groundwork for manipulating data, augmenting your data, and balancing imbalanced datasets or data with massive outliers.

Chapter 3, My First GAN in Under 100 Lines, covers how to take the theory we'll have discussed and produce a simple GAN model using Keras, TensorFlow, and Docker.

Chapter 4, Dreaming of New Outdoor Structures Using DCGAN, covers the building blocks required to build your first deep convolutional generative adversarial network (DCGAN) implementation.

Chapter 5, Pix2Pix Image-to-Image Translation, covers Pix2Pix, how it works, and how it is implemented.

Chapter 6, Style Transfering Your Image Using CycleGAN, explains what CycleGAN is, and how to parse the CycleGAN datasets and implementations.

Chapter 7, Using Simulated Images To Create Photo-Realistic Eyeballs with SimGAN, demonstrates how SimGAN works, and how it is implemented.

Chapter 8, From Images to 3D Models Using GANs, talks about 3D models and techniques to implement these 3D models using images.

To get the most out of this book

A basic knowledge of Python is a prerequisite, while a familiarity with machine learning concepts will be helpful.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packt.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Generative-Adversarial-Networks-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781789139907_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "You can run the nvidia-smi command to know which version of driver is installed on your system."

A block of code is set as follows:

docker volume ls -q -f driver=nvidia-docker

|

xargs -r -I{} -n1 docker ps -q -a -f volume={}

|

xargs -r docker rm -f

Any command-line input or output is written as follows:

sudo ./build.sh

Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Now, click Save and let's check to make sure that we have the appropriate directory structure with our files."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to increase your knowledge of it.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise, in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

Everything not saved will be lost

-Nintendo "Quit Screen" message

What Is a Generative Adversarial Network?

In this chapter, we'll cover the following recipes:

Generative and discriminative models

A neural network love story

Deep neural networks

Architecture structure basics

Basic building block- generator

Basic building block 

 loss functions

Training

GAN pieces come together in different ways

What does a GAN output?

Understanding the benefits of a GAN structure

Introduction

I'm sure you've heard of a neural network dreaming? Maybe you've heard that AI is coming for you? Well, I'm here to tell you that there's no need to worry just yet. A Neural Network dreaming isn't too far away from the truth though. Generative Adversarial Networks (GANs), represent a shift in architecture design for deep neural networks. This new architecture pits two or more neural networks against each other in adversarial training to produce generative models. Throughout this book, we'll focus on covering the basic implementation of this architecture and then focus on modern representations of this new architecture in the form of recipes.

GANs are a hot topic of research today in the field of deep learning. Popularity has soared with this architecture style, with it's ability to produce generative models that are typically hard to learn. There are a number of advantages to using this architecture: it generalizes with limited data, conceives new scenes from small datasets, and makes simulated data look more realistic. These are important topics in deep learning because many techniques today require large amounts of data. Using this new architecture, it's possible to drastically reduce the amount of data needed to complete these tasks. In extreme examples, these types of architectures can use 10% of the data needed for other types of deep learning problems.

By the end of this chapter, you'll have learned about the following concepts:

Do all GANs have the same architecture?

Are there any new concepts within the GAN architecture?

The basic construction of the GAN architecture in practice

Ready, set, go!

Generative and discriminative models

Machine learning (ML) and deep learning can be described by two terms: generative and discriminative modeling. When discussing the machine learning techniques that most people are familiar with, the thinking of a discriminative modeling technique, such as classification.

How to do it...

The difference between these two types of can be described by the following analogy:

Discriminative modeling

: Observe paintings and determine the style of painting based on observations.

Here are a few steps that describe how we would do this in machine learning:

First, we create a machine learning model that use convolutional layers or other learned features to understand the divisions in the data

Next, we collect a dataset that has both a training set (60-90% of your data) and a validation dataset (10-40% of your data)

Train the machine learning model using your data

 Use this model to predict which datapoint belongs to a particular class - in our example, which painting belongs to which author

Generative modeling

: Learn and reproduce paintings in various painters' styles and determine the painting style from the styles you learned.

Here are a few steps to describe a possible way to accomplish this type of modeling:

Create a machine learning model that learns how to reproduce different painting styles

Collect a training and validation dataset

Train the machine learning model using the data

Use this model to predict (inference) to produce examples of the paint author - use similarity metrics to verify the ability of the model to reproduce the painting style.

How it works...

Discriminative models will learn the boundary conditions between classes for a distribution:

Discriminative models get their power from more data

These

 

models

 

are not designed to work in an unsupervised manner or with unlabeled data

This can be described in a more graphical way, as follows:

Generative models will model the distribution of the classes for a given input distribution:

This creates a probabilistic model of each class in order to estimate the distribution

A generative model has the ability to use unlabeled data since it learns labels during the training process

This can be described in a more graphical way, as follows:

So, generative models are incredibly difficult to produce as they have to accurately model and reproduce the input distribution. The discriminative models are learning decision boundaries, which is why neural networks have been incredibly successful in recent years. The GAN architecture represents a radical departure from older techniques in the generative modeling area. We'll cover how neural networks are developed and then dive right in the GAN architecture development.

A neural network love story

Since you've come here to learn more about a specific neural network architecture, we're going to assume you have a baseline understanding of current machine and deep learning techniques that revolve around neural networks. Neural networks have exploded in popularity since the advent of the deep neural network-style architectures. By utilizing many hidden layers and large sums of data, modern deep learning techniques are able to exceed human-level performance in a dizzying number of applications. How is this possible? Neural networks are now able to learn baseline features and relationships in similar ways to our brains. Along those same lines, researchers have been exploring new styles of mixing neural networks to replicate the thought process that our brains take automatically. 

How to do it...

The story is a classic: researcher goes drinking with a few friends and has an epiphany- what if you were able to pit two neural networks against each other to solve a problem? Ian Goodfellow, affectionately called the GANfather, helped popularize this adversarial architecture with his hallmark paper in 2014, called Generative Adversarial Networks. Researchers all over the world began developing variations on this technique: can you pit three or more networks against each other? What happens when you provide more than one loss function? These are actually the types of questions you should be able to answer by the end of this book, because we'll focus on implementing modern renditions of this architecture to solve these types of problems.

How it works...

It's important to understand the difference and difficulties that surround generative and discriminative modeling. In recent years, discriminative modeling has seen some great successes. Typically requiring Markov decision processes in order for the generative modeling process to work, these techniques suffered from a lack of flexibility without heavy design tuning. That is, until the advent of the GANs architecture that we're discussing today. Goodfellow adequately summed up the issues surrounding discriminative and generative models in his paper in 2014:

Goodfellow and his coauthors presented a graphic on the challenges associated with generative modeling in the literature up until 2014

What are Goodfellow and his fellow authors getting at in this screenshot? Essentially, prior generative models were painful to train/build. GANs can have their challenges in terms of training and design, but represent a fundamental shift in flexibility in output given the ease of setup. In the Chapter 3, My First GAN in Under 100 Lines, we'll build a GAN network in under 100 lines of code. 

Deep neural networks