Classify MNIST Digits Using Convolutional Neural Network in Keras. End to End Beginner level Deep Learning Project.
Hello everyone, I hope you all are doing well. In this blog we will be making a very basic convolutional neural network (convnent) using keras library of python programming language to classify MNIST digits.
All the code written in this blog is present in my github- https://github.com/HarshMishra2002/mnist-digit-basic-convnent
convnet is a stack of Conv2D and MaxPooling2D layers.
Lets start our work…
from keras import layers
from keras import modelsmodel = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation = ‘relu’, input_shape = (28,28,1)))model.add(layers.MaxPooling2D((2,2)))model.add(layers.Conv2D(64, (3,3), activation = ‘relu’))model.add(layers.MaxPooling2D((2,2)))model.add(layers.Conv2D(64, (3,3), activation = ‘relu’))
Importantly, a convnet takes as input tensors of shape (image_height, image_width, image_channels) (not including the batch dimension). In this case, we’ll configure the convnet to process inputs of size (28, 28, 1), which is the format of MNIST images. We’ll do this by passing the argument input_shape=(28, 28, 1) to the first layer.
Let’s display the architecture of the convnet so far:
You can see that the output of every Conv2D and MaxPooling2D layer is a 3D tensor of shape (height, width, channels). The width and height dimensions tend to shrink.
as you go deeper in the network. The number of channels is controlled by the first argument passed to the Conv2D layers (32 or 64). The next step is to feed the last output tensor (of shape (3, 3, 64)) into a densely connected classifier network : a stack of Dense layers. These classifiers process vectors, which are 1D, whereas the current output is a 3D tensor. First we have to flatten the 3D outputs to 1D, and then add a few Dense layers on top.
model.add(layers.Flatten()) model.add(layers.Dense(64, activation=’relu’)) model.add(layers.Dense(10, activation=’softmax’))
We’ll do 10-way classification, using a final layer with 10 outputs and a softmax activation.
Here’s what the network looks like now:
model.summary()
As you can see, the (3, 3, 64) outputs are flattened into vectors of shape (576,) before going through two Dense layers. Now, let’s train the convnet on the MNIST digits.
from keras.datasets import mnist from keras.utils import to_categorical (train_images, train_labels), (test_images, test_labels) = mnist.load_data()train_images = train_images.reshape((60000, 28, 28, 1)) train_images = train_images.astype(‘float32’) / 255 test_images = test_images.reshape((10000, 28, 28, 1)) test_images = test_images.astype(‘float32’) / 255 train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels) model.compile(optimizer=’rmsprop’, loss=’categorical_crossentropy’, metrics=[‘accuracy’]) model.fit(train_images, train_labels, epochs=5, batch_size=64)
Let’s evaluate the model on the test data:
model.fit(train_images, train_labels, epochs= 5, batch_size = 64)
So, this is the end of our project here. We created a MNIST Digit classifier in this blog using keras. And that is it for today guys.
I hope you guys got to learn something new and enjoyed this blog. If you do like it than share it with your friends. Take care. keep learning.
You could also reach me through my Linkedin account- https://www.linkedin.com/in/harsh-mishra-4b79031b3/