Home Knowledge Base Focal loss

Focal loss is a cross-entropy variant that down-weights easy-to-classify examples and emphasizes hard misclassified samples — specifically designed to address extreme class imbalance in object detection by reducing the contribution of well-classified examples and focusing gradients on difficult, borderline cases, enabling single-stage detectors to match two-stage detector performance.

What Is Focal Loss?

Focal loss modifies standard cross-entropy by introducing a weighting term (1 - p_t)^γ (the focusing parameter) that multiplicatively scales the loss. This weighting factor automatically down-weights easy examples where the model is already confident, allowing the optimizer to focus on hard examples where the model struggles. The loss becomes especially valuable for datasets with severe class imbalance where easy negative examples vastly outnumber positive instances.

Mathematical Definition

Standard cross-entropy:

CE(p_t) = -log(p_t)

Focal loss modification:

FL(p_t) = -α_t * (1 - p_t)^γ * log(p_t)

Where:
- p_t = model's estimated probability for ground truth class
- γ (gamma) = focusing parameter (typically 2)
- α_t = class balancing weight (typically 0.25 for positives)

Effect of (1 - p_t)^γ:

Why Focal Loss Matters

One-Sentence Intuition

Focal loss says: "Don't waste computing resources on examples you already understand — pay attention to the confusing ones."

Focal Loss vs Standard Cross-Entropy

ScenarioEasy Negative (p=0.9)Hard Positive (p=0.1)
Standard CELoss = 0.105Loss = 2.303
Focal LossLoss = 0.0001Loss = 2.07
Scaling Factor0.1%90%

The easy negative contributes almost nothing while maintaining most of the hard positive's signal.

Parameter Selection

γ (Focusing Parameter):

α (Class Balance Weight):

Implementation

PyTorch focal loss (approximate):

def focal_loss(predictions, targets, gamma=2.0, alpha=0.25):
    ce = torch.nn.functional.cross_entropy(predictions, targets, reduction='none')
    p = torch.exp(-ce)  # confidence
    loss = alpha * (1 - p) ** gamma * ce
    return loss.mean()

# Or use third-party implementations
from torchvision.ops import sigmoid_focal_loss

Applications and Impact

Object Detection: RetinaNet — first single-stage detector overcoming accuracy gap with Faster R-CNN by using focal loss on 100k+ background anchors vs few hundred object instances.

Imbalanced Classification: Medical imaging (rare disease detection), fraud detection, rare event prediction — all benefit from focusing on positive class.

Segmentation: Semantic segmentation with background dominating — focal loss prevents background pixels from overwhelming foreground learning.

Text Classification: Imbalanced document classification — hard documents get more gradient signal.

Comparison to Alternatives

Focal loss is the solution for extreme class imbalance — enabling architectures to focus on what actually matters, transforming single-stage detectors from inferior to state-of-the-art through simple, elegant gradient reweighting.

focal lossclass imbalanceobject detection

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.