2

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?

emely_pi
  • 517
  • 5
  • 23

1 Answers1

0

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

Pocky
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – R. Marolahy Nov 27 '22 at 18:08