Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Stop rewarding the easy cases
In object detection, a model examines a huge number of candidate boxes, and almost all are obviously background. Under cross-entropy, each of those easy boxes still adds a little loss and gradient, and together they drown out the rare boxes that contain objects. Focal loss turns down the volume on examples the model already gets right: an example classified with probability 0.9 contributes 100 times less than under cross-entropy (with γ = 2), so attention shifts to the hard ones.
Detecting small objects (people, debris, other aircraft) in wide aerial images is a needle-in-a-haystack problem, and one-stage detectors such as RetinaNet and its descendants use focal loss for it. Knowing what it actually changes (gradient allocation and calibration) helps decide when it is worth using and how to set thresholds afterwards.
- 1. Focal loss down-weights…
- 2. With γ = 0 focal loss equals…
- 3. Focal loss was introduced for…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- p_t: the probability given to the true label: p_t = p if y = 1, 1 − p if y = 0. For p = 0.8: p_t = 0.8 if y = 1 and 0.2 if y = 0.
- Focal loss: FL = −(1 − p_t)^γ · log p_t.
- With γ = 2 and p_t = 0.5: 0.25 × log 2 ≈ 0.173, against cross-entropy’s 0.693.
- With p_t = 0.9 the modulating factor is (0.1)² = 0.01, so the loss is 1% of cross-entropy. With p_t = 0.1 it is 0.81.
- α-balancing: multiply by α for positives and 1 − α for negatives. RetinaNet used α = 0.25, which, perhaps surprisingly, down-weights positives; the paper found it worked best combined with γ = 2.
- Prior bias: initialise the final bias to b = log(π/(1 − π)), with π = 0.01, so every anchor starts at probability 0.01. That avoids a flood of loss from background at the start of training.
- 1. Modulating factor at p_t = 0.9, γ = 2?
- 2. RetinaNet's α for positives was…
- 3. The prior bias for π = 0.01 is about…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Implementing focal loss
Exercise 1.
- Focal loss with γ = 0 equals BCE, and with γ = 2 it is smaller on every example.
- The modulating factor is 0.01 for an easy example (p_t = 0.9) and 0.81 for a hard one (p_t = 0.1).
- The analytic logit gradient matches finite differences.
- α-balancing and the prior bias work as defined.
Practice.
- Compute focal loss from logits for stability; torchvision provides
sigmoid_focal_loss. - Normalise the summed loss by the number of positive anchors, not the number of anchors, as RetinaNet does.
- Focal loss changes the predicted probabilities: they are no longer calibrated estimates of P(y = 1). Choose thresholds on validation data, or recalibrate.
- Variants: the class-balanced focal loss; the quality focal loss (with soft IoU targets); the asymmetric loss for multi-label tasks.
torchvision.ops.sigmoid_focal_loss(logits, targets, alpha=0.25, gamma=2.0, reduction="sum") / num_pos- 1. RetinaNet normalises the summed focal loss by…
- 2. After training with focal loss, predicted probabilities are…
- 3. torchvision's focal loss works on…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
What focal loss changes. Its gradient with respect to the logit is the BCE gradient (p − y) multiplied by a factor that is small for easy examples. For a dense detector with 100 000 anchors, of which 100 are objects, the summed gradient from easy negatives under BCE can exceed that from the objects. Focal loss rebalances the gradient contribution without discarding examples, unlike hard-negative mining.
What it does not change.
- The ranking optimum. With enough capacity and training, BCE and focal loss can rank examples equally well.
- The rare class’s prior. It does not by itself raise recall at a threshold of 0.5.
Its benefits are mostly about optimisation in the extreme imbalance of dense detection, combined with the prior bias.
Calibration. Because focal loss’s minimiser is not p = η(x), its outputs are miscalibrated. Mukhoti et al. (2020) showed that focal loss can actually improve the calibration of overconfident networks, since it penalises confidence. Whether it helps or hurts calibration depends on whether the baseline is overconfident.
Relation to other ideas. Online hard example mining discards easy examples; focal loss smoothly down-weights them. Class weighting rebalances by label, focal loss by difficulty.
- 1. Focal loss rebalances by…
- 2. Hard example mining differs because it…
- 3. Focal loss can improve calibration when the baseline is…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Does focal loss help with imbalance?
History.
- K.-K. Sung and T. Poggio (1998) used bootstrapping on hard negatives.
- A. Shrivastava, A. Gupta and R. Girshick (2016) introduced online hard example mining.
- T.-Y. Lin, P. Goyal, R. Girshick, K. He and P. Dollár (2017) introduced focal loss and RetinaNet.
- J. Mukhoti et al. (2020) studied focal loss for calibration.
- X. Li et al. (2020) proposed generalised and quality focal losses.
Exercise 2. A dense-detection-like problem: 1% positives, 9% hard negatives near them and 90% easy negatives far away, with quadratic features. Logistic models are trained with Adam for 600 steps, the bias initialised to the 1% prior, on 10 000 points, and tested on 20 000. Each cell lists three seeds.
| loss | AP after 40 steps | final AP | mean predicted p (all test points) | recall at p > 0.5 |
|---|---|---|---|---|
| BCE | 0.260, 0.306, 0.310 | 0.244, 0.295, 0.312 | 0.009, 0.010, 0.010 | 0.02, 0.01, 0.07 |
| weighted BCE (99:1) | 0.165, 0.172, 0.187 | 0.236, 0.297, 0.289 | 0.085, 0.093, 0.090 | 0.95, 0.98, 0.99 |
| focal, γ = 2 | 0.229, 0.204, 0.234 | 0.240, 0.272, 0.300 | 0.039, 0.043, 0.046 | 0.01, 0.01, 0.06 |
| focal, γ = 2, α = 0.25 | 0.225, 0.211, 0.241 | 0.242, 0.281, 0.309 | 0.029, 0.032, 0.035 | 0.00, 0.00, 0.01 |
- Ranking was not improved. Final average precision was within about 0.03 across all four losses, with no consistent winner. Early in training BCE ranked best in every seed.
- The losses mainly moved the probabilities. BCE stayed calibrated (mean prediction 1%, the true rate). Weighted BCE inflated it ninefold, flagging 95–99% of positives at 0.5 but with many false alarms. Focal loss inflated it three- to fourfold.
- Why the published gains did not appear. Focal loss’s advantage is reported for deep dense detectors with around 100 000 anchors per image, where easy negatives dominate the gradient early in training. A linear model with a prior-initialised bias does not have that problem. These results show what focal loss changes; whether that helps a given detector must be tested on that detector.
Focal loss in AI, robotics and aerospace
- Aerial and satellite detection: small objects in large images give extreme foreground–background imbalance; RetinaNet-style detectors with focal loss are common baselines.
- Defect segmentation: focal or focal-Dice combinations train crack and corrosion segmenters where defects are a tiny fraction of pixels.
- Thresholding: because focal loss distorts probabilities, detection thresholds for alarm systems must be set on validation data, not taken as 0.5.
References
- T.-Y. Lin, P. Goyal, R. Girshick, K. He, P. Dollár (2017). “Focal loss for dense object detection.” Proc. ICCV.
- A. Shrivastava, A. Gupta, R. Girshick (2016). “Training region-based object detectors with online hard example mining.” Proc. CVPR.
- J. Mukhoti, V. Kulharia, A. Sanyal, S. Golodetz, P. Torr, P. Dokania (2020). “Calibrating deep neural networks using focal loss.” Advances in NeurIPS 33.
- X. Li et al. (2020). “Generalized focal loss.” Advances in NeurIPS 33.