我想从我的图像中采样矩形补丁affine_grid
/grid_sample
我创建了只包含255
值的数组
canvas1 = np.zeros((128, 128), dtype=np.uint8)
canvas1[:] = 255
我也创建了网格
theta = torch.FloatTensor([[
[11/2, 0, 63],
[0, 11/2, 63],
]])
grid = F.affine_grid(theta, (1, 1, 11, 11))
网格包含像这样的值
[[57.5000, 57.5000],
[58.6000, 57.5000],
[59.7000, 57.5000],
[60.8000, 57.5000],
[61.9000, 57.5000],
[63.0000, 57.5000],
[64.1000, 57.5000],
[65.2000, 57.5000],
[66.3000, 57.5000],
[67.4000, 57.5000],
[68.5000, 57.5000]],
...............
之后我打电话给 grid_sample
canvas1_torch = torch.FloatTensor(canvas1.astype(np.float32))
canvas1_torch = canvas1_torch.unsqueeze(0).unsqueeze(0)
sampled = F.grid_sample(canvas1_torch, grid, mode="bilinear")
不幸的是sampled
包含零值(但canvas1_torch[0, 0, 63, 65]
)是 255
我在做什么错?
您的网格值在 [-1, 1] 之外。
根据https://pytorch.org/docs/stable/nn.html#torch.nn.functional.grid_sample,这些值按照padding_mode
.
默认padding_mode
为“零”,您可能想要的是“边界”:F.grid_sample(canvas1_torch, grid, mode="bilinear", padding_mode="border")
返回所有值 255。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句