Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

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. As p_t approaches 1, (1 - p_t)^γ approaches 0, while a misclassified example with a small p_t keeps nearly its full cross-entropy loss. With the paper's best setting of γ = 2, an example classified with p_t = 0.9 gets 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.25 with γ = 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.

  • 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.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning