Question for torch.nn.functional.interpolate

hi, why does linear interpolation not preserve the points themselves?

import torch
import torch.nn.functional as F
torch.manual_seed(0)

a = torch.randn((16,64,8))
b = F.interpolate(a, (a.size(2)*2), mode=‘linear’)

a[0][0]
tensor([-1.1258, -1.1524, -0.2506, -0.4339, 0.8487, 0.6920, -0.3160, -2.1152])
b[0][0]
tensor([-1.1258, -1.1325, -1.1457, -0.9269, -0.4760, -0.2964, -0.3881, -0.1132,
0.5281, 0.8095, 0.7312, 0.4400, -0.0640, -0.7658, -1.6654, -2.1152])

It seems only the corner points are perserved but other points are changed. Is there a way to preserve the rest of points?

1 Like

That’s not possible out of the box, as it wouldn’t be a linear interpolation anymore.
For your use case you could have a look at the indices, which would be created:

xs = torch.linspace(0, 63, 128)
print(xs)
ys = torch.linspace(0, 7, 16)
print(ys)

which do not contain integer values between the start and end point (close to some, but not exactly equal).

Hi! I am experiencing a similar issue. What exactly do you mean by ‘as it wouldn’t be a linear interpolation anymore’? In my use case, I compared the pytorch interpolation result to the scipy.zoom(order=1) result, and they are very different. Specifically, the pytorch interpolation function extended the intensity distribution of the original volume, which I really don’t understand since I was just using trilinear interpolation. The range of the interpolated values should be confined within the range of the original value, right?

A linear interpolation can read values “between” or indices, so forcing to use discrete indices only would not be a linear interpolation anymore in my opinion.

Could you post an executable code snippet for your issue?