← Blog/blog/focal-loss-hard-example-tax

The loss that silences easy examples—and trusts every hard one

An object detector does not inspect one neat crop. It lays boxes across positions, sizes, and shapes, then asks whether each box contains an object. The paper calls these candidate boxes anchors. A one-stage detector evaluates roughly 100,000 of them per image, while only a few overlap real objects.

That creates extreme class imbalance: background examples outnumber foreground objects by as much as 1,000 to one. Even when each easy background box has a tiny loss, their sum can drown out the examples that still teach the model something. Focal loss changes who gets heard without throwing examples away.

01

Make confidence a volume knob

Let pt be the probability assigned to the correct class. Ordinary cross entropy is minus log(pt). Focal loss multiplies it by (1 − pt) raised to gamma. Gamma controls the focus: zero recovers cross entropy; larger values mute confident examples faster. Alpha separately balances positive and negative classes.

def focal_loss(logit, target, gamma=2.0, alpha=0.25):
    probability = 1.0 / (1.0 + math.exp(-logit))
    probability_correct = probability if target == 1 else 1.0 - probability
    alpha_target = alpha if target == 1 else 1.0 - alpha
    cross_entropy = max(logit, 0.0) - logit * target + math.log1p(math.exp(-abs(logit)))
    return alpha_target * (1.0 - probability_correct) ** gamma * cross_entropy

The stable cross-entropy expression works from the raw score, or logit, rather than taking the logarithm of a rounded probability. The tested TypeScript core also implements the exact gradient; the three snippets are faithful translations of the loss.

02

Easy examples fade continuously

gamma 0 (CE)gamma 1gamma 2
Analytical loss per example, computed by the tested core. The points run from 50% to 99% probability on the correct class; alpha is omitted to isolate gamma's effect.

At 90% confidence, gamma 2 multiplies cross entropy by 0.1 squared: exactly a 100-fold discount. A wrong prediction is not erased. At 50% confidence its loss is only four times smaller, so the model spends a larger share of its update on uncertain and incorrect anchors.

03

A thousand whispers can beat ten useful signals

Seed-free illustrative calculation, not a paper result: 1,000 easy negatives at 99% correct versus 10 hard positives at 60% correct. Totals omit alpha balancing so only focal modulation changes.

Cross entropy assigns the easy crowd about 10.05 units of total loss, versus 5.11 for the ten harder examples. Focal loss flips the allocation: about 0.001 versus 0.817. It behaves like soft hard-example mining, but remains differentiable and uses every anchor.

04

The best focus was moderate, not maximal

The authors tuned alpha for each gamma while holding the detector and evaluation setup fixed. Performance rose through gamma 2, then fell at gamma 5. The mechanism was robust across a range, but “focus harder” was not a monotonic recipe.

Focusing gammaBest alphaCOCO minival AP
0 (cross entropy)0.7531.1
0.50.5032.9
10.2533.7
20.2534
50.2532.2
Paper-reported RetinaNet-50-600 ablation (Table 1b). Higher AP is better; alpha was optimized separately for each gamma.
05

What I would probe next

  • Inject controlled label noise and measure which examples dominate the gradient.
  • Plot performance separately for rare, small, and ambiguous objects.
  • Retune gamma when the foreground-background ratio changes.

Focal loss sits directly on top of binary classification. Poke at the probability and cross-entropy mechanics on the logistic-regression page, then follow the gradients through the neural-network page.

References

  1. Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár (2017). Focal Loss for Dense Object Detection. ICCV 2017