Advanced Deep Learning with TensorFlow 2 and Keras (Updated for 2nd Edition)

Overview

Advanced Deep Learning with TensorFlow 2 and Keras (Updated for 2nd Edition)

This is the code repository for Advanced Deep Learning with TensoFlow 2 and Keras, published by Packt. It contains all the supporting project files necessary to work through the book from start to finish.

Please note that the code examples have been updated to support TensorFlow 2.0 Keras API only.

About the Book

Advanced Deep Learning with TensorFlow 2 and Keras, Second Edition is a completely updated edition of the bestselling guide to the advanced deep learning techniques available today. Revised for TensorFlow 2.x, this edition introduces you to the practical side of deep learning with new chapters on unsupervised learning using mutual information, object detection (SSD), and semantic segmentation (FCN and PSPNet), further allowing you to create your own cutting-edge AI projects.

Using Keras as an open-source deep learning library, the book features hands-on projects that show you how to create more effective AI with the most up-to-date techniques.

Starting with an overview of multi-layer perceptrons (MLPs), convolutional neural networks (CNNs), and recurrent neural networks (RNNs), the book then introduces more cutting-edge techniques as you explore deep neural network architectures, including ResNet and DenseNet, and how to create autoencoders. You will then learn about GANs, and how they can unlock new levels of AI performance.

Next, you’ll discover how a variational autoencoder (VAE) is implemented, and how GANs and VAEs have the generative power to synthesize data that can be extremely convincing to humans. You'll also learn to implement DRL such as Deep Q-Learning and Policy Gradient Methods, which are critical to many modern results in AI.

Related Products

Installation

It is recommended to run within conda enviroment. Pls download Anacoda from: Anaconda. To install anaconda:

sh

A machine with at least 1 NVIDIA GPU (1060 or better) is required. The code examples have been tested on 1060, 1080Ti, RTX 2080Ti, V100, RTX Quadro 8000 on Ubuntu 18.04 LTS. Below is a rough guide to install NVIDIA driver and CuDNN to enable GPU support.

sudo add-apt-repository ppa:graphics-drivers/ppa

sudo apt update

sudo ubuntu-drivers autoinstall

sudo reboot

nvidia-smi

At the time of writing, nvidia-smishows the NVIDIA driver version is 440.64 and CUDA version is 10.2.

We are almost there. The last set of packages must be installed as follows. Some steps might require sudo access.

conda create --name packt

conda activate packt

cd

git clone https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras

cd Advanced-Deep-Learning-with-Keras

pip install -r requirements.txt

sudo apt-get install python-pydot

sudo apt-get install ffmpeg

Test if a simple model can be trained without errors:

cd chapter1-keras-quick-tour

python3 mlp-mnist-1.3.2.py

The final output shows the accuracy of the trained model on MNIST test dataset is about 98.2%.

Alternative TensorFlow Installation

If you are having problems with CUDA libraries (ie tf could not load or find libcudart.so.10.X), TensorFlow and CUDA libraries can be installed together using conda:

pip uninstall tensorflow-gpu
conda install -c anaconda tensorflow-gpu

Advanced Deep Learning with TensorFlow 2 and Keras code examples used in the book.

Chapter 1 - Introduction

  1. MLP on MNIST
  2. CNN on MNIST
  3. RNN on MNIST

Chapter 2 - Deep Networks

  1. Functional API on MNIST
  2. Y-Network on MNIST
  3. ResNet v1 and v2 on CIFAR10
  4. DenseNet on CIFAR10

Chapter 3 - AutoEncoders

  1. Denoising AutoEncoders

Sample outputs for random digits:

Random Digits

  1. Colorization AutoEncoder

Sample outputs for random cifar10 images:

Colorized Images

Chapter 4 - Generative Adversarial Network (GAN)

  1. Deep Convolutional GAN (DCGAN)

Radford, Alec, Luke Metz, and Soumith Chintala. "Unsupervised representation learning with deep convolutional generative adversarial networks." arXiv preprint arXiv:1511.06434 (2015).

Sample outputs for random digits:

Random Digits

  1. Conditional (GAN)

Mirza, Mehdi, and Simon Osindero. "Conditional generative adversarial nets." arXiv preprint arXiv:1411.1784 (2014).

Sample outputs for digits 0 to 9:

Zero to Nine

Chapter 5 - Improved GAN

  1. Wasserstein GAN (WGAN)

Arjovsky, Martin, Soumith Chintala, and Léon Bottou. "Wasserstein GAN." arXiv preprint arXiv:1701.07875 (2017).

Sample outputs for random digits:

Random Digits

  1. Least Squares GAN (LSGAN)

Mao, Xudong, et al. "Least squares generative adversarial networks." 2017 IEEE International Conference on Computer Vision (ICCV). IEEE, 2017.

Sample outputs for random digits:

Random Digits

  1. Auxiliary Classfier GAN (ACGAN)

Odena, Augustus, Christopher Olah, and Jonathon Shlens. "Conditional image synthesis with auxiliary classifier GANs. Proceedings of the 34th International Conference on Machine Learning, Sydney, Australia, PMLR 70, 2017."

Sample outputs for digits 0 to 9:

Zero to Nine

Chapter 6 - GAN with Disentangled Latent Representations

  1. Information Maximizing GAN (InfoGAN)

Chen, Xi, et al. "Infogan: Interpretable representation learning by information maximizing generative adversarial nets." Advances in Neural Information Processing Systems. 2016.

Sample outputs for digits 0 to 9:

Zero to Nine

  1. Stacked GAN

Huang, Xun, et al. "Stacked generative adversarial networks." IEEE Conference on Computer Vision and Pattern Recognition (CVPR). Vol. 2. 2017

Sample outputs for digits 0 to 9:

Zero to Nine

Chapter 7 - Cross-Domain GAN

  1. CycleGAN

Zhu, Jun-Yan, et al. "Unpaired Image-to-Image Translation Using Cycle-Consistent Adversarial Networks." 2017 IEEE International Conference on Computer Vision (ICCV). IEEE, 2017.

Sample outputs for random cifar10 images:

Colorized Images

Sample outputs for MNIST to SVHN:

MNIST2SVHN

Chapter 8 - Variational Autoencoders (VAE)

  1. VAE MLP MNIST
  2. VAE CNN MNIST
  3. Conditional VAE and Beta VAE

Kingma, Diederik P., and Max Welling. "Auto-encoding Variational Bayes." arXiv preprint arXiv:1312.6114 (2013).

Sohn, Kihyuk, Honglak Lee, and Xinchen Yan. "Learning structured output representation using deep conditional generative models." Advances in Neural Information Processing Systems. 2015.

I. Higgins, L. Matthey, A. Pal, C. Burgess, X. Glorot, M. Botvinick, S. Mohamed, and A. Lerchner. β-VAE: Learning basic visual concepts with a constrained variational framework. ICLR, 2017.

Generated MNIST by navigating the latent space:

MNIST

Chapter 9 - Deep Reinforcement Learning

  1. Q-Learning
  2. Q-Learning on Frozen Lake Environment
  3. DQN and DDQN on Cartpole Environment

Mnih, Volodymyr, et al. "Human-level control through deep reinforcement learning." Nature 518.7540 (2015): 529

DQN on Cartpole Environment:

Cartpole

Chapter 10 - Policy Gradient Methods

  1. REINFORCE, REINFORCE with Baseline, Actor-Critic, A2C

Sutton and Barto, Reinforcement Learning: An Introduction

Mnih, Volodymyr, et al. "Asynchronous methods for deep reinforcement learning." International conference on machine learning. 2016.

Policy Gradient on MountainCar Continuous Environment:

Car

Chapter 11 - Object Detection

  1. Single-Shot Detection

Single-Shot Detection on 3 Objects SSD

Chapter 12 - Semantic Segmentation

  1. FCN

  2. PSPNet

Semantic Segmentation

Semantic Segmentation

Chapter 13 - Unsupervised Learning using Mutual Information

  1. Invariant Information Clustering

  2. MINE: Mutual Information Estimation

MINE MINE

Citation

If you find this work useful, please cite:

