Bilinear upsampling vs Bilinear interpolation

The input is

a=torch.Tensor([[1,3],[2,4]])
a.unsqueeze_(0).unsqueeze_(0)

I try two methods:

F.upsample_bilinear(a, scale_factor=2)

to get

tensor([[[[1.0000, 1.6667, 2.3333, 3.0000],
[1.3333, 2.0000, 2.6667, 3.3333],
[1.6667, 2.3333, 3.0000, 3.6667],
[2.0000, 2.6667, 3.3333, 4.0000]]]])

The second one

F.interpolate(a, scale_factor=2, mode=‘bilinear’)

returns

tensor([[[[1.0000, 1.5000, 2.5000, 3.0000],
[1.2500, 1.7500, 2.7500, 3.2500],
[1.7500, 2.2500, 3.2500, 3.7500],
[2.0000, 2.5000, 3.5000, 4.0000]]]])

Why do they return two different outputs? Both do bilinear interpolation.

Hi,

First of all, upsample_* methods are deprecated in favor of interpolate.

But the difference is because of the fact that upsample_* uses interpolate function with arg align_corners=True while default value for interpolate method is align_corners=False. Actually, if you use upsample_* method, it gives you a deprecation warning that states the mentioned behavior.

Following code gives you the same output as old (deprecated) code:

nn.functional.interpolate(a, scale_factor=2, mode='bilinear', align_corners=True)

Official documentations also explain this behavior clearly:
https://pytorch.org/docs/stable/generated/torch.nn.UpsamplingBilinear2d.html


https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.interpolate