Skip to content

falakshair01/cifar10-cnn-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CIFAR-10 CNN Image Classification

A PyTorch implementation of a Convolutional Neural Network for CIFAR-10 image classification, designed for deep learning capability assessment.

πŸ“‹ Project Overview

This project implements a CNN model that achieves >70% accuracy on the CIFAR-10 dataset, demonstrating proficiency in:

  • Deep learning model architecture design
  • PyTorch framework usage
  • Training pipeline implementation
  • Best practices in ML engineering

πŸ—οΈ Directory Structure

cifar10-cnn-project/
β”‚
β”œβ”€β”€ cifar10_cnn.py          # Main implementation file
β”œβ”€β”€ plot_results.py         # Visualization script
β”œβ”€β”€ analyze_results.py      # Analysis script
β”œβ”€β”€ README.md               # This file
β”œβ”€β”€ requirements.txt        # Python dependencies
β”‚
β”œβ”€β”€ data/                   # Dataset directory (auto-created)
β”‚   └── cifar-10-batches-py/
β”‚       β”œβ”€β”€ data_batch_1
β”‚       β”œβ”€β”€ data_batch_2
β”‚       β”œβ”€β”€ data_batch_3
β”‚       β”œβ”€β”€ data_batch_4
β”‚       β”œβ”€β”€ data_batch_5
β”‚       β”œβ”€β”€ test_batch
β”‚       └── batches.meta
β”‚
└── results/                # Training results (auto-generated)
    β”œβ”€β”€ training_results.json      # Complete metrics in JSON format
    β”œβ”€β”€ training_summary.txt       # Formatted training report
    β”œβ”€β”€ training_history.csv       # CSV for plotting/analysis
    β”œβ”€β”€ model_architecture.txt     # Model structure details
    β”œβ”€β”€ best_model.pth            # Best model checkpoint
    β”œβ”€β”€ final_model.pth           # Final model checkpoint
    └── plots/                     # Visualizations (if plot_results.py run)
        β”œβ”€β”€ accuracy_plot.png
        β”œβ”€β”€ loss_plot.png
        β”œβ”€β”€ learning_rate_plot.png
        β”œβ”€β”€ epoch_time_plot.png
        └── training_dashboard.png

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • CUDA-capable GPU (optional, but recommended)

Installation

  1. Clone or download the project:
mkdir cifar10-cnn-project
cd cifar10-cnn-project
git clone <URL>
  1. Create and activate virtual environment (recommended):
# On Linux/Mac
python3 -m venv venv
source venv/bin/activate

# On Windows
python -m venv venv
venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

Running the Model

python cifar10_cnn.py

The script will:

  1. Automatically download CIFAR-10 dataset (first run only)
  2. Initialize the CNN model
  3. Train for 20 epochs
  4. Display progress for each epoch
  5. Save the best model to results/best_model.pth
  6. Generate comprehensive results in results/ directory
  7. Report final test accuracy

πŸ“Š Output Files

After training, the following files are automatically generated in the results/ directory:

1. training_results.json

Complete training metrics in JSON format:

{
  "metadata": {
    "start_time": "2024-01-15 14:30:00",
    "device": "cuda",
    "best_test_acc": 78.45,
    "total_training_time_minutes": 12.5
  },
  "training_history": {
    "epoch": [1, 2, 3, ...],
    "train_loss": [1.523, 1.124, ...],
    "train_acc": [44.21, 59.87, ...],
    "test_acc": [52.34, 63.21, ...]
  }
}

2. training_summary.txt

Human-readable formatted report with:

  • Training configuration
  • Final results and best accuracy
  • Epoch-by-epoch progress table
  • Target achievement status

3. training_history.csv

CSV file for easy plotting and analysis in Excel, Python, or R:

epoch,train_loss,train_acc,test_loss,test_acc,learning_rate,epoch_time
1,1.5234,44.21,1.2891,52.34,0.001,45.23
2,1.1245,59.87,1.0234,63.21,0.001,44.89

4. model_architecture.txt

Complete model structure and parameter count

5. best_model.pth & final_model.pth

Model checkpoints with state dictionaries

🎯 Model Architecture

