CoolFace
Modelpublic

todorristov/car_classification_model

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes31downloads
Model Card

CAR CLASSIFICATION - Brand, Model & Model Year

This project is a deep learning pipeline that classifies car brand, model, and model year from a single image using a fine-tuned ConvNeXt model. It uses the Stanford Cars dataset and leverages transfer learning with facebook/convnext-large-224. Built in PyTorch, this modular and scalable pipeline supports training, evaluation, and inference.


๐Ÿ” Key Features

  • โ€”Download and preprocess image data from Hugging Face
  • โ€”Fine-tune pretrained ConvNeXt models (modern ConvNets inspired by transformers)
  • โ€”Track training metrics and model checkpoints
  • โ€”Predict the class of custom input images using saved models
  • โ€”Modular design for training, evaluation, and inference

๐Ÿงฐ Installation

๐Ÿ”ง Setup Instructions

  1. 1.Clone the repo from GitHub
bash
git clone https://github.com/Brainster-Data-Science-Academy/CarClassificationTeam1  

cd CarClassificationTeam1
  1. 1.Create and activate a virtual environment
bash
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. 1.Install dependencies
bash
pip install -r requirements.txt
  1. 1.Download the dataset
bash
python-m src.data.download.download.py

๐Ÿ“ Requirements

  • โ€”Python 3.8+
  • โ€”PyTorch 2.3.0+cu126
  • โ€”torchvision 0.18.0+cu126
  • โ€”torchaudio 2.3.0+cu126
  • โ€”transformers
  • โ€”datasets
  • โ€”Other dependencies as listed in requirements.txt

๐Ÿง  Model Architecture

[image]

We fine-tuned a pretrained ConvNeXt vision transformer model:

  • โ€”Model: ConvNeXt-Base (224x224 resolution)
  • โ€”Pretrained on: ImageNet-1k
  • โ€”Fine-tuned on: Stanford Cars (196 classes)
  • โ€”Transfer Learning: Only the last two ConvNeXt stages and the classification head were trained

Since the Stanford Cars dataset contains a relatively small number of training examples (~8,100 training and ~8,000 validation images), we adopted a transfer learning strategy. The ConvNeXt model was initialized with pretrained weights from ImageNet-1k, and only the final classification head was randomly initialized and fine-tuned for our 196 target classes.

To balance generalization and training efficiency, we unfroze and trained only the last two stages of the ConvNeXt backbone (Stages 3 and 4), along with the classification head. Earlier layers remained frozen to preserve robust pretrained features.

Data Augmentation:

python
transforms.Compose([
    transforms.RandomResizedCrop(image_size, scale=(0.8, 1.0), ratio=(0.75, 1.33)),
    transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(degrees=15),
    transforms.RandomGrayscale(p=0.1),
    transforms.ToTensor(),
    transforms.GaussianBlur(kernel_size=(5, 9), sigma=(0.1, 5)),
    transforms.RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3)),
    transforms.Normalize(mean=mean, std=std),
])

๐Ÿ“Š Performance

  • โ€”Train Accuracy: 98.62%
  • โ€”Validation Accuracy: 92.30%
  • โ€”Train Loss (Cross Entrophy): 0.9010
  • โ€”Validation Loss (Cross Entrophy): 1.1231

๐Ÿš€ Usage (Example)

python
from PIL import Image
from transformers import AutoImageProcessor, ConvNextForImageClassification
import torch

# Load model and processor
model = ConvNextForImageClassification.from_pretrained("todorristov/car_classification_model")
processor = AutoImageProcessor.from_pretrained("todorristov/car_classification_model")

# Load and preprocess image
image = Image.open("example.jpg").convert("RGB")
inputs = processor(images=image, return_tensors="pt")

# Predict
with torch.no_grad():
    logits = model(**inputs).logits
    predicted_class = logits.argmax(-1).item()

print(f"Predicted class ID: {predicted_class}")

๐Ÿ‹๏ธ Training Details

  • โ€”Framework: PyTorch
  • โ€”Hardware: NVIDIA RTX 4060
  • โ€”Epochs: 32 (early stopped training after 28 epochs)
  • โ€”Batch Size: 32
  • โ€”Optimizer: AdamW (lr=1e-4, weight_decay=1e-4)
  • โ€”Loss Function: Cross Entropy(label_smoothing=0.1)
  • โ€”Scheduler: ReduceLROnPlateau (factor=0.5, patience=2, min_lr=1e-6)

[image]

This result demonstrates the effectiveness of fine-tuning high-capacity pretrained models on medium-sized, domain-specific datasets. The model generalizes well despite visual similarities between different car models and years.


โš ๏ธ Limitations

  • โ€”Trained only on 196 classes from Stanford Cars (mostly 1990โ€“2012 U.S. models)
  • โ€”Poor performance on:
  • โ€”Damaged or modified vehicles
  • โ€”Non-standard angles or lighting
  • โ€”Not suitable for unseen/new car models โ€” retraining needed

๐Ÿ›  Project Details

  • โ€”Developed by: Todor Ristov, Goran Nikoloski, Milana Sokolova
  • โ€”For: TwinCar Project, Sols (Skopje, North Macedonia)
  • โ€”Language: Python
  • โ€”Framework: PyTorch
  • โ€”License: MIT

๐Ÿ”— Resources


๐Ÿค Contributing

Contributions are welcome! Please open an issue or submit a pull request. Make sure to update tests and documentation as needed.


๐Ÿ“‚ Project Structure

project_root/
โ”‚
โ”œโ”€โ”€ images/                     # Model architecture visualizations
โ”‚
โ”œโ”€โ”€ models/                     # Stores trained model checkpoints (e.g., best_model.pt)
โ”‚   โ””โ”€โ”€ best_model.pt
โ”‚
โ”œโ”€โ”€ notebooks/                  # Jupyter notebooks for model exploration and experiments
โ”‚
โ”œโ”€โ”€ reports/                    # Training logs (loss, accuracy, LR, time, etc.)
โ”‚
โ”œโ”€โ”€ src/                        # Source code
โ”‚   โ”œโ”€โ”€ data/                   # Data-related scripts
โ”‚   โ”‚   โ”œโ”€โ”€ datadownloader.py   # Downloads and saves dataset to local folders
โ”‚   โ”‚   โ””โ”€โ”€ datatransforms.py   # Data augmentation and preprocessing transforms
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ models/                 # Model utilities
โ”‚   โ”‚   โ””โ”€โ”€ load_model.py       # Loads model, processor, and device
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ utils/                  # Utility scripts
โ”‚   โ”‚   โ””โ”€โ”€ save_label_map.py   # Saves class label map
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ evaluate.py             # Evaluation logic per epoch
โ”‚   โ”œโ”€โ”€ inference.py            # Inference script for classifying new images
โ”‚   โ”œโ”€โ”€ train_utils.py          # Training helper functions (e.g., metric calc, logging)
โ”‚   โ”œโ”€โ”€ train.py                # Main training script
โ”‚   โ””โ”€โ”€ visualize.py            # Visualizations (e.g., confusion matrix, sample predictions)
โ”‚
โ”œโ”€โ”€ README.md                   # Project documentation
โ””โ”€โ”€ requirements.txt            # Project dependencies

๐Ÿ’ฌ Citation

@misc{twin-car-classification,
  title={Car Classification - Brand, Model & Model Year},
  author={Todor Ristov},
  year={2025},
  howpublished={\url{https://huggingface.co/todorristov/car_classification_model}},
  note={A deep learning pipeline for vehicle recognition.}
}

Feel free to โญ the repo and share your feedback!