Detection Loss Functions
What you'll learn: How object detectors train by combining classification loss and localization loss into a single training objective.
Why Two Losses?
Object detection solves two problems simultaneously:
- What is in the box? (classification)
- Where is the box? (localization)
Each needs its own loss function, then they're combined during training.
Classification Loss
For the "what" problem, detectors typically use cross-entropy loss — the same loss you've seen in image classification. It measures how well the network predicts the correct class label (including a "background" class for no object).
Localization Loss: Smooth L1
For the "where" problem, we need to compare predicted bounding boxes to ground truth boxes. The smooth L1 loss (also called Huber loss) measures how far off the predicted box coordinates are:
smooth_L1(x) = 0.5 * x² if |x| < 1
|x| - 0.5 otherwise
Why smooth L1 instead of plain L2 (squared error)? It's less sensitive to outliers. When predictions are way off, L2 produces huge gradients that can destabilize training. Smooth L1 acts quadratic near zero (precise adjustments) but linear for large errors (bounded gradients).
Analogy: Think of parking a car. When you're close to the spot (small error), you make tiny adjustments (quadratic). When you're far away (large error), you move steadily without panicking (linear).
Combined Loss
The total detection loss combines both:
Total Loss = λ_cls * Classification_Loss + λ_loc * Smooth_L1_Loss
The λ terms balance the two objectives. Typically, localization loss only applies to boxes containing actual objects (not background).
Key Takeaway: Detection loss functions combine classification loss (what) with smooth L1 loss (where) to train networks that both identify objects and precisely localize them with stable gradients.