File size: 7,285 Bytes
f10ca14 26d0865 f10ca14 26d0865 f10ca14 26d0865 f10ca14 26d0865 f10ca14 26d0865 f10ca14 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 7d8a5fa bbfaafd 7400dcb bbfaafd 7400dcb bbfaafd 26d0865 bbfaafd 7400dcb 26d0865 7400dcb 0b09704 85e9513 26d0865 8ce9f48 7400dcb 0b09704 7400dcb 26d0865 7400dcb 26d0865 0b09704 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 7400dcb 26d0865 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | ---
license: apache-2.0
datasets:
- tanganke/stanford_cars
language:
- en
metrics:
- accuracy
base_model:
- timm/efficientnetv2_rw_s.ra2_in1k
pipeline_tag: image-classification
---
# π EfficientNetV2 Car Classifier: Fine-Grained Vehicle Recognition
> **EfficientNetV2 Car Classifier** delivers robust, fine-grained recognition for 196 car makes and models, powered by EfficientNetV2, state-of-the-art augmentations, rigorous metric tracking, and full visual explainability with Grad-CAM.
> Developed by kikogazda, 2025.
---
## π Project Structure
<pre>
Efficient_NetV2_Edition/
βββ efficientnetv2_best_model.pth # Best model weights
βββ Last_model.ipynb # Full training & evaluation pipeline
βββ class_mapping.json # Class index to name mapping
βββ *.csv # Logs, splits, labels, and metrics
βββ *.png # Visualizations and Grad-CAM outputs
βββ README.md # Model card (this file)
βββ ... # Additional scripts, reports, and assets
</pre>
---
## π¦ Table of Contents
- [Overview](#overview)
- [Dataset & Preprocessing](#dataset--preprocessing)
- [Model Architecture](#model-architecture)
- [Training Pipeline](#training-pipeline)
- [Explainability (Grad-CAM)](#explainability-grad-cam)
- [Visualizations](#visualizations)
- [Metrics & Results](#metrics--results)
- [Hugging Face & Demo](#hugging-face--demo)
- [Download Resources](#download-resources)
- [Usage & Inference](#usage--inference)
---
## Overview
**EfficientNetV2 Car Classifier** tackles the real-world challenge of distinguishing between 196 car makes and models, even when differences are nearly imperceptible.
**Highlights:**
- Modern EfficientNetV2 backbone with transfer learning
- Aggressive, real-world augmentation pipeline
- Class balancing for rare makes/models
- Extensive, scriptable metric tracking and reporting
- End-to-end explainability with Grad-CAM
- Fully reproducible, robust, and deployment-ready
---
## Dataset & Preprocessing
- **Dataset:** [Stanford Cars 196](https://huggingface.co/datasets/tanganke/stanford_cars)
- 196 classes, 16,185 images (official train/test split)
- Detailed make/model/year for each image
- **Preprocessing:**
- Annotation CSV export and class mapping JSON
- Stratified train/val/test split (maintains class distribution)
- Outlier cleaning and normalization
- Augmentations: random resized crop, flip, rotate, color jitter, blur
- ImageNet mean/std normalization
---
## Model Architecture
- **Backbone:** EfficientNetV2 (pretrained)
- All but the last blocks frozen initially
- Custom classifier head for 196 classes (Linear β ReLU β Dropout β Linear)
- **Optimization:**
- Adam optimizer
- Cross-Entropy loss (with label smoothing)
- Learning rate scheduling (ReduceLROnPlateau)
- Early stopping (macro F1 on validation)
- WeightedRandomSampler for class balance
**Flow:**
Input β [Augmentations] β EfficientNetV2 Backbone β Custom Head β Softmax (196 classes)
---
## Training Pipeline
- **Epochs:** Up to 25 (early stopping enabled)
- **Batch Size:** 32 (weighted sampling)
- **Validation:** Macro/micro metrics, confusion matrix, Top-3/Top-5 accuracy
- **Logging:** All metrics and losses to CSV, plus high-res visual plots:
- Accuracy/F1 per epoch
- Precision/Recall (macro, weighted)
- Loss curve
- Top-3/Top-5 accuracy
- **Artifacts:** All reports, CSVs, and visuals in repo for transparency
---
## Explainability (Grad-CAM)
Grad-CAM overlays highlight image regions most responsible for model predictionsβletting you "see" what the network is using for its decisions.
- *Why?* Trust, transparency, debugging.
- *How?* For every prediction, a heatmap overlay shows most influential pixels.

*Heatmaps visualize key decision regions for each sample.*
---
## Visualizations
Here are key visualizations from the training and evaluation process, including loss curves, accuracy plots, and Grad-CAM++ overlays that illustrate what the model focuses on.
### π― Accuracy & F1 Score per Epoch
Visualizing training and validation accuracy alongside macro F1 score.

---
### π Training vs Validation Loss
Clear comparison of model learning over time.

---
### π Precision & Recall Trends
Macro and weighted precision/recall for detailed class-wise performance.

---
### π Top-3 and Top-5 Accuracy Over Epochs
Measuring how often the correct class is within the top predictions.

---
### π Top-20 Most Accurate Classes
Sorted bar plot of classes the model predicts with the highest accuracy.

---
### π§© Confusion Matrix
High-resolution heatmap showing misclassifications and accuracy by class.

---
## π Metrics & Results
| Metric | Value |
|------------------------|---------|
| train_loss | 0.97 |
| train_acc | 0.997 |
| val_loss | 1.40 |
| val_acc | 0.87 |
| val_precision_macro | 0.89 |
| val_precision_weighted | 0.89 |
| val_recall_macro | 0.87 |
| val_recall_weighted | 0.87 |
| val_f1_macro | 0.87 |
| val_f1_weighted | 0.88 |
| val_top3 | 0.95 |
| val_top5 | 0.97 |
---
## Hugging Face Demo
**Live Gradio Demo:**
[Click here to launch the demo](https://kikogazda-efficient-netv2.hf.space/)
---
## Download Resources
- **Stanford Cars 196 Dataset:**
[Direct download from Stanford](https://huggingface.co/datasets/tanganke/stanford_cars)
- **Trained Model Weights:**
[Download from Hugging Face (efficientnetv2_best_model.pth)](https://huggingface.co/kikogazda/Efficient_NetV2_Edition/resolve/main/efficientnetv2_best_model.pth)
- **Class mapping/metadata:**
Included as `class_mapping.json` in this repo
---
## Usage & Inference
### 1. Install dependencies
```bash
pip install -r requirements.txt
pip install torch torchvision pytorch-grad-cam gradio
import torch
from torchvision import transforms
from PIL import Image
import json
from efficientnet_pytorch import EfficientNet
# Load model
model = EfficientNet.from_pretrained('efficientnet-b2', num_classes=196)
model.load_state_dict(torch.load("efficientnetv2_best_model.pth", map_location="cpu"))
model.eval()
# Preprocess
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
img = Image.open("your_image.jpg").convert("RGB")
input_tensor = transform(img).unsqueeze(0)
# Predict
with torch.no_grad():
output = model(input_tensor)
pred = output.argmax(1).item()
# Class name
with open("class_mapping.json") as f:
class_map = json.load(f)
print("Predicted class:", class_map[str(pred)]) |