# Resizing? Any simple direct way?

Hello, is there a simple way, to resize an image? For example from (256,256) to (244,244)?
I looked at this thread Autogradable image resize and used the AvgPool2 method, but it seems quiet complicated for me, to resize an image from 256p to 244…
I am sitting on this problem for a quiet long time now…and I don’t find a way to fix it. Some help would be very appreciated!
thanks!

2 Likes

Hello,

You can use Torchvision transforms on your images. Pretty straight forward.

http://pytorch.org/docs/master/torchvision/transforms.html

Sorry, that is a quiet low level question, but I am quiet new to pytroch, but I tried resizing my image for ours now, also with .Resize.
Could you give me an simple example, how you use that? thanks!

The Data Loading and Processing Tutorial includes a session on image transforms.

I believe it would be best to follow/understand the tutorial than for me to try to summarize it.

3 Likes

I think an example, on how to simply apply a rescaling on my image would help me very much with understanding all that

First of all as Antonio stated read the transforms documentation it will teach you much more. Below is a simplified version of what is explained in the tutorials.

from skimage import io, transform

img = transform.resize(image, (desired_h, desired_w))

3 Likes

I use adaptive average pooling for resizing if I can / want move resizing step to GPU:

``````import torch
import torch.nn.functional as F

def resize2d(img, size):

img = torch.Tensor(3,256,256).uniform_()
print(img.size())
img = resize2d(img, (224,224))
print(img.size())``````
8 Likes
``````def resize_tensor(input_tensors, h, w):
final_output = None
batch_size, channel, height, width = input_tensors.shape
input_tensors = torch.squeeze(input_tensors, 1)

for img in input_tensors:
img_PIL = transforms.ToPILImage()(img)
img_PIL = torchvision.transforms.Resize([h,w])(img_PIL)
img_PIL = torchvision.transforms.ToTensor()(img_PIL)
if final_output is None:
final_output = img_PIL
else:
final_output = torch.cat((final_output, img_PIL), 0)
final_output = torch.unsqueeze(final_output, 1)
return final_output

``````

You have to convert to PIL and use transform functions there. Also, transform functions don’t support batch so I wrote a helper function. Hope this helps.

3 Likes

I would use this wonderful repo ResizeRight

Hi Antonio, the link is not working.

On the same lines, here’s a modified version directly using the transforms class. I lifted it directly from my training code.

``````transform = transforms.Compose([transforms.Resize((256, 256)), transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
img = Image.open(os.path.join(input_path, file)) #PIL image
img = transform(img) #torch.FloatTensor
``````