pytorch / pytorch/vision

CenterCrop is incompatible with torch.jit.trace

Open
#7,705 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

CenterCrop is not traceable

from torchvision.transforms import CenterCrop

c = CenterCrop(size=(112, 112))
torch.jit.trace(c, example_inputs=torch.ones((200, 200)))
TypeError: type Tensor doesn't define __round__ method

The reason being these 2 lines in torchvision.transforms.functional.py (line 590)

crop_top = int(round((image_height - crop_height) / 2.0))
crop_left = int(round((image_width - crop_width) / 2.0))

rewriting this with the val // 2 operator instead of int(round(val / 2.0)) will have exactly the same output, be simpler and traceable.

crop_top = (image_height - crop_height) // 2
crop_left = (image_width - crop_width) // 2

proof:
There are 2 situations:

  • val is even
  • val is odd

if even the result will be the same because val / 2.0 == something.0 and if odd val / 2.0 == something.5, round rounds down in that case so it will be the same too.

>>> round(2.5)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in torchvision.transforms.functional.py around line 590, where CenterCrop computes crop_top and crop_left, and reproduce the issue with the torch.jit.trace example from the report. Verify that the revised calculation allows tracing to complete and preserves the expected crop behavior for even and odd differences.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
computer-vision
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.