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.
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.
Resize tensor without converting to PIL image?
python - Resize PyTorch Tensor - Stack Overflow
Resize stack of tensors
How to resize a tensor or image
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))
I have a tensor with shape torch.Size([5, 240, 240, 155]) which is a 5 different channels with 155 images of 240x240. I want to make those images 128x128 so the final shape would look like this.
torch.Size([5, 128, 128, 155]).
transforms.Resize only affects the last two dimensions, and reshaping in messes up the images, as it does not understand that there are 155 different images and mixes them up. Is there a better way to do this than a for loop that iterates over every image, reshapes it and then concatenate them?
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