Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🚦 Traffic Sign Classifier

Python TensorFlow Accuracy License

A deep learning model for classifying German traffic signs using VGG16 Transfer Learning and Fine-Tuning, achieving 98% accuracy on the test set.


πŸ“Š Project Overview

This project implements a Convolutional Neural Network (CNN) that classifies 43 different types of German traffic signs from the German Traffic Signs Recognition Benchmark (GTSRB) dataset. The model uses VGG16 pre-trained on ImageNet with a two-phase training approach:

  1. Transfer Learning: Train custom classification head with frozen VGG16 base
  2. Fine-Tuning: Unfreeze last 4 VGG16 layers for task-specific optimization

Key Results

  • Training Accuracy: 99.99%
  • Validation Accuracy: 97.98%
  • Test Accuracy: ~98%
  • Training Time: ~6 hours on NVIDIA A100 GPU
  • Model Size: ~115 MB

🎯 Features

βœ… VGG16 Transfer Learning - Leverages ImageNet pre-trained weights
βœ… Fine-Tuning - Unfreezes deeper layers for domain-specific learning
βœ… Data Augmentation - Rotation, zoom, shift, brightness adjustments
βœ… Class Weights - Handles imbalanced dataset
βœ… Learning Rate Scheduling - Automatic reduction on plateau
βœ… Interactive Web App - Gradio interface deployed on Hugging Face
βœ… Comprehensive Evaluation - Confusion matrix, classification report, sample predictions


πŸ—οΈ Model Architecture

Phase 1: Transfer Learning

VGG16 Base (pre-trained on ImageNet, frozen)
    ↓
GlobalAveragePooling2D
    ↓
Dense(512, relu) + BatchNormalization + Dropout(0.5)
    ↓
Dense(256, relu) + BatchNormalization + Dropout(0.3)
    ↓
Dense(43, softmax)

Training Details:

  • Optimizer: Adam (LR = 0.001)
  • Epochs: 30 (with early stopping)
  • Batch Size: 64
  • Result: ~85% validation accuracy

Phase 2: Fine-Tuning

VGG16 Base (last 4 layers unfrozen)
    ↓
Same custom classification head

Training Details:

  • Optimizer: Adam (LR = 1e-5, 100x lower)
  • Epochs: 30 (with early stopping)
  • Batch Size: 32
  • Result: 97.98% validation accuracy

πŸ“ Project Structure

traffic-sign-classifier/
β”œβ”€β”€ Traffic_Sign_Classifier.ipynb   # Main training notebook
β”œβ”€β”€ app.py                          # Gradio web application
β”œβ”€β”€ requirements.txt                # Python dependencies
β”œβ”€β”€ README.md                       # This file
β”œβ”€β”€ examples/                       # Sample images
└── docs/                           # Documentation

πŸ“Š Dataset

German Traffic Signs Recognition Benchmark (GTSRB)

  • Training Set: 34,799 images
  • Validation Set: 4,410 images
  • Test Set: 12,630 images
  • Total Classes: 43
  • Image Size: 32Γ—32Γ—3 (RGB)
  • Input to Model: Resized to 224Γ—224Γ—3 for VGG16

Class Distribution

The dataset includes:

  • Speed Limits: 20, 30, 50, 60, 70, 80, 100, 120 km/h
  • Warning Signs: Dangerous curves, bumpy road, slippery road, road work, pedestrians, children crossing, etc.
  • Prohibitory Signs: No passing, no entry, stop, yield, no vehicles
  • Mandatory Signs: Turn right/left ahead, go straight, keep right/left, roundabout mandatory

πŸš€ Quick Start

1. Clone Repository

git clone https://github.com/rm4318/traffic-sign-classifier.git
cd traffic-sign-classifier

2. Install Dependencies

pip install -r requirements.txt

3. Run the Gradio App

python app.py

The app will launch at http://localhost:7860

4. Train Your Own Model

Open Traffic_Sign_Classifier.ipynb in Google Colab:

  1. Upload notebook to Colab
  2. Enable GPU: Runtime β†’ Change runtime type β†’ GPU (T4 or A100)
  3. Run all cells
  4. Training takes ~6 hours on A100 GPU
  5. Download the trained model: vgg16_finetuned_best.keras

πŸ“ˆ Training Results

Transfer Learning Phase

Metric Value
Best Val Accuracy 84.35%
Final Val Accuracy 84.99%
Training Time ~3 hours
Epochs Completed 20

Fine-Tuning Phase

