Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Convolutional Neural Networks (CNN) 
1.2.
MNIST Dataset
2.
Implementation 
3.
Importing Necessary Libraries 
4.
Loading Data 
5.
Visualization and Preprocessing
6.
Training 
7.
Results 
8.
FAQs 
9.
Key Takeaways 
Last Updated: Mar 27, 2024

Applying CNN on MNIST Dataset

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

 

We will be studying the MNIST Dataset and then applying CNN on the same and observing the results.

 

Before we go ahead, we need to understand CNN and the MNIST dataset.

Convolutional Neural Networks (CNN) 

 

→ ANN or Artificial Neural Network is a multi-layer fully-connected neural net that consists of many layers, including an input layer, multiple hidden layers, and an output layer. This is a very popular deep learning algorithm used in various classification tasks like audio and words. 

 

→ Similarly, we have Convolutional Neural Networks(CNNs) for image classification. 

 

→ CNNs are neural nets that share their parameters. A covnet is a sequence of layers, and each layer transforms one volume to another through a differentiable function. 

 

MNIST Dataset

 

→ It is a handwritten digit dataset that is instrumental in the field of pattern recognition and analysis. 

 

 

 

→ The MNIST dataset consists of 60,000 samples in the training set and 10,000 samples in the test set. 

 

Implementation 

Now that we've understood the basics of CNN and MNIST Dataset, we will be moving on to the implementation of CNN on the MNIST Dataset. 

 

We will be building a digit classifier using the dataset. The dataset is widely used in Machine Learning and Deep Learning.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Importing Necessary Libraries 

 

Firstly, we will load some basic libraries:-

 

(i) Numpy - for linear algebra. 

 

(ii) Pandas - for data analysis. 

 

(iii) Matplotlib - for data visualization.

 

(iv) Tensorflow - for neural networks.

 

import numpy as np

import pandas as pd 

from numpy import unique, argmax

from tensorflow.keras.datasets.mnist import load_data 

from tensorflow.keras import Sequential

from tensorflow.keras.layers import Conv2D

from tensorflow.keras.layers import Dense 

from tensorflow.keras.layers import Flatten 

from tensorflow.keras.layers import Dropout 

from tensorflow.keras.utils import plot_model

import matplotlib.pyplot as plt

from tensorflow.keras.datasets import mnist 

 

 

Loading Data 

 

We will load the MNIST Dataset using the Keras library and split it into training and testing sets.

#loading the MNIST Dataset 

(train_x, train_y), (test_x, test_y) = mnist.load_data()

 

Now, let us print the dimensions of the dataset. 

#printing the shapes 

print(train_x.shape, train_y.shape)

print(test_x.shape , test_y.shape)

 

Output

We notice 60,000 images in the train set and 10,000 images in the test set.

Visualization and Preprocessing

 

Now, we will reshape the datasets since the images are greyscaled. 

reshaping train and test sets 

train_x = train_x.reshape((train_x.shape[0], train_x.shape[1], train_x.shape[2], 1))

test_x = test_x .reshape((test_x.shape[0], test_x.shape[1], test_x.shape[2], 1))

 

Now, let us print the new dimensions. 

#printing the shapes 

print(train_x.shape, train_y.shape)

print(test_x.shape , test_y.shape)

 

Output

 

 

Now, we will perform normalization for scaling the image pixels. This will help improve the model's performance because the value will range from 0 to 1 instead of 0 to 255.  

#normalizing the pixel values of images 

train_x = train_x.astype('float32')/255.0

test_x = test_x.astype('float32')/255.0

 

 

Now, let us plot the first 20 images for better visualization. 

#plotting images of dataset 

fig = plt.figure(figsize = (10,3))

forin range(20):

    ax= fig.add_subplot(210, i+1, xticks=[], yticks=[])

    ax.imshow(np.squeeze(train_x[i]), cmap='gray')

    ax.set_title(train_y[i])

 

Output

 

 

Let us try to print the shape of a single image. 

shape = train_x.shape[1:]

shape

 

Output

 

 

Here, 1 indicates the channel of the image.

 

Training 

 

Now, let us build the CNN model by adding significant layers. 

#CNN Model 

model = Sequential()

#adding convolutional layer 

model.add(Conv2D(32, (3,3), activation='relu', input_shape= shape))

model.add(MaxPool2D((2,2)))

model.add(Conv2D(48, (3,3), activation='relu'))

model.add(MaxPool2D((2,2)))

model.add(Dropout(0.5))

model.add(Flatten())

model.add(Dense(500, activation='relu'))

model.add(Dense(10, activation='softmax'))

 

Let us print a summary of our model for better understanding. 

model.summary()

 

Output

 

 

Now let us compile the model and train it. 

#compiling model 

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

x=model.fit(train_x, train_y, epochs=10, batch_size = 128, verbose= 2 , validation_split = 0.1)

 

Results 

 

Finally, let us evaluate our model. 

loss, accuracy= model.evaluate(test_x, test_y, verbose = 0)

print(f'Accuracy: {accuracy*100}')

 

Output

 

 

FAQs 

  1. What is a Deep Learning?
    Deep learning is closely related to machine learning, but it learns to perform classification tasks directly from images, text, or sound. 
     
  2. What is the difference between ANN and CNN?
    ANN or Artificial Neural Network is a multi-layer fully-connected neural net that consists of many layers, including an input layer, multiple hidden layers, and an output layer. CNNs are neural nets that share their parameters. A covnet is a sequence of layers, and each layer transforms one volume to another through a differentiable function.

 

Key Takeaways 

 

Congratulations on making it this far. This blog discussed a fundamental overview of CNN and the MNIST Dataset !!

 

We learned about Data Loading and Data Visualisation, and finally, we applied CNN on the MNIST Dataset. 

Check out this article - Padding In Convolutional Neural Network

If you are preparing for the upcoming Campus Placements, don’t worry. Coding Ninjas has your back. Visit this link for a carefully crafted and designed course on-campus placements and interview preparation.

 

Live masterclass