This project implements a Convolutional Neural Network (CNN) for classifying images from the Fashion-MNIST dataset. Fashion-MNIST is a dataset consisting of 70,000 grayscale images (28x28 pixels) representing 10 categories of fashion items, such as t-shirts, trousers, dresses, and shoes.
The CNN model is designed to classify Fashion-MNIST images into their corresponding categories. The architecture consists of three convolutional layers with Batch normalization and ReLU activation, each followed by max pooling to reduce spatial dimensions. The network then flattens the features and passes them through two fully connected layers (3136→784→10) with ReLU activation and dropout regularization (0.3) for the final classification into 10 classes. The model uses CrossEntropy loss and the Adam optimizer for training, with TensorBoard integration for monitoring training and validation metrics.
The project supports both CPU and GPU (CUDA) execution, automatically detecting available NVIDIA hardware.
-
neural_network.py— Defines theFashion_MNIST_CNNclass, which implements the neural network architecture. The model uses three Conv2d layers (16, 32, and 64 filters with kernel size 3), BatchNorm2d layers, ReLU activation, MaxPool2d layers, a flattened fully connected layer (3136→784) with ReLU dropout (0.3), and a final linear layer producing 10 class logits. -
training.py— Handles the full training pipeline. It downloads the Fashion-MNIST dataset (if not already present), creates data loaders with batch size 64, configures the optimizer (Adam, learning rate 3e-4), and runs the training loop for 5 epochs. The script supports command-line arguments for learning rate (-lr), batch size (-b), and epochs (-e). Training and validation losses are logged to TensorBoard viatorch.utils.tensorboard. Validation accuracy is also printed each epoch. The trained model weights are saved toFashion_MNIST_CNN.pth. -
inference.py— Loads the trained model fromFashion_MNIST_CNN.pthand evaluates it on random test samples. It selects 9 random images from the test set, runs them through the model, and saves a 3×3 grid visualization (predictions.png) showing the ground truth labels alongside the model predictions, with correct predictions marked with a check mark and incorrect ones with a cross mark.
The project requires the following dependencies, which should be listed in requirements.txt:
torch— PyTorch deep learning frameworktorchvision— Computer vision utilities and dataset loadersmatplotlib— Plotting library for visualizing predictionsnumpy— Numerical computing librarytensorboard— ML visualization library
Install the dependencies using pip:
pip install -r requirements.txt
Train the model:
python training.py
You can customize training hyperparameters:
python training.py --lr 3e-4 --batch-size 64 --epochs 5
Alternatively, download a pretrained model here.
Run inference on the trained model:
python inference.py
Training metrics can be monitored live using TensorBoard:
tensorboard --logdir=runs