This project is a Convolutional Neural Network (CNN) built in PyTorch that classifies handwritten digits (0-9). It is trained on the 60,000-image MNIST dataset and achieves high accuracy.
The key feature is the inclusion of Grad-CAM (Gradient-weighted Class Activation Mapping), which generates a visual heatmap to explain why the model made its decision. The model also provides a confidence percentage for each prediction.
- High-Accuracy Model: The CNN is trained for 10 epochs to achieve >98% accuracy on the test set.
- Explainable AI (XAI): Uses Grad-CAM to create a heatmap, highlighting the pixels the model "looked at" to make its prediction.
- Confidence Score: Outputs the prediction's confidence as a percentage.
- Robust Prediction: Includes a pre-processing pipeline in
Cell 6to clean, center, and format user-uploaded images for the model.
Here is the model correctly identifying a user-uploaded image of the digit "2" with 99.99% confidence. The Grad-CAM heatmap clearly shows the model focused on the correct strokes of the digit.
- PyTorch
- OpenCV (cv2)
- Pillow (PIL)
- Matplotlib
- Google Colab (or Jupyter Notebook)
This is the fast-track guide to make predictions using the included pre-trained model.
Required Files:
MNIST_Digit_Classifier.ipynb(This notebook)my_mnist_model.pth(The pre-trained model file)
Steps:
- Open the Notebook: Open the
.ipynbfile in Google Colab or Jupyter. - Upload the Model:
- In Colab, click the folder icon (📁) on the left.
- Click the "Upload" icon and select your
my_mnist_model.pthfile.
- Run Setup Cells: Run the following cells in order:
Cell 1: Setup & ImportsCell 2: Load Data(This defines thetransform_testneeded for prediction)Cell 3: Model & Helper Functions(This defines theSmallCNNclass)
- SKIP Cell 4: You can skip
Cell 4: Run Trainingentirely. - Load Your Model: Run
Cell 5: Load Model & Grad-CAM Setup. This will load your uploaded.pthfile. - Predict! Run
Cell 6: Predict on Your Own Imageand upload a picture of a digit.
If you don't have the .pth file or want to train the model yourself, just run all cells in order.
- Run
Cell 1: Setup & Imports - Run
Cell 2: Load Data - Run
Cell 3: Model & Helper Functions - Run
Cell 4: Run Training- This will train the model for 10 epochs (about 2-3 minutes) and save the new
my_mnist_model.pthfile.
- This will train the model for 10 epochs (about 2-3 minutes) and save the new
- Run
Cell 5: Load Model & Grad-CAM Setup - Run
Cell 6: Predict on Your Own Image