Beginners: Keras Mnist Data Classification using CNN (Part 1)

This tutorial series is for the audience who are seeking how Convolutional neural network works in keras specially with sequential api. We will build a custom neural network having some CNN Layers. If you have no prior knowledge of CNN please check out my Short tutorial on how CNN networks work. So lets jump into the tutorial:

CNN networks are very good at learning image representation and hence can be used for different computer vision tasks such as image classification, object detection and segmentation etc. In this tutorial we will use MNIST Dataset. Wanna know more about this dataset check out this Guide.

Mnist dataset contains total 70K grey-scale images of different handwritten digits with dimensions 28*28. We will use this dataset for the illustration of image classification problem using keras api which is made on top of tensorflow to make it easy for AI developers to develop their own neural networks with simple few lines of codes.

We will do image classification task in few steps mentioned below to make it easy to understand:
1- Importing Keras libraries
2- Loading dataset
3- Preprocessing of dataset
4- Declaring keras Model layers
5- Training the model

1- Importing Keras and other important Libraries

First of all we will import some important libraries modules from keras API.

#Importing some Important Keras modules or libraries
import keras
from keras.models import Sequential
from keras.datasets import mnist
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
import numpy as np

we have imported keras sequential api module alng with mnist class from Keras Standard datasets. We have also imported some layers function like Convolution2D, MaxPooling2D etc . Numpy is very efficient library for handling of multidimensional data so we import this too.

2- Loading Dataset

Now we will load the dataset from keras datasets module and load it into training and testing variables:

#Loading Dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()

mnist.load_data function will download the dataset if your using this function for the very first time and it will some time depending on your internet speed. The dataset is already divided into Training and Testing set. Lets print out the shape of the loaded dataset.


#Remember x_train contains our input images of digits and y_train contains 
#labels of training images.Similarly, x_test contains testing images and 
#y_test contains labels of testing images.

Output will be something like this (60000, 28, 28) (60000,) (10000, 28, 28) (10000,). We can see x_train contains 60,000 images with dimension 28*28 and y_train contains 60,000 labels with values range from (1-10).

3- Preprocessing of dataset

As Mnist dataset contains grey scale images and keras convolutional model layer takes input as 4 dimensional so we need to append an extra dimension to our training and testing input images. Lets get it done by using numpy function expand_dims with parameter axis = -1 means append dimension at last position.

# Appending extra chanell dimension at last position
x_train = np.expand_dims(x_train,axis=-1)
x_test = np.expand_dims(x_test,axis=-1)

We have appended the extra chanell dimension and now our dimension size output will look like this (60000, 28, 28, 1) (10000, 28, 28, 1) .

Now, we need to convert our labels into one hot encoding using keras.utils module. In one hot encoding instead of having single number label for each image, we will have a vector of size equal to number of classes which in our case is 10. So each image will be represented by the one dimensional vector of size 10 having values 0 and 1. there will be one 1 value placed at class number and rest of the 9 numbers value will be zero. so let’s suppose if we have an image of digit 8 and its label y is 8, then in one hot encoding its label will look like this [0,0,0,0,0,0,0,1,0,0].

#Converting labels into one hot encoded vectors
num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

After converting our labels into suitable format, we now going to normalize the input image pixel values. As we know in grey scale image we have values between 0 and 255 so we divide each pixel with max value 255. This is called normalization of data. There are many other methods as well to normalize the data like subtracting mean image from each input image. But we will stick to normalizing with max value in this tutorial. Lets do it:

#Preprocessing the data 
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

Alright In this part we have done the basic steps like loading and preprocessing dataset. In the next part (2nd) we will start training the CNN model.

click here for PART 2 .

Code Link:

1 reply

Trackbacks & Pingbacks

  1. […] is 2nd part of this tutorial series. If you have not gone through First part, then please check that out first, because […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *