TORCH.NN.FUNCTIONAL.GRID_SAMPLE invariant to sign of the grid


I’ve been using TORCH.NN.FUNCTIONAL.GRID_SAMPLE to warp an optical flow (vector field) over an image, only rotating the image by the center, and I realized that the function returns the same result if I pass it the optical_flow or the -optical_flow. Is this a normal behaviour?

Here is my code:

import torch.nn.functional as Fu
transformed = Fu.grid_sample(input_image, optical_flow, align_corners=True, mode="bilinear")


When you say “optical flow” and “vector field”, are you entering displacements or are you entering coordinates in the -1…1 range? If not, you might you need to add the “current position” and/or scale to the input convention of grid_sample.

I’m using this example and see a difference when I change the sign of the coordinate field.

ls = torch.linspace(-1, 1, 50)
y = ls[:, None].expand(50, 50)
x = ls[None, :].expand(50, 50)
coords = torch.stack([y, x], -1)[None]
inp = torch.arange(20*20, dtype=torch.float).view(1, 1, 20, 20)
out = torch.nn.functional.grid_sample(inp, -coords)  # flip sign on coords and compare
from matplotlib import pyplot
pyplot.imshow(out[0, 0])

Best regards


ok thank you very much!

In your example it works as I expected, so I will look for a minimum reproducible example of my exact problem and I will update the post when I have it!

Again thank you for your answer :slight_smile: