Sub topic 8
Classification and Box Losses, and Focal Loss
Assume you know basic Python, NumPy, convolutional feature maps, gradient descent, and the difference between classification and regression. This chapter explains how a detector turns predictions and matched ground-truth boxes into training losses.
A detector usually predicts many candidate boxes. Each candidate needs two kinds of supervision:
- Classification loss measures whether the candidate contains an object and which class it belongs to.
- Box regression loss measures how close the predicted coordinates are to the ground-truth coordinates.
A total detection loss combines them:
L = L_classification + λ L_box
The value λ controls the relative importance of box accuracy. Some detectors also add an objectness loss or mask loss.
Classification loss
For one class, binary cross-entropy is common. If the target is y and the predicted probability is p, the loss is:
-y log(p) - (1 - y) log(1 - p)
A correct, confident prediction has a small loss. A confident wrong prediction has a large loss.
For several mutually exclusive classes, detectors can use softmax cross-entropy. The classes often include a background or “no object” class. In dense detection, however, most candidate locations are background. This creates class imbalance, meaning there are far more negative examples than positive examples.
If every background prediction contributes equally, easy negatives can dominate the total gradient. The model may spend too much effort learning that obvious background is background.
Box regression loss
A box can be represented by its corner coordinates (x1, y1, x2, y2) or by its center and size (cx, cy, w, h). Common regression losses include:
| Loss | Main behavior | Typical use |
|---|---|---|
| Smooth L1 | Penalizes small errors quadratically and large errors nearly linearly | Faster R-CNN-style box regression |
| IoU loss | Directly compares predicted and target overlap | Detectors that optimize overlap |
| GIoU or DIoU loss | Adds geometry when boxes do not overlap | Improving localization when IoU is zero |
Smooth L1 is less sensitive to extreme coordinate errors than squared error. IoU-based losses align more directly with evaluation, but they can provide weak information when boxes do not overlap.
A detector normally computes the box loss only for positive candidates. Background candidates have no object box to regress toward.
Focal loss reduces the effect of easy negatives
Focal loss modifies cross-entropy using the probability assigned to the correct class. Let p_t be that probability:
FL(p_t) = -α_t (1 - p_t)^γ log(p_t)
The focusing parameter γ reduces the contribution of easy examples. If p_t is close to 1, (1 - p_t)^γ is close to zero. Hard examples, with small p_t, retain more weight. The parameter α_t can give different base weights to positive and negative examples.
RetinaNet introduced focal loss to make single-stage dense detection easier to train despite its many background candidates.
import math
def focal_loss(probability, target, alpha=0.25, gamma=2.0):
"""Binary focal loss for one probability and target."""
p = min(max(probability, 1e-7), 1.0 - 1e-7)
p_t = p if target == 1 else 1.0 - p
alpha_t = alpha if target == 1 else 1.0 - alpha
return -alpha_t * (1.0 - p_t) ** gamma * math.log(p_t)
for probability, target in [(0.99, 0), (0.60, 0), (0.90, 1)]:
print(focal_loss(probability, target))The first example is an easy negative, so focal loss downweights it. The second is a harder negative and receives more attention.
Exercise
Why is a box regression loss usually applied only to positive candidates?
Show answer
A background candidate has no ground-truth object box. Applying a regression target to it would provide meaningless supervision.