A deep learning model for classifying German traffic signs using VGG16 Transfer Learning and Fine-Tuning, achieving 98% accuracy on the test set.
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:
- Transfer Learning: Train custom classification head with frozen VGG16 base
- Fine-Tuning: Unfreeze last 4 VGG16 layers for task-specific optimization
- Training Accuracy: 99.99%
- Validation Accuracy: 97.98%
- Test Accuracy: ~98%
- Training Time: ~6 hours on NVIDIA A100 GPU
- Model Size: ~115 MB
β
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
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
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
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
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
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
git clone https://github.com/rm4318/traffic-sign-classifier.git
cd traffic-sign-classifierpip install -r requirements.txtpython app.pyThe app will launch at http://localhost:7860
Open Traffic_Sign_Classifier.ipynb in Google Colab:
- Upload notebook to Colab
- Enable GPU: Runtime β Change runtime type β GPU (T4 or A100)
- Run all cells
- Training takes ~6 hours on A100 GPU
- Download the trained model:
vgg16_finetuned_best.keras
| Metric | Value |
|---|---|
| Best Val Accuracy | 84.35% |
| Final Val Accuracy | 84.99% |
| Training Time | ~3 hours |
| Epochs Completed | 20 |
| Metric | Value |
|---|---|
| Best Val Accuracy | 97.98% |
| Training Accuracy | 99.99% |
| Training Time | ~3.5 hours |
| Epochs Completed | 30 |
| Metric | Value |
|---|---|
| Test Accuracy | 97.98% |
| Test Precision | 97.95% |
| Test Recall | 97.98% |
| Test F1-Score | 97.96% |
- 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
- 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
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
)- 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
- Computed class weights using
sklearn.utils.class_weight - Applied during training to give more importance to underrepresented classes
- Improved performance on rare sign types
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:
- Create account on huggingface.co
- Create New Space β Select Gradio SDK
- Upload files:
app.pyrequirements.txtvgg16_finetuned_best.keras
- Your app will be live in 2-3 minutes!
# Install dependencies
pip install gradio tensorflow opencv-python-headless pillow
# Run app
python app.pyFROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]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
- Training: GPU recommended (A100, V100, or T4)
- Inference: CPU sufficient (predictions in 1-2 seconds)
- 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
β
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)
- 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


