因为在做3D的voxel相关工作,需要对某些3D feat中特征点进行采样,而网上大多文章都没讲清楚,于是在这记录并分享一下自己的理解。
2D\3D空间基于坐标点特征采样函数
import torch.nn.functional as F
output = F.grid_sample(input: Tensor,
grid: Tensor,
mode: str=...,
padding_mode: str=...,
align_corners: Optional[Any]=...)
Pytorch官方原文解读:
For each output location output[n, :, h, w], the size-2 vector grid[n, h, w] specifies input pixel locations x and y, which are used to interpolate the output value output[n, :, h, w]. In the case of 5D inputs, grid[n, d, h, w] specifies the x, y, z pixel locations for interpolating output[n, :, d, h, w]. mode argument specifies nearest or bilinear interpolation method to sample the input pixels.
对2D图像特征进行位置采样
- 输入
input
:维度为[batch_size, channel, H, W]
,其中H,W分别对应的是高和宽; - 输入
grid
:维度为[batch_size, num_sampled, 1, 2]
,其中num_sampled
对应的是采样点的个数;grid的最后一维的大小为2,即表示input中pixel的位置信息,这里一般会将x和y的取值范围归一化到[-1,1]之间,[-1,-1]表示input左上角的像素的坐标,[1,1]表示input右下角的像素的坐标。 - 输出
output
:维度为[batch_size, channel, num_sampled, 1]
,其表达的含义为,按照给定采样点得到对应input
上的的2Dfeature
;其中最后一维可以使用output = output.squeeze(3)
进行去除。
Example
:
img = torch.randn(1, 20, 160, 192) # [batch_size, channel, H, W]
grid = torch.randn(1, 32, 1, 2) # [batch_size, num_sampled, 1, 2]
output = F.grid_sample(img, grid, align_corners=False) # torch.Size([1, 20, 32, 1])
output = output.squeeze(3) # torch.Size([1, 20, 32, 1])
对3D空间Voxel特征进行voxel块位置采样
In the case of 5D inputs, grid
[n, d, h, w]
specifies the x, y, z pixel locations for interpolating output[n, :, d, h, w]
.
由官方文档可知,D、H、W分别对应3D空间中的x, y, z,因此:
- 输入
input
:维度为[batch_size, channel, D, H, W]
,其中H,W分别对应的是高和宽; - 输入
grid
:维度为[batch_size, num_sampled, 1, 3]
,其中num_sampled
对应的是采样点的个数,3对应的就是x, y, z
坐标; - 输出
output
:维度为[batch_size, channel, num_sampled, 1]
,其表达的含义为,按照给定采样点得到对应input
上的的2Dfeature
;其中最后一维可以使用output = output.squeeze(3)
进行去除。
Example
:
voxel = torch.randn(1, 20, 160, 192, 160) # [batch_size, channel, D, H, W]
grid = torch.randn(1, 32, 1, 1, 3) # [batch_size, num_sampled, 1, 1, 3]
output = F.grid_sample(voxel, grid, align_corners=False) # torch.Size([1, 20, 32, 1, 1])
output = output.squeeze(-1).squeeze(-1) # torch.Size([1, 20, 32])
align_corners参数
- 上采样
upsample
时align_corners
设为true
还是false
?- 个人认为,上采样时,应当为
True
;下采样时,应当为False
。
- 个人认为,上采样时,应当为
参考文章: