Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project Overview

The mnist_digit_classifier.py script builds, trains, and evaluates a deep learning model for image classification. It's a foundational example for understanding CNNs with PyTorch.

Script Description: scripts/mnist_digit_classifier.py

This script performs the following key steps:

  1. Data Loading and Preprocessing:
    • Downloads the MNIST dataset (training and test sets) from torchvision.datasets.
    • Automatically transforms images into PyTorch tensors using ToTensor().
    • Creates DataLoader objects for efficient batch processing, with shuffling for the training data.
    • Prints the shape of a sample batch to verify data dimensions.
  2. Device Configuration:
    • Automatically detects and utilizes a GPU (CUDA) if available; otherwise, it defaults to CPU.
  3. Neural Network Definition:
    • Defines a NeuralNetwork class inheriting from nn.Module.
    • It consists of:
      • A conv_stack (convolutional layers) with nn.Conv2d, nn.ReLU activations, and nn.MaxPool2d for feature extraction and downsampling. This stack includes three convolutional blocks.
      • A classifier (fully connected layers) with nn.Flatten, nn.Linear layers, nn.ReLU activations, and nn.Dropout for classification and regularization.
    • The forward method defines the pass through the network.
  4. Model Initialization:
    • Instantiates the NeuralNetwork model and moves it to the selected device (GPU/CPU).
  5. Loss Function and Optimizer:
    • Uses nn.CrossEntropyLoss() as the loss function, suitable for multi-class classification.
    • Employs the Adam optimizer with a learning rate of 1e-3 and weight_decay for regularization.
  6. Training and Testing Functions:
    • train(dataloader, model, loss_fn, optimizer): Iterates over the training data, performs forward and backward passes, updates model weights, and tracks training loss and accuracy per epoch.
    • test(dataloader, model, loss_fn): Evaluates the model's performance on the test set (without gradient calculation), tracking test loss and accuracy.
  7. Model Training Loop:
    • Trains the model for a specified number of epochs (default 8), calling the train and test functions for each epoch.
    • Prints progress including loss and accuracy for both training and testing.
  8. Model Saving:
    • Saves the trained model's state dictionary to mnist_model.pth.
  9. Results Visualization:
    • Plots the training and test loss over epochs.
    • Plots the training and test accuracy over epochs.
    • Displays 9 random test images along with their predicted and true labels to visually assess performance.
  10. Confusion Matrix:
    • Generates and plots a confusion matrix using sklearn.metrics.confusion_matrix to provide a detailed breakdown of correct and incorrect classifications for each digit.

Dataset

This project uses the MNIST dataset, which is a large database of handwritten digits commonly used for training various image processing systems. It consists of:

  • 60,000 training images.
  • 10,000 test images.
  • Each image is a 28x28 pixel grayscale image of a handwritten digit (0-9).

About

A PyTorch-based Convolutional Neural Network (CNN) for handwritten digit recognition on the MNIST dataset, including training, evaluation, and visualization.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages