This project implements a Convolutional Neural Network (CNN) using PyTorch to classify handwritten digits from the MNIST dataset. It includes model training, evaluation, and inference on custom images.
ML_/
├── data/ # (Optional) Data directory
├── MNIST/ # MNIST data (raw)
├── src/
│ ├── model.py # Model definition and training script
│ ├── test.py # Script to test the model on a custom image
│ ├── mnist_model.pth # Trained model weights
│ └── image.png # Example image for inference
├── README.md # Project documentation
└── ...
- Python 3.8+
- PyTorch
- torchvision
- Pillow
Install dependencies:
pip install torch torchvision pillowThe model is defined and trained in src/model.py. To train the model on the MNIST dataset and save the weights:
cd src
python model.pyThis will download the MNIST dataset (if not present), train the CNN, and save the model as mnist_model.pth.
To predict the digit in a custom image (image.png):
- Place a 28x28 grayscale image of a digit as
src/image.png(or modify the path intest.py). - Run:
cd src
python test.pyThe script will output the predicted digit.
- 1 convolutional layer (32 filters, 3x3 kernel)
- ReLU activations
- Max pooling
- Fully connected layer (output: 10 classes)
- The MNIST dataset is automatically downloaded to the
MNIST/raw/directory. - The model expects grayscale images of size 28x28 for inference.
MIT License