Skip to content

Commit b8de0b8

Browse files
authored
Recoded gaussian blur without using to_tensor (#5503)
1 parent 60449a4 commit b8de0b8

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

torchvision/prototype/transforms/functional/_misc.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import PIL.Image
44
import torch
55
from torchvision.transforms import functional_tensor as _FT
6-
from torchvision.transforms.functional import to_tensor, to_pil_image
6+
from torchvision.transforms.functional import pil_to_tensor, to_pil_image
77

88

99
normalize_image_tensor = _FT.normalize
@@ -39,4 +39,6 @@ def gaussian_blur_image_tensor(
3939

4040

4141
def gaussian_blur_image_pil(img: PIL.Image, kernel_size: List[int], sigma: Optional[List[float]] = None) -> PIL.Image:
42-
return to_pil_image(gaussian_blur_image_tensor(to_tensor(img), kernel_size=kernel_size, sigma=sigma))
42+
t_img = pil_to_tensor(img)
43+
output = gaussian_blur_image_tensor(t_img, kernel_size=kernel_size, sigma=sigma)
44+
return to_pil_image(output, mode=img.mode)

torchvision/transforms/functional.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,8 @@ def resize(
403403
mode).
404404
antialias (bool, optional): antialias flag. If ``img`` is PIL Image, the flag is ignored and anti-alias
405405
is always used. If ``img`` is Tensor, the flag is False by default and can be set to True for
406-
``InterpolationMode.BILINEAR`` only mode. This can help making the output for PIL images and tensors
407-
closer.
408-
409-
.. warning::
410-
There is no autodiff support for ``antialias=True`` option with input ``img`` as Tensor.
406+
``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` modes.
407+
This can help making the output for PIL images and tensors closer.
411408
412409
Returns:
413410
PIL Image or Tensor: Resized image.
@@ -1338,12 +1335,12 @@ def gaussian_blur(img: Tensor, kernel_size: List[int], sigma: Optional[List[floa
13381335
if not F_pil._is_pil_image(img):
13391336
raise TypeError(f"img should be PIL Image or Tensor. Got {type(img)}")
13401337

1341-
t_img = to_tensor(img)
1338+
t_img = pil_to_tensor(img)
13421339

13431340
output = F_t.gaussian_blur(t_img, kernel_size, sigma)
13441341

13451342
if not isinstance(img, torch.Tensor):
1346-
output = to_pil_image(output)
1343+
output = to_pil_image(output, mode=img.mode)
13471344
return output
13481345

13491346

0 commit comments

Comments
 (0)