Example-Research-COAT/Code/Python/COAT基础笔记.md

25 lines
1.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

## COAT 基础笔记
1、MultiScaleRoIAlign的基本原理和用法
**MultiScaleRoIAlign实际上是RoIAlign的增强版RoIAlign 用于将任意尺寸感兴趣区域的特征图,都转换为具有固定尺寸 `H × W `的小特征图**。与RoI pooling一样其基本原理是将 `h x w `的特征划分为 `H × W `网格,每个格子是大小近似为 h / H × w / W 的子窗口 然后将每个子窗口中的值最大池化到相应的输出网格单元中。想复习RoI pooling概念的可以看[这篇](https://deepsense.ai/region-of-interest-pooling-explained/)。
由于基于anchor的方法会有矩形框从而产生ROI区域。这些ROI区域尺寸是大小不一的。但是我们在后续对矩形框的位置回归和分类的时候是需要输入固定尺寸特征图的。所以ROIAlign核心的作用是把大小不一的ROI区域的特征图转换为固定尺寸大小以便后续操作。ROIAlign的原理介绍可以参考[这篇文章](https://blog.csdn.net/Bit_Coders/article/details/121203584)。
```python
# 这里roi模块的功能是把feat1、feat3特征转换为5x5的尺寸
roi = torchvision.ops.MultiScaleRoIAlign(['feat1', 'feat3'], 5, 2)
# 构建仿真的特征, 用于模拟rpn提取的ROI区域
i['feat1'] = torch.rand(1, 5, 64, 64)
# 这个特征实际没有使用,模拟实际情况下,部分不用的特征
i['feat2'] = torch.rand(1, 5, 32, 32)
i['feat3'] = torch.rand(1, 5, 16, 16)
# 创建随机的矩形框
boxes = torch.rand(6, 4) * 256; boxes[:, 2:] += boxes[:, :2]
# 原始图像尺寸
image_sizes = [(512, 512)]
# ROI对齐操作
output = roi(i, [boxes], image_sizes)
# torch.Size([6, 5, 5, 5]) : 对应的含义 6个矩形框、5个通道、5x5的Size
print(output.shape)
```