Pytorch——对2D Image \ 3D Voxel特征指定3D点进行Grid Sample采样

因为在做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上的的2D feature;其中最后一维可以使用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上的的2D feature;其中最后一维可以使用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参数

参考文章:

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值