Grid_sample : unexpected behaviour for high grid values outside [-1,1]

Using grid_sample in ‘border’ padding_mode shows unexpected behaviour when sampling far off the regular grid [-1,1].

For example,

img = torch.FloatTensor([0.3304,  0.9959,  0.9013,  0.4473,  0.6256,  0.4969,  0.6143,  0.4897,  0.6461,  0.6996,  0.8513,  0.8399,   0.3916,  0.6773,  0.6800,  0.9299]).view(1,1,4,4)
grid_x = torch.FloatTensor([-9.3808e+07,  9.3455e+07, -3.2075e+07, -9.7417e+07,  9.3614e+07,  3.0895e+06,  3.5309e+07, -4.3387e+07,  -4.5572e+07,  1.1426e+07,  1.0985e+07, -3.3500e+07,  6.3854e+06,  5.8119e+07, 6.1684e+07,  5.5792e+07]).view(1,4,4,1)
grid_y = torch.FloatTensor([ -9.1669e+07,  3.0808e+07, -3.5392e+07,  9.4505e+07,  3.8825e+07, -2.4498e+07, -3.2516e+07,  8.8106e+07,  2.6877e+07,  9.6970e+06, -5.1589e+06, -2.8973e+07,  1.6749e+07, -2.4837e+07,-5.2990e+07, -7.7938e+07]).view(1,4,4,1)
grid = torch.cat((grid_x,grid_y),dim=3)
p_img = grid_sample(img,grid,padding_mode='border')

Following is a sampled image:

Variable containing:
(0 ,0 ,.,.) = 
  0.0000  0.0000  0.0000  0.0000
  0.0000  0.0000  0.0000  0.0000
  0.0000  1.8598  0.4473  0.0000
  1.8598  0.0000  0.0000  0.0000
[torch.FloatTensor of size 1x1x4x4]

One can see that among all the non-zero entries none should be higher than 1.0 as these values are generated by interpolation from ‘border_padded’ pixels.
In my project occasionally I end up getting arbitrarily high grid co-ordinates, and I assumed that grid_sample will be able to handle them gracefully.
However, it appears on some rare occasions grid_sample misbehaves.
Can someone suggest the reason behind this behaviour?

1 Like

I have the same issue with Pytorch 0.4. Has it been corrected in 0.4.1 ?

1 Like