@book{atienza2020advanced,
  title={Advanced Deep Learning with TensorFlow 2 and Keras: Apply DL, GANs, VAEs, deep RL, unsupervised learning, object detection and segmentation, and more},
  author={Atienza, Rowel},
  year={2020},
  publisher={Packt Publishing Ltd}
}
Issues
  •  Error occurred when finalizing GeneratorDataset iterator | Trying to run image segmentation on custom dataset

    Error occurred when finalizing GeneratorDataset iterator | Trying to run image segmentation on custom dataset

    Hello, I am trying to train the segmentation model on my custom dataset, which has two classes 1: Copper and 2:Belmouth. I used generate_gt_segmentation.py codes to convert JSON to .npy file. While running the training process on google colab tf version 2.4.1 getting following error:

    tensorflow.python.framework.errors_impl.ResourceExhaustedError:  OOM when allocating tensor with shape[4,16,720,1280] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
    	 [[node fcn/ResNet56v2/conv2d_18/Conv2D (defined at fcn-12.3.1.py:154) ]]
    Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.
     [Op:__inference_train_function_16535]
    
    Function call stack:
    train_function
    
    2021-04-05 18:46:10.106657: W tensorflow/core/kernels/data/generator_dataset_op.cc:107] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter state is not initialized. The process may be terminated.
    	 [[{{node PyFunc}}]]
    

    I also noticed that data_generator.py code give the # of class = 4, which is wrong also number of classes supposed to be 3. How may I solve the issue?

    opened by MLHafizur 6
  • z_mean does not change in CVAE CNN

    z_mean does not change in CVAE CNN

    After the model CVAE CNN is trained, the values of z_mean encoded on the test set is the same.

    opened by tuthang102 4
  • SSD Data Generator in Keras

    SSD Data Generator in Keras

    Hi,

    I have a question regarding on of the methods in DataGenerator ('data_generator.py') within the SSD model in Keras.

    In the method def get_n_boxes , self.n_boxes is obtained as follows: self.n_boxes += np.prod(shape) // self.n_anchors

    However, shouldnt it be obtained in this way?: self.n_boxes += np.prod(shape) * self.n_anchors

    Isn't it the total number of boxes the result of multiplying the number of anchor boxes per feature map point by the total number of feature maps?

    Thank you! Pedro

    opened by pedrogalher 4
  • Error for dcgan-mnist-4.2.1.py

    Error for dcgan-mnist-4.2.1.py

    Thank you for your greate tutor. but when i run dcgan-mnist-4.2.1.py . It shows error:tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'discriminator_input' with dtype float and shape [?,28,28,1] [[{{node discriminator_input}}]]

    I just copy your code and run it and therefore it raise the error

    opened by ThinhNgVhust 2
  • Invalid Argument Error

    Invalid Argument Error

    Hello, When I tried to run the dcgan, cgan or wagan mode. I have a same error. InvalidArgumentError: You must feed a value for placeholder tensor 'discriminator_input_5' with dtype float and shape [?,28,28,1] [[node discriminator_input_6 (defined at C:/Users/xx/xx/xx.py:181) ]] [Op:__inference_keras_scratch_graph_40533] Function call stack: keras_scratch_graph (the tensor name changes every time).

    opened by mrshinest 2
  • WGAN fails.  No lib/gan exists.

    WGAN fails. No lib/gan exists.

    The Wasserstein GAN example fails because no lib/gan exists.

    Did @PacktPublishing intend to include the Chapter 4 DCGAN (or CGAN) as the base GAN?

    opened by stephenwithav 2
  • possible error at calculating the number of boxes

    possible error at calculating the number of boxes

    At chapter11-detection/data_generator.py , in get_n_boxes(self) function, at line 85, self.n_boxes += np.prod(shape) // self.n_anchors

    to calculate the number of boxes, it should be divided by 4 (xmin, xmax, ymin, ymax) but not the "self.n_anchors". In default, n_anchors = aspect_ration(3) + 1 = 4 works, but if we change the aspect ratio, it will give an error.

    thank you

    opened by enexs 2
  • from lib import gan

    from lib import gan

    this lib ,i have not find,and cannot import name 'gan'

    opened by kaiqi654321 2
  • Unable to locate 'dataset' mentioned in Chapter 11 or it is not there.

    Unable to locate 'dataset' mentioned in Chapter 11 or it is not there.

    I was looking for dataset/drinks/labels_train.csv but it doesn't seems to be there. Is there any API from which we can download this data set?

    opened by sowmya-debug 1
  • possibly unnecessary multiplication at the mask_offset function?

    possibly unnecessary multiplication at the mask_offset function?

    At the chapter11-detection/loss.py mask_offset function, multiplying offset with the mask (line 85) seems unnecessary to me, because the rows of the offset which are not assigned to an object are already zero. This is not a bug obviously, but maybe unnecessary, am I right? Thank you

    opened by enexs 1
