Hello People! In today’s article, we are going to implement a handwritten digit recognition app using the MNIST dataset. We’ll train both a linear model and a neural network to classify handwritten digits from the classic MNIST data set. We’ll then compare the performance of the linear and neural network classification models. Moving further, we’ll visualize the weights of a neural-network hidden layer.

What is Handwritten Digit Recognition?

The handwritten digit recognition is the ability of computers to recognize human handwritten digits. It is a hard task for the machine as handwritten digits are not perfect and can be made with many different flavors. The handwritten digit recognition is the solution to this problem. It uses the image of a digit and recognizes the digit present in the image.

Overview

Our goal is to map each input image to the correct numeric digit. We will create a NN with a few hidden layers and a Softmax layer at the top to select the winning class.

Setup

Firstly, we’ll download the data set, import TensorFlow and other utilities, and load the data into a pandas DataFrame. We’ll be using MNIST dataset. It is probably one of the most popular datasets among machine learning and deep learning enthusiasts. The MNIST dataset contains 60,000 training images of handwritten digits from zero to nine and 10,000 images for testing. So, the MNIST dataset has 10 different classes. Note that, we’ll be using a sample of the original MNIST training data; we’ve taken 20000 rows at random.

You can download the train dataset from here.

Each row represents one labeled example. Column 0 represents the label that a human rater has assigned for one handwritten digit. For example, if Column 0 contains ‘6’, then a human rater interpreted the handwritten character as the digit ‘6’. The ten digits 0-9 are represented, with a unique class label for each possible digit. Thus, this is a multi-class classification problem with 10 classes.

Columns 1 through 784 contain the feature values, one per pixel for the 28×28=784 pixel values. The pixel values are on a gray scale in which 0 represents white, 255 represents black, and values between 0 and 255 represent shades of gray. Most of the pixel values are 0; you may want to take a minute to confirm that they aren’t all 0. For example, adjust the following text block to print out the values in column 72.

To show the natural handwritten nature of the images to be classified, a plot of the first nine images in the dataset is also created.

Task 1: Build a Linear Model for MNIST

Now we’ll create a baseline model. This is critical as it involves developing the infrastructure for the test harness so that any model we design can be evaluated on the dataset. It establishes a baseline in model performance of the problem by which we can compare all the improvements.

The Linear Classifier provides a set of k one-vs-all classifiers, one for each of the k classes. We notice that in addition to reporting accuracy, and plotting Log Loss over time, we also display a confusion matrix. The confusion matrix shows which classes were mis-classified as other classes. 

Task 2: Replace the Linear Classifier with a Neural Network

Next, we’ll replace the LinearClassifier above with a DNNClassifier. Then, we’ll find a parameter combination that gives an accuracy of 0.95 or greater.

We may wish to experiment with additional regularization methods, such as dropout.

Task 3: Visualize the weights of the first hidden layer

The input layer of our model has 784 weights corresponding to the 28×28 pixel input images. The first hidden layer will have 784×N weights where N is the number of nodes in that layer. We can turn those weights back into 28×28 images by reshaping each of the N 1×784 arrays of weights into N arrays of size 28×28.

The first hidden layer of the neural network should be modeling some pretty low level features, so visualizing the weights will probably just show some fuzzy blobs or possibly a few parts of digits. It can be interesting to stop training at different numbers of iterations and see the effect.

Conclusion

In this article, we have successfully built a Python deep learning project on handwritten digit recognition. We have built and trained the neural network which is very effective for image classification purposes.

You can access the entire source code here.

-Krishna Pal Deora


1 Comment

marcelo frank furter · August 3, 2020 at 6:53 pm

Thank you for the good writeup. Look advanced to far added agreeable from you! By the way, how
could we communicate?

Leave a Reply

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

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert