ROI Align的实现原理2_代码实现


书接上文,上一次分析了一下ROI Align的原理是如何实现的,一直想知道具体细节是如何实现的,这里找了相关资料进行学习.

上一篇原理介绍博客:ROI Align的实现原理_dlvector的博客-CSDN博客

输入输出的官方文档

Arguments:
input (Tensor[N, C, H, W]): input tensor
boxes (Tensor[K, 5] or List[Tensor[L, 4]]): the box coordinates in (x1, y1, x2, y2)
format where the regions will be taken from. If a single Tensor is passed,
then the first column should contain the batch index. If a list of Tensors
is passed, then each Tensor will correspond to the boxes for an element i
in a batch
output_size (int or Tuple[int, int]): the size of the output after the cropping
is performed, as (height, width)

原来batch_id在第0维…且数值为绝对位置。其格式应为[batch_id, x1, y1, x2, y2]

,其中(x1, y1)为左上角,(x2, y2)为右下角。

from torchvision.ops import nms, roi_align, roi_pool
import torch

# fp = torch.randn([1, 1, 5, 5])
fp = torch.tensor(list(range(5 * 5))).float()
fp = fp.view(1, 1, 5, 5)
print(fp)
# [batch_id, x1, y1, x2, y2]
boxes = torch.tensor([[0, 0, 0, 1, 1]]).float()

pooled_features = roi_align(fp, boxes, [4, 4])
print(pooled_features)

pooled_features = roi_pool(fp, boxes, [4, 4])
print(pooled_features)

输出打印,差异点在于roi_align输出的是带有小数的(宽,高

tensor([[[[ 0.,  1.,  2.,  3.,  4.],
          [ 5.,  6.,  7.,  8.,  9.],
          [10., 11., 12., 13., 14.],
          [15., 16., 17., 18., 19.],
          [20., 21., 22., 23., 24.]]]])
tensor([[[[0.7500, 1.0000, 1.2500, 1.5000],
          [2.0000, 2.2500, 2.5000, 2.7500],
          [3.2500, 3.5000, 3.7500, 4.0000],
          [4.5000, 4.7500, 5.0000, 5.2500]]]])
tensor([[[[0., 0., 1., 1.],
          [0., 0., 1., 1.],
          [5., 5., 6., 6.],
          [5., 5., 6., 6.]]]])

参考:

https://phabricator.mitk.org/file/data/oh4aj4voo5ldehmaf6aj/PHID-FILE-yudyawc6exqxmt7f2qby/fil​​​​​​e

https://medium.com/@andrewjong/how-to-use-roi-pool-and-roi-align-in-your-neural-networks-pytorch-1-0-b43e3d22d073

faster-rcnn.pytorch/roi_align.py at master · jwyang/faster-rcnn.pytorch · GitHub

Region of interest pooling explained

https://tjmachinelearning.com/lectures/1718/instance/instance.pdf

【torchvision】roi_align、roi_pool使用说明_StevenGerrad的博客-CSDN博客

c++代码实现

你可能感兴趣的:(深度学习,图像识别,目标检测,深度学习,python)