From aa7487b4b169ae82b067f70ef69005f602a2d42f Mon Sep 17 00:00:00 2001 From: Kyle Matoba <22180455+kylematoba@users.noreply.github.com> Date: Mon, 9 May 2022 15:00:40 +0200 Subject: [PATCH 1/3] Requested here https://github.com/pytorch/vision/pull/5898#discussion_r864765799. --- torchvision/transforms/functional.py | 32 ++++++++++++---------- torchvision/transforms/transforms.py | 40 +++++++++++++++------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py index 053bd17d6f8..f0ffe09221e 100644 --- a/torchvision/transforms/functional.py +++ b/torchvision/transforms/functional.py @@ -392,7 +392,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. 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 @@ -414,8 +415,8 @@ def resize( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation should be of type InterpolationMode instead of int. " - "Please, use InterpolationMode enum." + "Argument interpolation of type int is deprecated since 0.13 and will be removed in 0.15." + "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -572,8 +573,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. Returns: PIL Image or Tensor: Cropped image. """ @@ -652,7 +653,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. 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. @@ -671,8 +673,8 @@ def perspective( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation should be of type InterpolationMode instead of int. " - "Please, use InterpolationMode enum." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -1012,7 +1014,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. 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. @@ -1048,8 +1051,8 @@ def rotate( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation should be of type InterpolationMode instead of int. " - "Please, use InterpolationMode enum." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -1105,7 +1108,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. 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. @@ -1137,8 +1141,8 @@ def affine( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation should be of type InterpolationMode instead of int. " - "Please, use InterpolationMode enum." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index 738a766ff79..0090182c245 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -297,7 +297,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. 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 @@ -329,8 +330,8 @@ def __init__(self, size, interpolation=InterpolationMode.BILINEAR, max_size=None # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation should be of type InterpolationMode instead of int. " - "Please, use InterpolationMode enum." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -754,7 +755,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. 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. """ @@ -767,8 +769,8 @@ def __init__(self, distortion_scale=0.5, p=0.5, interpolation=InterpolationMode. # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation should be of type InterpolationMode instead of int. " - "Please, use InterpolationMode enum." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -868,8 +870,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. """ def __init__(self, size, scale=(0.08, 1.0), ratio=(3.0 / 4.0, 4.0 / 3.0), interpolation=InterpolationMode.BILINEAR): @@ -887,8 +889,8 @@ def __init__(self, size, scale=(0.08, 1.0), ratio=(3.0 / 4.0, 4.0 / 3.0), interp # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation should be of type InterpolationMode instead of int. " - "Please, use InterpolationMode enum." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -1267,7 +1269,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. 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. @@ -1300,8 +1303,8 @@ def __init__( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation should be of type InterpolationMode instead of int. " - "Please, use InterpolationMode enum." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -1388,7 +1391,8 @@ 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 accepted, + but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. 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): @@ -1429,8 +1433,8 @@ def __init__( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation should be of type InterpolationMode instead of int. " - "Please, use InterpolationMode enum." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -1727,9 +1731,7 @@ def forward(self, img): # cast self.value to script acceptable type if isinstance(self.value, (int, float)): - value = [ - self.value, - ] + value = [self.value] elif isinstance(self.value, str): value = None elif isinstance(self.value, tuple): From 78ec78057a04ebd170805a6a97d1c0c37d8807e2 Mon Sep 17 00:00:00 2001 From: Kyle Matoba <22180455+kylematoba@users.noreply.github.com> Date: Mon, 9 May 2022 21:01:14 +0200 Subject: [PATCH 2/3] Fix tests --- test/test_transforms.py | 14 ++++++++++++-- torchvision/transforms/functional.py | 8 ++++---- torchvision/transforms/transforms.py | 10 +++++----- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 99bf1f69b9b..3005c324de3 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1878,7 +1878,12 @@ def test_random_rotation(): assert t.interpolation == transforms.InterpolationMode.BILINEAR # assert changed type warning - with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"): + with pytest.warns(UserWarning, + match=re.escape( + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " + "Please use InterpolationMode enum." + ), + ): t = transforms.RandomRotation((-10, 10), interpolation=2) assert t.interpolation == transforms.InterpolationMode.BILINEAR @@ -2233,7 +2238,12 @@ def test_random_affine(): assert t.fill == 10 # assert changed type warning - with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"): + with pytest.warns(UserWarning, + match=re.escape( + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " + "Please use InterpolationMode enum." + ), + ): t = transforms.RandomAffine(10, interpolation=2) assert t.interpolation == transforms.InterpolationMode.BILINEAR diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py index f0ffe09221e..c58eb582d26 100644 --- a/torchvision/transforms/functional.py +++ b/torchvision/transforms/functional.py @@ -415,7 +415,7 @@ def resize( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument interpolation of type int is deprecated since 0.13 and will be removed in 0.15." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -673,7 +673,7 @@ def perspective( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -1051,7 +1051,7 @@ def rotate( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -1141,7 +1141,7 @@ def affine( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index 0090182c245..b8770ae845e 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -330,7 +330,7 @@ def __init__(self, size, interpolation=InterpolationMode.BILINEAR, max_size=None # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -769,7 +769,7 @@ def __init__(self, distortion_scale=0.5, p=0.5, interpolation=InterpolationMode. # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -889,7 +889,7 @@ def __init__(self, size, scale=(0.08, 1.0), ratio=(3.0 / 4.0, 4.0 / 3.0), interp # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -1303,7 +1303,7 @@ def __init__( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) @@ -1433,7 +1433,7 @@ def __init__( # Backward compatibility with integer value if isinstance(interpolation, int): warnings.warn( - "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15." + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum." ) interpolation = _interpolation_modes_from_int(interpolation) From 57b22274287c5182f3ea4172f17ec32cef9beb84 Mon Sep 17 00:00:00 2001 From: Kyle Matoba <22180455+kylematoba@users.noreply.github.com> Date: Mon, 9 May 2022 21:23:11 +0200 Subject: [PATCH 3/3] ufmt, not black --- test/test_functional_tensor.py | 32 ++++++++++++++++++++++++++++---- test/test_transforms.py | 16 +++++++++------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index e158ff4f805..5551691000f 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -156,7 +156,13 @@ def test_rotate_deprecation_resample(self): def test_rotate_interpolation_type(self): tensor, _ = _create_data(26, 26) # assert changed type warning - with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"): + with pytest.warns( + UserWarning, + match=re.escape( + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " + "Please use InterpolationMode enum." + ), + ): res1 = F.rotate(tensor, 45, interpolation=2) res2 = F.rotate(tensor, 45, interpolation=BILINEAR) assert_equal(res1, res2) @@ -384,7 +390,13 @@ def test_warnings(self, device): assert_equal(res1, res2) # assert changed type warning - with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"): + with pytest.warns( + UserWarning, + match=re.escape( + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " + "Please use InterpolationMode enum." + ), + ): res1 = F.affine(tensor, 45, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=2) res2 = F.affine(tensor, 45, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=BILINEAR) assert_equal(res1, res2) @@ -504,7 +516,13 @@ def test_perspective_interpolation_warning(): spoints = [[0, 0], [33, 0], [33, 25], [0, 25]] epoints = [[3, 2], [32, 3], [30, 24], [2, 25]] tensor = torch.randint(0, 256, (3, 26, 26)) - with pytest.warns(UserWarning, match="Argument interpolation should be of type InterpolationMode"): + with pytest.warns( + UserWarning, + match=re.escape( + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " + "Please use InterpolationMode enum." + ), + ): res1 = F.perspective(tensor, startpoints=spoints, endpoints=epoints, interpolation=2) res2 = F.perspective(tensor, startpoints=spoints, endpoints=epoints, interpolation=BILINEAR) assert_equal(res1, res2) @@ -584,7 +602,13 @@ def test_resize_asserts(device): tensor, pil_img = _create_data(26, 36, device=device) # assert changed type warning - with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"): + with pytest.warns( + UserWarning, + match=re.escape( + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " + "Please use InterpolationMode enum." + ), + ): res1 = F.resize(tensor, size=32, interpolation=2) res2 = F.resize(tensor, size=32, interpolation=BILINEAR) diff --git a/test/test_transforms.py b/test/test_transforms.py index 3005c324de3..04f49cb1376 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1878,12 +1878,13 @@ def test_random_rotation(): assert t.interpolation == transforms.InterpolationMode.BILINEAR # assert changed type warning - with pytest.warns(UserWarning, - match=re.escape( - "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " - "Please use InterpolationMode enum." - ), - ): + with pytest.warns( + UserWarning, + match=re.escape( + "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " + "Please use InterpolationMode enum." + ), + ): t = transforms.RandomRotation((-10, 10), interpolation=2) assert t.interpolation == transforms.InterpolationMode.BILINEAR @@ -2238,7 +2239,8 @@ def test_random_affine(): assert t.fill == 10 # assert changed type warning - with pytest.warns(UserWarning, + with pytest.warns( + UserWarning, match=re.escape( "Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. " "Please use InterpolationMode enum."