How to interpret the output of affine_grid

I’m using affine transformation to generate a transformation grid. I would like to be able to interpret the output of affine grid.

input_data = torch.arange(25).reshape(1, 1, 5, 5).double()
identity_theta = torch.tensor((1,0,0,0,1,0)).reshape(2,3).double()
identity_grid = torch.nn.functional.affine_grid(identity_theta.unsqueeze(0), input_data.shape)
output_data = torch.nn.functional.grid_sample(input_data, identity_grid)

print(identity_grid)
print()
print(output_data)

The output is:

tensor([[[[-0.8000, -0.8000],
          [-0.4000, -0.8000],
          [ 0.0000, -0.8000],
          [ 0.4000, -0.8000],
          [ 0.8000, -0.8000]],

         [[-0.8000, -0.4000],
          [-0.4000, -0.4000],
          [ 0.0000, -0.4000],
          [ 0.4000, -0.4000],
          [ 0.8000, -0.4000]],

         [[-0.8000,  0.0000],
          [-0.4000,  0.0000],
          [ 0.0000,  0.0000],
          [ 0.4000,  0.0000],
          [ 0.8000,  0.0000]],

         [[-0.8000,  0.4000],
          [-0.4000,  0.4000],
          [ 0.0000,  0.4000],
          [ 0.4000,  0.4000],
          [ 0.8000,  0.4000]],

         [[-0.8000,  0.8000],
          [-0.4000,  0.8000],
          [ 0.0000,  0.8000],
          [ 0.4000,  0.8000],
          [ 0.8000,  0.8000]]]], dtype=torch.float64)

tensor([[[[ 0.0000,  1.0000,  2.0000,  3.0000,  4.0000],
          [ 5.0000,  6.0000,  7.0000,  8.0000,  9.0000],
          [10.0000, 11.0000, 12.0000, 13.0000, 14.0000],
          [15.0000, 16.0000, 17.0000, 18.0000, 19.0000],
          [20.0000, 21.0000, 22.0000, 23.0000, 24.0000]]]],
       dtype=torch.float64)

Can someone explain to me how to interpret the output of affine grid in this case? I want to understand how such a grid achieves identity transformation and in general other transformations as well.

Thanks in advance!!

1 Like

This well written blog can be util for you:

The first part is basically a from scratch numpy implementation of an affine transformation.

and the second part delves into stn:

Hope this can help you.

Cheers.

1 Like

@ivan-jgr thank you. It was of great help to understand STN. One thing I would like to know is can get the points after transformation or not? For an image I’m processing, I would like to know to what point (x,y) gets mapped to after transformation? Can we get this information using affine grid?

Hi, I have been trying to get the mapped points as well. Were you able to get the answer? It would be of great help.