CoolFace
Modelpublic

PranavDeployer221/mnist-handwritten-digit-classifier

sourceHugging Faceupdated 6d agoView on Hugging Face
1likes29downloads
Model Card

๐Ÿง  Handwritten Digit Recognition using Neural Network

An end-to-end Deep Learning application that recognizes handwritten digits (0โ€“9) from 28ร—28 grayscale images using a fully connected Neural Network built with TensorFlow/Keras and deployed as an interactive Streamlit application. # ๐Ÿง  Handwritten Digit Recognition using Neural Network
An end-to-end Deep Learning application that recognizes handwritten digits using a Neural Network built with TensorFlow/Keras and deployed with Streamlit.

๐Ÿ“Œ Overview

Handwritten Digit Recognition is a fundamental Computer Vision and Deep Learning problem where a machine learning model learns to identify numerical digits from handwritten images.

This project implements the complete Deep Learning lifecycleโ€”from raw pixel data and preprocessing to Neural Network training, evaluation, model serialization, and web deployment.

The system accepts a handwritten digit as input and predicts the corresponding digit class along with the model's confidence.

Core Pipeline

text
Raw Image
    โ†“
Image Preprocessing
    โ†“
Pixel Normalization
    โ†“
28 ร— 28 ร— 1 Representation
    โ†“
Flatten
    โ†“
Fully Connected Neural Network
    โ†“
Softmax Probability Distribution
    โ†“
Predicted Digit
    โ†“
Streamlit Application

๐ŸŽฏ Objectives

The primary objectives of this project are:

  • โ€”Build a Neural Network for multi-class image classification.
  • โ€”Understand the complete Deep Learning workflow.
  • โ€”Process and normalize image pixel data.
  • โ€”Implement a multi-layer fully connected architecture.
  • โ€”Train and validate the model on handwritten digit data.
  • โ€”Analyze model performance using multiple evaluation techniques.
  • โ€”Perform prediction on unseen test images.
  • โ€”Serialize the trained model for inference.
  • โ€”Integrate the model into an interactive web application.
  • โ€”Deploy the application for real-world accessibility.

๐Ÿ“Š Dataset

The model works with handwritten digit images represented as grayscale pixel values.

Each image contains:

text
Image dimensions: 28 ร— 28 pixels
Channels: 1 (grayscale)
Total pixels: 784
Classes: 10
Classes: 0โ€“9

Each image can therefore be represented as:

text
28 ร— 28 ร— 1

For the fully connected Neural Network, the image is flattened into:

text
28 ร— 28 ร— 1 = 784 features

Data Representation

text
Original Image
     โ†“
28 ร— 28 ร— 1
     โ†“
Flatten
     โ†“
784-dimensional vector

๐Ÿ” Exploratory Data Analysis

Before training the model, the dataset is analyzed to understand its structure and quality.

The exploration includes:

  • โ€”Dataset dimensions
  • โ€”Feature and target identification
  • โ€”Missing-value analysis
  • โ€”Pixel-value distribution
  • โ€”Label/class distribution
  • โ€”Image visualization
  • โ€”Data type inspection
  • โ€”Sample image analysis

Example visualization:

text
Pixel Matrix
     โ†“
28 ร— 28 values
     โ†“
Grayscale Image
     โ†“
Human-readable digit

โš™๏ธ Data Preprocessing

1. Pixel Normalization

Raw pixel values are scaled from:

text
0โ€“255

to:

text
0โ€“1

using:

python
X = X / 255.0

This provides a more suitable numerical range for Neural Network optimization.

2. Reshaping

The input images are represented as:

text
28 ร— 28 ร— 1

using:

python
X = X.reshape(-1, 28, 28, 1)

The additional dimension represents the grayscale channel.

3. Label Encoding

The digit labels are converted into a representation suitable for multi-class classification.

For example:

text
7

can be represented as:

text
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]

๐Ÿง  Neural Network Architecture

The project uses a fully connected feed-forward Neural Network.

