TL;DR
Focal loss is a change to cross-entropy loss for classifiers that see far more easy examples than hard ones. It multiplies each example's loss by a factor that shrinks as the model becomes more confident in the right answer, so the rare hard examples keep most of the training signal. It was introduced to train object detectors that look at many background regions for every object.
Builds on: Cross-Entropy Loss, Class Imbalance · Leads to: Object Detection models that use dense anchors
Where it's used
Object detection in images. An object detector scans a photo and checks thousands of candidate boxes (anchors). Nearly all of them are background, and most of those are easy: the detector is already sure they are empty. The 2017 RetinaNet detector used focal loss to train on this dense set of candidates and reached accuracy that had previously required two-stage detectors.
Rare-event screening. A system that flags fraudulent transactions or defective parts sees a few positives among a flood of ordinary cases. Plain cross-entropy lets the ordinary cases push the model toward always answering "normal", which scores well on accuracy and badly on recall. Focal loss keeps the model working on the rare cases.
Any heavily skewed classifier. Whenever one class is 100 times more common than another, the same problem appears. The fix is often a weighting, but focal loss gives a weighting that depends on each example's difficulty, not only its class.
How it works
We follow one example at a time through the loss. The model outputs a Probability for each class, often from a sigmoid output for a yes-or-no question. Call it , the probability the model gives to the label that is actually correct. For a positive example that is the predicted probability of "yes"; for a negative example it is one minus the predicted probability of "yes".
- Take the model's probability of the true label. Suppose the model says "object" with probability 0.9 and the true label is object. Then .
- Compute ordinary cross-entropy. The loss is . Check: is a little under zero, so its negative is a small positive number.
- Measure how easy the example is. The easy-ness factor is . Here it is . A factor near 0 means the example is easy; near 1 means the model is nearly wrong.
- Raise the factor to a power. The focal loss uses with a focusing parameter . With , the factor becomes . The easy example is now scaled to 1 percent of its old weight.
- Multiply. Focal loss is . Check: the cross-entropy was about 0.105, and the focal version is a hundredth of that.
- Do the same for a hard example. If , the cross-entropy is , and the factor is . The focal loss is about 1.865. The hard example keeps 81 percent of its loss while the easy one kept 1 percent.
flowchart LR A["Model probability of true label"] --> B["Cross-entropy: minus log of it"] A --> C["Factor: one minus it, raised to gamma"] B --> D["Multiply"] C --> D D --> E["Focal loss for this example"]
- cross-entropy (gamma = 0)
- focal loss (gamma = 2)
The math
For a binary classifier with predicted probability of the positive class, let if the true label is positive and otherwise. The focal loss is where is the focusing parameter and is a class weight in : for positive examples and for negative ones. Setting and gives ordinary cross-entropy.
Step 1 in symbols
With the true label positive and , . The cross-entropy term is , and .
Step 4 in symbols
The modulating factor is , a number that depends only on how sure the model is. For and , it is . For it is . The factor is largest when is near 0, where the model is wrong, and smallest when is near 1, where it is right.
Step 5 in symbols
With and :
Why the gradient matters
Training follows the slope of the loss with respect to the model's output. Differentiating the focal loss with respect to (the product rule applies) gives
For ordinary cross-entropy the slope is . At with , the focal slope is , against for cross-entropy. The focal slope is about 3 percent of the cross-entropy slope, so Gradient Descent takes its steps mainly from examples the model still gets wrong.
Worked examples
Use and . A positive example has in one case and in another.
- For : cross-entropy is . Factor is . Focal loss is .
- For : cross-entropy is . Factor is . Focal loss is .
- Interpretation: the confident example loses about 99 percent of its loss, while the uncertain one loses only 75 percent. The ratio between the two losses has grown from about 6.6 to about 165, in favour of the uncertain example.
A detector sees a background patch with and an object patch with . Use . Give the background class weight and the object class weight .
- Background patch cross-entropy: . Factor: . Weight . Focal loss: .
- Object patch cross-entropy: . Factor: . Weight . Focal loss: .
- Ratio of object loss to background loss: about with focal loss. Without focal loss the ratio is only .
- Interpretation: the ratio has grown about 65 times (). The hard object patch now outweighs the easy background patch by far more, and the easy background contributes almost nothing.
A training batch has 1000 easy background patches, each with , and 10 hard object patches, each with . Use the same settings as example 2.
- Plain cross-entropy total for the background: . For the objects: . The background is about 81 percent of the total.
- Focal loss total for the background: . For the objects: .
- Share of the focal total from the objects: . The objects now supply about 94 percent of the loss.
- Interpretation: the background still contributes, but no longer decides what the model learns. Gradients come mostly from the objects the detector is missing.
Try it: a positive example has , with and . What is its focal loss? Answer: , and , so the focal loss is .
Common mistakes and tradeoffs
It is tempting to think focal loss simply ignores easy examples. In fact it only down-weights them. A very easy example still adds a small loss, and when the easy examples are the majority, their total can still matter. The loss is a smooth weighting, not a filter.
It is tempting to think focal loss is always better than cross-entropy. In fact it adds two settings, and , that need tuning, and it can hurt when labels are noisy: a mislabelled example looks "hard" and gets emphasised. When classes are roughly balanced and most examples are informative, plain cross-entropy is simpler and often just as good.
It is tempting to treat and as doing the same job. In fact is a fixed weight per class, which fixes class imbalance, while weights each example by its difficulty, which fixes the imbalance between easy and hard examples. They address different problems, and the original paper's best setting used both, with .
When to use it: a classifier or detector with many more easy negatives than hard examples, where most of the loss comes from the majority. When not to use it: balanced problems, very noisy labels, or when a simple class weight already works. Alternatives include re-sampling the rare class, hard-example mining, and class-balanced weighting.
History
Before 2017, object detectors had to fight the same imbalance. A common remedy was hard-example mining: pick only the hardest background patches for each batch. Focal loss, introduced by Tsung-Yi Lin and colleagues in 2017 in the RetinaNet detector, gave a smooth version of this idea inside the loss itself. The paper showed that a one-stage detector using focal loss could match the accuracy of the two-stage detectors that were then the standard, which made the loss widely adopted in later detection and rare-event work.
Recap
- Cross-entropy gives the same weight to every example, so a flood of easy examples can dominate training.
- Focal loss multiplies each example's cross-entropy by , so confident correct examples are scaled down.
- The class weight still handles imbalance between classes; handles the difference in difficulty.
- Setting and recovers cross-entropy.
- Read next: Object Detection, where the loss was first used at scale, and Logistic Regression, the two-class model whose loss this modifies.