ROI Pooling
ROI Pooling
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:
- Divide the ROI into output_size x output_size bins.
- For bin (i, j), compute its boundaries:
- Ensure each bin covers at least one pixel: if h_end equals h_start, set h_end = h_start + 1 (same for width).
- Take the maximum value in each bin.
Return a list of 2D grids, one per ROI.
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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
ROI Pooling
ROI Pooling
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:
- Divide the ROI into output_size x output_size bins.
- For bin (i, j), compute its boundaries:
- Ensure each bin covers at least one pixel: if h_end equals h_start, set h_end = h_start + 1 (same for width).
- Take the maximum value in each bin.
Return a list of 2D grids, one per ROI.
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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number