CoolFace
Modelpublic

kikogazda/Efficient_NetV2_Edition

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
1likes
Model Card

πŸš— 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> EfficientNetV2Edition/ β”œβ”€β”€ efficientnetv2bestmodel.pth # Best model weights β”œβ”€β”€ Lastmodel.ipynb # Full training & evaluation pipeline β”œβ”€β”€ classmapping.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

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
  • β€”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.

[image] 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.

[image]


πŸ“‰ Training vs Validation Loss

Clear comparison of model learning over time.

[image]


πŸ“ˆ Precision & Recall Trends

Macro and weighted precision/recall for detailed class-wise performance.

[image]


πŸ“Š Top-3 and Top-5 Accuracy Over Epochs

Measuring how often the correct class is within the top predictions.

[image]


πŸ† Top-20 Most Accurate Classes

Sorted bar plot of classes the model predicts with the highest accuracy.

[image]


🧩 Confusion Matrix

High-resolution heatmap showing misclassifications and accuracy by class.

[image]


πŸ“ˆ Metrics & Results

MetricValue
train_loss0.97
train_acc0.997
val_loss1.40
val_acc0.87
valprecisionmacro0.89
valprecisionweighted0.89
valrecallmacro0.87
valrecallweighted0.87
valf1macro0.87
valf1weighted0.88
val_top30.95
val_top50.97

Hugging Face Demo

Live Gradio Demo: Click here to launch the demo


Download Resources


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)])