Learn Before
Code

Region of Interest Pooling Code Implementation

The region of interest (RoI) pooling layer can be implemented in deep learning frameworks like PyTorch using torchvision.ops.roi_pool. The function takes the CNN-extracted feature map, a tensor of region proposals, the desired output size, and a spatial scale factor. Each region proposal is represented by five elements: the object class index, followed by the (x,y)(x, y)-coordinates of its upper-left and lower-right corners. Because the feature map is typically downsampled relative to the original image, the spatial_scale argument is used to scale the region proposal coordinates from the original image space to the feature map space before pooling.

import torch import torchvision X = torch.arange(16.).reshape(1, 1, 4, 4) rois = torch.Tensor([[0, 0, 0, 20, 20], [0, 0, 10, 30, 30]]) torchvision.ops.roi_pool(X, rois, output_size=(2, 2), spatial_scale=0.1)

0

1

Updated 2026-05-21

Contributors are:

Who are from:

Tags

D2L

Dive into Deep Learning @ D2L