What is Focal Loss, and how does it solve extreme background class imbalance?
Focal Loss is a modified cross-entropy that fixes the extreme foreground-background imbalance in one-stage detectors, where the vast majority of the tens of thousands of candidate boxes are easy background. It multiplies the loss by a factor that shrinks as a prediction gets more confident and correct, so easy negatives contribute almost nothing and the model spends its effort on the rare, hard objects.
A one-stage detector evaluates a huge number of locations per image, and almost all of them are background. If every one contributes equally to the loss, the easy 'obviously background' cases drown out the handful of real objects, and training stalls. Focal Loss (from RetinaNet) was designed for exactly this.
The idea in one line
Take standard cross-entropy and multiply it by (1 - p_t) raised to a power gamma, where p_t is the model's confidence in the correct class. When the model is already right and confident (p_t near 1), that factor is tiny, so the example is effectively ignored. When it's wrong (p_t near 0), the factor is near 1 and the example keeps its full weight.
- gamma = 0 reduces to ordinary cross-entropy.
- gamma around 2 is the common default; higher values focus even harder on difficult cases.
- An alpha term can additionally weight foreground vs background.
The payoff is that a one-stage detector can train to two-stage-level accuracy without needing a region-proposal step to filter out the easy background first.
Related questions
Stuck on this in a real project?
Book a free call and I’ll give you a straight answer on your specific case.
Book a consultation