question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent API for tensor and PIL image for CenterCrop

See original GitHub issue

🐛 Bug

CenterCrop transform crops image center. However, when crop size is smaller than image size(in any dimension), the results are different for PIL.Image input and tensor input.

For PIL.Image, it does padding, while for tensor, the results are completely wrong, due to incorrect crop location computation(negative position values computed).

To Reproduce

Steps to reproduce the behavior:

import torch
import torchvision.transforms as transforms
import torchvision.transforms.functional as TF

pil_x = TF.to_pil_image(torch.randn(3, 24, 24))  
transforms.CenterCrop(50)(pil_x).size  # PIL image size is 50x50

tensor_x = torch.randn(3, 24, 24)
transforms.CenterCrop(50)(tensor_x).shape  # Tensor shape is torch.Size([3, 12, 12])

Expected behavior

Mainly, API should be consistent for both inputs or there should be two different methods for PIL & Tensor input(which is IMO less appealing). Side note - docs should be updated for what happens when crop_size is greater than image size. This condition, I believe is missing in documentation of other crop methods as well.

Environment

Collecting environment information...
PyTorch version: 1.7.0
Is debug build: True
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.10.2

Python version: 3.8 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: Quadro T2000
Nvidia driver version: 450.102.04
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.2
[pip3] torch==1.7.0
[pip3] torchaudio==0.7.0a0+ac17b64
[pip3] torchvision==0.8.1
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               10.2.89              hfd86e86_1  
[conda] mkl                       2020.2                      256  
[conda] mkl-service               2.3.0            py38he904b0f_0  
[conda] mkl_fft                   1.2.0            py38h23d657b_0  
[conda] mkl_random                1.1.1            py38h0573a6f_0  
[conda] numpy                     1.19.2           py38h54aff64_0  
[conda] numpy-base                1.19.2           py38hfa32c7d_0  
[conda] pytorch                   1.7.0           py3.8_cuda10.2.89_cudnn7.6.5_0    pytorch
[conda] torchaudio                0.7.0                      py38    pytorch
[conda] torchvision               0.8.1                py38_cu102    pytorch

cc @vfdev-5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
vfdev-5commented, Jan 26, 2021

Sounds good! Please, take a look at our contributing guide which may help you with the dev setup etc. A draft PR is also good such that we can iterate over the implementation if needed and do not hesitate to ask questions here for details.

1reaction
saurabheightscommented, Jan 26, 2021

No worries and thank you for pad_symmetric, it would work fine here.

Tested on PIL version ‘6.2.1’(found this is too old, so retested on newer versions 😄) and ‘7.2.0’. And yep, there was padding in both.

Regarding ETA - I will hopefully make changes in 3/4 days(its only few hour work but working first time on vision lib, so might get delayed in setup by a little).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Torch vision cropping behavior when crop region larger than ...
Dear, CenterCrop has different behaviour for PIL and tensor image. In case of PIL image, it pads the image, while for tensor, it...
Read more >
Getting the 'TypeError: pic should be PIL Image or ndarray ...
ToTensor converts a PIL Image or numpy.ndarray to tensor. ... first convert the numpy array to a PIL Image object using transforms.
Read more >
tf.image.resize | TensorFlow v2.11.0
Resize images to size using the specified method. ... TensorFlow · API · TensorFlow v2.11.0 · Python. Was this helpful?
Read more >
How to crop an image at center in PyTorch? - GeeksforGeeks
We can crop an image in PyTorch by using the CenterCrop() method. This method accepts images like PIL Image, Tensor Image, and a...
Read more >
The Devil lives in the details | capeblog
io.images.read_image and the torchvision.transforms.Resize . This transform can accept PIL.Image.Image or Tensors, in short ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found