I’m sorry but this function can’t be correct, with the size
parameter it doesn’t work as expected in any way or with any size.
>>> batch_size, c, h, w = 2, 3, 5, 6
>>> x = torch.randn(batch_size, c, h, w)
>>> x.shape
torch.Size([2, 3, 5, 6])
>>> x = F.interpolate(x, size=(2, 3, 10, 12))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/oak-d/venvs/depthai/lib/python3.8/site-packages/torch/nn/functional.py", line 3652, in interpolate
raise ValueError(
ValueError: size shape must match input shape. Input is 2D, size is 4
>>> batch_size, c, h, w = 1, 3, 5, 6
>>> x = torch.randn(c, h, w)
>>> x.shape
torch.Size([3, 5, 6])
>>> x
tensor([[[ 0.1398, 2.6164, 1.5493, -3.3864, -0.4263, 0.0538],
[ 0.6553, 1.3214, -0.2174, -0.4452, -0.7426, -0.3066],
[-0.8942, 0.7908, -0.7685, 0.9150, 0.7674, 0.3924],
[ 0.7903, 0.5168, -0.3655, -0.6867, -0.5429, -1.5122],
[-0.5823, 0.5922, -0.0183, -0.5923, 0.0494, -0.0694]],
[[-0.6175, -0.0933, 0.6786, -0.3152, -0.8401, -0.4897],
[-0.7455, 0.4002, -0.2659, -0.5124, -0.1008, 1.5574],
[-0.6002, -0.3244, -0.5347, 0.7308, -2.1288, 0.6471],
[ 0.2617, -1.5748, -0.1861, -0.8950, 1.6602, 0.3907],
[ 1.9359, -1.3339, 0.1310, -1.5444, -0.4120, 1.6819]],
[[-1.1157, 1.1354, 0.9137, -2.6809, -0.4930, -0.5975],
[ 1.4078, 1.0812, 0.3403, 0.9431, -2.5339, 0.0154],
[-1.4800, 0.3183, -0.2253, -0.3619, -0.0167, -2.0883],
[ 1.6934, -1.6267, 1.0259, -0.7392, 0.0561, -0.7352],
[ 1.1638, 0.7155, -0.1465, 1.4306, 0.5155, 0.2121]]])
>>> x = F.interpolate(x, size=(3, 10, 12))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/oak-d/venvs/depthai/lib/python3.8/site-packages/torch/nn/functional.py", line 3652, in interpolate
raise ValueError(
ValueError: size shape must match input shape. Input is 1D, size is 3
>>> x = torch.randn(h, w)
>>> x.shape
torch.Size([5, 6])
>>> x = F.interpolate(x, size=(10, 12))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/oak-d/venvs/depthai/lib/python3.8/site-packages/torch/nn/functional.py", line 3652, in interpolate
raise ValueError(
ValueError: size shape must match input shape. Input is 0D, size is 2
I’ve been trying to resize a 4D-tensor for hours, with torch.nn.functional.interpolate
or with torchvision.transforms.Resize
, without success. What is the “canonical” way to resize a batch with PyTorch? Upgrading to torch==1.12.1
and torchvision==0.13.1
has no effect.