Resizing with single value after padsquare(from augly) gives RuntimeError: stack expects each tensor to be equal size

John_J_Watson: <p>I am facing a bit of a bizzare problem: I have a bunch of different sized images and I am trying to train+infer on these, and I have the following example transform code:</p>
<pre><code class="lang-auto">        self.infer_transform = transforms.Compose([
            imaugs.PadSquare(p=1),
            transforms.Resize([384], interpolation=torchvision.transforms.InterpolationMode.BICUBIC),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
</code></pre>
<p>When I use a batchsize &gt;1 I get thrown this:</p>
<pre><code class="lang-auto">RuntimeError: stack expects each tensor to be equal size, but got [3, 384, 384] at entry 0 and [3, 385, 384] at entry 3
</code></pre>
<p>I find this really bizarre, since, after PadSquare, when I resize using a single int, it should give me a square image back - but it seems like it does not… why is this? is this a bug? It almost seems like some round-off error (got [3, 384, 384] at entry 0 and [3, 385, 384]).</p>
<p>Hoever, if I do this:</p>
<pre><code class="lang-auto">        self.infer_transform = transforms.Compose([
            imaugs.PadSquare(p=1),
            transforms.Resize((384,384), interpolation=torchvision.transforms.InterpolationMode.BICUBIC),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
</code></pre>
<p>it works fine…</p>
<p>What is the reason behind this? I am perplexed! When I try out sample images in say colab, they seem to have the same size…</p>

ptrblck: <aside class="quote no-group" data-username="John_J_Watson" data-post="1" data-topic="134296">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/john_j_watson/48/16421_2.png" class="avatar"> John_J_Watson:</div>
<blockquote>
<p>find this really bizarre, since, after PadSquare, when I resize using a single int, it should give me a square image back - but it seems like it does not… why is this? is this a bug?</p>
</blockquote>
</aside>
<p>No, it’s not a bug and is expected behavior for non-square input images.<br>
From the <a href="https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.Resize">docs</a>:</p>
<blockquote>
<p><strong>size</strong> (<em>sequence</em> <em>or</em> <a href="https://docs.python.org/3/library/functions.html#int"><em>int</em></a>) –<br>
Desired output size. If size is a sequence like (h, w), output size will be matched to this. 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).</p>
</blockquote>
<p>Using the <code>tuple</code> is thus the right approach for non-square inputs.</p>

John_J_Watson: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> pardon my ignorance, but because I padSquare first, which gives me a square image, the input to the next compose (Resize) is square - and therefore an int should be fine - is it not the case? <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=10" title=":frowning:" class="emoji" alt=":frowning:"></p>

ptrblck: <p>Yes, you are right. If you are padding the image so that it has the same height and width, a single value in <code>Resize</code> should work.<br>
Could you add this <code>my_print</code> transformation to check which shapes are used?</p>
<pre><code class="lang-python">def my_print(img):
    print(np.array(img).shape)
    return img

transform = transforms.Compose([
    transforms.Lambda(lambda x: my_print(x)),
    imaugs.PadSquare(p=1),
    transforms.Lambda(lambda x: my_print(x)),
    transforms.Resize([384], interpolation=transforms.InterpolationMode.BICUBIC),
    transforms.Lambda(lambda x: my_print(x)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
</code></pre>

John_J_Watson: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> thank you for this <code>my_print</code> func - I will try this as soon as the gpus become free. What I see is that for some reason, Ihave an extra pixel across, so I seem to get [3.385,385] instead of [3,384,385]. Currently, I just centercrop it to 384 and resolve the issue, but yes something does not add up!</p>
<p>Will get back with results.</p>
<p>Thank you again. Your help is invaluable.</p>