Course contentsShow
← Machine Learning and Deep Learning
Lesson 969 of 3,53822. Computer Vision TasksFree lesson

RetinaNet and Focal Loss

Addressing class imbalance in one-stage detectors with focal loss to down-weight easy examples.

RetinaNet and Focal Loss

What You'll Learn

You'll discover how RetinaNet solves the extreme class imbalance problem in one-stage detectors by making the model focus on hard examples while ignoring easy ones.

The Class Imbalance Crisis

One-stage detectors like YOLO and SSD evaluate thousands of potential object locations per image. The vast majority (often 99%+) are easy background examples—places where there's obviously no object. This creates a massive imbalance: your model drowns in easy negatives while struggling to learn from rare, hard positive examples.

It's like studying for an exam where 99 questions are trivially easy ("Is 2+2 equal to 5?") and only 1 is challenging. The easy questions dominate your attention, preventing you from learning what matters.

Focal Loss: The Solution

Focal Loss modifies the standard cross-entropy loss by adding a modulating factor that down-weights easy examples. Instead of treating all misclassifications equally, it focuses training on hard, misclassified examples:

  • Easy examples (confident correct predictions): almost zero loss contribution
  • Hard examples (uncertain or wrong predictions): full loss contribution

The formula adds a term (1 - p)^γ where p is the predicted probability and γ (gamma) is a focusing parameter (typically 2). When the model is confident and correct (p close to 1), this term approaches zero, effectively ignoring that example.

RetinaNet Architecture

RetinaNet combines:

  • A Feature Pyramid Network backbone for multi-scale features
  • Focal Loss to handle class imbalance
  • Dense anchor-based detection like SSD

This combination achieves two-stage detector accuracy with one-stage detector speed.

Key Takeaway: Focal Loss dynamically adjusts training focus by down-weighting easy examples and emphasizing hard cases, solving the class imbalance problem that plagued earlier one-stage detectors and enabling RetinaNet to match two-stage performance.