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

transforms v2 rotate can create different sizes between tensors and PIL with expand=True #7714

Closed
pmeier opened this issue Jul 3, 2023 · 2 comments · Fixed by #7715
Closed

Comments

@pmeier
Copy link
Collaborator

pmeier commented Jul 3, 2023

import torch
from torchvision.transforms.v2 import functional as F

input_tensor = torch.rand(3, 17, 11)
input_pil = F.to_pil_image(input_tensor)

kwargs = dict(angle=-10.9, center=(1.2, 4.9), expand=True)

output_tensor = F.rotate(input_tensor, **kwargs)
output_pil = F.rotate(input_pil, **kwargs)

shape_tensor = F.get_image_size(output_tensor)
shape_pil = F.get_image_size(output_pil)

assert shape_tensor == shape_pil, f"{shape_tensor} != {shape_pil}"
AssertionError: [15, 20] != [15, 19]

Replacing the import with from torchvision.transforms import functional as F, i.e. using transforms v1, does not have this problem.

@pmeier
Copy link
Collaborator Author

pmeier commented Jul 3, 2023

We had some subtle issues here before, which supposedly should have been fixed by #7271.

@pmeier
Copy link
Collaborator Author

pmeier commented Jul 3, 2023

PIL kernel is the culprit:

import torch
from torchvision.transforms.v2 import functional as F

input_tensor = torch.rand(3, 17, 11)
input_pil = F.to_pil_image(input_tensor)

kwargs = dict(angle=-10.9, expand=True)

output_w_center = F.rotate(input_tensor, **kwargs, center=(1.2, 4.9))
output_wo_center = F.rotate(input_pil, **kwargs, center=None)

shape_w_center = F.get_image_size(output_w_center)
shape_wo_center = F.get_image_size(output_wo_center)

assert shape_w_center == shape_wo_center, f"{shape_w_center} != {shape_wo_center}"
AssertionError: [15, 20] != [15, 19]

Meaning, #7271 did its job, but we need to expand it to the PIL kernel as well.

The issue is that center theoretically should have no effect on the output size if expand=True is given. Practically, rounding effects take place which lead the off-by-one errors. The difference between v1 and v2 is that we not only warn users about setting both parameters

if center is not None and expand:
warnings.warn("The provided center argument has no effect on the result if expand is True")

we also explicitly unset center in this case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant