Torch vision cropping behavior when crop region larger than image itself

Zhiqiu_Lin: <p>Hey! I would like to know the default behavior of the CenterCrop() or RandomCrop() in torch vision library. If an image is smaller than the specified crop region, what will be returned as a result?</p>

ptrblck: <p>While <code>CenterCrop</code> apparently adds zero-padding to the borders, <code>RandomCrop</code> fails:</p>
<pre><code class="lang-python">import torchvision.transforms as transforms
import torchvision.transforms.functional as TF

x = TF.to_pil_image(torch.randn(3, 24, 24))
transforms.CenterCrop(50)(x)
transforms.RandomCrop(50)(x)
</code></pre>

saurabheights: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="21654">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<pre><code class="lang-auto">transforms.CenterCrop(50)(x)
</code></pre>
</blockquote>
</aside>
<p>Dear,<br>
CenterCrop has different behaviour for PIL and tensor image.  In case of PIL image, it pads the image, while for tensor, it crops incorrectly[Checked code, it just computes offset(which are negative) and uses tensor indexing to extract the crop area which is incorrect.</p>
<p>For RandomCrop, api behaviour is consistent for both PIL and np.array for default arguments.</p>
<p>The code to reproduce CenterCrop inconsistency with output:-</p>
<pre><code class="lang-auto">pil_x = TF.to_pil_image(torch.randn(3, 24, 24))
transforms.CenterCrop(50)(pil_x).size
(50, 50)
tensor_x = torch.randn(3, 24, 24)
transforms.CenterCrop(50)(tensor_x).shape
torch.Size([3, 12, 12])
transforms.RandomCrop(50)(pil_x).size
Traceback (most recent call last):
  File "/home/skhanduja/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/203.6682.179/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "&lt;input&gt;", line 1, in &lt;module&gt;
  File "/home/skhanduja/miniconda3/envs/wsss/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/skhanduja/miniconda3/envs/wsss/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 585, in forward
    i, j, h, w = self.get_params(img, self.size)
  File "/home/skhanduja/miniconda3/envs/wsss/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 541, in get_params
    raise ValueError(
ValueError: Required crop size (50, 50) is larger then input image size (24, 24)
transforms.RandomCrop(50)(tensor_x).size
Traceback (most recent call last):
  File "/home/skhanduja/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/203.6682.179/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "&lt;input&gt;", line 1, in &lt;module&gt;
  File "/home/skhanduja/miniconda3/envs/wsss/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/skhanduja/miniconda3/envs/wsss/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 585, in forward
    i, j, h, w = self.get_params(img, self.size)
  File "/home/skhanduja/miniconda3/envs/wsss/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 541, in get_params
    raise ValueError(
ValueError: Required crop size (50, 50) is larger then input image size (24, 24)
</code></pre>
<p>Posting here so others who come here, dont make the mistake. Also, will it be alright if I add GITHUB Issue for this?</p>
<p>P.S. Congrats on 10k replies on forum. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Update - The issue link is <a href="https://github.com/pytorch/vision/issues/3297" class="inline-onebox" rel="noopener nofollow ugc">Inconsistent API for tensor and PIL image for CenterCrop · Issue #3297 · pytorch/vision · GitHub</a></p>

ptrblck: <p>Thanks for checking this issue and narrowing it down!</p>
<aside class="quote no-group" data-username="saurabheights" data-post="3" data-topic="21654">
<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/saurabheights/48/2923_2.png" class="avatar"> saurabheights:</div>
<blockquote>
<p>Also, will it be alright if I add GITHUB Issue for this?</p>
</blockquote>
</aside>
<p>Yes, please create an issue in the <a href="https://github.com/pytorch/vision/issues"><code>torchvision</code> repo</a>.</p>
<aside class="quote no-group" data-username="saurabheights" data-post="3" data-topic="21654">
<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/saurabheights/48/2923_2.png" class="avatar"> saurabheights:</div>
<blockquote>
<p>P.S. Congrats on 10k replies on forum. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>Thanks <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>