HU
hurkanugur/MNIST-Digit-Classifier
This project implements a CNN for handwritten digit classification on the MNIST dataset using PyTorch. It uses stacked convolutional layers with dropout, batch normalization, and max pooling to classify 28ร28 grayscale digits (0โ9) with Softmax output.
cnnsconvolutional-neural-networksdeep-learningdigit-classificationdigit-classifierdigit-recognition-mnistgradiogradio-interfacehandwritten-digit-classificationhandwritten-digit-recognitionmax-poolingmnistmnist-classificationmnist-classifiermnist-handwriting-recognitionmulti-class-classificationpytorchsoftmax
๐ MNIST Handwritten Digit Classifier
๐ Overview
This project predicts handwritten digit classes (0โ9) using the MNIST dataset and a convolutional neural network (CNN) built with PyTorch. It demonstrates a full machine learning pipeline from data loading to inference, including:
- ๐ง CNN with stacked convolutional layers, Batch Normalization, Max Pooling, LeakyReLU activation, and Dropout
- โ๏ธ Cross-Entropy Loss for multi-class classification
- ๐ Adam optimizer for gradient updates
- ๐ Mini-batch training with
DataLoader - ๐ Train/Validation/Test split for robust evaluation
- ๐ Live training & validation loss monitoring
- โ Softmax activation on the output for probability distribution across 10 classes
- ๐จ Interactive Gradio Interface for real-time prediction
๐ผ๏ธ Application Screenshot
Below is a preview of the Gradio Interface used for real-time classification:
๐งฉ Libraries
- PyTorch โ model, training, and inference
- pandas โ data handling
- matplotlib โ loss visualization
- pickle โ saving/loading normalization params and trained model
- Gradio โ interactive web interface for real-time model demos
โ๏ธ Requirements
- Python 3.13+
- Recommended editor: VS Code
๐ฆ Installation
- Clone the repository
git clone https://github.com/hurkanugur/MNIST-Digit-Classifier.git- Navigate to the
MNIST-Digit-Classifierdirectory
cd MNIST-Digit-Classifier- Install dependencies
pip install -r requirements.txt๐ง Setup Python Environment in VS Code
View โ Command Palette โ Python: Create Environment- Choose Venv and your Python version
- Select requirements.txt to install dependencies
- Click OK
๐ Project Structure
assets/
โโโ app_screenshot.png # Screenshot of the application
โโโ 1, 2, 3 ... 9.png # Digit samples
data/
โโโ MNIST # MNIST dataset
model/
โโโ mnist_digit_classifier.pth # Trained model
src/
โโโ config.py # Paths, hyperparameters, split ratios
โโโ dataset.py # Data loading & preprocessing
โโโ device_manager.py # Selects and manages compute device
โโโ train.py # Training pipeline
โโโ inference.py # Inference pipeline
โโโ model.py # Neural network definition
โโโ visualize.py # Training/validation plots
main/
โโโ main_train.py # Entry point for training
โโโ main_inference.py # Entry point for inference
requirements.txt # Python dependencies๐ Model Architecture
Input (1ร28ร28)
Conv Block 1:
โ Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1, padding_mode="reflect")
โ BatchNorm2d(32)
โ ReLU
โ MaxPool2d(kernel_size=2, stride=2)
โ Dropout(0.25)
Conv Block 2:
โ Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1, padding_mode="reflect")
โ BatchNorm2d(64)
โ ReLU
โ MaxPool2d(kernel_size=2, stride=2)
โ Dropout(0.25)
Fully Connected:
โ Flatten
โ Linear(64ร7ร7, 128)
โ ReLU
โ BatchNorm1d(128)
โ Dropout(0.5)
โ Linear(128, 10)
โ Softmax(Output)๐ Train the Model
Navigate to the project directory:
cd MNIST-Digit-ClassifierRun the training script:
python -m main.main_trainor
python3 -m main.main_train๐ Run Inference / Make Predictions
Navigate to the project directory:
cd MNIST-Digit-ClassifierRun the app:
python -m main.main_inferenceor
python3 -m main.main_inference