Sub topic 4
Anchors: Reference Boxes at Every Location
This chapter assumes basic Python and NumPy, convolutional neural networks, feature maps, gradient descent, and the difference between classification and regression.
An object detector must predict both a class and a box. Predicting four unrelated image coordinates at every pixel is difficult. Anchor boxes make the box prediction easier by providing reference shapes.
An anchor is a predefined rectangle attached to one location in a feature map. The detector predicts how to adjust that rectangle and which object class it represents.
Where anchors come from
A backbone network converts an image into a feature map. Suppose the feature map has height H and width W. Each cell represents a region of the original image. At each cell, a detector places several anchors with different sizes and shapes.
The center of an anchor maps to the corresponding location in the input image. If the feature map has a stride of 16, neighboring cells usually represent centers 16 pixels apart in the original image.
Common anchor settings use several scales and aspect ratios:
| Anchor property | Example values | Purpose |
|---|---|---|
| Scale | 32, 64, 128 pixels | Represents small, medium, and large objects |
| Aspect ratio | 0.5, 1.0, 2.0 | Represents tall, square, and wide objects |
| Location | Every feature-map cell | Covers different image regions |
If there are three scales and three aspect ratios, each feature-map cell has nine anchors. A feature map of size 50 by 50 therefore produces 22,500 candidate boxes.
Generating anchors
Let an anchor have width w and height h. For an aspect ratio defined as w / h, a useful formula is:
w = size × sqrt(ratio)h = size / sqrt(ratio)
This keeps the area approximately constant while changing the shape.
import math
def generate_anchors(height, width, stride=16,
sizes=(32, 64), ratios=(0.5, 1.0, 2.0)):
"""Return anchors as (x1, y1, x2, y2) image coordinates."""
anchors = []
for row in range(height):
for col in range(width):
center_x = (col + 0.5) * stride
center_y = (row + 0.5) * stride
for size in sizes:
for ratio in ratios:
anchor_width = size * math.sqrt(ratio)
anchor_height = size / math.sqrt(ratio)
x1 = center_x - anchor_width / 2
y1 = center_y - anchor_height / 2
x2 = center_x + anchor_width / 2
y2 = center_y + anchor_height / 2
anchors.append((x1, y1, x2, y2))
return anchors
anchors = generate_anchors(height=2, width=2)
print(len(anchors)) # 24: 2 × 2 locations × 2 sizes × 3 ratios
print(anchors[0])These anchors are only starting guesses. During training, each anchor is compared with ground-truth boxes. An anchor that overlaps an object sufficiently can become a positive training example. The network then learns class scores and box adjustments for it.
At inference time, many anchors may predict the same object. A later step scores these predictions and removes duplicates with non-maximum suppression. Anchors provide coverage; regression and suppression turn that coverage into final detections.
Exercise
A feature map has 10 × 20 locations, with four scales and three aspect ratios at each location. How many anchors are generated?
Show answer
10 × 20 × 4 × 3 = 2,400 anchors.