A complete end-to-end machine learning application that recognizes handwritten digits (0-9) using a deep learning CNN model. The project includes a trained PyTorch model, FastAPI backend, and React frontend.
- Upload handwritten digit images in any size (automatically resized to 28Γ28)
- Real-time predictions with confidence percentage
- Image preview before and after prediction
- Responsive React UI with smooth animations
- REST API built with FastAPI for scalability
- CNN model trained on MNIST dataset with 7 epochs
- Cross-origin support for frontend-backend communication
MNIST_Project_Alex/
βββ app/
β βββ main.py # Training script with validation & test evaluation
β βββ load_model.py # FastAPI backend server
β βββ requirements.txt # Python dependencies
βββ model/
β βββ mnist_model.pth # Trained PyTorch model weights
β βββ training_loss.png # Training visualization
βββ frontend/
β βββ src/
β β βββ App.jsx # Main React component
β β βββ App.css # Styling
β β βββ index.jsx # Entry point
β βββ public/
β β βββ index.html # HTML template
β βββ package.json # NPM dependencies
βββ notebooks/
β βββ mnist.ipynb # Jupyter notebook for exploration
βββ README.md # This file
- Python 3.8+
- Node.js 14+
- pip (Python package manager)
- npm (Node package manager)
cd app
pip install -r requirements.txt
python load_model.pyThe API will run on http://localhost:8000
Visit http://localhost:8000/docs for interactive API documentation (Swagger UI)
Open a new terminal in the frontend directory:
cd frontend
npm install
npm startThe app will run on http://localhost:3000 and automatically open in your browser.
- Start both servers (Backend on 8000, Frontend on 3000)
- Select an image β Click "π Choose Image" to upload a handwritten digit
- View preview β The selected image displays instantly
- Get prediction β Click "Predict" button to analyze the digit
- See results β View predicted digit and confidence percentage
- Try another β Click "π Try Another" to reset and upload a new image
The project uses a Convolutional Neural Network (CNN) with the following layers:
Conv2d(1, 32, kernel_size=5) β ReLU β MaxPool2d
Conv2d(32, 64, kernel_size=5) β ReLU β MaxPool2d
Flatten β Linear(64Γ7Γ7, 128) β ReLU β Linear(128, 10)
Training Details:
- Dataset: MNIST (70,000 handwritten digit images)
- Train/Validation Split: 80/20
- Epochs: 7
- Batch Size: 64
- Optimizer: Adam (lr=0.001)
To retrain the model with your own settings:
cd app
python main.pyThis will:
- Load MNIST dataset
- Split into train/validation (80/20)
- Train for 7 epochs
- Display loss metrics
- Save model as
../model/mnist_model.pth - Generate
training_loss.pngvisualization - Evaluate on test dataset
Upload an image and get digit prediction.
Request:
curl -F "file=@digit.png" http://localhost:8000/predictResponse:
{
"prediction": 7,
"confidence": "99.85%",
"probabilities": [0.001, 0.002, ..., 0.999, 0.003]
}Get API info.
curl http://localhost:8000/Create app/requirements.txt:
torch==2.0.0
torchvision==0.15.0
fastapi==0.104.0
uvicorn==0.24.0
pillow==10.0.0
python-multipart==0.0.6
Install:
cd app
pip install -r requirements.txtcd frontend
npm installAutomatically installs:
- react, react-dom
- axios
- react-scripts
- Check if backend is running on port 8000
- Run
python load_model.pyin the app folder - Refresh browser (Ctrl+Shift+R)
- Ensure
model/mnist_model.pthexists - If missing, run
python main.pyto train the model
- Backend: Change port in
load_model.pyβuvicorn.run(app, port=8001) - Frontend: Run
npm start -- --port 3001
- Backend CORS is pre-configured for
http://localhost:3000 - For production, update CORS origins in
load_model.py
- Training Accuracy: ~99%+
- Test Accuracy: ~98%+
- Prediction Time: <100ms per image
- Model Size: ~500KB
1. Select Image (Frontend)
β
2. Send to Backend (HTTP POST)
β
3. Preprocess (Grayscale + Resize to 28Γ28)
β
4. Forward through CNN
β
5. Get Softmax Probabilities
β
6. Display Prediction & Confidence (Frontend)
- Backend: Containerized FastAPI app
- Frontend: Static build served by nginx
- Memory efficient and portable
- Package both frontend and backend
- Update API endpoint in React app
- Deploy with CI/CD pipeline
Backend:
- PyTorch β Deep learning framework
- FastAPI β Modern async web framework
- Uvicorn β ASGI server
- Pillow β Image processing
Frontend:
- React 18 β UI library
- CSS3 β Styling with gradients & animations
- FileReader API β Client-side image handling
ML/Data:
- MNIST Dataset β 70,000 handwritten digits
- TorchVision β Dataset utilities
This project is open source and available under the MIT License.
Created as a full-stack machine learning project demonstrating end-to-end workflow from model training to deployment.
Feel free to fork, modify, and improve this project! Some ideas:
- Add more digit recognition models (ResNet, VGG)
- Implement digit drawing canvas in frontend
- Add batch prediction
- Create mobile app version
- Deploy to cloud
Questions? Check the troubleshooting section or open an issue on GitHub.
Happy digit prediction! π