Previous tutorialFaster R-CNN: Region Proposal NetworkNext tutorialYOLO Architecture: Backbone, Neck & Head
OD-05Intermediate 90 min
SSD, RetinaNet & Focal Loss
Study two one-stage detectors that differ in how they handle class imbalance: SSD with hard negative mining and RetinaNet with focal loss — and understand why focal loss is a more principled solution.
PyTorchtorchvisionMatplotlib
Code Structure Note
These notes are provided only to help students understand the code structure. Students should review, optimize, and adapt the code before using it in assignments, projects, or experiments.
Topics Covered
- 1SSD architecture: VGG16 backbone + 6 multi-scale feature layers, ~8 732 default boxes
- 2SSD matching: jaccard overlap ≥ 0.5, hard negative mining at 3:1 ratio
- 3Class-imbalance problem: 99%+ background anchors swamp classification loss
- 4Focal loss: modulating factor (1−pₜ)^γ down-weights easy negatives
- 5RetinaNet: FPN backbone + two-branch head (class + box) with focal loss
Learning Objectives
- Explain how SSD tiles default boxes across 6 feature map scales and count the total
- Derive the focal loss formula and explain the effect of γ on easy vs. hard example weighting
- Compare SSD and RetinaNet accuracy on small objects and explain the architectural reason
- Load torchvision RetinaNet and visualise per-level feature map predictions
Key Takeaways
- SSD300 produces ~8 732 boxes across 6 scales — the minority are foreground, creating extreme imbalance
- Hard negative mining is a sampling heuristic; focal loss is a loss reformulation that doesn't require sampling
- RetinaNet with ResNet-50-FPN achieves Faster R-CNN accuracy at single-stage speed
- γ = 2 in focal loss means a well-classified example (pₜ=0.9) contributes 100× less loss than γ = 0
Tutorial Infographic
Notebook Resources
Jupyter Notebook
Use this section to practice the tutorial code. Kaggle opens the prepared notebook, Colab lets you experiment in your own runtime, and the .ipynb download is best for local editing or submission.
Kaggle: follow the official tutorial notebook.
Colab: modify and rerun quickly.
IPYNB: keep a local copy.
Hands-On Exercises
- 1Load pretrained SSD300 and RetinaNet; run both on 10 identical images and compare AP@50
- 2Implement focal loss from scratch and verify it equals CE loss when γ = 0
- 3Plot the focal loss curve for γ ∈ {0, 0.5, 1, 2, 5} to visualise the down-weighting effect
- 4Visualise SSD predictions per feature layer and note which layer detects which object sizes
Tools & Libraries
PyTorchtorchvisionMatplotlib