Anchor Boxes: Every Scale, Every Shape

Computer VisionMedium+20 XP+15

Anchor Boxes: Every Scale, Every Shape

Object detectors don't guess boxes from nothing. They start from a fixed grid of anchors — reference boxes at every location, in a spread of sizes and shapes — and only learn how to nudge them.

Around a centre point, each (scale, ratio) pair defines one anchor:

1width = scale * sqrt(ratio)
2height = scale / sqrt(ratio)

Task: write generate_anchors(center_x, center_y, scales, aspect_ratios) returning boxes as [x1, y1, x2, y2] (top-left, bottom-right), every value rounded to 4 decimal places.

  • Produce one anchor per (scale, ratio) combination, looping scales on the outside, ratios on the inside.
  • The box is centred on (center_x, center_y) — half the width each side, half the height above and below.
  • Empty scales returns an empty list.

That sqrt in both formulas isn't decoration. It keeps width * height = scale² for every ratio, so changing shape doesn't quietly change area — the two knobs stay independent.

Examples

center_x = 0.0
center_y = 0.0
scales = [2.0]
aspect_ratios = [1.0]
returns [[-1.0, -1.0, 1.0, 1.0]]
center_x = 5.0
center_y = 5.0
scales = [4.0]
aspect_ratios = [1.0, 4.0]
returns [[3.0, 3.0, 7.0, 7.0], [1.0, 4.0, 9.0, 6.0]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run