Book Image

Advanced Deep Learning with TensorFlow 2 and Keras - Second Edition

By : Rowel Atienza
Book Image

Advanced Deep Learning with TensorFlow 2 and Keras - Second Edition

By: Rowel Atienza

Overview of this 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.
Table of Contents (16 chapters)
14
Other Books You May Enjoy
15
Index

To get the most out of this book

  • Deep learning and Python: The reader should have a fundamental knowledge of deep learning and its implementation in Python. While previous experience in using Keras to implement deep learning algorithms is important, it is not required. Chapter 1, Introducing Advanced Deep Learning with Keras, offers a review of deep learning concepts and their implementation in tf.keras.
  • Math: The discussions in this book assume that the reader is familiar with calculus, linear algebra, statistics, and probability at college level.
  • GPU: The majority of the tf.keras implementations in this book require a GPU. Without a GPU, it is not practical to execute many of the code examples because of the time involved (many hours to days). The examples in this book use reasonable amounts of data as much as possible in order to minimize the use of high-performance computers. The reader is expected to have access to at least NVIDIA GTX 1060.
  • Editor: The example code in this book was edited using vim in Ubuntu Linux 18.04 LTS and MacOS Catalina. Any Python-aware text editor is acceptable.
  • TensorFlow 2: The code examples in this book are written using the Keras API of TensorFlow 2 or tf2. Please ensure that the NVIDIA GPU driver and tf2 are both properly installed.
  • GitHub: We learn by example and experimentation. Please git pull or fork the code bundle for the book from its GitHub repository. After getting the code, examine it. Run it. Change it. Run it again. Do creative experiments by tweaking the code. It is the only way to appreciate all the theory explained in the chapters. Giving a star on the book's GitHub repository https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras is also highly appreciated.

Download the example code files

The code bundle for the book is hosted on GitHub at:

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

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 color images of figures used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781838821654_ColorImages.pdf.

Conventions used

The code in this book is in Python. More specifically, Python 3. For example:

A block of code is set as follows:

def build_generator(inputs, image_size):
    """Build a Generator Model
    Stack of BN-ReLU-Conv2DTranpose to generate fake images
    Output activation is sigmoid instead of tanh in [1].
    Sigmoid converges easily.
    Arguments:
        inputs (Layer): Input layer of the generator 
            the z-vector)
        image_size (tensor): Target size of one side
            (assuming square image)
    Returns:
        generator (Model): Generator Model
    """
    image_resize = image_size // 4
    # network parameters 
    kernel_size = 5
    layer_filters = [128, 64, 32, 1]
    x = Dense(image_resize * image_resize * layer_filters[0])(inputs)
    x = Reshape((image_resize, image_resize, layer_filters[0]))(x)
    for filters in layer_filters:
        # first two convolution layers use strides = 2
        # the last two use strides = 1
        if filters > layer_filters[-2]:
            strides = 2
        else:
            strides = 1
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        x = Conv2DTranspose(filters=filters,
                            kernel_size=kernel_size,
                            strides=strides,
                            padding='same')(x)
    x = Activation('sigmoid')(x)
    generator = Model(inputs, x, name='generator')
    return generator

When we wish to draw your attention to a particular part of a code block,the relevant lines or items are set in bold:

# generate fake images
fake_images = generator.predict([noise, fake_labels])
# real + fake images = 1 batch of train data
x = np.concatenate((real_images, fake_images))
# real + fake labels = 1 batch of train data labels
labels = np.concatenate((real_labels, fake_labels))

Whenever possible, docstrings are is included. At the very least, text comments are used to minimize space usage.

Any command-line code execution is written as follows:

python3 dcgan-mnist-4.2.1.py

The above example has the following layout: algorithm-dataset-chapter.section.number.py. The command-line example is DCGAN on the MNIST dataset in Chapter 4, Generative Adversarial Networks (GANs) second section and first listing. In some cases, the explicit command line to execute is not written but it is assumed to be:

python3 name-of-the-file-in-listing

The file name of the code example is included in the Listing caption. This book uses Listing to identify code examples in the text.

Bold: Indicates a new term, an important word, or words that you see on the screen, for example, in menus or dialog boxes, also appear in the text like this. For example: StackedGAN has two additional loss functions, Conditional and Entropy.

Warnings or important notes appear like this.