How to interpret the output of affine_grid

username: pgp
cooked: <p>I’m using affine transformation to generate a transformation grid. I would like to be able to interpret the output of affine grid.</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>The output is:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>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.</p>
<p>Thanks in advance!!</p>

username: ivan-jgr
cooked: <p>This well written blog can be util for you:</p>
<p>The first part is basically a from scratch numpy implementation of an affine transformation.</p>
<aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://kevinzakka.github.io/assets/favicon.ico" class="site-icon" width="256" height="256">
      <a href="https://kevinzakka.github.io/2017/01/10/stn-part1/" target="_blank" rel="noopener nofollow ugc" title="12:00AM - 10 January 2017">Kevin Zakka’s Blog – 10 Jan 17</a>
  </header>
  <article class="onebox-body">
    <img src="http://kevinzakka.github.io/" class="thumbnail" width="" height="">

<h3><a href="https://kevinzakka.github.io/2017/01/10/stn-part1/" target="_blank" rel="noopener nofollow ugc">Deep Learning Paper Implementations: Spatial Transformer Networks - Part I</a></h3>

<p>Part I covers affine image transformations and bilinear interpolation.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>and the second part delves into stn:</p>
<aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://kevinzakka.github.io/assets/favicon.ico" class="site-icon" width="256" height="256">
      <a href="https://kevinzakka.github.io/2017/01/18/stn-part2/" target="_blank" rel="noopener nofollow ugc" title="12:00AM - 18 January 2017">Kevin Zakka’s Blog – 18 Jan 17</a>
  </header>
  <article class="onebox-body">
    <img src="http://kevinzakka.github.io/" class="thumbnail" width="" height="">

<h3><a href="https://kevinzakka.github.io/2017/01/18/stn-part2/" target="_blank" rel="noopener nofollow ugc">Deep Learning Paper Implementations: Spatial Transformer Networks - Part II</a></h3>

<p>In part II, we cover the Spatial Transformer module and summarize its paper in detail.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Hope this can help you.</p>
<p>Cheers.</p>

username: pgp
cooked: <p><a class="mention" href="/u/ivan-jgr">@ivan-jgr</a> 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?</p>

username: nik-u
cooked: <p>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.</p>