Introduction

Handwritten digit recognition is a classic computer vision and image classification problem where a machine learning model learns to identify handwritten digits from images.

In this project, we build a Convolutional Neural Network (CNN) using TensorFlow and Keras to recognize handwritten digits from 0 to 9.

The project uses the MNIST dataset, which contains grayscale images of handwritten digits. The model is trained on the MNIST training data and evaluated on the official held-out MNIST test dataset.

The project also includes a dedicated external-image preprocessing pipeline that allows the trained model to process and predict digits from real-world handwritten images.

The overall workflow is:

Whiteboard
Loading diagram...

he trained model achieved 99.56% accuracy on the official MNIST test set.

1. Understanding the MNIST Dataset

What is MNIST?

MNIST stands for Modified National Institute of Standards and Technology.

It is one of the most widely used datasets for learning and evaluating image classification models.

The dataset contains handwritten digits from:

0, 1, 2, 3, 4, 5, 6, 7, 8, and 9

Each image represents one handwritten digit.

MNIST Image Properties

PropertyValue
Image TypeGrayscale
Image Size28 × 28 pixels
Channels1
Classes10
Pixel Range0–255
Total Images70,000

Suggested Visual

Insert MNIST sample images showing several handwritten digits from 0 to 9 here.

2. Dataset Split

Whiteboard
Loading diagram...

The original 60,000 training images are divided into:

  • 54,000 training images
  • 6,000 validation images

The official 10,000 test images remain separate for final evaluation.

3. Loading and Preparing the Dataset

The images are converted to float32, and a channel dimension is added so that each image has the shape 28 × 28 × 1.

A stratified split is then used to create the validation set.

4. Data Pipeline

Whiteboard
Loading diagram...

Code

The project uses a batch size of 128.

5. Normalization

The original pixel values are:

0–255

The model converts them into:

0–1

Whiteboard
Loading diagram...

Code

Normalization is included directly inside the model so that the same preprocessing rule is used consistently.

6. Data Augmentation

Real handwritten digits can be rotated, shifted, resized, or written with different contrast.

The project uses:

  • Random Rotation
  • Random Translation
  • Random Zoom
  • Random Contrast

Code