-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Conversation
There was a problem hiding this 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 ...
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) |
There was a problem hiding this comment.
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
There was a problem hiding this 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: |
There was a problem hiding this comment.
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 <
).
There was a problem hiding this comment.
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!
test/test_transforms_tensor.py
Outdated
@@ -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} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
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
, but once you fix it the PR is good to merge. |
There was a problem hiding this 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.
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>
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.