Predicting a box from scratch is hard for a network. Anchor-based detectors make it easier by laying down a dense grid of anchor boxes, fixed reference rectangles of several sizes and shapes at every position, and asking the network only to adjust each one. A feature map with fh x fw cells, whose stride is the number of image pixels per cell, gets anchors centred at each cell's centre. At every location there is one anchor per combination of scale (the square root of the box area) and aspect ratio (height divided by width). Each anchor keeps the area scale^2 while its shape changes with the ratio.
Implement generate_anchors.
generate_anchors(fh, fw, stride, scales, ratios) returns an array of shape (fh * fw * len(scales) * len(ratios), 4) of boxes (x1, y1, x2, y2).(i, j) has centre cy = (i + 0.5) * stride, cx = (j + 0.5) * stride. For a scale s and ratio r = h / w, the anchor has w = s / sqrt(r) and h = s * sqrt(r), so w * h = s**2.i outer, j inner), then for each cell scales outer and ratios inner.(cx - w/2, cy - h/2, cx + w/2, cy + h/2).Compute the (len(scales) * len(ratios), 2) array of widths and heights once, then combine it with the grid of centres by broadcasting.
A ratio of 1 gives a square, a ratio above 1 gives a tall box.
No results yet
Run your code to check it against the examples, or submit to run every test.