Skip to content

gperdrizet/CIFAR10

Repository files navigation

CIFAR-10 Image Classification Tutorial

PyPI package Documentation build

Python PyTorch Optuna License

This repository demonstrates deep learning techniques for image classification using the CIFAR-10 benchmark dataset. Through a series of progressively more sophisticated models, it shows how different neural network architectures and training techniques improve classification performance.

The tools developed for this project are available via PyPI for use in your own projects.

About the dataset

CIFAR-10 consists of 60,000 32×32 color images across 10 classes:

  • Training set: 50,000 images (40,000 for training, 10,000 for validation)
  • Test set: 10,000 images
  • Classes: airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck

CIFAR-10 Examples

The models

  1. 01-DNN.ipynb - Baseline fully-connected deep neural network on grayscale images
  2. 02-CNN.ipynb - Convolutional neural network to exploit spatial relationships
  3. 03-RGB-CNN.ipynb - CNN modified to process full RGB color information
  4. 04-architecture_optimization.ipynb - Optuna optimization of CNN architecture (conv blocks, filters, dropout)
  5. 05-training-optimization.ipynb - Optuna optimization of training hyperparameters (optimizer, learning rate, weight decay)
  6. 06-augmented-CNN.ipynb - Optimized architecture + training with data augmentation
  7. 07-resnet50.ipynb - Transfer learning with pre-trained ResNet50
  8. 08-results.ipynb - Model comparison and performance analysis

Results

Results

Getting started

Prerequisites

Setup

  1. Clone this repository:
git clone https://github.com/gperdrizet/CIFAR10.git
  1. Open the repository directory in VS Code

  2. When prompted, choose your configuration:

    • DeepLearning GPU: For Linux/WSL2 with NVIDIA GPU (recommended for training)
    • DeepLearning CPU: For Mac or machines without GPU (suitable for code development and testing)
  3. Click "Reopen in Container" or use the command palette (Ctrl+Shift+P / Cmd+Shift+P) and select "Dev Containers: Reopen in Container"

  4. Wait for the container to build and initialize (first time only). The devcontainer will:

    • Set up the Python environment
    • Install all dependencies (GPU or CPU version of PyTorch)
    • Download the CIFAR-10 dataset

Note: The CPU configuration works on any machine but training will be significantly slower (10-50x). It's perfect for following along with the material without a GPU. See .devcontainer/README.md for more details.

Running the notebooks

Once the container environment loads, you can start running the notebooks!

Training control: Each notebook has parameters to control whether to train or load existing models:

  • retrain_model = True (default): Train a new model
  • retrain_model = False: Load an existing model (from local disk or Hugging Face Hub)
  • model_source = 'local' or 'huggingface': Choose where to load from

Note: Run notebooks in order - some depend on output generated by earlier notebooks.

Optional: Upload models to Hugging Face Hub

Each notebook includes optional Hugging Face Hub integration to share your trained models:

  1. Copy .env.template to .env and add your repository details
  2. Create a Hugging Face account at huggingface.co
  3. Run huggingface-cli login in the terminal and enter your access token
  4. Set upload_to_hf = True in the upload cells

Note: Students can download pre-trained models from the default repository (gperdrizet/CIFAR10) without any setup by setting retrain_model=False and model_source='huggingface'.

See HUGGINGFACE.md for detailed setup instructions.

Environment

This project uses Dev Containers with two configurations to support both GPU and CPU-only machines:

GPU Configuration (recommended for training):

  • Based on deeplearning-GPU template
  • Pre-installed PyTorch with CUDA 12.6 support
  • Works with any NVIDIA GPU from GTX 1050 to RTX 5090 (Pascal architecture and newer)
  • Requires: Linux/WSL2, NVIDIA drivers ≥545, Docker with GPU support

CPU Configuration (for Mac and non-GPU machines):

  • Based on Microsoft's Python 3.10 devcontainer
  • PyTorch CPU-only (auto-installed)
  • Works on any machine (Mac, Windows, Linux)
  • Good for code development, testing, and learning without GPU

Why use this setup?

  • Zero configuration: CUDA, cuDNN, and ML frameworks are pre-installed
  • Cross-platform: Identical environment across different machines
  • Reproducible: Everyone gets the exact same dependencies
  • Flexible: Choose GPU or CPU based on your hardware

The code automatically detects your hardware, so both configurations work seamlessly. Training on CPU will be slower but still functional.

Resource Link
Dev Container Guide .devcontainer/README.md
GPU Template Repository gperdrizet/deeplearning-GPU
Docker Image gperdrizet/deeplearning-gpu

Dependencies

Core requirements:

  • python ≥3.10, <3.13
  • torch ≥2.0
  • torchvision ≥0.15
  • numpy ≥1.24, <2.0
  • matplotlib ≥3.7
  • optuna ≥3.0
  • scikit-learn ≥1.3

Citation

If you find this project helpful for your learning or work, feel free to reference it:

@software{cifar10_tutorial,
  author = {Perdrizet, George},
  title = {CIFAR-10 Image Classification Tutorial},
  year = {2024},
  url = {https://github.com/gperdrizet/CIFAR10}
}

License

This project is licensed under the GPLv3 License - see the LICENSE file for details.

About

Demonstration of the CIFAR-10 deeplearning task with PyTorch and TensorFlow

Topics

Resources

License

Stars

Watchers

Forks

Contributors