State-of-the-Art Deep Learning Models in TensorFlow: Modern Machine Learning in the Google Colab Ecosystem

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Use TensorFlow 2.x in the Google Colab ecosystem to create state-of-the-art deep learning models guided by hands-on examples. The Colab ecosystem provides a free cloud service with easy access to on-demand GPU (and TPU) hardware acceleration for fast execution of the models you learn to build. This book teaches you state-of-the-art deep learning models in an applied manner with the only requirement being an Internet connection. The Colab ecosystem provides everything else that you need, including Python, TensorFlow 2.x, GPU and TPU support, and Jupyter Notebooks.

The book begins with an example-driven approach to building input pipelines that feed all machine learning models. You will learn how to provision a workspace on the Colab ecosystem to enable construction of effective input pipelines in a step-by-step manner. From there, you will progress into data augmentation techniques and TensorFlow datasets to gain a deeper understanding of how to work with complex datasets. You will find coverage of Tensor Processing Units (TPUs) and transfer learning followed by state-of-the-art deep learning models, including autoencoders, generative adversarial networks, fast style transfer, object detection, and reinforcement learning.

Author Dr. Paper provides all the applied math, programming, and concepts you need to master the content. Examples range from relatively simple to very complex when necessary. Examples are carefully explained, concise, accurate, and complete. Care is taken to walk you through each topic through clear examples written in Python that you can try out and experiment with in the Google Colab ecosystem in the comfort of your own home or office.


What You Will Learn
  • Take advantage of the built-in support of the Google Colab ecosystem
  • Work with TensorFlow data sets
  • Create input pipelines to feed state-of-the-art deep learning models
  • Create pipelined state-of-the-art deep learning models with clean and reliable Python code
  • Leverage pre-trained deep learning models to solve complex machine learning tasks
  • Create a simple environment to teach an intelligent agent to make automated decisions


Who This Book Is For

Readers who want to learn the highly popular TensorFlow deep learning platform, those who wish to master the basics of state-of-the-art deep learning models, and those looking to build competency with a modern cloud service tool such as Google Colab


Author(s): David Paper
Edition: 1
Publisher: Apress
Year: 2021

Language: English
Pages: 398
Tags: deep learning; tensorflow; machine learning; Google Colab

