Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

✍️ Handwritten Digit Recognizer

Python TensorFlow Gradio License: MIT

A Convolutional Neural Network (CNN) trained on the MNIST dataset that achieves ~99.4% test accuracy on handwritten digit recognition (0–9).
Includes an interactive Gradio web demo where you can draw a digit and get an instant prediction.


🖼️ Demo

Draw on canvas Upload an image
Draw any digit with your mouse or touchscreen Upload a 28×28 (or any size) grayscale image

📁 Project Structure

handwritten-digit-recognizer/
├── train.py          # Train the CNN on MNIST and save model.h5
├── predict.py        # CLI tool to run inference on any image
├── app.py            # Interactive Gradio web demo
├── requirements.txt  # Python dependencies
└── README.md

🚀 Quick Start

1. Install Dependencies

pip install -r requirements.txt

2. Train the Model

python train.py

This will:

  • Automatically download the MNIST dataset (~11 MB)
  • Train the CNN for up to 30 epochs with early stopping
  • Save the best model to model.h5
  • Generate training_curves.png

Expected output:

Test Accuracy : 99.40%
✅ Model saved to model.h5

3. Launch the Web Demo

python app.py

Opens a Gradio interface in your browser at http://localhost:7860.

4. CLI Prediction

python predict.py --image my_digit.png
python predict.py --image my_digit.png --show   # also display a visualisation

🧠 Model Architecture

Input (28×28×1)
  │
  ├─ Conv2D(32, 3×3) + BatchNorm → Conv2D(32, 3×3) + BatchNorm
  ├─ MaxPool(2×2) → Dropout(0.25)
  │
  ├─ Conv2D(64, 3×3) + BatchNorm → Conv2D(64, 3×3) + BatchNorm
  ├─ MaxPool(2×2) → Dropout(0.25)
  │
  ├─ Flatten → Dense(256) + BatchNorm → Dropout(0.5)
  └─ Dense(10, softmax)

Training details:

Parameter Value
Optimizer Adam (lr = 1e-3)
Loss Categorical cross-entropy
Batch size 128
Max epochs 30 (early stopping)
Data augmentation Random rotation ±10°, zoom ±10%, translation ±10%
Test accuracy ~99.4%

📊 Results

Metric Value
Training accuracy ~99.8%
Validation accuracy ~99.5%
Test accuracy ~99.4%
Parameters ~430,000

🛠️ Requirements

  • Python 3.9+
  • TensorFlow / Keras 2.12+
  • NumPy
  • Pillow
  • Matplotlib
  • Gradio 4+

📜 License

MIT License — see LICENSE.

About

CNN trained on MNIST to recognize handwritten digits (0-9) with a Gradio web demo. ~99.4% test accuracy.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages