Resize images while maintaining the aspect ratio

username: sparshgarg23
cooked: <p>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.</p>
<p>As per the tutorial on semantic segmentation in albumentations ,it’s mentioned that</p>
<pre><code class="lang-auto">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
</code></pre>

username: sudomaze
cooked: <p><code>torchvision.transforms.Resize</code> will apply resize based on the passed <code>size</code> value. Here is an example:</p>
<pre><code class="lang-python">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)
</code></pre>
<pre><code class="lang-bash">&gt;&gt;&gt; torch.Size([3, 1080, 1080])
&gt;&gt;&gt; torch.Size([3, 512, 512])
</code></pre>
<p><a href="https://pytorch.org/vision/main/generated/torchvision.transforms.Resize.html" rel="noopener nofollow ugc">Check the doc for more information</a></p>

username: sparshgarg23
cooked: <p>thanks for replying back,but does resize maintain the original aspect ratio.The docs doesn’t mention anything about aspect ratio</p>

username: sudomaze
cooked: <p>In the docs:</p>
<pre><code class="lang-markdown">If size is an int, smaller edge of the image will be matched to this number. i.e, if height &gt; width, then image will be rescaled to (size * height / width, size).
</code></pre>
<p>If you pass an <code>int</code> as a value of <code>size</code>, it will set the smallest value between <code>width</code> and <code>height</code> and update the largest value between them to keep the same aspect ratio of the original image dim. Here is an example:</p>
<pre><code class="lang-python">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])
</code></pre>

username: sparshgarg23
cooked: <p>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.<br>
for example img shape is 1500x1001,after doing resize as proposed size comes out to be 767x512.<br>
also checked the aspect ratio ,for  the original image(1500x1001), AR is 1.4985014985014986 and after resizing it is  1.498046875</p>
<p>For second image shape is 1300x866 ,after resize size is  768x512.</p>
<p>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?</p>

username: sudomaze
cooked: <blockquote>
<p>will this in turn have any effect on network performance</p>
</blockquote>
<p>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).</p>
<blockquote>
<p>what is your opinion on the difference in the aspect ratio.Is this much difference acceptable</p>
</blockquote>
<p>I think a small variation in aspect ratio (<code>1.4985014985014986</code> vs. <code>1.49609375</code>, <code>0.0024</code>) 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.</p>

username: sparshgarg23
cooked: <p>thanks for the clarification and your help with the resizing function.</p>