Skip to content
10 changes: 10 additions & 0 deletions torchvision/transforms/v2/functional/_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,16 @@ def rotate_image(
center: Optional[List[float]] = None,
fill: _FillTypeJIT = None,
) -> torch.Tensor:
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

if angle == 0:
return image.clone()

if angle in (90, 180, 270):
return torch.rot90(image, k=angle // 90, dims=(-1, -2))

interpolation = _check_interpolation(interpolation)

input_height, input_width = image.shape[-2:]
Expand Down