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}
}
Comments
  •  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
  • 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
  • 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
  • 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
  • The average precision and the average recall have the same result values

    The average precision and the average recall have the same result values

    https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/7bfce716e89cee447726209a28469405413b19ed/chapter11-detection/ssd-11.6.1.py#L298

    It seems that the calculation for fn (false negatives) is incomplete. With "fn = abs(len(gt_class_ids) - tp)" every fp (false positive) is also a fn (false negative). Maybe it should be: "fn = abs(len(gt_class_ids) - tp - fp)"

    opened by jaku-soft 1
  • Why the last dimension of the tensor is size=4

    Why the last dimension of the tensor is size=4

    Hi,

    I just started ith Object Detection and I'm finding this book incredibly useful. As I'm now going throug the function "anchor_boxes" withon the script "layer_utils.py", I do not completely understand why the tensor of anchor boxes has this dimensions: tensor = (feature_map_height, feature_map_width, n_boxes, 4)

    I do not understand why the last dimension is size 4. What is the reason behind it?

    Thank you!

    opened by pedrogalher 1
  • In dqn-cartpole-9.6.1.py, line 115, where does

    In dqn-cartpole-9.6.1.py, line 115, where does "reward" variable come from?

    
    def get_target_q_value(self, next_state):
            # max Q value among next state's actions
            if self.ddqn:
                # DDQN
                # current Q Network selects the action
                # a'_max = argmax_a' Q(s', a')
                action = np.argmax(self.q_model.predict(next_state)[0])
                # target Q Network evaluates the action
                # Q_max = Q_target(s', a'_max)
                q_value = self.target_q_model.predict(next_state)[0][action]
            else:
                # DQN chooses the max Q value among next actions
                # selection and evaluation of action is on the target Q Network
                # Q_max = max_a' Q_target(s', a')
                q_value = np.amax(self.target_q_model.predict(next_state)[0])
    
            # Q_max = reward + gamma * Q_max
            q_value *= self.gamma
            q_value += reward
            return q_value
    
    opened by PepSalehi 1
  • Warning for dcgan-mnist-4.2.1.py

    Warning for dcgan-mnist-4.2.1.py

    UserWarning: Discrepancy between trainable weights and collected trainable weights, did you set `model.trainable` without calling `model.compile` after ?
    

    I have yet to figure out whether this is a serious issue. Is there a clean way to get rid of this warning?

    opened by ea42gh 1
  • ssd evaluate

    ssd evaluate

    hi, In the SSD object detection part. I executed evaluate code. but the result metrics were zero. I didn't know what was wrong.

    image

    below is the code I executed

    git clone https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras.git cd Advanced-Deep-Learning-with-Keras-master/chapter11-detection python3 ssd-11.6.1.py --restore-weights=ResNet56v2-4layer-norm-smooth_l1-extra_anchors-drinks-200.h5 --evaluate --normalize

    I use docker. my image is [tensorflow/tensorflow:latest-gpu-py3-jupyter] . my python version is 3.6.9 and tf version is 2.1.0

    thank you.

    opened by Doldolee 0
  • Project dependencies may have API risk issues

    Project dependencies may have API risk issues

    Hi, In Advanced-Deep-Learning-with-Keras, inappropriate dependency versioning constraints can cause risks.

    Below are the dependencies and version constraints that the project is using

    numpy
    scipy
    Pillow
    matplotlib
    scikit-image
    tensorflow-gpu
    h5py
    graphviz
    pydot
    pydot_ng
    tensorflow-addons
    termcolor
    gym
    tensorflow-probability
    

    The version constraint == will introduce the risk of dependency conflicts because the scope of dependencies is too strict. The version constraint No Upper Bound and * will introduce the risk of the missing API Error because the latest version of the dependencies may remove some APIs.

    After further analysis, in this project, The version constraint of dependency numpy can be changed to >=1.8.0,<=1.23.0rc3. The version constraint of dependency scipy can be changed to >=0.10.0,<=1.8.1. The version constraint of dependency Pillow can be changed to ==9.2.0. The version constraint of dependency Pillow can be changed to >=2.0.0,<=9.1.1. The version constraint of dependency matplotlib can be changed to >=0.86,<=0.86.2. The version constraint of dependency matplotlib can be changed to >=1.3.0,<=3.0.3. The version constraint of dependency scikit-image can be changed to >=0.9.0,<=0.11.3.

    The above modification suggestions can reduce the dependency conflicts as much as possible, and introduce the latest version as much as possible without calling Error in the projects.

    The invocation of the current project includes all the following methods.

    The calling methods from the numpy
    numpy.linalg.pinv
    
    The calling methods from the scipy
    scipy.stats.contingency.margins
    
    The calling methods from the Pillow
    PIL.Image.fromarray
    
    The calling methods from the matplotlib
    matplotlib.patches.Rectangle
    matplotlib.lines.Line2D
    
    The calling methods from the scikit-image
    skimage.util.random_noise
    skimage.img_as_float
    

    @developer Could please help me check this issue? May I pull a request to fix it? Thank you very much.

    opened by PyDeps 1
  • SSD train

    SSD train

    HI I have some question. I'm trying to train SSD model.

    just clone your repository and run 'python ssd-11.6.1.py --train'

    I think your weights files epoch is 200. so I training 200 epoch.

    but inference result was very low

    can you give me a comment about SSD train way?

    opened by Doldolee 4
