STAT 852 Machine Learning Project | December 2023
Author: Diksha Jethnani
This project implements a Convolutional Neural Network (CNN) for multi-class image classification using the CIFAR-10 dataset. The model achieves high accuracy through carefully designed architecture including convolutional layers, pooling layers, and fully connected layers with optimized hyperparameters.
- Test Accuracy: 67.57%
- Training Accuracy: 82.93%
- Model Complexity: ~420K parameters
- Training Time: < 1 minute per epoch
The CIFAR-10 dataset consists of 60,000 32x32 color images in 10 classes:
| Class ID | Label | Training Images | Test Images |
|---|---|---|---|
| 0 | Airplane | 5,000 | 1,000 |
| 1 | Automobile | 5,000 | 1,000 |
| 2 | Bird | 5,000 | 1,000 |
| 3 | Cat | 5,000 | 1,000 |
| 4 | Deer | 5,000 | 1,000 |
| 5 | Dog | 5,000 | 1,000 |
| 6 | Frog | 5,000 | 1,000 |
| 7 | Horse | 5,000 | 1,000 |
| 8 | Ship | 5,000 | 1,000 |
| 9 | Truck | 5,000 | 1,000 |
Total: 50,000 training images + 10,000 test images
The CNN architecture consists of multiple convolutional layers followed by pooling and fully connected layers:
Input (32x32x3 RGB Image)
β
Conv2D (32 filters, 3x3) + ReLU
β
Conv2D (32 filters, 3x3) + ReLU
β
MaxPooling2D (2x2)
β
Conv2D (64 filters, 3x3) + ReLU
β
Conv2D (64 filters, 3x3) + ReLU
β
MaxPooling2D (2x2)
β
Conv2D (128 filters, 3x3) + ReLU
β
Flatten
β
Dense (128 units) + ReLU
β
Dropout (0.5)
β
Dense (10 units) + Softmax
β
Output (10 classes)
| Layer Type | Output Shape | Parameters | Activation |
|---|---|---|---|
| Conv2D_1 | (30, 30, 32) | 896 | ReLU |
| Conv2D_2 | (28, 28, 32) | 9,248 | ReLU |
| MaxPooling2D_1 | (14, 14, 32) | 0 | - |
| Conv2D_3 | (12, 12, 64) | 18,496 | ReLU |
| Conv2D_4 | (10, 10, 64) | 36,928 | ReLU |
| MaxPooling2D_2 | (5, 5, 64) | 0 | - |
| Conv2D_5 | (3, 3, 128) | 73,856 | ReLU |
| Flatten | (1152) | 0 | - |
| Dense_1 | (128) | 147,584 | ReLU |
| Dropout | (128) | 0 | - |
| Dense_2 | (10) | 1,290 | Softmax |
Total Parameters: 288,298
Trainable Parameters: 288,298
- Extract spatial features from images using learnable filters
- Preserve spatial relationships between pixels
- Apply sliding window operation with learned kernels
- Max Pooling (2x2): Reduce spatial dimensions while retaining important features
- Downsample feature maps to reduce computational complexity
- Help achieve translation invariance
- ReLU (Rectified Linear Unit): Introduce non-linearity, prevent vanishing gradients
- Softmax: Convert final layer outputs to probability distribution
- Dropout (0.5): Randomly deactivate 50% of neurons during training to prevent overfitting
- Improves model generalization to unseen data
- Movement of filter across input image
- Stride = 1: Filter moves one pixel at a time
- Larger stride β smaller output dimensions
Formula for output size:
O = 1 + (N - F) / S
Where:
- O = Output size
- N = Input size
- F = Filter size
- S = Stride
- Add zeros around image borders to preserve spatial dimensions
- Zero Padding: Prevents information loss at image edges
- Allows filters to process border pixels
Formula with padding:
O = 1 + (N + 2P - F) / S
Where P = Padding size
- Python 3.8+
- TensorFlow 2.x / Keras - Deep learning framework
- NumPy - Numerical computations
- Matplotlib - Visualization
- scikit-learn - Model evaluation metrics
# Optimizer
optimizer = Adam(learning_rate=0.001)
# Loss Function
loss = sparse_categorical_crossentropy
# Training Configuration
- Epochs: 25
- Batch Size: 32
- Validation Split: 20%
- Early Stopping: Enabled| Metric | Training Set | Test Set |
|---|---|---|
| Accuracy | 82.93% | 67.57% |
| Loss | 0.4724 | 0.9624 |
| Kappa Score | - | 0.6047 |
The model shows consistent improvement over epochs with some overfitting in later stages:
- Epoch 1: Train Acc: 31.8% | Val Acc: 48.9%
- Epoch 10: Train Acc: 71.2% | Val Acc: 64.3%
- Epoch 20: Train Acc: 80.8% | Val Acc: 66.8%
- Epoch 25: Train Acc: 82.9% | Val Acc: 67.6%
Best performing classes:
- Ship: ~77% accuracy
- Automobile: ~75% accuracy
- Truck: ~73% accuracy
Challenging classes:
- Cat: ~52% accuracy
- Dog: ~55% accuracy
- Bird: ~58% accuracy
Note: Animal classes (cat, dog, bird, deer) show lower accuracy due to higher intra-class variation and similarity between classes.
pip install tensorflow numpy matplotlib scikit-learn- Clone the repository:
git clone https://github.com/yourusername/CNN-Image-Classification-CIFAR10.git
cd CNN-Image-Classification-CIFAR10- Install dependencies:
pip install -r requirements.txt# Load and run the R Markdown file
# Or execute the Python equivalent:
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
# Load data
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
# Normalize pixel values
X_train = X_train / 255.0
X_test = X_test / 255.0
# Build model
model = Sequential([
Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
Conv2D(32, (3,3), activation='relu'),
MaxPooling2D(2,2),
Conv2D(64, (3,3), activation='relu'),
Conv2D(64, (3,3), activation='relu'),
MaxPooling2D(2,2),
Conv2D(128, (3,3), activation='relu'),
Flatten(),
Dense(128, activation='relu'),
Dropout(0.5),
Dense(10, activation='softmax')
])
# Compile
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train
history = model.fit(X_train, y_train,
epochs=25,
batch_size=32,
validation_split=0.2)# Test set evaluation
test_loss, test_accuracy = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {test_accuracy*100:.2f}%")
# Generate predictions
predictions = model.predict(X_test)
predicted_classes = predictions.argmax(axis=1)CNN-Image-Classification-CIFAR10/
βββ README.md # This file
βββ Project_2.rmd # R Markdown implementation
βββ stat_852_final_project.pdf # Project report
βββ requirements.txt # Python dependencies
βββ src/
β βββ model.py # Model architecture
β βββ train.py # Training script
β βββ evaluate.py # Evaluation utilities
β βββ visualize.py # Visualization functions
βββ notebooks/
β βββ CNN_CIFAR10_Analysis.ipynb # Jupyter notebook
βββ results/
β βββ confusion_matrix.png # Confusion matrix
β βββ training_history.png # Loss/accuracy plots
β βββ model_architecture.png # Architecture diagram
βββ models/
βββ cnn_cifar10_final.h5 # Trained model weights
This project demonstrates:
β
Deep Learning Fundamentals: Understanding of CNN architecture and components
β
Computer Vision: Image preprocessing, normalization, and feature extraction
β
Model Optimization: Hyperparameter tuning (stride, padding, filters, dropout)
β
Evaluation: Confusion matrix analysis, per-class metrics, overfitting detection
β
Implementation Skills: TensorFlow/Keras, Python, R
β
Documentation: Clear code comments, professional reporting
Traditional ANNs struggle with image data because:
- Fully connected layers require massive parameters (32x32x3 image = 3,072 inputs per neuron)
- Loss of spatial information: ANNs flatten images, losing 2D structure
- No translation invariance: ANNs can't recognize objects in different positions
CNNs solve these problems through:
- Local connectivity: Filters connect to small image regions
- Weight sharing: Same filter applied across entire image (fewer parameters)
- Spatial hierarchy: Early layers detect edges β middle layers detect shapes β final layers detect objects
- Translation invariance: Pooling layers make detection position-independent
-
Albawi, S., Mohammed, T. A., and Al-Zawi, S. (2017). Understanding of a convolutional neural network. International Conference on Engineering and Technology (ICET), pages 1-6.
-
Hastie, T., Tibshirani, R., Friedman, J. H., and Friedman, J. H. (2009). The elements of statistical learning: data mining, inference, and prediction. Springer.
- Course: STAT 852 - Machine Learning
- Institution: Simon Fraser University
- Grade: 93.4/100
- Feedback Highlights:
- "Pretty good job of covering the important elements and explaining them reasonably well"
- "Above average difficulty topic"
- Presentation score: 94.5/100
Diksha Jethnani
π§ d.jethnani1999@gmail.com
πΌ LinkedIn | π± GitHub
This project is part of academic coursework at Simon Fraser University. The code and analysis are available for educational purposes.
- Professor: Dr. Tom Loughin
- Dataset: CIFAR-10 (Canadian Institute for Advanced Research)
- Framework: TensorFlow/Keras Development Team
- Institution: Simon Fraser University, Department of Statistics
Last Updated: February 2026