Search

Search papers and lessons

Focal Loss

Level 4 · 10 min read

Focal loss is cross-entropy with each example scaled down by how confident the model already is, so the many easy examples stop drowning out the rare hard ones.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Calculus
  • Learning basics
  • Losses & regularization
  • Inference
  • ReadReady to readNot yet

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 ptp_t, 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".

  1. 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 pt=0.9p_t = 0.9.
  2. Compute ordinary cross-entropy. The loss is −ln⁡pt=−ln⁡0.9≈0.105-\ln p_t = -\ln 0.9 \approx 0.105. Check: ln⁡0.9\ln 0.9 is a little under zero, so its negative is a small positive number.
  3. Measure how easy the example is. The easy-ness factor is (1−pt)(1 - p_t). Here it is 1−0.9=0.11 - 0.9 = 0.1. A factor near 0 means the example is easy; near 1 means the model is nearly wrong.
  4. Raise the factor to a power. The focal loss uses (1−pt)γ(1 - p_t)^{\gamma} with a focusing parameter γ≥0\gamma \ge 0. With γ=2\gamma = 2, the factor becomes 0.12=0.010.1^2 = 0.01. The easy example is now scaled to 1 percent of its old weight.
  5. Multiply. Focal loss is −(1−pt)γln⁡pt=0.01×0.105≈0.00105-(1 - p_t)^{\gamma} \ln p_t = 0.01 \times 0.105 \approx 0.00105. Check: the cross-entropy was about 0.105, and the focal version is a hundredth of that.
  6. Do the same for a hard example. If pt=0.1p_t = 0.1, the cross-entropy is −ln⁡0.1≈2.303-\ln 0.1 \approx 2.303, and the factor is (0.9)2=0.81(0.9)^2 = 0.81. 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"]
0.20.40.60.81012345p_t, probability of the true labelloss
  • cross-entropy (gamma = 0)
  • focal loss (gamma = 2)
At p_t = 0.9 the focal loss is 100 times smaller than cross-entropy. At p_t = 0.1 the two are still close (1.87 against 2.30), so hard examples keep most of their loss.

The math

Definition: Focal loss

For a binary classifier with predicted probability pp of the positive class, let pt=pp_t = p if the true label is positive and pt=1−pp_t = 1 - p otherwise. The focal loss is FL(pt)=− αt (1−pt)γln⁡pt\mathrm{FL}(p_t) = -\,\alpha_t\,(1 - p_t)^{\gamma} \ln p_t where γ≥0\gamma \ge 0 is the focusing parameter and αt\alpha_t is a class weight in [0,1][0, 1]: α\alpha for positive examples and 1−α1 - \alpha for negative ones. Setting γ=0\gamma = 0 and αt=1\alpha_t = 1 gives ordinary cross-entropy.

Step 1 in symbols

With the true label positive and p=0.9p = 0.9, pt=0.9p_t = 0.9. The cross-entropy term is −ln⁡pt-\ln p_t, and 1−pt=0.11 - p_t = 0.1.

Step 4 in symbols

The modulating factor is (1−pt)γ(1 - p_t)^{\gamma}, a number that depends only on how sure the model is. For pt=0.9p_t = 0.9 and γ=2\gamma = 2, it is (0.1)2=0.01(0.1)^2 = 0.01. For pt=0.1p_t = 0.1 it is (0.9)2=0.81(0.9)^2 = 0.81. The factor is largest when ptp_t is near 0, where the model is wrong, and smallest when ptp_t is near 1, where it is right.

Step 5 in symbols

With αt=1\alpha_t = 1 and γ=2\gamma = 2: FL(0.9)=(0.1)2×(−ln⁡0.9)≈0.01×0.105=0.00105\mathrm{FL}(0.9) = (0.1)^2 \times (-\ln 0.9) \approx 0.01 \times 0.105 = 0.00105

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 ptp_t (the product rule applies) gives

d FLdpt=γ (1−pt)γ−1ln⁡pt−(1−pt)γpt\frac{d\,\mathrm{FL}}{dp_t} = \gamma\,(1 - p_t)^{\gamma - 1}\ln p_t - \frac{(1 - p_t)^{\gamma}}{p_t}

For ordinary cross-entropy the slope is −1/pt-1/p_t. At pt=0.9p_t = 0.9 with γ=2\gamma = 2, the focal slope is 0.2×ln⁡0.9−0.01/0.9≈−0.0320.2 \times \ln 0.9 - 0.01 / 0.9 \approx -0.032, against −1/0.9≈−1.11-1/0.9 \approx -1.11 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

Example
1. Tiny: one example, two confidence levels

Use γ=2\gamma = 2 and αt=1\alpha_t = 1. A positive example has pt=0.9p_t = 0.9 in one case and pt=0.5p_t = 0.5 in another.

  1. For pt=0.9p_t = 0.9: cross-entropy is −ln⁡0.9≈0.105-\ln 0.9 \approx 0.105. Factor is (0.1)2=0.01(0.1)^2 = 0.01. Focal loss is 0.01×0.105≈0.001050.01 \times 0.105 \approx 0.00105.
  2. For pt=0.5p_t = 0.5: cross-entropy is −ln⁡0.5≈0.693-\ln 0.5 \approx 0.693. Factor is (0.5)2=0.25(0.5)^2 = 0.25. Focal loss is 0.25×0.693≈0.1730.25 \times 0.693 \approx 0.173.
  3. 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.
Example
2. The standard case: easy background, hard object

A detector sees a background patch with pt=0.95p_t = 0.95 and an object patch with pt=0.3p_t = 0.3. Use γ=2\gamma = 2. Give the background class weight αt=0.75\alpha_t = 0.75 and the object class weight αt=0.25\alpha_t = 0.25.

  1. Background patch cross-entropy: −ln⁡0.95≈0.0513-\ln 0.95 \approx 0.0513. Factor: (0.05)2=0.0025(0.05)^2 = 0.0025. Weight 0.750.75. Focal loss: 0.75×0.0025×0.0513≈0.0000960.75 \times 0.0025 \times 0.0513 \approx 0.000096.
  2. Object patch cross-entropy: −ln⁡0.3≈1.204-\ln 0.3 \approx 1.204. Factor: (0.7)2=0.49(0.7)^2 = 0.49. Weight 0.250.25. Focal loss: 0.25×0.49×1.204≈0.1470.25 \times 0.49 \times 1.204 \approx 0.147.
  3. Ratio of object loss to background loss: about 0.147/0.000096≈15000.147 / 0.000096 \approx 1500 with focal loss. Without focal loss the ratio is only 1.204/0.0513≈231.204 / 0.0513 \approx 23.
  4. Interpretation: the ratio has grown about 65 times (1500/231500 / 23). The hard object patch now outweighs the easy background patch by far more, and the easy background contributes almost nothing.
Example
3. A realistic batch of a detector

A training batch has 1000 easy background patches, each with pt=0.95p_t = 0.95, and 10 hard object patches, each with pt=0.3p_t = 0.3. Use the same settings as example 2.

  1. Plain cross-entropy total for the background: 1000×0.0513≈51.31000 \times 0.0513 \approx 51.3. For the objects: 10×1.204≈12.010 \times 1.204 \approx 12.0. The background is about 81 percent of the total.
  2. Focal loss total for the background: 1000×0.000096≈0.0961000 \times 0.000096 \approx 0.096. For the objects: 10×0.147≈1.4710 \times 0.147 \approx 1.47.
  3. Share of the focal total from the objects: 1.47/(1.47+0.096)≈0.941.47 / (1.47 + 0.096) \approx 0.94. The objects now supply about 94 percent of the loss.
  4. 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 pt=0.6p_t = 0.6, with γ=2\gamma = 2 and αt=0.25\alpha_t = 0.25. What is its focal loss? Answer: (1−0.6)2=0.16(1 - 0.6)^2 = 0.16, and −ln⁡0.6≈0.511-\ln 0.6 \approx 0.511, so the focal loss is 0.25×0.16×0.511≈0.02040.25 \times 0.16 \times 0.511 \approx 0.0204.

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, γ\gamma and α\alpha, 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 α\alpha and γ\gamma as doing the same job. In fact α\alpha is a fixed weight per class, which fixes class imbalance, while γ\gamma 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, γ=2\gamma = 2 with α=0.25\alpha = 0.25.

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 (1−pt)γ(1 - p_t)^{\gamma}, so confident correct examples are scaled down.
  • The class weight αt\alpha_t still handles imbalance between classes; γ\gamma handles the difference in difficulty.
  • Setting γ=0\gamma = 0 and αt=1\alpha_t = 1 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.

Check yourself

QuestionFor a positive example with p_t = 0.8, what is the modulating factor (1 - p_t) raised to gamma = 2?Show answer
(1−0.8)2=0.22=0.04(1 - 0.8)^2 = 0.2^2 = 0.04.
QuestionWith gamma = 2 and alpha_t = 1, what is the focal loss when p_t = 0.5?Show answer
Cross-entropy is −ln⁡0.5≈0.693-\ln 0.5 \approx 0.693, and the factor is 0.250.25. Focal loss is 0.25×0.693≈0.1730.25 \times 0.693 \approx 0.173.
QuestionWhy does a very confident correct prediction contribute almost no gradient under focal loss?Show answer
Its factor (1−pt)γ(1 - p_t)^{\gamma} is close to zero when ptp_t is near 1, so the loss and its gradient are scaled down almost to nothing, and gradient descent focuses on examples with larger 1−pt1 - p_t.
QuestionA dataset has 500 easy negatives with p_t = 0.97 and 5 hard positives with p_t = 0.2. Using gamma = 2 and alpha_t = 1 for both, which group contributes more to the total loss, and why?Show answer
The hard positives. Each has factor (1−0.2)2=0.64(1 - 0.2)^2 = 0.64 and cross-entropy −ln⁡0.2≈1.609-\ln 0.2 \approx 1.609, so each contributes about 1.031.03, for a total of about 5.15.1. Each easy negative contributes 0.032×0.0305≈0.00002750.03^2 \times 0.0305 \approx 0.0000275, so 500 of them total about 0.0140.014. The rare hard group dominates, which is the intended effect.

Sources