Owner
Packt
Providing books, eBooks, video tutorials, and articles for IT developers, administrators, and users.
Packt
A tutorial showing how to train, convert, and run TensorFlow Lite object detection models on Android devices, the Raspberry Pi, and more!

A tutorial showing how to train, convert, and run TensorFlow Lite object detection models on Android devices, the Raspberry Pi, and more!

Evan 1k Oct 18, 2021
TensorFlow ROCm port

Documentation TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries, a

ROCm Software Platform 571 Oct 19, 2021
An Open Source Machine Learning Framework for Everyone

Documentation TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries, a

null 159.9k Oct 15, 2021
An Open Source Machine Learning Framework for Everyone

Documentation TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries, a

null 160.1k Oct 23, 2021
An Open Source Machine Learning Framework for Everyone

Documentation TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries, a

null 153.2k Feb 13, 2021
Annotated notes and summaries of the TensorFlow white paper, along with SVG figures and links to documentation

TensorFlow White Paper Notes Features Notes broken down section by section, as well as subsection by subsection Relevant links to documentation, resou

Sam Abrahams 438 Sep 10, 2021
tf2onnx - Convert TensorFlow, Keras and Tflite models to ONNX.

tf2onnx converts TensorFlow (tf-1.x or tf-2.x), tf.keras and tflite models to ONNX via command line or python api.

Open Neural Network Exchange 1.2k Oct 22, 2021
Keras community contributions

keras-contrib : Keras community contributions Keras-contrib is deprecated. Use TensorFlow Addons. The future of Keras-contrib: We're migrating to tens

Keras 1.5k Oct 14, 2021
Tutorial to set up TensorFlow Object Detection API on the Raspberry Pi

A tutorial showing how to set up TensorFlow's Object Detection API on the Raspberry Pi

Evan 1k Oct 22, 2021
High level network definitions with pre-trained weights in TensorFlow

TensorNets High level network definitions with pre-trained weights in TensorFlow (tested with 2.1.0 >= TF >= 1.4.0). Guiding principles Applicability.

Taehoon Lee 991 Oct 18, 2021
Keras attention models including botnet,CoaT,CoAtNet,CMT,cotnet,halonet,resnest,resnext,resnetd,volo,mlp-mixer,resmlp,gmlp,levit

Keras_cv_attention_models Keras_cv_attention_models Usage Basic Usage Layers Model surgery AotNet ResNetD ResNeXt ResNetQ BotNet VOLO ResNeSt HaloNet

null 37 Oct 15, 2021
Tensorflow 2 Object Detection API kurulumu, GPU desteği, custom model hazırlama

Tensorflow 2 Object Detection API Bu tutorial, TensorFlow 2.x'in kararlı sürümü olan TensorFlow 2.3'ye yöneliktir. Bu, görüntülerde / videoda nesne a

null 40 Sep 20, 2021
Distributed Deep learning with Keras & Spark

Elephas: Distributed Deep Learning with Keras & Spark Elephas is an extension of Keras, which allows you to run distributed deep learning models at sc

Max Pumperla 1.5k Oct 17, 2021
Build tensorflow keras model pipelines in a single line of code. Created by Ram Seshadri. Collaborators welcome. Permission granted upon request.

deep_autoviml Build keras pipelines and models in a single line of code! Table of Contents Motivation How it works Technology Install Usage API Image

AutoViz and Auto_ViML 70 Oct 9, 2021
Mesh TensorFlow: Model Parallelism Made Easier

Mesh TensorFlow - Model Parallelism Made Easier Introduction Mesh TensorFlow (mtf) is a language for distributed deep learning, capable of specifying

null 1.1k Oct 14, 2021
Solutions of Reinforcement Learning 2nd Edition

Solutions of Reinforcement Learning, An Introduction

YIFAN WANG 1000 Oct 14, 2021
A resource for learning about ML, DL, PyTorch and TensorFlow. Feedback always appreciated :)

A resource for learning about ML, DL, PyTorch and TensorFlow. Feedback always appreciated :)

Aladdin Persson 2.3k Oct 24, 2021
TensorFlow 2 AI/ML library wrapper for openFrameworks

ofxTensorFlow2 This is an openFrameworks addon for the TensorFlow 2 ML (Machine Learning) library

Center for Art and Media Karlsruhe 45 Oct 5, 2021