Problems
Loading...
1 / 1

ROI Pooling

Computer Vision
Hard

In object detection, different regions of interest (ROIs) have different sizes, but downstream classifiers need fixed-size inputs. ROI Pooling extracts a fixed-size feature map from each region by dividing it into bins and applying max pooling within each bin.

Given a 2D feature map, a list of ROIs (bounding boxes in feature map coordinates), and a target output size, apply ROI Pooling to produce a fixed-size output for each ROI.

Algorithm

For each ROI [x1, y1, x2, y2] with height roi_h = y2 - y1 and width roi_w = x2 - x1:

  1. Divide the ROI into output_size x output_size bins.
  2. For bin (i, j), compute its boundaries:
hstart=y1+iroi_houtput_sizehend=y1+(i+1)roi_houtput_sizeh_{start} = y_1 + \left\lfloor \frac{i \cdot roi\_h}{output\_size} \right\rfloor \qquad h_{end} = y_1 + \left\lfloor \frac{(i+1) \cdot roi\_h}{output\_size} \right\rfloor wstart=x1+jroi_woutput_sizewend=x1+(j+1)roi_woutput_sizew_{start} = x_1 + \left\lfloor \frac{j \cdot roi\_w}{output\_size} \right\rfloor \qquad w_{end} = x_1 + \left\lfloor \frac{(j+1) \cdot roi\_w}{output\_size} \right\rfloor
  1. Ensure each bin covers at least one pixel: if h_end equals h_start, set h_end = h_start + 1 (same for width).
  2. Take the maximum value in each bin.

Return a list of 2D grids, one per ROI.

Loading visualization...

Examples

Input: feature_map = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], rois = [[0,0,4,4]], output_size = 2

Output: [[[6, 8], [14, 16]]]

The 4x4 ROI splits evenly into four 2x2 bins. Max of each: top-left=6, top-right=8, bottom-left=14, bottom-right=16.

Input: feature_map = [[10,20,30],[40,50,60],[70,80,90]], rois = [[0,0,3,3]], output_size = 2

Output: [[[10, 30], [70, 90]]]

3x3 ROI into 2x2 output: bins are uneven (floor-based). Top-left bin covers 1 cell (10), top-right covers 2 (20,30→30), bottom-left covers 2 (40,70→70), bottom-right covers 4 (50,60,80,90→90).

Hint 1

Compute bin boundaries using floor(i * roi_size / output_size) to handle uneven division.

Hint 2

When the ROI is smaller than the output size, bins may collapse to zero width. Always ensure at least one pixel per bin.

Requirements

  • Divide each ROI into output_size x output_size bins using floor division
  • Apply max pooling within each bin
  • Ensure each bin covers at least one pixel
  • Process multiple ROIs independently

Constraints

  • 1 <= feature map height, width <= 100
  • 1 <= len(rois) <= 100
  • ROIs are [x1, y1, x2, y2] with integer coordinates
  • 1 <= output_size <= 7
  • Time limit: 300 ms
Try Similar Problems