Torch vision cropping behavior when crop region larger than image itself

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?

1 Like

While CenterCrop apparently adds zero-padding to the borders, RandomCrop fails:

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)
1 Like

Dear,
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.

For RandomCrop, api behaviour is consistent for both PIL and np.array for default arguments.

The code to reproduce CenterCrop inconsistency with output:-

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 "<input>", line 1, in <module>
  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 "<input>", line 1, in <module>
  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)

Posting here so others who come here, dont make the mistake. Also, will it be alright if I add GITHUB Issue for this?

P.S. Congrats on 10k replies on forum. :slight_smile:

Update - The issue link is Inconsistent API for tensor and PIL image for CenterCrop · Issue #3297 · pytorch/vision · GitHub

Thanks for checking this issue and narrowing it down!

Yes, please create an issue in the torchvision repo.

Thanks :slight_smile:

1 Like