Hi, I happened to find this repo (`flip` when sampling features from plane · Issue #4 · ChrisWu1997/SingleShapeGen · GitHub) and was having questions about flipping the coordinates in F.grid_sample. Here is the code snippet from the issue. Is this a bug or am I missing something? Why flip(-1)
solve the issue in this case?
def make_coord(H: int, W: int, device="cpu", normalize=True):
xs = torch.arange(H, device=device).float()
ys = torch.arange(W, device=device).float()
if normalize:
xs = xs / (H - 1) * 2 - 1 # (-1, 1)
ys = ys / (W - 1) * 2 - 1 # (-1, 1)
coords = torch.stack(torch.meshgrid(xs, ys), dim=-1)
return coords
coords = make_coord(3, 4)
coords = coords.unsqueeze(0)
print(coords.shape)
arr = torch.zeros((3, 4))
arr[0, 1] = 1
arr = arr.unsqueeze(0).unsqueeze(0)
print(arr)
# tensor([[[[0., 1., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.]]]])
samples = F.grid_sample(arr, coords, align_corners=True)
print(samples)
# tensor([[[[0.0000, 0.0000, 0.0000, 0.0000],
# [0.5000, 0.1667, 0.0000, 0.0000],
# [0.0000, 0.0000, 0.0000, 0.0000]]]])
# wrong!
samples = F.grid_sample(arr, coords.flip(-1), align_corners=True)
print(samples)
# tensor([[[[0., 1., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.]]]])
# correct!