Skip to content
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

Fast rotation for right angles #8295

Merged
merged 13 commits into from
Mar 13, 2024
Merged

Fast rotation for right angles #8295

merged 13 commits into from
Mar 13, 2024

Conversation

gau-nernst
Copy link
Contributor

@gau-nernst gau-nernst commented Mar 5, 2024

Fixes #8281

cc @vfdev-5

Copy link

pytorch-bot bot commented Mar 5, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/8295

Note: Links to docs will display an error until the docs builds have been completed.

❌ 3 New Failures

As of commit 4260a3b with merge base c7bcfad (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link

Hi @gau-nernst!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@gau-nernst
Copy link
Contributor Author

@NicolasHug I signed the CLA but the status did not change. Not sure what went wrong. Do I need to use the same email as my GitHub account? I signed the CLA with a different email. Then I tried again with my GitHub email. Still no changes here.

angle = angle % 360 # shift angle to [0, 360) range

# fast path: transpose without affine transform
if expand or center is None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

why if expand ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was reading the NOTE section of v2.RandomRotation, and it says that

setting center has no effect if expand=True

From what I understand, if expand=True, we can ignore center, and thus we can safely use torch.rot90().

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can remove expand from the check

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@gau-nernst
Copy link
Contributor Author

Regarding testing, I think we should test for angles 0, 90, 180, 270. Should I add them here?

class TestRotate:
_EXHAUSTIVE_TYPE_AFFINE_KWARGS = dict(
# float, int
angle=[-10.9, 18],
# None
# two-list of float, two-list of int, two-tuple of float, two-tuple of int
center=[None, [1.2, 4.9], [-3, 1], (2.5, -4.7), (3, 2)],
)

My concern is that it will create many more test cases.

The alternative is to write a separate test for angles 0, 90, 180, 270, and we don't use too many values for other parameters (center, expand, fill).

@NicolasHug
Copy link
Member

Thanks @gau-nernst

The alternative is to write a separate test for angles 0, 90, 180, 270, and we don't use too many values for other parameters (center, expand, fill).

Yes, we can probably create a small separate test to check these specific values, and simply test the output of PIL vs our tensor implementation

@gau-nernst gau-nernst requested a review from vfdev-5 March 12, 2024 11:34
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks a lot @gau-nernst for the PR and @vfdev-5 for the reviews. This is good to go on my side, let's just wait for @vfdev-5 's input before merging

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.

lgtm as well, thanks @gau-nernst !

angle = angle % 360 # shift angle to [0, 360) range

# fast path: transpose without affine transform
if center is None:
Copy link
Member

Choose a reason for hiding this comment

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

The tests are failing and I think it's because we should set expand to True. When expand is False we're not supposed to be changing the shape of the output (which is what rot90 does!).

Suggested change
if center is None:
if expand and center is None:

Copy link
Collaborator

Choose a reason for hiding this comment

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

Here is what does PIL here:
https://github.com/python-pillow/Pillow/blob/8f63748e50378424628155994efd7e0739a4d1d1/src/PIL/Image.py#L2287-L2296

Yes, I agree that it is incorrect to omit expand for image with h != w, sorry for an incorrect suggestion previously.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we also check for square input then? So even if expand is false, rot90 is still valid.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Following their code they: 1) accept angle 0 and 180 whatever provided expand value and 2) for rotations 90 or 270 they do if angle in (90, 270) and (expand or self.width == self.height).
I think this is reasonable and we can do the same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will update accordingly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the code. Also updated the test cases to cover everything. I just realized the tests didn't fail on my machine because I was running inside an environment with torchvision installed (big mistake!).

Copy link
Member

Choose a reason for hiding this comment

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

No worries @gau-nernst thanks for the update. I can confirm the tests are passing now (tried locally). let's just wait for the CI to be green before merging

@NicolasHug NicolasHug merged commit ba64d65 into pytorch:main Mar 13, 2024
72 of 75 checks passed
Copy link

Hey @NicolasHug!

You merged this PR, but no labels were added.
The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

facebook-github-bot pushed a commit that referenced this pull request Mar 21, 2024
Summary: Co-authored-by: Thien Tran <[email protected]>

Reviewed By: vmoens

Differential Revision: D55062767

fbshipit-source-id: b67a7f7fecab8a33143b95e6233637c336c74cd4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: done
Development

Successfully merging this pull request may close these issues.

Fast rotation for 90, 180, and 270 degree
5 participants