Skip to content

[prototype] Clean up and port the resize kernel in V2 #6892

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

Merged
merged 6 commits into from
Nov 3, 2022
Merged
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
28 changes: 24 additions & 4 deletions torchvision/prototype/transforms/functional/_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import PIL.Image
import torch
from torch.nn.functional import interpolate
from torchvision.prototype import features
from torchvision.transforms import functional_pil as _FP, functional_tensor as _FT
from torchvision.transforms.functional import (
Expand Down Expand Up @@ -115,20 +116,37 @@ def resize_image_tensor(
max_size: Optional[int] = None,
antialias: bool = False,
) -> torch.Tensor:
align_corners: Optional[bool] = None
if interpolation == InterpolationMode.BILINEAR or interpolation == InterpolationMode.BICUBIC:
align_corners = False
elif antialias:
raise ValueError("Antialias option is supported for bilinear and bicubic interpolation modes only")

shape = image.shape
num_channels, old_height, old_width = shape[-3:]
new_height, new_width = _compute_resized_output_size((old_height, old_width), size=size, max_size=max_size)

if image.numel() > 0:
image = image.reshape(-1, num_channels, old_height, old_width)

image = _FT.resize(
dtype = image.dtype
need_cast = dtype not in (torch.float32, torch.float64)
if need_cast:
image = image.to(dtype=torch.float32)

image = interpolate(
image,
size=[new_height, new_width],
interpolation=interpolation.value,
mode=interpolation.value,
align_corners=align_corners,
antialias=antialias,
)

if need_cast:
if interpolation == InterpolationMode.BICUBIC and dtype == torch.uint8:
image = image.clamp_(min=0, max=255)
image = image.round_().to(dtype=dtype)
Comment on lines +146 to +148
Copy link
Collaborator

Choose a reason for hiding this comment

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

Reminder to self that I need to address this in #6830.


return image.reshape(shape[:-3] + (num_channels, new_height, new_width))


Expand Down Expand Up @@ -1312,9 +1330,11 @@ def resized_crop(

def _parse_five_crop_size(size: List[int]) -> List[int]:
if isinstance(size, numbers.Number):
size = [int(size), int(size)]
s = int(size)
size = [s, s]
elif isinstance(size, (tuple, list)) and len(size) == 1:
size = [size[0], size[0]]
s = size[0]
size = [s, s]

if len(size) != 2:
raise ValueError("Please provide only two dimensions (h, w) for size.")
Expand Down