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

Fixed resize to only match the smaller edge when an int is given #2518

Merged
merged 2 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/test_functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def test_resize(self):
if dt is not None:
# This is a trivial cast to float of uint8 data to test all cases
tensor = tensor.to(dt)
for size in [32, [32, ], [32, 32], (32, 32), ]:
for size in [32, 26, [32, ], [32, 32], (32, 32), [26, 35]]:
for interpolation in [BILINEAR, BICUBIC, NEAREST]:
resized_tensor = F_t.resize(tensor, size=size, interpolation=interpolation)
resized_pil_img = F_pil.resize(pil_img, size=size, interpolation=interpolation)
Expand Down
4 changes: 2 additions & 2 deletions test/test_transforms_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def test_resize(self):
if dt is not None:
# This is a trivial cast to float of uint8 data to test all cases
tensor = tensor.to(dt)
for size in [32, [32, ], [32, 32], (32, 32), ]:
for size in [32, 34, [32, ], [32, 32], (32, 32), [34, 35]]:
for interpolation in [BILINEAR, BICUBIC, NEAREST]:

resized_tensor = F.resize(tensor, size=size, interpolation=interpolation)
Expand All @@ -250,7 +250,7 @@ def test_resized_crop(self):

for scale in [(0.7, 1.2), [0.7, 1.2]]:
for ratio in [(0.75, 1.333), [0.75, 1.333]]:
for size in [(32, ), [32, ], [32, 32], (32, 32)]:
for size in [(32, ), [44, ], [32, ], [32, 32], (32, 32), [44, 55]]:
for interpolation in [NEAREST, BILINEAR, BICUBIC]:
transform = T.RandomResizedCrop(
size=size, scale=scale, ratio=ratio, interpolation=interpolation
Expand Down
4 changes: 2 additions & 2 deletions torchvision/transforms/functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ def resize(img: Tensor, size: List[int], interpolation: int = 2) -> Tensor:
else:
size_w = int(size_h * w / h)

if (w <= h and w == size_w) or (h <= w and h == size_h):
return img
Comment on lines -589 to -590
Copy link
Collaborator

Choose a reason for hiding this comment

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

@vfdev-5 Could you explain what your intention for this guarding clause was? Is this just to detect the case if no resizing needs to happen? If yes, why not simply do

if (w,h) == (size_w, size_h):
    return 

Copy link
Contributor Author

@ag14774 ag14774 Jul 30, 2020

Choose a reason for hiding this comment

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

I think the intention is that (according to the function docstring), if only one number is given as size, we resize to match the smaller dimension to that number. The check there is for the case when the smaller dimension matches the number given and no resizing needs to happen. But it doesn't mean that both dims need to match

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry for delayed reply! Yes, this function should mimic PIL implementation : https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional_pil.py#L324 where definitely this condition is applied in case when if isinstance(size, int) or len(size) == 1. Thanks for the fix @ag14774 !

if (w <= h and w == size_w) or (h <= w and h == size_h):
return img

# make image NCHW
need_squeeze = False
Expand Down