我正在实现本文中的基本架构: PyTorch 中的https://arxiv.org/pdf/1705.08260.pdf。
它由自动编码器和空间转换器组成。自编码器的输出与右图像一起被馈送到 ST 或可以说是双线性采样器,并且该双线性插值的输出用于计算左图像与其自身之间的 L1 损失。
但是有一个问题,我真的不认为这段代码会做我想做的事。PyTorch中grid_sample函数的官方文档是指网格必须在-1和1范围内,但网格本身的最大值大于1。如果这段代码是正确的,那么我应该重写网格归一化的行?
我的第一个想法是像这样重写它:(grid / torch.max(grid) - 0.5) * 2
所以值介于 -1 和 1 之间,那么我应该删除 padding_mode 参数,因为没有值超出范围。
如果这是正确的,那么让我知道,这样我就可以确定这是正确的道路。
def bilinear_sampler(images, disps):
N, C, H, W = images.size()
mesh_x, mesh_y = np.array(np.meshgrid(np.linspace(0, 1, W),
np.linspace(0, 1, H),
indexing='xy'))
mesh_x, mesh_y = torch.from_numpy(mesh_x).cuda(), torch.from_numpy(mesh_y).cuda()
mesh_x = mesh_x.repeat(N, 1, 1).type_as(images)
mesh_y = mesh_y.repeat(N, 1, 1).type_as(images)
grid = torch.stack((mesh_x + disps.squeeze(), mesh_y), 3)
output = F.grid_sample(images, grid * 2 - 1, mode='bilinear',
padding_mode='zeros', align_corners=False)
return output