Sub topic 7
Deciding Which Anchors Are Positives
This chapter assumes you know basic Python and NumPy, convolutional feature maps, gradient descent, and the difference between classification and regression. It also assumes that Intersection over Union (IoU) measures how much two boxes overlap.
An object detector may place thousands of anchor boxes over an image. During training, each anchor needs a target:
- Positive: this anchor should learn to classify an object and refine its box.
- Negative: this anchor should learn that no target object is present.
- Ignored: this anchor contributes no classification loss because its match is ambiguous.
The process of assigning these labels is called label assignment or anchor matching.
Matching anchors to ground truth
For each anchor, the training code computes its IoU with every ground-truth box. A ground-truth box is the correctly labeled box provided in the training data. The anchor is then assigned according to thresholds.
A common rule is:
| Anchor condition | Training label |
|---|---|
| IoU at least 0.7 | Positive |
| IoU below 0.3 | Negative |
| IoU from 0.3 up to 0.7 | Ignored |
The exact thresholds depend on the detector. Faster R-CNN, for example, commonly uses an IoU of at least 0.7 for positive anchors and below 0.3 for negative anchors.
An anchor can also be made positive when it has the highest IoU for a ground-truth box, even if that IoU is below the positive threshold. This rule prevents a small or unusual object from having no positive anchor at all.
If an anchor is positive, its target class is the class of the matched object. Its box-regression target describes how to move and resize the anchor so that it matches the ground-truth box. Negative anchors receive the background class and usually receive no box-regression target.
Why ignore ambiguous anchors?
An anchor with moderate overlap may cover part of an object but also include substantial background. Treating every such anchor as negative would give the model many confusing examples. Ignoring these anchors removes uncertain signals from the loss.
The number of negative anchors is usually much larger than the number of positive anchors. Training on every negative anchor would let easy background examples dominate the gradient. Detectors therefore use techniques such as negative sampling, which selects only some negatives, or focal loss, which reduces the contribution from easy examples.
Common errors
A frequent mistake is to match an anchor with the closest center instead of the highest IoU. Center distance can be useful in anchor-free methods, but anchor-based assignment must consider both position and size.
Another error is to forget forced matching. Without it, a ground-truth object may have no positive anchor, so the detector receives no direct training signal for that object.
At inference time, anchors do not receive positive or negative labels. The model predicts scores and box offsets, and post-processing selects the final detections.
Exercise
An anchor has IoU 0.75 with one ground-truth box and 0.10 with all others. With positive and negative thresholds of 0.7 and 0.3, what label does it receive?
Show answer
It is positive and is matched to the ground-truth box with IoU 0.75.