Convolutional neural networks for classifying variants of MNIST dataset
This project implements and evaluates Convolutional Neural Networks (CNNs) for classifying different variants of the MNIST dataset. The implementation includes both handcrafted feature extraction and CNN-based approaches, with a focus on comparing their performance across various dataset modifications.
The project evaluates the models on three MNIST dataset variants:
- Normal MNIST (baseline) - Original MNIST dataset without modifications
- Scaled MNIST - Pixel values are transformed using random scaling and offset:
- Each image I is modified as I ← aI + b
- Where a and b are random numbers ∈ [0, 1]
- This introduces random brightness and contrast variations
- Jittered MNIST - Images are randomly translated:
- Random translation within [-10, 10] pixels in both horizontal and vertical directions
- This simulates position variations in digit placement
The CNN model implements the following architecture:
- Input Layer: 28x28 grayscale images
- First Convolutional Block:
- Conv2D layer (32 filters, 3x3 kernel)
- BatchNorm2D
- ReLU activation
- Conv2D layer (32 filters, 3x3 kernel)
- BatchNorm2D
- ReLU activation
- MaxPooling2D (2x2)
- Dropout (0.25)
- Second Convolutional Block:
- Conv2D layer (64 filters, 3x3 kernel)
- BatchNorm2D
- ReLU activation
- Conv2D layer (64 filters, 3x3 kernel)
- BatchNorm2D
- ReLU activation
- MaxPooling2D (2x2)
- Dropout (0.25)
- Classifier:
- Flattening Layer
- Dense Layer (512 units with BatchNorm and ReLU)
- Dropout (0.25)
- Dense Layer (1024 units with BatchNorm and ReLU)
- Dropout (0.5)
- Output Layer (10 units)
The model was trained using Bayesian optimization to find optimal hyperparameters for each dataset variant.
- Test Accuracy: 96.20%
- Best Hyperparameters:
- Batch Size: 34
- Learning Rate: 0.0092
- Number of Epochs: 92
- Best Validation Accuracy: 96.77%
- Test Accuracy: 95.20%
- Best Hyperparameters:
- Batch Size: 32
- Learning Rate: 0.0028
- Number of Epochs: 60
- Best Validation Accuracy: 94.17%
- Test Accuracy: 79.80%
- Best Hyperparameters:
- Batch Size: 116
- Learning Rate: 0.0042
- Number of Epochs: 62
- Best Validation Accuracy: 84.27%
- The CNN model achieves high accuracy (>95%) on both normal and scaled MNIST datasets
- Performance significantly degrades on jittered MNIST (79.80%), indicating sensitivity to pixel-level noise
- Bayesian optimization found different optimal hyperparameters for each dataset variant
- The model architecture with batch normalization and dropout shows good regularization properties
.
├── code/
│ ├── CNNModel.py # CNN model implementation
│ ├── TestCNN.ipynb # Main notebook for CNN evaluation
│ ├── TestCNN.html # HTML export of the notebook
│ ├── digitFeatures.py # Handcrafted feature extraction
│ ├── linearModel.py # Linear model implementation
│ └── testHandcrafted.py # Handcrafted feature testing
├── data/ # Dataset storage
├── requirements.txt # Project dependencies
└── README.md # Project documentation
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Unix/macOS # or .\venv\Scripts\activate # On Windows
-
Install dependencies:
pip install -r requirements.txt
-
Run the notebook:
jupyter notebook code/TestCNN.ipynb
- Python 3.10+
- PyTorch
- NumPy
- Matplotlib
- Jupyter
- scikit-learn
- bayesian-optimization
- utils (custom module for data loading and preprocessing)