CIFAR10CNN(
  Conv Block 1: [Conv2d(3β†’32) β†’ BatchNorm β†’ ReLU] Γ— 2 β†’ MaxPool
  Conv Block 2: [Conv2d(32β†’64) β†’ BatchNorm β†’ ReLU] Γ— 2 β†’ MaxPool
  Conv Block 3: [Conv2d(64β†’128) β†’ BatchNorm β†’ ReLU] Γ— 2 β†’ MaxPool
  FC Layers: Flatten β†’ Linear(2048β†’256) β†’ ReLU β†’ Dropout
            β†’ Linear(256β†’128) β†’ ReLU β†’ Dropout
            β†’ Linear(128β†’10)
)

Total Parameters: ~1.2M

πŸ“Š Expected Performance

Metric Value
Training Time (GPU) ~10-15 minutes
Training Time (CPU) ~45-60 minutes
Final Test Accuracy 75-80%
Target Accuracy β‰₯70% βœ“

Training Progress Example

Epoch [1/20] | Train Loss: 1.5234 | Train Acc: 44.21% | Test Loss: 1.2891 | Test Acc: 52.34% | Time: 45.23s
Epoch [2/20] | Train Loss: 1.1245 | Train Acc: 59.87% | Test Loss: 1.0234 | Test Acc: 63.21% | Time: 44.89s
...
Epoch [20/20] | Train Loss: 0.3421 | Train Acc: 88.12% | Test Loss: 0.6234 | Test Acc: 78.45% | Time: 45.01s

βœ“ SUCCESS: Final test accuracy β‰₯ 70%

πŸ”§ Configuration

Hyperparameters can be modified in the main() function:

num_epochs = 20        # Number of training epochs
batch_size = 128       # Batch size for training
learning_rate = 0.001  # Initial learning rate

πŸŽ“ Key Features

1. Data Augmentation

  • Random cropping with padding
  • Random horizontal flipping
  • Normalization using dataset statistics

2. Training Enhancements

  • Batch normalization for stable training
  • Dropout for regularization
  • Learning rate scheduling
  • Model checkpointing (saves best model)

3. Code Quality

  • Clean, modular architecture
  • Comprehensive documentation
  • Device-agnostic (CPU/GPU)
  • Reproducible results (fixed random seed)

πŸ“ˆ Monitoring Training

The script prints detailed metrics for each epoch:

  • Train Loss & Accuracy: Performance on training set
  • Test Loss & Accuracy: Performance on validation set
  • Time per Epoch: Training speed
  • Best Model: Automatically saved when test accuracy improves

πŸ› Troubleshooting

CUDA Out of Memory

# Reduce batch size in main()
batch_size = 64  # or 32

Slow Training

  • Use GPU if available
  • Reduce num_workers if CPU bottleneck
  • Decrease batch size

Accuracy Below 70%

  • Train for more epochs (e.g., 30-40)
  • Adjust learning rate
  • Increase model capacity

πŸ’Ύ Loading Saved Model

# Load best model
checkpoint = torch.load('results/best_model.pth')
model = CIFAR10CNN(num_classes=10)
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()

print(f"Loaded model from epoch {checkpoint['epoch']}")
print(f"Test accuracy: {checkpoint['test_acc']:.2f}%")

πŸ“Š Analyzing Results

Using Excel

Simply open results/training_history.csv in Excel and create charts!

🎯 Quick Performance Review

After training, check:

  1. training_summary.txt - Quick overview of results
  2. training_results.json - Detailed metrics for analysis
  3. training_history.csv - For plotting and visualization

🧩 Results

The CNN model achieved a best test accuracy of 83.78% on the CIFAR-10 dataset after 20 epochs of training.



Accuracy Over Epochs

Loss Over Epochs

Epoch Time (s)

Learning Rate Schedule

Overall Training Dashboard

πŸ“§ Author Information

Candidate for: Master's Research Position
Research Area: Artificial Intelligence
Task: Deep Learning Capability

πŸ“„ License

This project is created for academic evaluation purposes.

πŸ™ Acknowledgments

  • CIFAR-10 dataset: Alex Krizhevsky, Vinod Nair, and Geoffrey Hinton
  • PyTorch framework: Facebook AI Research

Note: This implementation follows best practices in deep learning and demonstrates proficiency in PyTorch, model architecture design, and training pipeline implementation.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages