This repository contains an implementation of a Convolutional Neural Network (CNN) for the FashionMNIST dataset using PyTorch. FashionMNIST is a dataset of Zalando's article images—consisting of 60,000 training examples and 10,000 test examples. Each example is a 28x28 grayscale image associated with a label from 10 classes.
fashion-mnist-cnn/
│
├── src/
│ ├── dataset.py # Data loading and preprocessing
│ ├── model.py # CNN architecture definition
│ ├── train.py # Training loop and logic
│ ├── evaluate.py # Model evaluation script
│ └── utils.py # Utility functions for training and evaluation
│
├── tests/
│ └── test_model.py # Unit tests for the model
│
├── requirements.txt # Dependencies for the project
├── README.md # This file
└── LICENSE # Project licenseThe CNN model defined in model.py employs the following structure:
- Input Layer: Takes in a 1x28x28 image (grayscale).
- Convolutional Layers:
- Two convolutional blocks:
- First Block:
- Conv2D: 64 filters, kernel size 3x3, padding 1
- ReLU activation
- Batch Normalization
- Conv2D: 64 filters, kernel size 3x3, padding 1
- ReLU activation
- Batch Normalization
- Max Pooling: kernel size 2, stride 2
- Dropout: p=0.25 for regularization
- Second Block:
- Conv2D: 128 filters, kernel size 3x3, padding 1
- ReLU activation
- Batch Normalization
- Conv2D: 128 filters, kernel size 3x3, padding 1
- ReLU activation
- Batch Normalization
- Max Pooling: kernel size 2, stride 2
- Dropout: p=0.25 for regularization
- First Block:
- Two convolutional blocks:
- Fully Connected Layers:
- Flatten layer
- Dense layer with 256 units, ReLU activation, followed by Batch Normalization
- Dropout with p=0.5
- Output layer with 10 units (one for each class in FashionMNIST), no explicit softmax as CrossEntropyLoss in PyTorch includes it.
The training script train.py uses:
- Optimizer: Adam with learning rate = 0.01
- Loss Function: Cross Entropy Loss
- Batch Size: 64
- Epochs: Configurable, default set to 20
- Device: Automatic selection between CUDA (if available) or CPU
- Data Augmentation: Random rotation up to 10 degrees, random horizontal flips to increase dataset diversity.
- Learning Rate Scheduler: StepLR with step_size=7, gamma=0.1 to decrease learning rate over time.
- Early Stopping: Implemented to prevent overfitting, patience set to 5 epochs.
The evaluate.py script provides:
- Metrics: Accuracy, Confusion Matrix, Classification Report
- Visualization: Examples of misclassified images, ROC curves for multi-class classification if extended.
-
Clone this repository:
git clone https://github.com/mathiasmendozav/DeepCNN-Pytorch-FashionMNIST cd DeepCNN-Pytorch-FashionMNIST -
Install dependencies:
pip install -r requirements.txt
-
Download the FashionMNIST dataset: This is automatically handled by
src/dataset.pyon the first run. -
Train the model:
python src/train.py
-
Evaluate the model:
python src/evaluate.py
- TensorBoard Integration: For real-time tracking of training metrics.
- Model Checkpointing: Saving the best model based on validation loss.
- Hyperparameter Tuning: Script included for tuning using grid search or random search over defined parameters.
Contributions are welcome. Please fork the repository, create a feature branch, and submit a pull request. Ensure your code passes the existing unit tests and adds new tests if introducing new functionality.
This project is licensed under the MIT License.
