Applying convolutional neural network on mnist dataset

MNIST dataset:
mnist dataset — this is a bunch of handwritten images as shown in the picture below.


We can get 99.06 accuracy % using CNN (Convolutionary neural Network) with functional model. The reason for using the functional model is to keep it simple when connecting layers.

  • First, include all the required libraries

    import numpy as np

    import keras 

    from keras.datasets import mnist

    from keras.models import Model

    from keras.layers import Dense, Input

    from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten

    from keras import backend as k

  • Create train data and test data

    Test data: Used to test the model for training the model. 
    Train data: Used to train our model.

    In the future, quality sizes images are used img_rows and img_cols . In the mnist dataset, these are 28 and 28. We also need to check the data format, ie. “Channel_first” or “channel_last”. At CNN, we can normalize data before it turns out that long computation times can be reduced to less time. For example, we can normalize the x_train and x_test data by dividing them by 255.

    Data format check:

    (x_train, y_train), (x_test, y_test) = mnist.load_data ()

    img_rows, img_cols = 28 , 28

     

    if k.image_data_format () = = `channels_first` :

      x_train = x_train.reshape (x_train.shape [ 0 ], 1 , img_rows, img_cols)

      x_test = x_test.reshape (x_test.shape [ 0 ], 1 , img_rows, img_cols)

    inpx = ( 1 , img_rows, img_cols)

     

    else :

    x_train = x_train.reshape (x_train.shape [ 0 ], img_rows, img_cols, 1 )

    x_test = x_test.reshape (x_test.shape [ 0 ] , img_rows, img_cols, 1 )

      inpx = (img_rows, img_cols, 1 )

     

    x_train = x_train.astype ( `float32` )

    x_test = x_test.astype ( `float32` )

    x_train / = 255

    x_test / = 255

  • Description of the output classes:

    Because the model output can be any digits from 0 to 9., we need 10 classes at the output. To draw an output for 10 classes, use the keras.utils.to_categorical function, which will have 10 columns. Of those 10 columns, only one value will be one and the other 9 will be zero, and that one output value will denote the class of the digit.

    y_train = keras.utils.to_categorical (y_train)

    y_test = keras.utils.to_categorical (y_test)

    The dataset is now ready, so let`s move on to the cnn model:

    inpx = Input (shape = inpx)

    layer1 = Conv2D ( 32 , kernel_size = ( 3 , 3 ), activation = ` relu` ) (inpx)

    layer2 = Conv2D ( 64 , ( 3 , 3 ), activation = ` relu` ) (layer1)

    layer3 = MaxPooling2D (pool_size = ( 3 , 3 )) (layer2)

    layer4 = Dropout ( 0.5 ) (layer3)

    layer5 = Flatten () (layer4)

    layer6 = Dense ( 250 , activation = `sigmoid` ) (layer5)

    layer7 = Dense ( 10 , activation = `softmax` ) (layer6)

  • Explanation of work you of each layer in the CNN model:

    layer1 — a Conv2d layer that convolves the image using 32 filters of each size (3 * 3). 
    layer2 is again a Conv2D layer, which is also used to collapse the image and uses 64 filters of each size (3 * 3). 
    layer3 — this is the MaxPooling2D layer that picks the maximum value from the size matrix (3 * 3). 
    layer4 shows a drop rate of 0.5. 
    layer5 flattens the output received from layer4, and this anti-aliasing output is passed to layer6. 
    layer6 — it is a hidden layer of a neural network containing 250 neurons. 
    layer7 — this is an output layer with 10 neurons for 10 output classes using the softmax function.

  • Calling the compile and fit function:

    model = Model ([inpx], layer7)

    model. compile (optimizer = keras.optimizers.Adadelta (),

    loss = keras.losses.categorical_crossentropy,

    metrics = [ `accuracy` ])

      

    model.fit (x_train, y_train, epochs = 12 , batch_size = 500 )


    First, we made a model object as shown in the lines above, where [inpx] — this is the entrance to the model, and layer7 — this is the output of the model. We compiled the model using the required optimizer, loss function and printed the precision, and in the latter case, model.fit was called with parameters such as x_train (means image vectors), y_train (means label), number of epochs, and batch size. Using the x_train fitting function, the y_train dataset is fed to a model with a specific batch size.

  • Evaluate function:
    model.evaluate provides an estimate for test data. those. provides test data for the model. The model will now predict the data class and the predicted class will match the label y_test to give us precision.

    score = model.evaluate (x_test, y_test, verbose = 0 )

    print ( `loss =` , score [ 0 ])

    print ( `accuracy =` , score [ 1 ])

    Output: