From 7874b06e2cf795e6c40a512488ba1912fbb8f801 Mon Sep 17 00:00:00 2001 From: Kyle Matoba <22180455+kylematoba@users.noreply.github.com> Date: Wed, 27 Apr 2022 12:06:11 +0200 Subject: [PATCH 1/6] Update transforms for PIL deprecation --- torchvision/transforms/functional_pil.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torchvision/transforms/functional_pil.py b/torchvision/transforms/functional_pil.py index 5362ebd593c..6edad55fd5b 100644 --- a/torchvision/transforms/functional_pil.py +++ b/torchvision/transforms/functional_pil.py @@ -54,7 +54,7 @@ def hflip(img: Image.Image) -> Image.Image: if not _is_pil_image(img): raise TypeError(f"img should be PIL Image. Got {type(img)}") - return img.transpose(Image.FLIP_LEFT_RIGHT) + return img.transpose(Image.Transpose.FLIP_LEFT_RIGHT) @torch.jit.unused @@ -62,7 +62,7 @@ def vflip(img: Image.Image) -> Image.Image: if not _is_pil_image(img): raise TypeError(f"img should be PIL Image. Got {type(img)}") - return img.transpose(Image.FLIP_TOP_BOTTOM) + return img.transpose(Image.Transpose.FLIP_TOP_BOTTOM) @torch.jit.unused @@ -323,7 +323,7 @@ def affine( output_size = img.size opts = _parse_fill(fill, img) - return img.transform(output_size, Image.AFFINE, matrix, interpolation, **opts) + return img.transform(output_size, Image.Transform.AFFINE, matrix, interpolation, **opts) @torch.jit.unused @@ -356,7 +356,7 @@ def perspective( opts = _parse_fill(fill, img) - return img.transform(img.size, Image.PERSPECTIVE, perspective_coeffs, interpolation, **opts) + return img.transform(img.size, Image.Transform.PERSPECTIVE, perspective_coeffs, interpolation, **opts) @torch.jit.unused From 3cfe376ec42ddc3b2be8aa5af8aefecdf0118c68 Mon Sep 17 00:00:00 2001 From: Kyle Matoba <22180455+kylematoba@users.noreply.github.com> Date: Sat, 30 Apr 2022 12:36:42 +0200 Subject: [PATCH 2/6] Changes agreed at pytorch/vision#5898 --- test/test_onnx.py | 3 ++- test/test_transforms.py | 3 ++- test/test_transforms_tensor.py | 9 +++++---- torchvision/transforms/_pil_constants.py | 22 ++++++++++++++++++++++ torchvision/transforms/functional.py | 10 +++++----- torchvision/transforms/functional_pil.py | 17 +++++++++-------- torchvision/transforms/transforms.py | 11 +++++------ 7 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 torchvision/transforms/_pil_constants.py diff --git a/test/test_onnx.py b/test/test_onnx.py index 375d0fd1c6f..9218d02bf3e 100644 --- a/test/test_onnx.py +++ b/test/test_onnx.py @@ -413,11 +413,12 @@ def get_image(self, rel_path: str, size: Tuple[int, int]) -> torch.Tensor: import os from PIL import Image + import torchvision.transforms._pil_constants as _pil_constants from torchvision.transforms import functional as F data_dir = os.path.join(os.path.dirname(__file__), "assets") path = os.path.join(data_dir, *rel_path.split("/")) - image = Image.open(path).convert("RGB").resize(size, Image.BILINEAR) + image = Image.open(path).convert("RGB").resize(size, _pil_constants.BILINEAR) return F.convert_image_dtype(F.pil_to_tensor(image)) diff --git a/test/test_transforms.py b/test/test_transforms.py index 75abd31e148..3f9e8733acf 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -12,6 +12,7 @@ import torchvision.transforms.functional_tensor as F_t from PIL import Image from torch._utils_internal import get_file_path_2 +import torchvision.transforms._pil_constants as _pil_constants try: import accimage @@ -173,7 +174,7 @@ def test_accimage_pil_to_tensor(self): def test_accimage_resize(self): trans = transforms.Compose( [ - transforms.Resize(256, interpolation=Image.LINEAR), + transforms.Resize(256, interpolation=_pil_constants.LINEAR), transforms.PILToTensor(), transforms.ConvertImageDtype(dtype=torch.float), ] diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 8cebe666b50..faf2e153974 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -15,11 +15,12 @@ cpu_and_gpu, assert_equal, ) -from PIL import Image + from torchvision import transforms as T from torchvision.transforms import InterpolationMode from torchvision.transforms import functional as F from torchvision.transforms.autoaugment import _apply_op +import torchvision.transforms._pil_constants as _pil_constants NEAREST, BILINEAR, BICUBIC = InterpolationMode.NEAREST, InterpolationMode.BILINEAR, InterpolationMode.BICUBIC @@ -771,13 +772,13 @@ def shear(pil_img, level, mode, resample): matrix = (1, level, 0, 0, 1, 0) elif mode == "Y": matrix = (1, 0, 0, level, 1, 0) - return pil_img.transform((image_size, image_size), Image.AFFINE, matrix, resample=resample) + return pil_img.transform((image_size, image_size), _pil_constants.AFFINE, matrix, resample=resample) t_img, pil_img = _create_data(image_size, image_size) resample_pil = { - F.InterpolationMode.NEAREST: Image.NEAREST, - F.InterpolationMode.BILINEAR: Image.BILINEAR, + F.InterpolationMode.NEAREST: _pil_constants.NEAREST, + F.InterpolationMode.BILINEAR: _pil_constants.BILINEAR, }[interpolation] level = 0.3 diff --git a/torchvision/transforms/_pil_constants.py b/torchvision/transforms/_pil_constants.py new file mode 100644 index 00000000000..cd2689a33d3 --- /dev/null +++ b/torchvision/transforms/_pil_constants.py @@ -0,0 +1,22 @@ + +import PIL +from PIL import Image + +if tuple(int(part) for part in PIL.__version__.split(".")) >= (9, 1, 0): + FLIP_LEFT_RIGHT = Image.Transpose.FLIP_LEFT_RIGHT + FLIP_TOP_BOTTOM = Image.Transpose.FLIP_TOP_BOTTOM + BILINEAR = Image.Resampling.BILINEAR + NEAREST = Image.Resampling.NEAREST + AFFINE = Image.Transform.AFFINE + BICUBIC = Image.Resampling.BICUBIC + PERSPECTIVE = Image.Transform.PERSPECTIVE + LINEAR = Image.Resampling.BILINEAR +else: + FLIP_LEFT_RIGHT = Image.FLIP_LEFT_RIGHT + FLIP_TOP_BOTTOM = Image.FLIP_TOP_BOTTOM + BILINEAR = Image.BILINEAR + NEAREST = Image.NEAREST + AFFINE = Image.AFFINE + BICUBIC = Image.BICUBIC + PERSPECTIVE = Image.PERSPECTIVE + LINEAR = Image.LINEAR diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py index e964b10e18e..7ca88109e9e 100644 --- a/torchvision/transforms/functional.py +++ b/torchvision/transforms/functional.py @@ -392,7 +392,7 @@ def resize( :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. max_size (int, optional): The maximum allowed for the longer edge of the resized image: if the longer edge of the image is greater than ``max_size`` after being resized according to ``size``, then @@ -572,7 +572,7 @@ def resized_crop( :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. Returns: PIL Image or Tensor: Cropped image. @@ -652,7 +652,7 @@ def perspective( interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. fill (sequence or number, optional): Pixel fill value for the area outside the transformed image. If given a number, the value is used for all bands respectively. @@ -1012,7 +1012,7 @@ def rotate( interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. expand (bool, optional): Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. @@ -1105,7 +1105,7 @@ def affine( interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. fill (sequence or number, optional): Pixel fill value for the area outside the transformed image. If given a number, the value is used for all bands respectively. diff --git a/torchvision/transforms/functional_pil.py b/torchvision/transforms/functional_pil.py index 6edad55fd5b..0203ee4495b 100644 --- a/torchvision/transforms/functional_pil.py +++ b/torchvision/transforms/functional_pil.py @@ -10,6 +10,7 @@ import accimage except ImportError: accimage = None +from . import _pil_constants @torch.jit.unused @@ -54,7 +55,7 @@ def hflip(img: Image.Image) -> Image.Image: if not _is_pil_image(img): raise TypeError(f"img should be PIL Image. Got {type(img)}") - return img.transpose(Image.Transpose.FLIP_LEFT_RIGHT) + return img.transpose(_pil_constants.FLIP_LEFT_RIGHT) @torch.jit.unused @@ -62,7 +63,7 @@ def vflip(img: Image.Image) -> Image.Image: if not _is_pil_image(img): raise TypeError(f"img should be PIL Image. Got {type(img)}") - return img.transpose(Image.Transpose.FLIP_TOP_BOTTOM) + return img.transpose(_pil_constants.FLIP_TOP_BOTTOM) @torch.jit.unused @@ -240,7 +241,7 @@ def crop( def resize( img: Image.Image, size: Union[Sequence[int], int], - interpolation: int = Image.BILINEAR, + interpolation: int = _pil_constants.BILINEAR, max_size: Optional[int] = None, ) -> Image.Image: @@ -314,7 +315,7 @@ def _parse_fill( def affine( img: Image.Image, matrix: List[float], - interpolation: int = Image.NEAREST, + interpolation: int = _pil_constants.NEAREST, fill: Optional[Union[float, List[float], Tuple[float, ...]]] = 0, ) -> Image.Image: @@ -323,14 +324,14 @@ def affine( output_size = img.size opts = _parse_fill(fill, img) - return img.transform(output_size, Image.Transform.AFFINE, matrix, interpolation, **opts) + return img.transform(output_size, _pil_constants.AFFINE, matrix, interpolation, **opts) @torch.jit.unused def rotate( img: Image.Image, angle: float, - interpolation: int = Image.NEAREST, + interpolation: int = _pil_constants.NEAREST, expand: bool = False, center: Optional[Tuple[int, int]] = None, fill: Optional[Union[float, List[float], Tuple[float, ...]]] = 0, @@ -347,7 +348,7 @@ def rotate( def perspective( img: Image.Image, perspective_coeffs: float, - interpolation: int = Image.BICUBIC, + interpolation: int = _pil_constants.BICUBIC, fill: Optional[Union[float, List[float], Tuple[float, ...]]] = 0, ) -> Image.Image: @@ -356,7 +357,7 @@ def perspective( opts = _parse_fill(fill, img) - return img.transform(img.size, Image.Transform.PERSPECTIVE, perspective_coeffs, interpolation, **opts) + return img.transform(img.size, _pil_constants.PERSPECTIVE, perspective_coeffs, interpolation, **opts) @torch.jit.unused diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index dc179f680ee..2692c172abb 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -17,7 +17,6 @@ from . import functional as F from .functional import InterpolationMode, _interpolation_modes_from_int - __all__ = [ "Compose", "ToTensor", @@ -298,7 +297,7 @@ class Resize(torch.nn.Module): :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. max_size (int, optional): The maximum allowed for the longer edge of the resized image: if the longer edge of the image is greater than ``max_size`` after being resized according to ``size``, then @@ -755,7 +754,7 @@ class RandomPerspective(torch.nn.Module): interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. fill (sequence or number): Pixel fill value for the area outside the transformed image. Default is ``0``. If given a number, the value is used for all bands respectively. """ @@ -869,7 +868,7 @@ class RandomResizedCrop(torch.nn.Module): :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. """ @@ -1268,7 +1267,7 @@ class RandomRotation(torch.nn.Module): interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. expand (bool, optional): Optional expansion flag. If true, expands the output to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. @@ -1389,7 +1388,7 @@ class RandomAffine(torch.nn.Module): interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. fill (sequence or number): Pixel fill value for the area outside the transformed image. Default is ``0``. If given a number, the value is used for all bands respectively. fillcolor (sequence or number, optional): From a74be25f2584cbd5627a70f2a31cf2657be39078 Mon Sep 17 00:00:00 2001 From: Kyle Matoba <22180455+kylematoba@users.noreply.github.com> Date: Mon, 2 May 2022 13:31:28 +0200 Subject: [PATCH 3/6] black, sort constants, version check --- torchvision/transforms/_pil_constants.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/torchvision/transforms/_pil_constants.py b/torchvision/transforms/_pil_constants.py index cd2689a33d3..b670619795b 100644 --- a/torchvision/transforms/_pil_constants.py +++ b/torchvision/transforms/_pil_constants.py @@ -1,22 +1,23 @@ - import PIL from PIL import Image -if tuple(int(part) for part in PIL.__version__.split(".")) >= (9, 1, 0): - FLIP_LEFT_RIGHT = Image.Transpose.FLIP_LEFT_RIGHT - FLIP_TOP_BOTTOM = Image.Transpose.FLIP_TOP_BOTTOM +if tuple(int(part) for part in PIL.__version__.split(".")) >= (9, 1): + BICUBIC = Image.Resampling.BICUBIC BILINEAR = Image.Resampling.BILINEAR + LINEAR = Image.Resampling.BILINEAR NEAREST = Image.Resampling.NEAREST + AFFINE = Image.Transform.AFFINE - BICUBIC = Image.Resampling.BICUBIC + FLIP_LEFT_RIGHT = Image.Transpose.FLIP_LEFT_RIGHT + FLIP_TOP_BOTTOM = Image.Transpose.FLIP_TOP_BOTTOM PERSPECTIVE = Image.Transform.PERSPECTIVE - LINEAR = Image.Resampling.BILINEAR else: - FLIP_LEFT_RIGHT = Image.FLIP_LEFT_RIGHT - FLIP_TOP_BOTTOM = Image.FLIP_TOP_BOTTOM + BICUBIC = Image.BICUBIC BILINEAR = Image.BILINEAR NEAREST = Image.NEAREST + LINEAR = Image.LINEAR + AFFINE = Image.AFFINE - BICUBIC = Image.BICUBIC + FLIP_LEFT_RIGHT = Image.FLIP_LEFT_RIGHT + FLIP_TOP_BOTTOM = Image.FLIP_TOP_BOTTOM PERSPECTIVE = Image.PERSPECTIVE - LINEAR = Image.LINEAR From 1ac68ca785b2e45f89eb55577bffd5f4caa084da Mon Sep 17 00:00:00 2001 From: Kyle Matoba <22180455+kylematoba@users.noreply.github.com> Date: Mon, 2 May 2022 13:49:48 +0200 Subject: [PATCH 4/6] Format tests --- test/test_onnx.py | 2 +- test/test_transforms.py | 2 +- test/test_transforms_tensor.py | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/test/test_onnx.py b/test/test_onnx.py index 9218d02bf3e..ba0880a621d 100644 --- a/test/test_onnx.py +++ b/test/test_onnx.py @@ -412,8 +412,8 @@ def forward(self_module, images, features): def get_image(self, rel_path: str, size: Tuple[int, int]) -> torch.Tensor: import os - from PIL import Image import torchvision.transforms._pil_constants as _pil_constants + from PIL import Image from torchvision.transforms import functional as F data_dir = os.path.join(os.path.dirname(__file__), "assets") diff --git a/test/test_transforms.py b/test/test_transforms.py index 3f9e8733acf..99bf1f69b9b 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -8,11 +8,11 @@ import pytest import torch import torchvision.transforms as transforms +import torchvision.transforms._pil_constants as _pil_constants import torchvision.transforms.functional as F import torchvision.transforms.functional_tensor as F_t from PIL import Image from torch._utils_internal import get_file_path_2 -import torchvision.transforms._pil_constants as _pil_constants try: import accimage diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index faf2e153974..ba2321ec455 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -4,6 +4,7 @@ import numpy as np import pytest import torch +import torchvision.transforms._pil_constants as _pil_constants from common_utils import ( get_tmp_dir, int_dtypes, @@ -15,12 +16,10 @@ cpu_and_gpu, assert_equal, ) - from torchvision import transforms as T from torchvision.transforms import InterpolationMode from torchvision.transforms import functional as F from torchvision.transforms.autoaugment import _apply_op -import torchvision.transforms._pil_constants as _pil_constants NEAREST, BILINEAR, BICUBIC = InterpolationMode.NEAREST, InterpolationMode.BILINEAR, InterpolationMode.BICUBIC From 77a2dfbe8aa3a0bb10026f031ed95cd7d624a405 Mon Sep 17 00:00:00 2001 From: Kyle Matoba <22180455+kylematoba@users.noreply.github.com> Date: Thu, 5 May 2022 13:42:01 +0200 Subject: [PATCH 5/6] Square brackets --- torchvision/transforms/functional.py | 10 +++++----- torchvision/transforms/transforms.py | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py index 7ca88109e9e..053bd17d6f8 100644 --- a/torchvision/transforms/functional.py +++ b/torchvision/transforms/functional.py @@ -392,7 +392,7 @@ def resize( :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. max_size (int, optional): The maximum allowed for the longer edge of the resized image: if the longer edge of the image is greater than ``max_size`` after being resized according to ``size``, then @@ -572,7 +572,7 @@ def resized_crop( :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. Returns: PIL Image or Tensor: Cropped image. @@ -652,7 +652,7 @@ def perspective( interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. fill (sequence or number, optional): Pixel fill value for the area outside the transformed image. If given a number, the value is used for all bands respectively. @@ -1012,7 +1012,7 @@ def rotate( interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. expand (bool, optional): Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. @@ -1105,7 +1105,7 @@ def affine( interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. fill (sequence or number, optional): Pixel fill value for the area outside the transformed image. If given a number, the value is used for all bands respectively. diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index 2692c172abb..738a766ff79 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -297,7 +297,7 @@ class Resize(torch.nn.Module): :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. max_size (int, optional): The maximum allowed for the longer edge of the resized image: if the longer edge of the image is greater than ``max_size`` after being resized according to ``size``, then @@ -754,7 +754,7 @@ class RandomPerspective(torch.nn.Module): interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. fill (sequence or number): Pixel fill value for the area outside the transformed image. Default is ``0``. If given a number, the value is used for all bands respectively. """ @@ -868,7 +868,7 @@ class RandomResizedCrop(torch.nn.Module): :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. """ @@ -1267,7 +1267,7 @@ class RandomRotation(torch.nn.Module): interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. expand (bool, optional): Optional expansion flag. If true, expands the output to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. @@ -1388,7 +1388,7 @@ class RandomAffine(torch.nn.Module): interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. - For backward compatibility integer values (e.g. ``PIL.Image(.Resampling).NEAREST``) are still acceptable. + For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable. fill (sequence or number): Pixel fill value for the area outside the transformed image. Default is ``0``. If given a number, the value is used for all bands respectively. fillcolor (sequence or number, optional): From 3ee554288a6b2d91933d92829bb18619550e3240 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 6 May 2022 11:46:32 +0200 Subject: [PATCH 6/6] Update torchvision/transforms/_pil_constants.py Co-authored-by: Nicolas Hug --- torchvision/transforms/_pil_constants.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/torchvision/transforms/_pil_constants.py b/torchvision/transforms/_pil_constants.py index b670619795b..b8de5ec94f2 100644 --- a/torchvision/transforms/_pil_constants.py +++ b/torchvision/transforms/_pil_constants.py @@ -1,6 +1,9 @@ import PIL from PIL import Image +# See https://pillow.readthedocs.io/en/stable/releasenotes/9.1.0.html#deprecations +# TODO: Remove this file once PIL minimal version is >= 9.1 + if tuple(int(part) for part in PIL.__version__.split(".")) >= (9, 1): BICUBIC = Image.Resampling.BICUBIC BILINEAR = Image.Resampling.BILINEAR