It seems like you are looking for interpolate (a function in nn.functional):
import torch.nn.functional as nnf
x = torch.rand(5, 1, 44, 44)
out = nnf.interpolate(x, size=(224, 224), mode='bicubic', align_corners=False)
If you really care about the accuracy of the interpolation, you should have a look at ResizeRight: a pytorch/numpy package that accurately deals with all sorts of "edge cases" when resizing images. This can have an effect when directly merging features of different scales: inaccurate interpolation may result in misalignments.
I want to resize my [512,512,96] tensor to [64,64,64]. Torchvision.transoforms.Resize only works with the last two dimensions
image = T.Resize(64)(image) turns torch.Size([512, 512, 96]) to torch.Size([512, 341, 64]) ???(no idea where the 341)
image = T.Resize((64,64))(image) turns torch.Size([512, 512, 96]) to torch.Size([512, 64, 64]) .
image = T.Resize((64,64,64))(image) throws this error
If size is a sequence, it should have 1 or 2 values
Any ideas, besides iterating over all the images in a for loop and concatenating them
It seems like you are looking for interpolate (a function in nn.functional):
import torch.nn.functional as nnf
x = torch.rand(5, 1, 44, 44)
out = nnf.interpolate(x, size=(224, 224), mode='bicubic', align_corners=False)
If you really care about the accuracy of the interpolation, you should have a look at ResizeRight: a pytorch/numpy package that accurately deals with all sorts of "edge cases" when resizing images. This can have an effect when directly merging features of different scales: inaccurate interpolation may result in misalignments.
The TorchVision transforms.functional.resize() function is what you're looking for:
import torchvision.transforms.functional as F
t = torch.randn([5, 1, 44, 44])
t_resized = F.resize(t, 224)
If you wish to use another interpolation mode than bilinear, you can specify this with the interpolation argument.
Pytorch resize 3d numpy array
python - Resize PyTorch Tensor - Stack Overflow
Resizing the ground truth of 3D CT scan
Reshape 3D Pytorch Tensor
You can instead choose to go with tensor.reshape(new_shape) or torch.reshape(tensor, new_shape) as in:
# a `Variable` tensor
In [15]: ten = torch.randn(6, requires_grad=True)
# this would throw RuntimeError error
In [16]: ten.resize_(2, 3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-16-094491c46baa> in <module>()
----> 1 ten.resize_(2, 3)
RuntimeError: cannot resize variables that require grad
The above RuntimeError can be resolved or avoided by using tensor.reshape(new_shape)
In [17]: ten.reshape(2, 3)
Out[17]:
tensor([[-0.2185, -0.6335, -0.0041],
[-1.0147, -1.6359, 0.6965]])
# yet another way of changing tensor shape
In [18]: torch.reshape(ten, (2, 3))
Out[18]:
tensor([[-0.2185, -0.6335, -0.0041],
[-1.0147, -1.6359, 0.6965]])
Please can you try something like:
import torch
x = torch.tensor([[1, 2], [3, 4], [5, 6]])
print(":::",x.resize_(2, 2))
print("::::",x.resize_(3, 3))