Table of Contents
About the Author
Introduction
Chapter 1: Build TensorFlow Input Pipelines
What Are Input Pipelines?
Why Build Input Pipelines?
Manual Workflow
Automated Workflow
Basic Input Pipeline Mechanics
High-Performance Pipelines
Google Developers Codelabs
Create a New Notebook in Colab
Import the TensorFlow Library
GPU Hardware Accelerator
Create a TensorFlow Dataset
Consume the Dataset
Dataset Structure
Create a Dataset from Memory
Load and Inspect Data
Scale and Create the tf.data.Dataset
Verify Scaling
Check Tensor Shape
Inspect Tensors
Preserve the Shape
Visualize
Define Class Labels
Convert a Numerical Label to a Class Label
Create a Plot of Examples from the Dataset
Build the Consumable Input Pipeline
Configure the Dataset for Performance
Build the Model
Compile and Train the Model
Load a TensorFlow Dataset as NumPy
Inspect Shapes and Pixel Intensity
Scale
Prepare Data for TensorFlow Consumption
Build the Consumable Input Pipeline
Build the Model
Compile and Train the Model
Create a Dataset from Files
Download and Inspect the Dataset
Parse Data with the tf.keras.preprocessing Utility
Set Parameters
Create Training and Test Sets
Inspect Tensors
Get Class Names
Display Examples
Scale the Data
Configure the Dataset for Performance
Build the Input Pipeline
Build the Model
Compile and Train the Model
Get Flowers from Google Cloud Storage
Read Flowers as JPEG Files and Perform Simple Processing
Read and Process Flowers as TFRecord Files
Read TFRecord Files
Set Parameters for Training
Create Functions to Load and Process TFRecord Files
Create Training and Test Sets
Model Data
Summary
Chapter 2: Increase the Diversity of Your Dataset with Data Augmentation
Data Augmentation
Import the TensorFlow Library
GPU Hardware Accelerator
Augment with a Keras API
Get Data
Split Data
Load Images Off Disk into Train and Test Sets
Inspect the Training Set
Get Number of Classes
Create a Scaling Function
Build the Input Pipeline
Data Augmentation with Preprocessing Layers
Display an Augmented Image
Create the Model
Compile and Train the Model
Visualize Performance
Apply Augmentation on Images
Set an Index Variable
Set an Image
Show an Example
Create Functions to Show Images
Crop an Image
Randomly Flip Image Left to Right
Randomly Flip Image Up to Down
Flip Image Up to Down
Rotate Image 90 Degrees
Adjust Gamma
Adjust Contrast
Adjust Brightness
Adjust Saturation
Hue
Apply Transformations Directly on Images
Create an Augmentation Function
Display an Augmented Image
Build the Input Pipeline
Create the Model
Compile and Train the Model
Data Augmentation with ImageGenerator
Process Flowers Data
Create Datasets
Create the Model
Compile and Train the Model
Augment Training Data
Recompile and Train the Model
Inspect the Data
Visualize
Summary
Chapter 3: TensorFlow Datasets
An Introduction to TensorFlow Datasets
Import the TensorFlow Library
GPU Hardware Accelerator
Available Datasets
Load a Dataset
TFDS Metadata
Iterate Over a Dataset
As a Dictionary
As Tuples
As NumPy Arrays
Visualization
tfds.as_dataframe
Take Examples
tfds.show_examples
Load Fashion-MNIST
Metadata
Display Split Information
Visualize
Slicing API
Slicing Instructions
Instructions as Strings
Instructions with the ReadInstruction API
Performance Tips
Auto-caching
Benchmark Datasets
Reloading a TFDS Object
Load Fashion-MNIST as a Single Tensor
Ready Data for TensorFlow Consumption
Build the Input Pipeline
Build the Model
Compile and Train the Model
Load Beans as a tf.data.Dataset
Metadata
Visualize
Check Shapes
Reformat Images
Configure Dataset for Performance
Build the Model
Compile and Train the Model
Predict
Summary
Chapter 4: Deep Learning with TensorFlow Datasets
An Experiment with cats_vs_dogs
Import the TensorFlow Library
GPU Hardware Accelerator
Begin the Experiment
Load the TFDS Object
Metadata
Split the Data
Visualize
Inspect Examples
Reformat Images
Build the Input Pipeline
Visualize and Inspect Examples from a Batch
Build the Model
Compile and Train the Model
Evaluate the Model for Generalizability
Visualize Performance
Augmentation with Preprocessing Layers
Build the Model
Compile and Train the Model
Evaluate the Model for Generalizability
Visualize Performance
Apply Data Augmentation on Images
Build the Input Pipeline
Display an Augmented Image
Build the Model
Compile and Train the Model
Evaluate the Model for Generalizability
Visualize Performance
Predictions
Visualize Predictions
An Experiment with rock_paper_scissors
Configure TensorBoard
Load Data
Inspect the Data
Preprocess the Data
Visualize Processed Data
Augment Training Data
Create Data Augmentation Functions
Augment Train Data
Build the Input Pipeline
Create the Model
Compile and Train
Visualize Performance
Close TensorBoard Server
Chapter 5: Introduction to Tensor Processing Units
TPU Hardware Accelerator
Advantages of Cloud TPU
When to Use Cloud TPU
CPUs
GPUs
TPUs
Import the TensorFlow Library
Enable TPU Runtime
TPU Detection
Configure the TPU for This Notebook
Create a Distribution Strategy
Manual Device Placement
Run a Computation in All TPU Cores
Eager Execution
Experiments
Digits Experiment
Preprocess the Data
Build the Input Pipeline
Model Data Within TPU Scope
MNIST Experiment
Build the Input Pipeline
Model Data Within TPU Scope
Fashion-MNIST Experiment
Transform Datasets into Image and Label Sets
Model Data Within TPU Scope
Save the Trained Model
Make Inferences
Flowers Experiment
Read Flowers Data as TFRecord Files
Set Parameters for Training
Create Functions to Load and Process TFRecord Files
Create Train and Test Sets
Model Data
Make Inferences
Chapter 6: Simple Transfer Learning with TensorFlow Hub
Pre-trained Models for Transfer Learning
Import the TensorFlow Library
GPU Hardware Accelerator
TensorFlow Hub
MobileNet-v2
Flowers MobileNet-v2 Experiment
Load Flowers as a TFDS Object
Explore Metadata
Display Images and Shapes
Build the Input Pipeline
Create a Feature Vector
Freeze the Pre-trained Model
Create a Classification Head
Compile and Train the Model
Visualize Performance
Make Predictions from Test Data
Inspect the First Prediction
Inspect the First Batch of Predictions
Plot Predictions
Flowers Inception-v3 Experiment
Build the Input Pipeline
Build the Model
Compile and Train
Visualize Performance
Predictions
Plot Model Predictions
Summary
Chapter 7: Advanced Transfer Learning
Transfer Learning
Import the TensorFlow Library
GPU Hardware Accelerator
Beans Experiment
Load Beans
Explore the Data
Visualize
Reformat Images
Build the Input Pipeline
Model with Xception
Create a Model
Model the Data
Model Trained Data with Unfrozen Layers
Model Beans with Inception
Create a Model
Model the Data
Model Trained Data with Unfrozen Layers
Generalize on Unseen Data
Stanford Dogs Experiment
Model Stanford Dogs with MobileNet
Load Data
Metadata
Visualize Examples
Check Image Shape
Build the Input Pipeline
Create the Model
Compile and Train
Model Trained Data with Unfrozen Layers
Generalize
Flowers Experiment
Read Flowers as TFRecords
Create Data Splits
Create Functions to Load and Process TFRecord Files
Create Training and Test Sets
Create the Model
Compile and Train
Visualize
Generalize
Rock-Paper-Scissors Experiment
Load the Data
Visualize
Build the Input Pipeline
Create the Model
Compile and Train
Visualize
Generalize
Tips and Concepts
Chapter 8: Stacked Autoencoders
Import the TensorFlow Library
GPU Hardware Accelerator
Basic Stacked Encoder Experiment
Load Data
Scale Data
Build the Stacked Encoder Model
Compile and Train
Visualize Performance
Visualize the Reconstructions
Breakdown
Dimensionality Reduction
Tying Weights Experiment
Define a Custom Layer
Build the Tied Weights Model
Compile and Train
Visualize Training Performance
Visualize Reconstructions
Denoising Experiment
Build the Denoising Model
Compile and Train
Visualize Training Performance
Visualize Reconstructions
Dropout Experiment
Build the Dropout Model
Compile and Train
Visualize Training Performance
Visualize Reconstructions
Summary
Chapter 9: Convolutional and Variational Autoencoders
Import the TensorFlow Library
GPU Hardware Accelerator
Convolutional Encoder Experiment
Load Data
Inspect Data
Display Examples
Get Training Data
Inspect Shapes
Preprocess Image Data
Create a Convolutional Autoencoder
Compile and Train
Visualize Training Performance
Visualize Reconstructions
Variational Autoencoder Experiment
Load Data
Inspect Data
Scale
Create a Custom Layer to Sample Codings
Create the VAE Model
Compile and Train
Add Latent Loss and Reconstruction Loss to the Model
Visualize Reconstructions
Generate New Images
TFP Experiment
Create a TFP VAE Model
TFP VAE Encoder
TFP VAE Decoder
Build the TFP VAE Model
Compile and Train
Efficacy Test
Summary
Chapter 10: Generative Adversarial Networks
Import the TensorFlow Library
GPU Hardware Accelerator
GAN Experiment
Load Data
Scale
Build the GAN
Compile the Discriminator Model
Build the Input Pipeline
Create a Custom Loop for Training
Train the GAN
DCGAN Experiment with Small Images
Create the Generator
Create the Discriminator
Create the DCGAN
Compile the Discriminator Model
Reshape
Build the Input Pipeline
Train
Generate Images with the Trained Generator
DCGAN Experiment with Large Images
Inspect Metadata
Load Data for Training
Massage the Data
Build the Input Pipeline
Build the Model
Compile the Discriminator Model
Rescale
Train Model and Generate Images
Summary
Chapter 11: Progressive Growing Generative Adversarial Networks
Latent Space Learning
Import the TensorFlow Library
GPU Hardware Accelerator
Create Environment for Experiments
Install Packages for Creating Animations
Install Libraries
Create Functions for Image Display
Create Latent Space Dimensions
Set Verbosity for Error Logging
Image Generation Experiment
Create a Function to Interpolate a Hypersphere
Load the Pre-trained Model
Generate and Display an Image
Create a Function to Generate Multiple Images
Create an Animation
Display Interpolated Image Vectors
Display Multiple Images from a Single Vector
Create a Target Latent Vector from an Uploaded Image
Custom Loop Learning Experiment
Create the Feature Vector
Display an Image from the Feature Vector
Create the Target Image
Create a Function to Find the Closest Latent Vector
Create the Loss Function
Train the Model
Training Loss
Animate
Compare Learned Images to the Target
Try a Different Loss Function Algorithm
Create a Target from an Uploaded Image
Create a Target from a Google Drive Image
Create a Target from a Wikimedia Commons Image
Latent Vectors and Image Arrays
Generate a New Image from a Latent Vector
Generate a New Image from an Image Vector
Summary
Chapter 12: Fast Style Transfer
Why Is Style Transfer Important?
Arbitrary Neural Artistic Stylization
Import the TensorFlow Library
GPU Hardware Accelerator
ANAS Experiment
Import Requisite Libraries
Get Images from Google Drive
Preprocess Images
Display Processed Images
Prepare Image Batches
Load the Model
Feed the Model
Explore the Pastiche
Visualize
Image Stylization with Multiple Images
Get Images
Process Images
Visualize Processed Images
Create Reference Dictionaries
Create a Pastiche
TensorFlow Lite Experiment
Architecture for a Pre-trained TensorFlow Lite Model
Crop Images
Display Cropped Images
Stylize Images
Create the Style Prediction Model
Create the Style Transform Model
Create the Pastiche
Style Blending
Prepare the Content Image
Blend the Style Bottleneck Vectors
Save the Pastiche
Summary
Chapter 13: Object Detection
Object Detection in a Natural Scene
Detection vs. Classification
Bounding Boxes
Basic Structure
Import the TensorFlow Library
GPU Hardware Accelerator
Object Detection Experiment
Import Requisite Libraries
Create Functions for the Experiment
Load a Pre-trained Object Detection Model
Load an Image from Google Drive
Prepare the Image
Run Object Detection on the Image
Detect Images from Complex Scenes
Create a Download Function
Load an Image Scene
Detect
Detect on More Scenes
Find the Source
Find the Source of a Wikipedia Commons Image
Summary
Chapter 14: An Introduction to Reinforcement Learning
Challenges of Reinforcement Learning
Import the TensorFlow Library
GPU Hardware Accelerator
Reinforcement Learning Experiment
Install and Configure OpenAI Gym on Colab
Import Libraries
Create an Environment
Display the Rendering from the Environment
Display Actions
Simple Neural Network Reward Policy
Model Predictions
Animate
Implement a Basic Reward Policy
Reinforce Policy Gradient Algorithm
How Do Policy Gradients Work?
Train the Model with Policy Gradients
Discount and Normalize the Rewards
Train the Learner
Render the Frames from the Reinforce Policy Gradient Algorithm
Animate the Policy
Summary
Index