Computer Vision · Deep Learning

ResNet-50 Image Classifier

A production-grade image classification system built with PyTorch, fine-tuned from ImageNet weights on a custom 12-class object dataset. Packaged as a Dockerized REST API with Grad-CAM visual explainability.

PyTorch ResNet-50 FastAPI Docker Grad-CAM Mixed Precision REST API
94.2%
Top-1 Accuracy
99.1%
Top-5 Accuracy
18ms
GPU Inference
120ms
CPU Inference

Project Overview

This project demonstrates an end-to-end deep learning pipeline for image classification. Starting from a pretrained ResNet-50 backbone, the model is fine-tuned on a 12-class custom dataset using modern training techniques including mixed-precision training (FP16), cosine annealing scheduling, and an augmentation-heavy data pipeline.

The final model is served through a FastAPI REST interface and containerized with Docker for reproducible deployment. Grad-CAM integration provides visual explanations of model decisions, making predictions interpretable for downstream users.

Architecture

1

Data Pipeline

Custom Dataset class with ImageFolder format. Augmentations: flip, color jitter, rotation, normalization.

2

Model Architecture

ResNet-50 pretrained on ImageNet. Custom classification head with dropout for 12-class output.

3

Training Loop

AdamW optimizer + CosineAnnealingLR. Mixed precision with torch.cuda.amp — 38% faster convergence.

4

Evaluation

Top-1 / Top-5 accuracy, confusion matrix, and Grad-CAM heatmaps for visual explainability.

5

REST API

FastAPI server with POST /predict endpoint. Returns class, confidence, and top-5 rankings.

6

Deployment

Fully Dockerized. Build once, run anywhere — on CPU or GPU with identical behaviour.

Results

MetricScore
Top-1 Accuracy94.2%
Top-5 Accuracy99.1%
Inference Latency (CPU)~120ms per image
Inference Latency (GPU)~18ms per image
Mixed Precision Speedup38% faster convergence

Training Details

Model

  • Base: torchvision.models.resnet50(pretrained=True)
  • Custom head: FC + Dropout
  • 12-class output

Optimisation

  • Optimiser: AdamW (wd=1e-4)
  • Scheduler: CosineAnnealingLR
  • Mixed Precision: FP16

Augmentations

  • RandomHorizontalFlip
  • ColorJitter
  • RandomRotation(15°)
  • Normalize (ImageNet stats)

Requirements

  • Python 3.9+
  • PyTorch 2.0+
  • CUDA 11.8+ (optional)

Quickstart

1. Install dependencies

bash
pip install -r requirements.txt

2. Prepare dataset (ImageFolder format)

file structure
data/
├── train/
│   ├── class_1/
│   └── class_2/
└── val/
    ├── class_1/
    └── class_2/

3. Train

bash
python src/train.py \
  --data_dir data/ \
  --epochs 30 \
  --batch_size 64 \
  --lr 1e-4 \
  --output_dir outputs/

4. Evaluate with Grad-CAM

bash
python src/evaluate.py \
  --checkpoint outputs/best_model.pth \
  --data_dir data/val/ \
  --gradcam

5. Run REST API

bash
uvicorn src.api:app --host 0.0.0.0 --port 8000
# POST /predict with form-data: file=<image>

Docker

bash
docker build -t resnet50-classifier .
docker run -p 8000:8000 resnet50-classifier

API Usage

python
import requests

with open("image.jpg", "rb") as f:
    response = requests.post(
        "http://localhost:8000/predict",
        files={"file": f}
    )

print(response.json())
# {
#   "predicted_class": "cat",
#   "confidence": 0.9731,
#   "top5": [["cat", 0.973], ["lynx", 0.018], ...]
# }

Project Structure

resnet50-image-classifier/
  ├── src/
    ├── model.py  # ResNet-50 architecture + custom head
    ├── train.py  # Training loop with mixed precision
    ├── inference.py  # Single image + batch inference
    ├── dataset.py  # Custom Dataset + augmentation pipeline
    ├── evaluate.py  # Metrics, confusion matrix, Grad-CAM
    └── api.py  # FastAPI REST inference server
  ├── tests/
    └── test_model.py
  ├── Dockerfile
  ├── requirements.txt
  └── README.md