text
                 Input Image
              28 ร— 28 ร— 1
                    โ”‚
                    โ–ผ
                 Flatten
                    โ”‚
                    โ–ผ
              784 Features
                    โ”‚
                    โ–ผ
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚ Dense Layer           โ”‚
        โ”‚ 128 Neurons           โ”‚
        โ”‚ ReLU Activation       โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ”‚
                    โ–ผ
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚ Dense Layer           โ”‚
        โ”‚ 64 Neurons            โ”‚
        โ”‚ ReLU Activation       โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ”‚
                    โ–ผ
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚ Output Layer          โ”‚
        โ”‚ 10 Neurons            โ”‚
        โ”‚ Softmax Activation    โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ”‚
                    โ–ผ
             Digit Prediction
              0 โ€“ 9

๐Ÿ”ฌ Architecture Details

LayerConfigurationPurpose
Input28ร—28ร—1Receives image
Flatten784 unitsConverts image to vector
Dense128 neuronsLearns feature representations
Dense64 neuronsLearns higher-level representations
Output10 neuronsPredicts digit classes

Activation Functions

ReLU

The hidden layers use the Rectified Linear Unit activation function:

text
ReLU(x) = max(0, x)

It introduces non-linearity and allows the network to learn complex patterns.

Softmax

The output layer uses Softmax to produce a probability distribution across the ten digit classes.

Example:

text
0 โ†’ 0.01
1 โ†’ 0.00
2 โ†’ 0.02
3 โ†’ 0.01
4 โ†’ 0.00
5 โ†’ 0.01
6 โ†’ 0.00
7 โ†’ 0.93
8 โ†’ 0.01
9 โ†’ 0.01

Final prediction:

text
7

โšก Model Compilation

The model is compiled using:

text
Optimizer:
Adam

Loss Function:
Categorical Crossentropy

Metric:
Accuracy

Adam Optimizer

Adam is used to efficiently update the network weights during training.

Categorical Crossentropy

The loss function measures the difference between the true class distribution and the predicted probability distribution.


๐Ÿ‹๏ธ Model Training

The model learns through multiple training epochs.

The training process follows:

text
Input Image
     โ†“
Forward Propagation
     โ†“
Prediction
     โ†“
Loss Calculation
     โ†“
Backpropagation
     โ†“
Weight Updates
     โ†“
Improved Model

Training performance is monitored using:

  • โ€”Training loss
  • โ€”Validation loss
  • โ€”Training accuracy
  • โ€”Validation accuracy

Training history is visualized to analyze convergence and identify potential overfitting.


๐Ÿ“ˆ Model Evaluation

Model performance is evaluated using multiple metrics rather than relying only on accuracy.

Evaluation techniques

  • โ€”Accuracy
  • โ€”Loss
  • โ€”Confusion Matrix
  • โ€”Classification Report
  • โ€”Individual predictions
  • โ€”Error analysis

Confusion Matrix

The confusion matrix helps identify which digit classes the model confuses with one another.

For example:

text
Actual 7 โ†’ Predicted 7 โœ“
Actual 5 โ†’ Predicted 3 โœ—
Actual 9 โ†’ Predicted 4 โœ—

This provides a deeper understanding of model behavior.


๐Ÿ”Ž Error Analysis

Incorrect predictions are inspected individually to understand model weaknesses.

The analysis includes:

text
Actual Label
      โ†“
Model Prediction
      โ†“
Compare
      โ†“
Identify Incorrect Samples
      โ†“
Visual Inspection

This helps identify difficult handwriting patterns and provides opportunities for future model improvements.


๐Ÿ”ฎ Inference Pipeline

Once training is complete, the trained model is used to make predictions on unseen images.

text
Test Image
    โ†“
Normalize Pixel Values
    โ†“
Reshape โ†’ 28 ร— 28 ร— 1
    โ†“
Neural Network
    โ†“
Softmax Probabilities
    โ†“
Argmax
    โ†“
Predicted Digit

Example:

text
Input โ†’ Handwritten "7"

Model Output:
7 โ†’ 0.98

Prediction:
7

๐Ÿ’พ Model Serialization

After training, the model is saved in Keras format:

text
handwritten_digit_recognition.keras

The saved model contains the trained network configuration and learned parameters required for inference.

It can later be loaded without retraining:

python
model = tf.keras.models.load_model(
    "handwritten_digit_recognition.keras"
)

๐ŸŒ Streamlit Application

The trained model is integrated into a Streamlit interface to transform the machine learning model into an interactive application.

Application Workflow

text
User
 โ†“
Draw / Provide Digit
 โ†“
Image Processing
 โ†“
Normalization
 โ†“
28 ร— 28 ร— 1
 โ†“
Saved Neural Network
 โ†“
Prediction
 โ†“
Digit + Confidence

Application Features

  • โ€”Interactive user interface
  • โ€”Handwritten digit input
  • โ€”Automatic image preprocessing
  • โ€”Real-time prediction
  • โ€”Prediction confidence
  • โ€”Lightweight deployment

๐Ÿš€ Deployment

The application is designed for deployment using:

text
GitHub
   โ†“
Streamlit Community Cloud
   โ†“
Live Web Application

Deployment Architecture

text
                    User
                     โ”‚
                     โ–ผ
             Streamlit Web App
                     โ”‚
                     โ–ผ
              Image Processing
                     โ”‚
                     โ–ผ
          TensorFlow/Keras Model
                     โ”‚
                     โ–ผ
              Digit Prediction

๐Ÿ“ Project Structure

text
handwritten-digit-recognition-neural-network/
โ”‚
โ”œโ”€โ”€ app.py
โ”‚
โ”œโ”€โ”€ handwritten_digit_recognition.keras
โ”‚
โ”œโ”€โ”€ requirements.txt
โ”‚
โ”œโ”€โ”€ README.md
โ”‚
โ””โ”€โ”€ notebook/
    โ”‚
    โ””โ”€โ”€ handwritten_digit_recognition.ipynb

๐Ÿ› ๏ธ Technology Stack

Programming

  • โ€”Python

Data Processing

  • โ€”NumPy
  • โ€”Pandas

Visualization

  • โ€”Matplotlib

Machine Learning

  • โ€”Scikit-learn

Deep Learning

  • โ€”TensorFlow
  • โ€”Keras

Application

  • โ€”Streamlit

Development Environment

  • โ€”Google Colab
  • โ€”Jupyter Notebook

Version Control

  • โ€”Git
  • โ€”GitHub

Deployment

  • โ€”Streamlit Community Cloud

โš™๏ธ Installation

Clone the repository:

bash
git clone https://github.com/YOUR_USERNAME/handwritten-digit-recognition-neural-network.git

Navigate to the project:

bash
cd handwritten-digit-recognition-neural-network

Install dependencies:

bash
pip install -r requirements.txt

โ–ถ๏ธ Run Locally

Start the Streamlit application:

bash
streamlit run app.py

The application will become available through the local Streamlit server.


๐Ÿ“Š Results

The project evaluates the trained Neural Network using:

text
โœ“ Validation Accuracy
โœ“ Validation Loss
โœ“ Confusion Matrix
โœ“ Classification Report
โœ“ Prediction Visualization
โœ“ Error Analysis
Model performance: Add the final accuracy, loss, and other evaluation results here after completing training.

Example:

text
Validation Accuracy: XX.XX%
Validation Loss: X.XXXX

๐Ÿ’ก Key Learning Outcomes

This project provided practical experience with:

Deep Learning Fundamentals

  • โ€”Neural Networks
  • โ€”Dense layers
  • โ€”Forward propagation
  • โ€”Backpropagation
  • โ€”Activation functions
  • โ€”Loss functions
  • โ€”Optimization
  • โ€”Model training

