The prior_box operator is used in the SSD (Single Shot MultiBox Detector) algorithm to generate candidate bounding boxes (priors) for each point in an input feature map.
Core Logic:
- For every pixel in the input
height $\times$ width feature map, it generates num_priors candidate boxes. - The total number of boxes generated is
height * width * num_priors. - Each box is represented by coordinates
(x1, y1, x2, y2), which are normalized to the range [0, 1] relative to the original image dimensions (im_width, im_height). - The number of priors (
num_priors) is determined by the lengths of min_sizes, max_sizes, and aspect_ratios. - Center Point: The center of the boxes for a specific pixel is calculated using an
offset and the step sizes (step_w, step_h) relative to the image dimensions. - Clipping: If
is_clip is set to True, the coordinates are clipped to the [0, 1] range using max(coord, 0) and min(coord, 1). - Variance: The operator also outputs a
var tensor, which replicates the input variance (a 4-element array) for every generated box to facilitate decoding.
# Conceptual logic for box generation
pixel_pos = GetPixelPos(width, height, 1, 4) # shape (W, H, 1, 4)
box_offset = GetBoxOffset(1, 1, num_priors, 4) # shape (1, 1, num_priors, 4)
lower_bound = [0]
upper_bound = [W, H, W, H]
output = pixel_pos + box_offset
output = max(lower_bound, output)
output = min(upper_bound, output)