How does grid_sample(x, grid) work?

Ho,
the implementation is in this file. In particular, the forward method (that computed the output) is this one.
Here is a quick description of what the C code is doing:

  • here check that the input is valid.
  • here initialize stuff and make the output have the right size.
  • Then it iterates over the batch then height then width.
  • For each of these, it gets the input coordinated ix and iy here.
  • here it computes the 4 points around it that will be used for the interpolation.
  • here it computes the weights for each of these 4 points.
  • here it clips stuff if you asked for it.
  • Then it iterates over the channels.
  • It get the 4 points values, compute the interpolation value then save it in the output tensor.
25 Likes