Focal Loss for Dense Object Detection

Tsung-Yi Lin, Priya Goyal, Ross B. Girshick, Kaiming He, Piotr Dollár

Abstract

The highest accuracy object detectors to date are based on a two-stage approach popularized by R-CNN, where a classifier is applied to a sparse set of candidate object locations. In contrast, one-stage detectors that are applied over a regular, dense sampling of possible object locations have the potential to be faster and simpler, but have trailed the accuracy of two-stage detectors thus far. In this paper, we investigate why this is the case. We discover that the extreme foreground-background class imbalance encountered during training of dense detectors is the central cause. We propose to address this class imbalance by reshaping the standard cross entropy loss such that it down-weights the loss assigned to well-classified examples. Our novel Focal Loss focuses training on a sparse set of hard examples and prevents the vast number of easy negatives from overwhelming the detector during training. To evaluate the effectiveness of our loss, we design and train a simple dense detector we call RetinaNet. Our results show that when trained with the focal loss, RetinaNet is able to match the speed of previous one-stage detectors while surpassing the accuracy of all existing state-of-the-art two-stage detectors.

Introduction

In the field of object detection, two primary architectures have dominated: two-stage detectors like Faster R-CNN and one-stage detectors like YOLO or SSD. Two-stage detectors first generate a sparse set of candidate "proposals" and then classify them, a process that naturally filters out the vast majority of background noise. Conversely, one-stage detectors evaluate a dense grid of thousands of possible locations across an image.

While one-stage detectors offer advantages in speed and simplicity, they have historically lagged in accuracy. This research identifies extreme class imbalance as the primary culprit. During training, a one-stage detector may evaluate ~100,000 locations, but only a handful contain actual objects. Standard Cross Entropy (CE) loss treats all errors equally; as a result, the cumulative loss from the massive number of "easy" background negatives overwhelms the "hard" gradients from actual objects, leading to sub-optimal training.

Methods

Focal Loss

To address the class imbalance, we introduce Focal Loss, which reshapes the standard Cross Entropy loss to focus on "hard" examples. By adding a modulating factor $(1 - p_t)^\gamma$ to the loss, we effectively down-weight easy examples:

$$FL(p_t) = -(1 - p_t)^\gamma \log(p_t)$$

When an example is misclassified and $p_t$ is small, the factor is near 1, and the loss remains largely unaffected. However, as $p_t \to 1$ (indicating a well-classified, "easy" example), the factor approaches 0, discounting the loss contribution. The parameter $\gamma$ smoothly adjusts the rate of down-weighting; we found $\gamma = 2$ to be optimal.

Focal Loss vs. Cross Entropy: The Focal Loss down-weights easy examples, reducing their contribution to the total loss even when their individual loss is small.

RetinaNet Architecture

To evaluate Focal Loss, we designed RetinaNet, a dense detector composed of:

  • Backbone: A ResNet architecture for deep feature extraction.
  • Feature Pyramid Network (FPN): To provide a rich, multi-scale feature hierarchy.
  • Subnets: Two separate subnets for classifying anchor boxes and regressing box offsets.

Crucially, we found that initialization is vital. We initialize the "prior" probability of the rare object class to be very low (e.g., 0.01) to prevent the initial massive background loss from causing training instability.

Results

We evaluated RetinaNet on the COCO dataset. The results demonstrate that the gap between one-stage and two-stage detectors was primarily an optimization challenge rather than an architectural one.

Speed vs. Accuracy on COCO: RetinaNet surpasses the accuracy of all previous one-stage and two-stage detectors while maintaining high inference speeds.

Key findings include:

  • State-of-the-Art Accuracy: RetinaNet-101-800 achieved 39.1 AP on COCO, outperforming both the best one-stage (DSSD: 33.2 AP) and two-stage (Faster R-CNN with FPN: 36.2 AP) models of the time.
  • Efficiency: RetinaNet-50-500 runs at 73ms per image, preserving the speed benefits of one-stage architectures.
  • Ablation Study: Using Focal Loss provided a 2.9 AP gain over a standard Cross Entropy baseline on the same architecture.

Conclusion

Focal Loss effectively addresses the class imbalance problem in dense object detection by preventing easy negatives from dominating the training signal. RetinaNet proves that one-stage detectors can achieve state-of-the-art accuracy without sacrificing speed. Future work may involve exploring more lightweight backbones for real-time applications and further refining the loss function for multi-class complexity.

Summary updated

BibTeX
@inproceedings{lin2017focal,
  author = {Tsung-Yi Lin and Priya Goyal and Ross B. Girshick and Kaiming He and Piotr Dollár},
  title = {{Focal Loss for Dense Object Detection}},
  booktitle = {IEEE/CVF International Conference on Computer Vision},
  year = {2017},
  url = {https://openreview.net/forum?id=H1bA7bfdbB},
}
Figure