PyTorch grid_sample returns zero array
I want to sample rectangular patch from my image by affine_grid/grid_sample
I created array which contains only 255 values

canvas1 = np.zeros((128, 128), dtype=np.uint8)
canvas1[:] = 255


Also i created grid

theta = torch.FloatTensor([[
    [11/2, 0, 63],
    [0, 11/2, 63],
]])
grid = F.affine_grid(theta, (1, 1, 11, 11))


Grid contains values like

     [[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]],
...............


After that i called 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")


Unfortunately sampled contains zero values (but canvas1_torch[0, 0, 63, 65]) is 255

What i am doing wrong?

Your grid values are outside [-1, 1].

According to https://pytorch.org/docs/stable/nn.html#torch.nn.functional.grid_sample, such values are handled as defined by padding_mode.

Default padding_mode is 'zeros', what you probably want is "border": F.grid_sample(canvas1_torch, grid, mode="bilinear", padding_mode="border") returns all values 255.
