How to use pytorch's grid_sample()?
I am having some trouble getting torch.nn.functional working as I would like, illustrated by the below example:

import torch
import torch.nn.functional as F
import numpy as np

sz = 5
input_arr = torch.from_numpy(np.arange(sz*sz).reshape(1,1,sz,sz)).float()
indices = torch.from_numpy(np.array([-1,-1, -0.5,-0.5, 0,0, 0.5,0.5, 1,1]).reshape(1, 1, 5, 2)).float()

out = F.grid_sample(input_arr, indices)
print(input_arr)
print(out)


Since the indices are just the diagonals of the input, I'd expect to get something like tensor([[[[0., 6., 12., 18., 24.]]]]) (since (-1,-1) should give the top left and (1,1) should give the bottom right, according to the docs). However, I am getting this as output to the console:

tensor([[[[ 0.,  1.,  2.,  3.,  4.],
          [ 5.,  6.,  7.,  8.,  9.],
          [10., 11., 12., 13., 14.],
          [15., 16., 17., 18., 19.],
          [20., 21., 22., 23., 24.]]]])
tensor([[[[ 0.0000,  4.5000, 12.0000, 19.5000,  6.0000]]]])


What am I doing wrong? Many thanks!

Have you tried passing the argument align_corners = True? If you read the documentation it states that :

WARNING
When align_corners = True, the grid positions depend on the pixel size
relative to the input image size, and so the locations sampled by
grid_sample() will differ for the same input given at different
resolutions (that is, after being upsampled or downsampled). The
default behavior up to version 1.2.0 was align_corners = True. Since
then, the default behavior has been changed to align_corners = False,
in order to bring it in line with the default for interpolate().

And to double check, I ran the code with and without align_corners = True, to get both the correct output you required and the incorrect output you described.
# align_corners = False
out = F.grid_sample(input_arr, indices, align_corners = False)
print(out) # tensor([[[[ 0.0000,  4.5000, 12.0000, 19.5000,  6.0000]]]])

And
# align_corners = True
out = F.grid_sample(input_arr, indices, align_corners = True)
print(out) # tensor([[[[ 0.,  6., 12., 18., 24.]]]])

