A PyTorch implementation of LeNet-5 for handwritten digit recognition using the MNIST dataset, with Flask API for inference.
- Python 3.8+
- PyTorch
- Conda (recommended)
# Create conda environment
conda create -n mnist-lenet python=3.8
conda activate mnist-lenet
# Install dependencies
conda install pytorch torchvision -c pytorch
conda install flask pillowmodel.py: LeNet-5 neural network architecturedataset.py: MNIST data loading and preprocessingtrain.py: Training scriptinference.py: Model inference utilitiesmain.py: Training and Flask API endpoint
Run training to generate model weights:
python train.pyWeights saved to: ./weights/lenet5.pth
python main.py --image_path /path/to/digit/image.pngStart server:
python main.pyMake POST request to /infer with JSON payload:
{
"img_path": "/path/to/digit/image.png"
}- LeNet-5 CNN architecture
- MNIST dataset preprocessing
- Single/batch image prediction
- Top-k predictions
- Flask inference API
Automatically detects and uses CUDA if available, otherwise falls back to CPU.