What is the difference between torch.nn.functional.grid_sample and torch.nn.functional.interpolate?

845 Views Asked by At

Let's say I have an image I want to downsample to half its resolution via either grid_sample or interpolate from the torch.nn.functional library. I select mode ='bilinear' for both cases.

For grid_sample, I'd do the following:

dh = torch.linspace(-1,1, h/2)
dw = torch.linspace(-1,1, w/2)
mesh, meshy = torch.meshgrid((dh,dw))
grid = torch.stack.((meshy,meshx),2)
grid = grid.unsqueeze(0) #add batch dim

x_downsampled = torch.nn.functional.grid_sample(img, grid, mode='bilinear')

For interpolate, I'd do:

x_downsampled = torch.nn.functional.interpolate(img, size=(h/2,w/2), mode='bilinear')

What do both methods differently? Which one is better for my example?

1

There are 1 best solutions below

1
On

Second for brevity. Grid_sample is more suitable for non-uniform interpolation.