3.5. CNN model using Tensorflow - Keras#

After Building Convolutional Neural Network (CNN model) from scratch using Numpy in Python (link to previous chapter), and after developing CNN using Pytorch (link to previous chapter), we will finally develop the CNN model using Tensorflow - Keras.

Note: The CNN model we developed from scratch almost follows the way the models are developed in Keras.

Import necessary libraries#

Here we import a Conv2D, MaxPooling2D, Dense layer, an Activation layer and a Dropout layer. Then we will also import optimizers Adam and RMSprop.

Then we finally import the to_categorical function which is nothing but one hot vector function.

import numpy as np
import matplotlib.pyplot as plt # plotting library
%matplotlib inline

from keras.models import Sequential
from keras.layers import Dense , Activation, Dropout, Conv2D, MaxPooling2D, Flatten
from keras.optimizers import Adam ,RMSprop
from keras.utils import to_categorical

Data Loading and pre-processing#

Next we import and load the CIFAR-10 dataset

Note

CIFAR-10 is a dataset that has a collection of images of 10 different classes. This dataset is widely used for research purposes to test different machine learning models and especially for computer vision problems.

# import dataset
from keras.datasets import cifar10

# load dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170498071/170498071 [==============================] - 17s 0us/step

After loading the CIFAR-10 dataset, the number of labels is computed as:

# compute the number of labels
num_labels = len(np.unique(y_train))

Now we will perform One hot vector encoding (link to previous chapter) on the target data

# convert to one-hot vector
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

Now we will normalize the data

# normalize
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

Model architecture#

The next step is to design the model architecture.

# Creating a sequential model and adding layers to it

model = Sequential()
model.add(Conv2D(32, (3,3), padding='same', activation='relu', input_shape=(32,32,3)))
model.add(Conv2D(64, (3,3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax')) # number of classes (output) = 10

Keras library provides us summary() method to check the model description.

model.summary()
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_2 (Conv2D)           (None, 32, 32, 32)        896       
                                                                 
 conv2d_3 (Conv2D)           (None, 32, 32, 64)        18496     
                                                                 
 max_pooling2d_1 (MaxPooling  (None, 16, 16, 64)       0         
 2D)                                                             
                                                                 
 dropout_1 (Dropout)         (None, 16, 16, 64)        0         
                                                                 
 flatten (Flatten)           (None, 16384)             0         
                                                                 
 dense (Dense)               (None, 512)               8389120   
                                                                 
 dropout_2 (Dropout)         (None, 512)               0         
                                                                 
 dense_1 (Dense)             (None, 10)                5130      
                                                                 
=================================================================
Total params: 8,413,642
Trainable params: 8,413,642
Non-trainable params: 0
_________________________________________________________________

Executing the CNN model using Keras#

This section comprises of

  • Compiling the model with the compile() method.

  • Training the model with fit() method.

  • Evaluating the model performance with evaluate() method.

Compiling the model

model.compile(loss='categorical_crossentropy', 
              optimizer='adam',
              metrics=['accuracy'])

Training the model

model.fit(x_train, y_train, epochs=5, batch_size=64)
Epoch 1/5
782/782 [==============================] - 16s 9ms/step - loss: 1.4291 - accuracy: 0.4883
Epoch 2/5
782/782 [==============================] - 7s 9ms/step - loss: 1.0486 - accuracy: 0.6302
Epoch 3/5
782/782 [==============================] - 6s 8ms/step - loss: 0.8918 - accuracy: 0.6865
Epoch 4/5
782/782 [==============================] - 6s 8ms/step - loss: 0.7818 - accuracy: 0.7243
Epoch 5/5
782/782 [==============================] - 7s 9ms/step - loss: 0.6781 - accuracy: 0.7621
<keras.callbacks.History at 0x7f1b91046640>

Evaluating model performance with evaluate() method

loss, acc = model.evaluate(x_test, y_test, batch_size=64)
print("\nTest accuracy: %.1f%%" % (100.0 * acc))
157/157 [==============================] - 1s 4ms/step - loss: 0.8279 - accuracy: 0.7111

Test accuracy: 71.1%

We get the test accuracy of 71.1%. With more complex model, we can increase the accuracy of CIFAR-10 as much as we want. The main thing is that we have learnt how to build our very first CNN model using Keras. It is that simple!