Skip to content

F.crop behavior diverges between tensor and PIL images #6613

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

Closed
pmeier opened this issue Sep 20, 2022 · 2 comments · Fixed by #6615
Closed

F.crop behavior diverges between tensor and PIL images #6613

pmeier opened this issue Sep 20, 2022 · 2 comments · Fixed by #6615

Comments

@pmeier
Copy link
Collaborator

pmeier commented Sep 20, 2022

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

image_tensor = torch.rand(3, 7, 33)
image_pil = F.to_image_pil(image_tensor)

print(
    F.get_spatial_size(F.crop(image_tensor, top=9, left=9, height=20, width=12)),
    F.get_spatial_size(F.crop(image_pil, top=9, left=9, height=20, width=12)),
)
[22, 12] [20, 12]

cc @vfdev-5 @datumbox @bjuncek

@pmeier
Copy link
Collaborator Author

pmeier commented Sep 20, 2022

Actually, this is not a prototype issue:

import torch
from torchvision.transforms import functional as F

image_tensor = torch.rand(3, 7, 33)
image_pil = F.to_pil_image(image_tensor)

print(
    F.get_image_size(F.crop(image_tensor, top=9, left=9, height=20, width=12)),
    F.get_image_size(F.crop(image_pil, top=9, left=9, height=20, width=12)),
)
[12, 22] [12, 20]

@pmeier pmeier removed the prototype label Sep 20, 2022
@pmeier pmeier changed the title prototype F.pad behavior diverges between tensor and PIL images F.pad behavior diverges between tensor and PIL images Sep 20, 2022
@vfdev-5 vfdev-5 changed the title F.pad behavior diverges between tensor and PIL images F.crop behavior diverges between tensor and PIL images Sep 20, 2022
@vfdev-5 vfdev-5 self-assigned this Sep 20, 2022
@vfdev-5
Copy link
Collaborator

vfdev-5 commented Sep 20, 2022

Seems like a bug in padding_ltrb, it may have to be something like :

        padding_ltrb = [
-            max(-left, 0), 
-            max(-top, 0), 
+            max(-left + min(right, 0), 0),
+            max(-top + min(bottom, 0), 0), 
-            max(right - w, 0), 
+            max(right - max(w, left), 0), 
-            max(bottom - h, 0), 
+            max(bottom - max(h, top), 0),
        ]        

This will also fix the issue with too negative top-left corner and small crop size:

import torch
from torchvision.transforms import functional as F

image_tensor = torch.arange(5 * 5, dtype=torch.float).reshape(1, 5, 5)
image_pil = F.to_pil_image(image_tensor)

o1 = F.crop(image_tensor, top=-10, left=-10, height=3, width=3)
o2 = F.crop(image_pil, top=-10, left=-10, height=3, width=3)
o1.shape, o2.size
> (torch.Size([1, 10, 10]), (3, 3))

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.

2 participants