Data Engineering

  • โ€”CSV data loading
  • โ€”Feature/target separation
  • โ€”Image reshaping
  • โ€”Pixel normalization
  • โ€”Label encoding

Model Evaluation

  • โ€”Accuracy
  • โ€”Loss curves
  • โ€”Confusion matrices
  • โ€”Classification reports
  • โ€”Error analysis

Deployment

  • โ€”Model serialization
  • โ€”Loading trained models
  • โ€”Streamlit application development
  • โ€”ML inference pipelines
  • โ€”Cloud deployment

๐Ÿšง Limitations

Although the model performs well on MNIST-style handwritten digits, the system may perform poorly on real-world handwriting that differs significantly from the training distribution.

Potential challenges include:

  • โ€”Different writing styles
  • โ€”Image rotation
  • โ€”Different stroke thickness
  • โ€”Poor contrast
  • โ€”Background noise
  • โ€”Incorrect image positioning
  • โ€”Non-standard image dimensions

The model is primarily designed for images similar to the training data.


๐Ÿ”ฎ Future Improvements

The project can be extended in several directions.

Deep Learning Improvements

  • โ€”Replace the Dense Neural Network with a CNN
  • โ€”Add Dropout for regularization
  • โ€”Perform hyperparameter tuning
  • โ€”Experiment with different optimizers
  • โ€”Compare multiple architectures

Computer Vision Improvements

  • โ€”Image centering
  • โ€”Noise removal
  • โ€”Thresholding
  • โ€”Stroke normalization
  • โ€”Automatic resizing

Application Improvements

  • โ€”Confidence visualization
  • โ€”Prediction probability chart
  • โ€”Clear/reset drawing functionality
  • โ€”Multiple digit recognition
  • โ€”Batch image prediction
  • โ€”Improved UI/UX

Production Improvements

  • โ€”FastAPI inference backend
  • โ€”React frontend
  • โ€”Docker containerization
  • โ€”REST API
  • โ€”Cloud-based model serving
  • โ€”Model monitoring

๐Ÿ”ฌ Next Version: CNN

A natural next step for this project is replacing the fully connected Neural Network with a Convolutional Neural Network (CNN).

Current architecture:

text
Image
 โ†“
Flatten
 โ†“
Dense
 โ†“
Dense
 โ†“
Output

Future architecture:

text
Image
 โ†“
Convolution
 โ†“
Pooling
 โ†“
Convolution
 โ†“
Pooling
 โ†“
Flatten
 โ†“
Dense
 โ†“
Output

CNNs are generally better suited for image-related tasks because they can learn spatial and local visual features more effectively.


๐ŸŽ“ Project Significance

This project demonstrates the transition from traditional Machine Learning to Deep Learning by implementing a complete neural-network-based image classification system.

Rather than stopping at model training, the project extends through:

text
Data
 โ†“
Preprocessing
 โ†“
Deep Learning
 โ†“
Evaluation
 โ†“
Inference
 โ†“
Model Serialization
 โ†“
Web Application
 โ†“
Deployment

This makes the project an end-to-end AI application rather than only a notebook-based experiment.

๐Ÿš€ Live Demo

Try the deployed application:

๐Ÿ‘‰ https://handwritten-digit-recognition-neural-network-oqnf6mdndfsmdyfzk.streamlit.app/

Draw a handwritten digit from 0โ€“9 and the trained Neural Network will predict the digit with a confidence score.


๐Ÿ‘จโ€๐Ÿ’ป Author

Pranav Sharma

Computer Science Undergraduate focused on:

  • โ€”Artificial Intelligence
  • โ€”Machine Learning
  • โ€”Generative AI
  • โ€”Deep Learning
  • โ€”Software Engineering

Building practical AI-powered applications and exploring the intersection of Machine Learning and software development.


โญ Acknowledgements

This project was developed as part of my Deep Learning learning journey, with the goal of understanding Neural Networks from fundamentals through deployment.


๐Ÿ“œ License

This project is available under the MIT Licence.