Skip to content

Make crop work the same for pil and tensor #3770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 17, 2021
Merged

Make crop work the same for pil and tensor #3770

merged 9 commits into from
May 17, 2021

Conversation

jaesuny
Copy link
Contributor

@jaesuny jaesuny commented May 4, 2021

In the transforms.functional.crop, if the cropping area is out of the image, in the case of pil, the box will be padded by zeros, whereas in the tensor, only the inside of the image will be returned.

See this notebook.

In this PR functional_tensor.crop pad if needed.

Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @jaesuny , I think we can avoid calling pad all the times. Let's improve the code like that.
I think documentation update would be also needed ...

Comment on lines 129 to 130
padding = [max(-left, 0), max(-top, 0), max(right - w, 0), max(bottom - h, 0)]
return pad(img[..., top:top + height, left:left + width], padding)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's only call pad if needed like here: https://github.com/pytorch/vision/pull/3333/files

@jaesuny
Copy link
Contributor Author

jaesuny commented May 6, 2021

Thanks for the PR @jaesuny , I think we can avoid calling pad all the times. Let's improve the code like that.
I think documentation update would be also needed ...

Thanks for the review @vfdev-5 . Updated based on your feedback.

@fmassa fmassa self-requested a review May 11, 2021 09:21
Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I have a few comments, let me know what you think

right = left + width
bottom = top + height

if left < 0 or top < 0 or right > w or bottom < h:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this condition for bottom correct? I would have imagined that it should be similar than the one for right (which uses a > instead of a <).

Copy link
Contributor Author

@jaesuny jaesuny May 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed it. Fixed this.
Thank you!

@@ -188,6 +188,10 @@ def test_crop(self):
'crop', 'RandomCrop', fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs
)

# Test transforms.functional.crop including outside the image area
fn_kwargs = {"top": -2, "left": 8, "height": 4, "width": 5}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add more tests exercising other use-cases, like when top is negative and height + top is larger than the image size?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests considering all directions!

@fmassa
Copy link
Member

fmassa commented May 17, 2021

Thanks for the changes!

Lint is failing in https://app.circleci.com/pipelines/github/pytorch/vision/8117/workflows/29ea8926-e7f7-448b-b705-53905de1de37/jobs/580130 with the following message

./test/test_transforms_tensor.py:192:68: E261 at least two spaces before inline comment
./test/test_transforms_tensor.py:195:68: E261 at least two spaces before inline comment
./test/test_transforms_tensor.py:198:67: E261 at least two spaces before inline comment
./test/test_transforms_tensor.py:201:67: E261 at least two spaces before inline comment
./test/test_transforms_tensor.py:204:71: E261 at least two spaces before inline comment

, but once you fix it the PR is good to merge.

Copy link
Contributor

@datumbox datumbox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appeasing the formatter gods.

@datumbox datumbox merged commit 6397190 into pytorch:master May 17, 2021
facebook-github-bot pushed a commit that referenced this pull request May 19, 2021
Summary:
* Make crop work the same for pil and tensor

* Only call pad if needed in functional_tensor.crop

* Fix top-left functional_tensor.crop

* Update document for functional.crop

* Add other test cases of functional.crop

* Fix bug

* Fixing formattter

* Fix stylings

Reviewed By: cpuhrsch

Differential Revision: D28538755

fbshipit-source-id: d38117a80ac6985e3f6a42863ee16903b6f5cea3

Co-authored-by: Vasilis Vryniotis <datumbox@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants