Repository navigation
Use rot90 for right-angle tensor rotations in transforms.functional.rotate - #9749
Open
davrens101 wants to merge 1 commit into
Open
davrens101 wants to merge 1 commit into
davrens101 wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9749
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
torchvision.transforms.functional.rotateon a tensor withexpand=Truereturns an output that is one pixel too large in each dimension for exact right-angle rotations when height and width differ by an odd number. PIL and the v2rotatereturn the exact transposed size. This also affectstransforms.RandomRotation((90, 90), expand=True)on tensors.Seen on current main (4260301):
The cause is that the v1 tensor path always goes through
_compute_affine_output_size, which shifts the rotated corners by the unrotated half-extents. For a 90 degree turn of a 64x33 image the corners land on half-pixel positions, soceil(max) - floor(min)gives 65x34. PIL special-cases right angles with a transpose, and the v2rotate_imagealready has the same fast path (#8295), so v1 was the odd one out.This change adds the same fast path to the tensor branch of
transforms.functional.rotate: forcenter is None, angles of 0 and 180 degrees, and 90/270 degrees whenexpand=Trueor the image is square, it usestorch.rot90(orclone()for 0). Other cases still go through the affine grid.torch.rot90is scriptable and the scripted function is covered in the new test.Tests: I added
test_rotate_right_anglestotest/test_functional_tensor.py. It compares eager and scriptedrotateagainst PIL for angles 0, 90, 180, 270, -90, 450 withexpandon and off, on 33x64, 26x26 and 16x20 images. Shape is always compared; pixel equality is checked whenexpand=True, when the image is square, or when the angle is a multiple of 180 (non-squareexpand=Falsequarter turns can't be exact against PIL, which rotates and crops, and that path is unchanged).test/test_functional_tensor.pyin full with the fix: 11307 passed, 11305 skipped.test/test_transforms.py -k rotat: 6 passed.Limits: CPU only, CUDA tests were skipped. I ran against a CPU wheel (0.29.2) with the Python sources from main copied over it, since I can't build the C++ extension on this machine;
ufmtitself was not run, flake8 and black were. Behaviour change to note: right-angle tensor rotations are now exact transposes instead of nearest-neighbour grid samples, matching what v2 and PIL already do.I used an AI coding assistant to write and test this change, and the numbers above come from its runs on my machine.