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

该博客介绍了ROIAlign的实现原理,通过一个实例展示了如何在PyTorch中使用`torchvision.ops.roi_align`和`roi_pool`函数。作者通过输入张量和边界框坐标,输出了经过池化操作后的特征图,对比了ROIAlign与ROIPool的区别,ROIAlign的输出包含了浮点数,而ROIPool的输出则是量化到整数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

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

输入输出的官方文档

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的博客-优快云博客

c++代码实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值