Why RandomResizedCrop always selects the same spot?

Mehran_Ziadloo: <p>I’m trying to crop a part of the image randomly and it seems to me the RandomResizedCrop class fits the bill perfectly. It’s just that for some reason, across different runs, the transformation always picks the exact same spot from the original image and that is the middle section. I made sure that I’m not setting any seeds for the random packages and I don’t know what I’m doing wrong.</p>
<p>This is the code I have:</p>
<pre data-code-wrap="python"><code class="lang-python">from torchvision.io import read_image
from torchvision.transforms import v2
import matplotlib.pyplot as plt


img = read_image("./some_image.png")
t = v2.Compose([
    lambda x: x / 255.0,
    v2.RandomResizedCrop(size=64),
])
v = t(img)

plt.imshow(img.permute(1, 2, 0))
plt.show()

plt.imshow(v.permute(1, 2, 0))
plt.show()
</code></pre>

ptrblck: <p>I cannot reproduce the issue and get different crops when closing the terminal and rerunning the script.</p>

Mehran_Ziadloo: <p>Could you please test it with this image? It’s one sample from IAM Handwritten text dataset:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/5/55e3ba064f789aa311a216a419e3781094cadea0.png" data-download-href="https://discuss.pytorch.org/uploads/default/55e3ba064f789aa311a216a419e3781094cadea0" title="a01-000u-s00-00"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55e3ba064f789aa311a216a419e3781094cadea0_2_690x36.png" alt="a01-000u-s00-00" data-base62-sha1="cfOucqCXnaXP173UZpVKvxOJFaU" width="690" height="36" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55e3ba064f789aa311a216a419e3781094cadea0_2_690x36.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55e3ba064f789aa311a216a419e3781094cadea0_2_1035x54.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55e3ba064f789aa311a216a419e3781094cadea0_2_1380x72.png 2x" data-dominant-color="E0E0E0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">a01-000u-s00-00</span><span class="informations">1661×89 32.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Maybe it has something to do with the image size!? For me it always returns:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/d/6d64b90b32efeec7012b62ef9693a42bb85e8ac4.png" alt="image" data-base62-sha1="fBJQcq4BvJFVRw5qtWwYAU1IFik" width="416" height="415"></p>
<p>By always I mean when I rerun the same Jupyer notebook cell. And I’m running the same exact code above (just changing the file path).</p>

Mehran_Ziadloo: <p>For the record, I just tested the code with the sample image in a Google Colab and I can reproduce the problem. I created two cells with the same code and both of them output the same crop of the image (depicted above). Either my expectations are not aligned or this is a bug. It would be great if someone could please confirm one or the other.</p>

ptrblck: <p>I can reproduce the central crop using your flat image and it seems <a href="https://github.com/pytorch/vision/blob/423a1b0ebdea077cc69478812890845741048d2e/torchvision/transforms/transforms.py#L937-L947">this random sampling</a> fails giving e.g.:</p>
<pre data-code-wrap="python"><code class="lang-python">failed with: w: 114, h: 130, width: 1661, height: 89
failed with: w: 341, h: 349, width: 1661, height: 89
failed with: w: 190, h: 221, width: 1661, height: 89
failed with: w: 356, h: 282, width: 1661, height: 89
failed with: w: 345, h: 342, width: 1661, height: 89
failed with: w: 362, h: 336, width: 1661, height: 89
failed with: w: 124, h: 112, width: 1661, height: 89
failed with: w: 358, h: 291, width: 1661, height: 89
failed with: w: 346, h: 332, width: 1661, height: 89
failed with: w: 410, h: 332, width: 1661, height: 89
</code></pre>
<p>after adding some debug <code>print</code> statements.<br>
In cast the sampled <code>h</code> is too large. You might thus need to reduce the <code>scale</code>.</p>