Owner
Packt
Providing books, eBooks, video tutorials, and articles for IT developers, administrators, and users.
Packt
Simple Linear 2nd ODE Solver GUI - A 2nd constant coefficient linear ODE solver with simple GUI using euler's method

Simple_Linear_2nd_ODE_Solver_GUI Description It is a 2nd constant coefficient li

:) 4 Feb 5, 2022
MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.

MMdnn MMdnn is a comprehensive and cross-framework tool to convert, visualize and diagnose deep learning (DL) models. The "MM" stands for model manage

Microsoft 5.7k Jan 9, 2023
A deep learning network built with TensorFlow and Keras to classify gender and estimate age.

Convolutional Neural Network (CNN). This repository contains a source code of a deep learning network built with TensorFlow and Keras to classify gend

Pawel Dziemiach 1 Dec 18, 2021
A deep learning network built with TensorFlow and Keras to classify gender and estimate age.

Convolutional Neural Network (CNN). This repository contains a source code of a deep learning network built with TensorFlow and Keras to classify gend

Pawel Dziemiach 1 Dec 19, 2021
Realtime Face Anti Spoofing with Face Detector based on Deep Learning using Tensorflow/Keras and OpenCV

Realtime Face Anti-Spoofing Detection ?? Realtime Face Anti Spoofing Detection with Face Detector to detect real and fake faces Please star this repo

Prem Kumar 86 Aug 3, 2022
QKeras: a quantization deep learning library for Tensorflow Keras

QKeras github.com/google/qkeras QKeras 0.8 highlights: Automatic quantization using QKeras; Stochastic behavior (including stochastic rouding) is disa

Google 437 Jan 3, 2023
Vision Deep-Learning using Tensorflow, Keras.

Welcome! I am a computer vision deep learning developer working in Korea. This is my blog, and you can see everything I've studied here. https://www.n

kimminjun 6 Dec 14, 2022
Keras udrl - Keras implementation of Upside Down Reinforcement Learning

keras_udrl Keras implementation of Upside Down Reinforcement Learning This is me

Eder Santana 7 Jan 24, 2022
This is 2nd term discrete maths project done by UCU students that uses backtracking to solve various problems.

Backtracking Project Sponsors This is a project made by UCU students: Olha Liuba - crossword solver implementation Hanna Yershova - sudoku solver impl

Dasha 4 Oct 17, 2021
2nd solution of ICDAR 2021 Competition on Scientific Literature Parsing, Task B.

TableMASTER-mmocr Contents About The Project Method Description Dependency Getting Started Prerequisites Installation Usage Data preprocess Train Infe

Jianquan Ye 298 Dec 21, 2022
Kaggle G2Net Gravitational Wave Detection : 2nd place solution

Kaggle G2Net Gravitational Wave Detection : 2nd place solution

Hiroshechka Y 33 Dec 26, 2022
The 2nd Version Of Slothybot

SlothyBot Go to this website: "https://bitly.com/SlothyBot" The 2nd Version Of Slothybot. The Bot Has Many Features, Such As: Moderation Commands; Kic

Slothy 0 Jun 1, 2022
Xview3 solution - XView3 challenge, 2nd place solution

Xview3, 2nd place solution https://iuu.xview.us/ test split aggregate score publ

Selim Seferbekov 24 Nov 23, 2022
This is the solution for 2nd rank in Kaggle competition: Feedback Prize - Evaluating Student Writing.

Feedback Prize - Evaluating Student Writing This is the solution for 2nd rank in Kaggle competition: Feedback Prize - Evaluating Student Writing. The

Udbhav Bamba 41 Dec 14, 2022
Deep GPs built on top of TensorFlow/Keras and GPflow

GPflux Documentation | Tutorials | API reference | Slack What does GPflux do? GPflux is a toolbox dedicated to Deep Gaussian processes (DGP), the hier

Secondmind Labs 107 Nov 2, 2022
Classification models 1D Zoo - Keras and TF.Keras

Classification models 1D Zoo - Keras and TF.Keras This repository contains 1D variants of popular CNN models for classification like ResNets, DenseNet

Roman Solovyev 12 Jan 6, 2023
The 7th edition of NTIRE: New Trends in Image Restoration and Enhancement workshop will be held on June 2022 in conjunction with CVPR 2022.

NTIRE 2022 - Image Inpainting Challenge Important dates 2022.02.01: Release of train data (input and output images) and validation data (only input) 2

Andrés Romero 37 Nov 27, 2022
This is an implementation of Googles Yogi-Optimizer in Keras (tf.keras)

Yogi-Optimizer_Keras This is an implementation of Googles Yogi-Optimizer in Keras (tf.keras) The NeurIPS-Paper can be found here: http://papers.nips.c

null 14 Sep 13, 2022