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)
- Project Overview
- Results
- Project Structure
- Getting Started
- CNN Architecture
- Technologies Used
- Notebooks Guide
- Contributing
- License
This project demonstrates the complete deep learning pipeline:
- Data Exploration β Understanding the MNIST dataset structure and distribution
- Model Training β Training a CNN from scratch using PyTorch (with Apple Silicon MPS acceleration)
- FastAI Baseline β Training a transfer learning baseline with FastAI for comparison
- Interactive Prediction β Drawing digits and getting real-time predictions via:
- An OpenCV desktop canvas application
- A Gradio browser-based web UI
- Model Evaluation β Confusion matrices, misclassification analysis, and per-class metrics
| 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 |
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
- Python 3.10+
- macOS with Apple Silicon (M1/M2/M3/M4/M5) recommended for MPS GPU acceleration
# Clone the repository
git clone git@github.com:shard-c6/cv-ml-project.git
cd cv-ml-project
# Install dependencies
pip install -r requirements.txtThe trained weights are already included (models/digit_cnn.pth), but you can retrain:
python3 src/train.pypython3 app/canvas.py- Draw a digit with your mouse
- Press
pto predict - Press
cto clear - Press
qto quit
python3 app/web_ui.pyOpen http://127.0.0.1:7860 in your browser, draw a digit, and click Identify Digit.
python3 tests/test_predict.pyInput (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)
| 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 |
| 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 |
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.
- 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
This project is licensed under the MIT License - see the LICENSE file for details.