Resize images while maintaining the aspect ratio

Does torch.resize allow me to resize an image from any arbitary size say (1080x1080)to 512x512 while maintaining the original aspect ratio.if not,then are there any utilites which I can use to resize my image using torch while still keeping the original aspect ratio.

As per the tutorial on semantic segmentation in albumentations ,it’s mentioned that

This approach may be problematic if images in your dataset have different aspect ratios. For example, suppose you are resizing an image with the size 1024x512 pixels (so an image with an aspect ratio of 2:1) to 256x256 pixels (1:1 aspect ratio). In that case, this transformation will distort the image and may also affect the quality of predictions.
This is applicable to my case where some images might have dimension 1080x1080 and others might have 720x425

torchvision.transforms.Resize will apply resize based on the passed size value. Here is an example:

import torch
import torchvision

x = torch.rand(3,1080,1080)
print(x.shape)

resize = torchvision.transforms.Resize(
    # (H, W)
    size=(512,512)
)

y = resize(x)
print(y.shape)
>>> torch.Size([3, 1080, 1080])
>>> torch.Size([3, 512, 512])

Check the doc for more information

thanks for replying back,but does resize maintain the original aspect ratio.The docs doesn’t mention anything about aspect ratio

In the docs:

If size is an int, smaller edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to (size * height / width, size).

If you pass an int as a value of size, it will set the smallest value between width and height and update the largest value between them to keep the same aspect ratio of the original image dim. Here is an example:

import torch
import torchvision

x = torch.rand(3,1024,512)
print(x.shape) # torch.Size([3, 1024, 512])

resize = torchvision.transforms.Resize(
    size=256
)

y = resize(x)
print(y.shape) # torch.Size([3, 512, 256])

ok but then the images will no longer be of the required dimension which is 512x512 or 256x256.But it does answer the original question which is to resize while preserving aspect ratio.
for example img shape is 1500x1001,after doing resize as proposed size comes out to be 767x512.
also checked the aspect ratio ,for the original image(1500x1001), AR is 1.4985014985014986 and after resizing it is 1.498046875

For second image shape is 1300x866 ,after resize size is 768x512.

So will this in turn have any effect on network performance,and secondly,what is your opinion on the difference in the aspect ratio.Is this much difference acceptable?

will this in turn have any effect on network performance

Resizing an image to a lower size will always cause some loss in the information that exists in the original image no matter what the aspect ratio is because it is reduced and approximated using the Interpolation method used. We usually reduce the size of images to a lower dimension so we can fit more examples into GPU or speed up the process of computing, so it is always something we do in deep learning. It depends on the application at hand, but we don’t typically pass the raw image size to our model because we expect a certain image size as an input so this standardization helps with that while we get a computation boost (can fit more examples into memory and compute faster).

what is your opinion on the difference in the aspect ratio.Is this much difference acceptable

I think a small variation in aspect ratio (1.4985014985014986 vs. 1.49609375, 0.0024) is acceptable and it isn’t the real problem when resizing. The real problem is how much you are resizing that will cause losing information from the original image. For example, if your origin image size is (1080, 1080) and you resized it to (244, 244), although you have downscaled your image size by 4.42 times, if you can still identify the object of interest as a human, most probably the model can learn from the visual features that you noticed.

1 Like

thanks for the clarification and your help with the resizing function.