Paper commentary
Focal Loss for Dense Object Detection
Focal loss lets one-stage detectors match two-stage accuracy
- Problem One-stage detectors have trailed two-stage accuracy because easy background locations overwhelm training. Most of the roughly 100k locations per image are easy negatives.
- Method Focal loss multiplies cross entropy by a factor that shrinks as confidence in the correct class grows. So easy examples lose most of their loss while hard examples keep theirs.
- Results Focal loss scored 36.0 AP with ResNet-101-FPN, compared to 32.8 for the best OHEM variant.
Why dense one-stage detectors fall behind
Two-stage detectors first narrow the image to a small set of candidate locations, often 1-2k, and then classify only those. Sampling rules such as a fixed 1:3 foreground-to-background ratio or online hard example mining keep the remaining imbalance manageable. A one-stage detector has no such filter, so it must score a much larger grid of locations.
In practice that grid holds about 100k locations per image, and only a few of them contain objects. Most of those locations are easy negatives that give training little useful signal. Summed over so many examples, their small losses can overwhelm the rare class and lead to degenerate models. Sampling heuristics help, but training still stays dominated by easy background examples.
The authors identify this class imbalance as the main obstacle keeping one-stage detectors from matching two-stage accuracy. So the question becomes whether a change to the loss, rather than to the network design, can remove that obstacle.
Reshaping cross entropy with a modulating factor
The key idea is to keep cross entropy but multiply it by a factor that shrinks as the model grows confident in the correct class. The authors start from binary cross entropy, which takes the negative log of the probability the model gives the true class. They write p_t for that probability, so the loss reads as \( \mathrm{CE}(p_t) = -\log(p_t) \).
Alpha balances the two classes but treats easy and hard examples alike, and that is the gap the focal loss targets.
The focal loss multiplies cross entropy by a modulating factor (1 minus p_t) raised to gamma, with gamma at least 0, giving \( \mathrm{FL}(p_t) = -(1-p_t)^{\gamma}\log(p_t) \). When p_t is small the factor stays near 1, so a misclassified example keeps almost its full loss. As p_t approaches 1 the factor goes to 0, so confident examples lose most of their loss.
With gamma equal to 2, an example at p_t = 0.9 gets a loss 100 times lower than cross entropy gives, and at about 0.968 the reduction is 1000 times. Gamma is the single knob: at gamma 0 the loss is cross entropy, and larger values discount easy examples more.
The loss layer computes the sigmoid together with the loss, which gives better numerical stability. The exact form is not treated as crucial, and the appendix introduces an alternative, FL*, that behaves similarly. On RetinaNet-50-600, FL* with gamma 2 and beta 1 scored 33.8 AP, compared to 34.0 for focal loss at gamma 2. The authors read this as evidence that any loss with similar properties should work equally well.
RetinaNet's pyramid, anchors and subnets
RetinaNet places the focal loss on a single dense detector that scores anchors in one pass. A ResNet backbone feeds a Feature Pyramid Network, and two small subnetworks sit on every pyramid level. One subnetwork classifies anchors and the other regresses box offsets, and the two do not share parameters.
Each location gets nine anchors, made of three aspect ratios at three scales. An anchor is positive when its IoU with a ground-truth box meets the 0.5 threshold, background when its IoU is below 0.4, and ignored in between.
The classification subnet applies four 3x3 convolutions with 256 filters and ReLU, then a 3x3 convolution with K times A filters and sigmoid outputs. The box subnet has the same structure but ends in 4A linear outputs, one set of offsets per anchor. Its regressor is class-agnostic, which the authors found uses fewer parameters with equal effect.
The authors say these higher-level choices mattered more than the exact values of individual hyperparameters.
Training applies the focal loss to every anchor in the image, around 100k of them, instead of a sampled subset of 256. The summed loss is divided by the number of anchors assigned to ground-truth boxes, not by the total anchor count, because most anchors are easy negatives with negligible loss. The bias of the final classification layer is set so that each anchor starts at foreground probability pi = 0.01, using \( b = -\log((1-\pi)/\pi) \).
The training loss adds the focal loss to the smooth L1 box loss, and synchronized SGD runs over 8 GPUs with 16 images per minibatch. The default schedule runs 90k iterations from learning rate 0.01, dividing by 10 at 60k and again at 80k.
At inference the image passes through the network once. Each pyramid level keeps at most 1k top predictions above confidence 0.05, and non-maximum suppression at 0.5 produces the final detections.
Does focal loss beat cross entropy and OHEM?
Plain cross entropy with no changes to initialization made training diverge. Setting the prior foreground probability to 0.01 in the last layer made learning work, and ResNet-50 then got 30.2 AP. So initialization alone can rescue cross entropy, which makes the later gap to focal loss the more telling comparison.
The authors note that changing gamma matters more than the choice of alpha. Pushing gamma to 5 dropped AP to 32.2, so the gain has a range.
Online hard example mining keeps only the highest-loss examples, so it discards easy ones entirely. Focal loss scored 36.0 AP with ResNet-101-FPN, a 3.2-point gap that suggests down-weighting easy examples works better than discarding them. The 1:3 OHEM variant did worse, and its best row scored 31.1 AP.
For positives, the normalized loss curves look much alike across gamma values, so the focusing has little effect there. For negatives, a larger gamma concentrates the loss on a small fraction of hard samples. This is one converged model, so the result describes that model only.
Anchor density and the speed-accuracy trade-off
The authors varied anchors per location, from one square anchor up to 12 spanning scales and aspect ratios. A single square anchor already scored 30.3 AP with ResNet-50, while 3 scales and 3 aspect ratios raised it to 34.0 AP. Beyond about 6 to 9 anchors the authors saw no further gain, so coverage saturates.
Larger backbones and larger image scales raise accuracy and also slow inference. With ResNet-50-FPN, AP rose from 30.5 at a 400-pixel scale to 35.7 at 800 pixels, and time rose from 64 ms to 153 ms. RetinaNet-101-600 ran in 122 ms per image, compared to 172 ms for the ResNet-101-FPN Faster R-CNN, both measured on an Nvidia M40 GPU. The authors say this matches the Faster R-CNN accuracy at that point, while larger scales surpass it and remain faster.
Test-dev results against other detectors
Table 2 lists single-model test-dev results, and the comparisons use public numbers from other work. The ResNet-101-FPN RetinaNet, trained with scale jitter for 1.5× longer, scored 39.1 AP.
Compared to Faster R-CNN with Inception-ResNet-v2-TDM at 36.8 AP, the top two-stage row, RetinaNet is 2.3 points higher. Swapping in a ResNeXt-101-FPN backbone raised the score to 40.8 AP. The authors credit the loss rather than the network design, since the detector itself is deliberately simple.
Limits of the evidence
An early attempt with a hinge loss on pt was unstable and gave no meaningful results. The authors call the multi-class extension straightforward but keep the study to the binary loss. Ablations use an image scale of 600 pixels, while the headline test-dev numbers come from larger scales and longer training. Anchor gains saturate beyond 6 to 9 anchors, and no operating point is given for the high frame-rate regime, which the authors leave to future work.
The speed comparisons use public numbers from other papers, so they are not controlled experiments.
Paper info
- Authors
- Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár
- Venue
- arXiv · Preprint
- First published
- 2017-8-7
- Added
- 2026-11-6
- Updated
- 2018-2-7
- Version
- v2
- arXiv
- 1708.02002
- Category
- cs.CV
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár (2017). Focal Loss for Dense Object Detection. arXiv:1708.02002v2. https://arxiv.org/abs/1708.02002v2
BibTeX
@misc{lin2017focal,
title = {{Focal Loss for Dense Object Detection}},
author = {Tsung-Yi Lin and Priya Goyal and Ross Girshick and Kaiming He and Piotr Dollár},
year = {2017},
eprint = {1708.02002},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1708.02002v2}
}