Skip to content

Latest commit

Β 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Convolutional Neural Networks for Image Classification

STAT 852 Machine Learning Project | December 2023
Author: Diksha Jethnani

Python TensorFlow Keras


🎯 Project Overview

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.

Key Achievement

  • Test Accuracy: 67.57%
  • Training Accuracy: 82.93%
  • Model Complexity: ~420K parameters
  • Training Time: < 1 minute per epoch

πŸ“Š Dataset: CIFAR-10

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


πŸ—οΈ CNN Architecture

Model Structure

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)

Architecture Details

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


πŸ” Key CNN Concepts Implemented

1. Convolutional Layers

  • Extract spatial features from images using learnable filters
  • Preserve spatial relationships between pixels
  • Apply sliding window operation with learned kernels

2. Pooling Layers

  • Max Pooling (2x2): Reduce spatial dimensions while retaining important features
  • Downsample feature maps to reduce computational complexity
  • Help achieve translation invariance

3. Activation Functions

  • ReLU (Rectified Linear Unit): Introduce non-linearity, prevent vanishing gradients
  • Softmax: Convert final layer outputs to probability distribution

4. Regularization Techniques

  • Dropout (0.5): Randomly deactivate 50% of neurons during training to prevent overfitting
  • Improves model generalization to unseen data

5. Hyperparameters

Stride

  • 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

Padding

  • 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


πŸ’» Implementation

Technologies Used

  • Python 3.8+
  • TensorFlow 2.x / Keras - Deep learning framework
  • NumPy - Numerical computations
  • Matplotlib - Visualization
  • scikit-learn - Model evaluation metrics

Model Training

# 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

πŸ“ˆ Results

Performance Metrics

Metric Training Set Test Set
Accuracy 82.93% 67.57%
Loss 0.4724 0.9624
Kappa Score - 0.6047

Training History

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%

Per-Class Performance

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.


πŸš€ Getting Started

Prerequisites

pip install tensorflow numpy matplotlib scikit-learn

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/CNN-Image-Classification-CIFAR10.git
cd CNN-Image-Classification-CIFAR10
  1. Install dependencies:
pip install -r requirements.txt

Usage

Train the Model

# 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)

Evaluate the Model

# 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)

πŸ“ Repository Structure

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

πŸŽ“ Learning Outcomes

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


πŸ”¬ Key Insights

Why CNNs for Images?

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:

  1. Local connectivity: Filters connect to small image regions
  2. Weight sharing: Same filter applied across entire image (fewer parameters)
  3. Spatial hierarchy: Early layers detect edges β†’ middle layers detect shapes β†’ final layers detect objects
  4. Translation invariance: Pooling layers make detection position-independent

πŸ“š References

  • 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.


πŸ† Academic Recognition

  • 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

πŸ“§ Contact

Diksha Jethnani
πŸ“§ d.jethnani1999@gmail.com
πŸ’Ό LinkedIn | 🐱 GitHub


πŸ“ License

This project is part of academic coursework at Simon Fraser University. The code and analysis are available for educational purposes.


πŸ™ Acknowledgments

  • 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

About

Convolutional Neural Network for multi-class image classification using CIFAR-10 dataset | MSc Project

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages