Skip to content

shard-c6/cv-ml-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 MNIST Handwritten Digit Recognition

Python 3.10+ PyTorch License: MIT Open Issues Stars

A deep learning project that identifies handwritten digits (0–9) using a custom Convolutional Neural Network (CNN) built with PyTorch, trained on the MNIST dataset, with interactive prediction interfaces powered by OpenCV and Gradio.

(A placeholder for a demo GIF or screenshot showing the Gradio or OpenCV app in action)

πŸ“‘ Table of Contents


🎯 Project Overview

This project demonstrates the complete deep learning pipeline:

  1. Data Exploration β€” Understanding the MNIST dataset structure and distribution
  2. Model Training β€” Training a CNN from scratch using PyTorch (with Apple Silicon MPS acceleration)
  3. FastAI Baseline β€” Training a transfer learning baseline with FastAI for comparison
  4. Interactive Prediction β€” Drawing digits and getting real-time predictions via:
    • An OpenCV desktop canvas application
    • A Gradio browser-based web UI
  5. Model Evaluation β€” Confusion matrices, misclassification analysis, and per-class metrics

πŸ“Š Results

Metric Value
Training Accuracy 98.27%
Validation Accuracy 99.14%
Architecture 2-layer CNN with Dropout
Training Device Apple Silicon M5 (MPS)
Training Time ~5 epochs

πŸ—οΈ Project Structure

CV_ML_Project/
β”œβ”€β”€ README.md                         # This file
β”œβ”€β”€ requirements.txt                  # Python dependencies
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ 01_mnist_exploration.ipynb    # Data exploration & visualization
β”‚   β”œβ”€β”€ 02_fastai_training.ipynb      # FastAI transfer learning baseline
β”‚   β”œβ”€β”€ 03_pytorch_from_scratch.ipynb # Custom CNN training notebook
β”‚   └── 04_evaluation.ipynb           # Model evaluation & metrics
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ model.py                      # CNN architecture (DigitCNN)
β”‚   β”œβ”€β”€ train.py                      # Standalone training script
β”‚   β”œβ”€β”€ predict.py                    # Single-image prediction utility
β”‚   └── preprocess.py                 # Image preprocessing pipeline
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ canvas.py                     # OpenCV interactive drawing canvas
β”‚   └── web_ui.py                     # Gradio web-based drawing UI
β”œβ”€β”€ models/
β”‚   └── digit_cnn.pth                 # Trained model weights
β”œβ”€β”€ data/
β”‚   └── MNIST/                        # Auto-downloaded MNIST dataset
└── tests/
    └── test_predict.py               # Unit tests for pipeline

πŸš€ Getting Started

Prerequisites

  • Python 3.10+
  • macOS with Apple Silicon (M1/M2/M3/M4/M5) recommended for MPS GPU acceleration

Installation

# Clone the repository
git clone git@github.com:shard-c6/cv-ml-project.git
cd cv-ml-project

# Install dependencies
pip install -r requirements.txt

Train the Model

The trained weights are already included (models/digit_cnn.pth), but you can retrain:

python3 src/train.py

Run the OpenCV Canvas App

python3 app/canvas.py
  • Draw a digit with your mouse
  • Press p to predict
  • Press c to clear
  • Press q to quit

Run the Gradio Web UI

python3 app/web_ui.py

Open http://127.0.0.1:7860 in your browser, draw a digit, and click Identify Digit.

Run Tests

python3 tests/test_predict.py

🧠 CNN Architecture

Input (1 Γ— 28 Γ— 28)
  ↓
Conv2d(1 β†’ 32, 3Γ—3, padding=1) β†’ ReLU β†’ MaxPool2d(2Γ—2) β†’ Dropout2d(0.15)
  ↓
Conv2d(32 β†’ 64, 3Γ—3, padding=1) β†’ ReLU β†’ MaxPool2d(2Γ—2) β†’ Dropout2d(0.25)
  ↓
Flatten β†’ Linear(64Γ—7Γ—7 β†’ 128) β†’ ReLU β†’ Dropout(0.5)
  ↓
Linear(128 β†’ 10) β†’ Output (digit 0–9)

πŸ”§ Technologies Used

Technology Purpose
PyTorch Deep learning framework, CNN implementation
FastAI High-level training API, transfer learning
OpenCV Image preprocessing, interactive drawing canvas
Gradio Browser-based web UI for predictions
torchvision MNIST dataset loading and transforms
Matplotlib Data visualization and evaluation plots
NumPy Numerical computing and array operations

πŸ““ Notebooks Guide

Notebook Description
01_mnist_exploration.ipynb Load MNIST, visualize samples, check class distribution
02_fastai_training.ipynb Train a ResNet18 baseline with FastAI, confusion matrix
03_pytorch_from_scratch.ipynb Build and train a CNN from scratch in PyTorch
04_evaluation.ipynb Full evaluation: confusion matrix, misclassified examples

🀝 Contributing

Contributions are welcome! Please check out the CONTRIBUTING.md guide to see how you can help, whether by reporting bugs, suggesting features, or opening pull requests.


πŸ™ Acknowledgments

  • The MNIST Database by Yann LeCun, Corinna Cortes, and Christopher J.C. Burges
  • PyTorch & FastAI for making deep learning accessible
  • Gradio for the intuitive web UI framework

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


⬆️ Back to Top

About

🧠 MNIST Handwritten Digit Recognition β€” CNN built from scratch with PyTorch, achieving 99.14% accuracy. Interactive prediction via OpenCV canvas & Gradio web UI.

Topics

Resources

License

Code of conduct

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors