This tutorial shows how to make a Convolutional Neural Network for recognition images from CIFAR-10 data-set with the Following concept.

  1. Data Augmentation
  2. Save/load checkpoint
  3. Save Model
  4. Visualizing Learning in Tensorbord

Why Keras API


Keras is an API that makes building deep-learning models easier and faster. It’s a deep-learning toolbox. It’s all about ease of use, reducing complexity and reducing cognitive load.

The key idea with Keras is to put deep learning into the hands of everyone. The future of deep learning will be part of every developer’s toolbox. It will not just be a tool for experts and researchers. This will happen with Keras API. Keras is not a library or as a code base. It’s more of an API specification. It’s an API specification that has several different implementations. There’s the Theano implementations or Tensor flow implementation.

If you’re a TensorFlow user, it gives you access to the full scope of the Keras API to make your life easier without leaving your existing TensorFlow workflow. So you can just start using the Keras API at no loss of flexibility. You don’t have to adopt all of Keras, you can just the layers you need.

CIFAR-10 dataset


The size of all images in this dataset is 32x32x3 (RGB). There are 50,000 images for training a model and 10,000 images for evaluating the performance of the model. The classes and randomly selected 10 images of each class could be seen in the picture below.

CIFAR10 Dataset

Load Data

The keras.datasets module includes methods to load and fetch CIFAR-10 datasets.

Rescale(/255.0)

Our original images consist in RGB coefficients in the 0-255, but such values would be too high for our models to process (given a typical learning rate), so we target values between 0 and 1 instead of by scaling with a 1/255. factor.

Data Augmentation


You can augment data via a number of random transformations so that our model would never see twice the exact same image. This helps prevent overfitting and helps the model generalize better.

In Keras this can be done via the tf.keras.preprocessing.image.ImageDataGenerator class. This class allows you to
configure random transformations and normalization operations to be done on your image data during training and instantiate generators of augmented image batches and labels) via .flow(data, labels) or .flow_from_directory(directory). These generators can then be used with the Keras model methods that accept data generators as inputs, fit_generator, evaluate_generator and predict_generator.

These are just a few of the options available for more, see the documentation.

Create Model


In our case we will use a small convnNet alongside data augmentation and dropout. Dropout helps reduce overfitting, by preventing a layer from seeing twice the exact same pattern. You could say that both dropout and data augmentation tend to disrupt random correlations occurring in your data.

BatchNormalization normalizes the activation of the previous layer at each batch, i.e. applies a transformation that maintains the mean activation close to 0 and the activation standard deviation close to 1. It addresses the problem of internal covariate shift. It also acts as a regularizer, in some cases eliminating the need for Dropout.

CNN model for CIFAR10

The code snippet below is our TensoFlow model using Keras API, a simple stack of 2 convolution layers with a ReLU activation and followed by max-pooling layers.

Compile Model

We are now ready to compile our model. The categorical crossentropy function has been picked out as a loss function because we have more than 2 labels.

Visualizing Learning in Tensorbord


This code writes a log for TensorBoard, which allows you to visualize dynamic graphs of your training and test metrics, as well as activation histograms for the different layers in your model. See for more options.

Run flowing command in command prompt after training starts.

Save and restore models


Model progress can be saved during—and after—training. This means a model can resume where it left off and avoid long training times or pick-up training where you left of—in case the training process was interrupted.

Then load the weights from the checkpoint, and re-evaluate:

Train Model


We are now ready to compile our model using  fit_generator()

The output of the above python implementation for object recognition task is shown below:

CIFAR-10 Output

Evaluate and predict


The tf.keras.Model.evaluate and tf.keras.Model.predict methods can use NumPy data and a tf.data.Dataset.

 

CIFAR-10 evaluate model
Once you choose fit a final deep learning model in Keras, you can use it to make predictions on new data instances.

Tensorflow Keras model prediction

Save Model


You can save the entire model to a file that contains the weight values, the model’s configuration, and the optimizer’s configuration. This allows you to checkpoint a model and resume training later—from the exact same state—without access to the original code. Keras provides a basic save format using the HDF5 standard.

Install TensorFlow dependencies:

In this tutorial, you discovered how you can train CNN image classification mode using TensorFlow Keras High-Level API.

Specifically, you learned:

    • How to save and load a checkpoint.
    • How to use the Tensorboard callback of Keras.
    • How to save the model.

Download this project from GitHub

Leave a Reply