Metric Value
Best Val Accuracy 97.98%
Training Accuracy 99.99%
Training Time ~3.5 hours
Epochs Completed 30

Test Set Performance

Metric Value
Test Accuracy 97.98%
Test Precision 97.95%
Test Recall 97.98%
Test F1-Score 97.96%

🧠 Key Techniques

1. Transfer Learning

  • Used VGG16 pre-trained on ImageNet (1.4M images, 1000 classes)
  • Leveraged low-level features (edges, textures) and mid-level features (shapes, patterns)
  • Froze all VGG16 layers to prevent catastrophic forgetting
  • Trained only custom classification head

2. Fine-Tuning

  • Unfroze last 4 layers of VGG16 (block5_conv1, block5_conv2, block5_conv3, block5_pool)
  • Used 100x lower learning rate (1e-5) for gentle updates
  • Allowed model to adapt ImageNet features to traffic sign domain

3. Data Augmentation

ImageDataGenerator(
    rotation_range=10,          # Β±10Β° rotation
    zoom_range=0.1,             # Β±10% zoom
    width_shift_range=0.1,      # Β±10% horizontal shift
    height_shift_range=0.1,     # Β±10% vertical shift
    shear_range=0.1,            # Β±10% shear
    brightness_range=[0.8, 1.2] # 80-120% brightness
)

4. Regularization

  • Dropout: 50%, 30% in classification head
  • Batch Normalization: After each Dense layer
  • Early Stopping: Patience of 15 epochs
  • Learning Rate Reduction: Factor of 0.5, patience of 5 epochs

5. Class Imbalance Handling

  • Computed class weights using sklearn.utils.class_weight
  • Applied during training to give more importance to underrepresented classes
  • Improved performance on rare sign types

🌐 Deployment

Hugging Face Spaces (Recommended)

The model is deployed on Hugging Face Spaces for free permanent hosting:

Live Demo: https://huggingface.co/spaces/rm4318/Traffic_Sign_Classifier

To deploy your own:

  1. Create account on huggingface.co
  2. Create New Space β†’ Select Gradio SDK
  3. Upload files:
    • app.py
    • requirements.txt
    • vgg16_finetuned_best.keras
  4. Your app will be live in 2-3 minutes!

Run Locally

# Install dependencies
pip install gradio tensorflow opencv-python-headless pillow

# Run app
python app.py

Docker (Optional)

FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

πŸ“Έ Screenshots

Web Application

Gradio App Interface

Training Results

Training History

Confusion Matrix

Confusion Matrix


πŸ”¬ Technical Details

Dependencies

tensorflow==2.15.0
gradio==4.12.0
numpy==1.24.3
opencv-python-headless==4.8.1.78
pillow==10.1.0
pandas==2.1.3
scikit-learn==1.3.2
matplotlib==3.8.2

Hardware Requirements

  • Training: GPU recommended (A100, V100, or T4)
  • Inference: CPU sufficient (predictions in 1-2 seconds)

Model Specifications

  • Total Parameters: 21,062,091
  • Trainable Parameters (Transfer): 3,539,243 (classification head only)
  • Trainable Parameters (Fine-tune): 12,848,619 (head + last 4 VGG16 layers)
  • Model Size: 115 MB
  • Input Shape: (224, 224, 3)
  • Output Shape: (43,) - probability distribution over 43 classes

πŸ“Š Performance Analysis

Strengths

βœ… High Accuracy: 98% on test set
βœ… Robust to Variations: Handles rotation, zoom, lighting changes
βœ… Fast Inference: ~1-2 seconds per image on CPU
βœ… Well-Generalized: Low overfitting (99.99% train, 97.98% val)

Limitations

⚠️ Dataset-Specific: Trained only on German traffic signs
⚠️ Low Resolution: Original images are 32Γ—32 (very small)
⚠️ Lighting Sensitive: May struggle with extreme lighting conditions
⚠️ Occlusion: Performance degrades with partially occluded signs

Future Improvements

  • Train on multi-country traffic sign datasets
  • Implement object detection for sign localization
  • Add real-time video stream processing
  • Deploy as mobile app using TensorFlow Lite
  • Ensemble with other architectures (ResNet, EfficientNet)

Built with ❀️ using TensorFlow, Keras, and Gradio

Last Updated: December 2024

About

A deep learning traffic sign classifier using VGG16 transfer learning and fine-tuning, achieving 98% accuracy on the German Traffic Signs Recognition Benchmark (GTSRB) dataset with 43 classes.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages