Skip to content

couder-04/VisionBench

Repository files navigation

🐱 Animal Classifier 🐶

Classical ML → CNNs → Vision Transformers

A comprehensive benchmarking study across six architectures — from Logistic Regression to Vision Transformers — on binary image classification.

Python PyTorch HuggingFace scikit-learn License: MIT


📌 Table of Contents


🔭 Overview

This project systematically evaluates six image classification architectures spanning three generations of computer vision, all benchmarked on the same Cat vs. Dog binary classification task.

The goal is to measure not just accuracy, but the full picture: training time, inference speed, parameter count, FLOPs, and generalization capability — providing a practical guide to choosing the right model for real-world constraints.

Traditional ML  ──────────────────────────►  Vision Transformers
   (seconds)       CNNs    Transfer Learning        (minutes)
Low accuracy  ──────────────────────────►  High accuracy
Few params    ──────────────────────────►  Millions of params

🚀 Models Evaluated

🧮 Traditional Machine Learning (from scratch)

Model Approach
Logistic Regression Hand-crafted pixel/feature vectors, gradient descent
Support Vector Machine (SVM) Kernel-based margin maximization on flattened inputs

🧠 Deep Learning

Model Approach
Custom CNN Lightweight convolutional network built from scratch
ResNet-34 Fine-tuned on pretrained ImageNet weights (skip connections)
EfficientNet-B0 Fine-tuned compound-scaled network (best efficiency/accuracy tradeoff)

🔮 Vision Transformers

Model Approach
ViT-Base/Patch16-224 Fine-tuned attention-based transformer on 16×16 image patches

📊 Dataset

Binary image classification dataset:

dataset/
├── cats/   🐱  (n images)
└── dogs/   🐶  (n images)

Train/validation split is performed programmatically inside each notebook.

Preprocessing pipeline:

Step Detail
Resize 224 × 224 px (ViT & CNNs)
Normalize ImageNet mean/std ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
Augmentation Random flip, crop, color jitter, rotation
Label encoding Cat → 0, Dog → 1

📈 Results & Benchmarks

Model Performance Comparison

Model Accuracy Precision Recall F1 Score Params Training Time
Logistic Regression 61% 0.61 0.60 0.61 12k 5min
SVM 61% 0.62 0.59 0.61 12k 10min
Custom CNN 72% 0.75 0.67 0.71 93K 20min
ResNet-34 (FT) 98% 0.98 0.98 0.98— 21.8M 30 min
EfficientNet-B0 (FT) 96% 0.97 0.95 0.96 5.3M 31 min
ViT-Base/16 (FT) 99%+ 0.99 0.99 0.99 86M 2hrs

📉 Training Curves & Confusion Matrices

Logistic Regression

Logistic Regression Results


Support Vector Machine (SVM)

SVM Results


Custom CNN

Custom CNN Results


ResNet-34 (Fine-Tuned)

ResNet-34 Results


EfficientNet-B0 (Fine-Tuned)

EfficientNet-B0 Results


ViT-Base/Patch16-224 (Fine-Tuned)

ViT-Base/Patch16-224 Results


🛠️ Key Techniques

  • Data Augmentation — Random crops, flips, and color jitter for regularization
  • Feature Extraction — Pixel flattening for classical ML; learned hierarchical features for CNNs
  • Transfer Learning — Leveraging ImageNet pretraining for rapid convergence
  • Fine-Tuning — Unfreezing top layers to adapt pretrained representations
  • Hyperparameter Tuning — Learning rate scheduling, weight decay, batch size sweeps
  • Performance Benchmarking — Accuracy, speed, complexity across all models
  • Custom Inference — Single-image prediction pipeline
  • Confusion Matrix Analysis — Per-class error identification
  • Precision / Recall / F1 — Balanced evaluation beyond raw accuracy

🏗️ Project Structure

Animal_Classifier/
│
├── 📓 dataset_analysis.ipynb       # Exploratory data analysis & class distribution
├── 📓 logistic_reg.ipynb           # Logistic Regression (from scratch)
├── 📓 svm.ipynb                    # SVM (from scratch)
├── 📓 small-cnn-final.ipynb        # Custom lightweight CNN
├── 📓 resnet-34-ft.ipynb           # ResNet-34 fine-tuning
├── 📓 efficient_net_B0_FT.ipynb    # EfficientNet-B0 fine-tuning
├── 📓 vit_base_patch_16_FT.ipynb   # ViT-Base/16 fine-tuning
│
├── 📄 requirements.txt             # Python dependencies
├── 🐱 cat.png                      # Sample inference image
└── 🐶 dog.jpeg                     # Sample inference image

⚡ Getting Started

1. Clone the repository

git clone https://github.com/couder-04/VisionBench.git
cd VisionBench

2. Set up environment

python -m venv venv
source venv/bin/activate        # On Windows: venv\Scripts\activate
pip install -r requirements.txt

3. Prepare dataset

Place your dataset in the following structure (or update the paths inside the notebooks):

dataset/
├── cats/
└── dogs/

The train/validation split is handled automatically inside each notebook.

4. Run notebooks in order

jupyter notebook dataset_analysis.ipynb   # Start here

Then proceed through each model notebook in any order.


🔄 Training Pipeline

┌─────────────────────────────────────────────────────────┐
│                   TRAINING PIPELINE                     │
│                                                         │
│  Dataset Analysis  ──►  Data Preprocessing             │
│                               │                         │
│                               ▼                         │
│                      Data Augmentation                  │
│                               │                         │
│                    ┌──────────┴──────────┐              │
│                    ▼                     ▼              │
│             Train from Scratch     Fine-Tune Pretrained │
│             (ML / Custom CNN)      (ResNet/EffNet/ViT)  │
│                    │                     │              │
│                    └──────────┬──────────┘              │
│                               ▼                         │
│                          Evaluation                     │
│                               │                         │
│                               ▼                         │
│                    Benchmark Comparison                 │
│                               │                         │
│                               ▼                         │
│                   Custom Image Inference                │
└─────────────────────────────────────────────────────────┘

📏 Evaluation Metrics

Every model is evaluated on a consistent set of metrics for fair comparison:

Metric Purpose
Accuracy Overall correct predictions
Precision True positives out of predicted positives
Recall True positives out of actual positives
F1 Score Harmonic mean of precision and recall
Confusion Matrix Per-class breakdown of errors
Training Time Wall-clock training duration
Inference Time Per-image prediction latency
Parameters (#) Model size in parameter count
FLOPs Computational cost per forward pass

🔍 Sample Inference

from PIL import Image
from torchvision import transforms
import torch

# Load your trained model
model.eval()

# Preprocessing pipeline
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406],
                         [0.229, 0.224, 0.225])
])

# Run inference
img = Image.open("dog.jpeg").convert("RGB")
tensor = transform(img).unsqueeze(0)          # Add batch dimension

with torch.no_grad():
    output = model(tensor)
    pred = torch.argmax(output, dim=1).item()

label_map = {0: "🐱 Cat", 1: "🐶 Dog"}
print(f"Prediction: {label_map[pred]}")
# Prediction: 🐶 Dog

⚙️ Technology Stack

Library Purpose
Python Core language
PyTorch Deep learning framework
Torchvision Pretrained models & transforms
HuggingFace ViT model & tokenizer
scikit-learn Classical ML & evaluation
NumPy Numerical computation
Matplotlib Plotting & visualization
Seaborn Statistical visualization

🎯 Future Enhancements

  • MobileNetV3 — Lightweight mobile-optimized benchmark
  • ConvNeXt — Modern pure-CNN architecture comparison
  • Swin Transformer — Hierarchical ViT fine-tuning
  • Automated Hyperparameter Optimization — Optuna / Ray Tune integration
  • Streamlit Web App — Interactive drag-and-drop inference demo
  • FastAPI Deployment — REST endpoint for production inference
  • Model Quantization — INT8 / FP16 optimization for edge deployment
  • Grad-CAM Visualizations — Saliency maps for model explainability
  • ONNX Export — Cross-framework model portability

Found this project useful? ⭐ Star the repository to support the work!

Built with curiosity — from pixels to patches.

About

Benchmarking Cat vs. Dog classification across 6 architectures — Logistic Regression, SVM, Custom CNN, ResNet-34, EfficientNet-B0, and ViT-Base. From pixel vectors to attention patches.

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors