Focal Loss
Focal loss scales cross-entropy so easy examples barely count and rare, hard ones drive training. Covers the formula, gamma, RetinaNet, and variants.
Focal loss is a modified cross-entropy loss function that down-weights examples a model already classifies well, so training concentrates on the hard, misclassified ones. Lin et al. introduced it in 2017 in the paper Focal Loss for Dense Object Detection, together with the RetinaNet detector, to handle the extreme imbalance between background and objects in object detection. It is now widely used in classification problems where one class vastly outnumbers the others.
The Imbalance Problem#
One-stage object detectors classify every candidate location in an image, and RetinaNet evaluates about 100,000 of them per image. Almost all of those locations are background that the model learns to reject early in training. Under standard cross-entropy, each easy negative still adds a small loss, and their sheer number makes them dominate the total loss and its gradients, drowning out the few locations that contain objects. Two-stage object detectors avoid this by filtering candidates with a region proposal stage and sampling a fixed ratio of foreground to background, and earlier one-stage detectors relied on hard negative mining. Focal loss fixes the imbalance inside the loss itself, so every candidate can be trained on without sampling.
How Focal Loss Works#
Write p_t for the probability the model assigns to the correct class. Cross-entropy is CE = -log(p_t), and focal loss multiplies it by a modulating factor:
FL = -α_t (1 - p_t)^γ log(p_t)
- Focusing parameter
γ(gamma): Controls how quickly the loss of easy examples shrinks. Atγ = 0, focal loss equals cross-entropy. Asp_tapproaches 1,(1 - p_t)^γapproaches 0, while a misclassified example with a smallp_tkeeps nearly its full cross-entropy loss. With the paper's best setting ofγ = 2, an example classified withp_t = 0.9gets 100 times less loss than under cross-entropy. - Balancing weight
α_t(alpha): A fixed weight for the positive class and its complement for the negative class. RetinaNet usesα = 0.25withγ = 2, and the paper also normalizes each image's total loss by the number of anchors assigned to objects.
The sigmoid_focal_loss function in torchvision implements the formula. Setting alpha=-1 disables the balancing weight, which isolates the effect of γ on an easy, a moderate, and a hard positive example:
import torch
import torch.nn.functional as F
from torchvision.ops import sigmoid_focal_loss
# Logits for three positive examples: easy, moderate, and hard
logits = torch.tensor([4.0, 1.0, -2.0])
targets = torch.ones(3)
# alpha=-1 turns off class weighting so only the gamma focusing term differs
bce = F.binary_cross_entropy_with_logits(logits, targets, reduction="none")
focal = sigmoid_focal_loss(logits, targets, alpha=-1, gamma=2.0, reduction="none")
for p, b, f in zip(logits.sigmoid(), bce, focal):
print(f"p={p:.2f} cross-entropy={b:.6f} focal={f:.6f} ratio={b / f:,.1f}x")Focal loss shrinks the easy example's loss (p = 0.98) by a factor of about 3,091, the moderate one's (p = 0.73) by 13.8, and the hard one's (p = 0.12) by only 1.3, so the hard example now carries almost all of the training signal.
Where Focal Loss Is Used#
Beyond RetinaNet, focal loss and its variants train the classification branch of many dense and transformer-based detectors, including DETR-family models such as Deformable DETR. In medical image analysis, lesions often cover a tiny fraction of the pixels, so image segmentation models commonly pair focal loss with Dice loss to keep small findings from being ignored and to raise recall. In Ultralytics, the RT-DETR trainer scores classes with Varifocal Loss, and the ultralytics.utils.loss module provides a FocalLoss class with defaults of gamma=1.5 and alpha=0.25.
Focal Loss vs. Related Techniques#
- Class weighting: Weighted cross-entropy gives every example of a rare class the same fixed boost based on class frequency. Focal loss weights each example by how hard it currently is, so the weights change as the model learns.
- Hard negative mining: Keeps only the highest-loss negatives and discards the rest. Focal loss keeps every example and shrinks the easy ones smoothly.
- Varifocal Loss: An asymmetric variant from VarifocalNet that down-weights only negatives and trains positives toward an IoU-aware score, so class confidence also reflects localization quality.
- Distribution Focal Loss (DFL): A box regression loss from Generalized Focal Loss that treats each box edge as a discrete probability distribution. It shares only the name with focal loss and applies to box regression.










