megengine.functional.nn.roi_align#

roi_align(inp, rois, output_shape, mode='average', spatial_scale=1.0, sample_points=2, aligned=True)[源代码]#

RoI Align 是在指定输入的感兴趣区域上获得特征图,如 Mask R-CNN 论文中所述。

参数:
  • inp (Tensor) – 输入的特征图,形状为 (n, c, h, w)

  • rois (Tensor) – 一个形状为 (K, 5) 的张量,其表示从坐标为 (idx, x1, y1, x2, y2) 的图像框中要提取的区域,坐标中的 (x1, y1)(x2, y2) 必须满足 0 <= x1 < x20 <= y1 < y2. 第一列 idx 应包含输入批处理中相应元素的索引,即 [0, n - 1] 中的数字。

  • output_shape (Union[int, tuple, list]) – 输出 rois 的形状: (height, width)

  • mode (str) – “max” 或者 “average”, 作用同池化,默认为 “average”

  • spatial_scale (float) – 使用这个数值放缩输入框。默认:1.0

  • sample_points (Union[int, tuple, list]) – 每个输出样本要采取的输入样本数。0 表示密集采样。默认:2

  • aligned (bool) – 通过 aligned=True 来表明是否对齐输入特征,如果为 True 则首先适当缩放 ROI,然后再移动 -0.5. 默认为 True.

返回类型:

Tensor

返回:

输出张量。

实际案例

>>> import numpy as np
>>> np.random.seed(42)
>>> inp = Tensor(np.random.randn(1, 1, 128, 128))
>>> rois = Tensor(np.random.random((4, 5)))
>>> y = F.vision.roi_align(inp, rois, (2, 2))
>>> y.numpy()[0].round(decimals=4)
array([[[0.175 , 0.175 ],
        [0.1359, 0.1359]]], dtype=float32)