Skip to content

Commit f212961

Browse files
YosuaMichaelpmeierdatumbox
authored andcommitted
[fbsync] Deprecate int as interpolation argument type (#5974)
Summary: * Requested here #5898 (comment). * Fix tests * ufmt, not black Reviewed By: NicolasHug Differential Revision: D36760927 fbshipit-source-id: 8ed8c6eaf32928f71ba6d4b3cf23a4b84f310d81 Co-authored-by: Philip Meier <github.pmeier@posteo.de> Co-authored-by: Vasilis Vryniotis <datumbox@users.noreply.github.com>
1 parent 5fe8887 commit f212961

File tree

4 files changed

+81
-39
lines changed

4 files changed

+81
-39
lines changed

test/test_functional_tensor.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,13 @@ def test_rotate_deprecation_resample(self):
156156
def test_rotate_interpolation_type(self):
157157
tensor, _ = _create_data(26, 26)
158158
# assert changed type warning
159-
with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"):
159+
with pytest.warns(
160+
UserWarning,
161+
match=re.escape(
162+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
163+
"Please use InterpolationMode enum."
164+
),
165+
):
160166
res1 = F.rotate(tensor, 45, interpolation=2)
161167
res2 = F.rotate(tensor, 45, interpolation=BILINEAR)
162168
assert_equal(res1, res2)
@@ -384,7 +390,13 @@ def test_warnings(self, device):
384390
assert_equal(res1, res2)
385391

386392
# assert changed type warning
387-
with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"):
393+
with pytest.warns(
394+
UserWarning,
395+
match=re.escape(
396+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
397+
"Please use InterpolationMode enum."
398+
),
399+
):
388400
res1 = F.affine(tensor, 45, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=2)
389401
res2 = F.affine(tensor, 45, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=BILINEAR)
390402
assert_equal(res1, res2)
@@ -504,7 +516,13 @@ def test_perspective_interpolation_warning():
504516
spoints = [[0, 0], [33, 0], [33, 25], [0, 25]]
505517
epoints = [[3, 2], [32, 3], [30, 24], [2, 25]]
506518
tensor = torch.randint(0, 256, (3, 26, 26))
507-
with pytest.warns(UserWarning, match="Argument interpolation should be of type InterpolationMode"):
519+
with pytest.warns(
520+
UserWarning,
521+
match=re.escape(
522+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
523+
"Please use InterpolationMode enum."
524+
),
525+
):
508526
res1 = F.perspective(tensor, startpoints=spoints, endpoints=epoints, interpolation=2)
509527
res2 = F.perspective(tensor, startpoints=spoints, endpoints=epoints, interpolation=BILINEAR)
510528
assert_equal(res1, res2)
@@ -584,7 +602,13 @@ def test_resize_asserts(device):
584602
tensor, pil_img = _create_data(26, 36, device=device)
585603

586604
# assert changed type warning
587-
with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"):
605+
with pytest.warns(
606+
UserWarning,
607+
match=re.escape(
608+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
609+
"Please use InterpolationMode enum."
610+
),
611+
):
588612
res1 = F.resize(tensor, size=32, interpolation=2)
589613

590614
res2 = F.resize(tensor, size=32, interpolation=BILINEAR)

test/test_transforms.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,13 @@ def test_random_rotation():
18781878
assert t.interpolation == transforms.InterpolationMode.BILINEAR
18791879

18801880
# assert changed type warning
1881-
with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"):
1881+
with pytest.warns(
1882+
UserWarning,
1883+
match=re.escape(
1884+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
1885+
"Please use InterpolationMode enum."
1886+
),
1887+
):
18821888
t = transforms.RandomRotation((-10, 10), interpolation=2)
18831889
assert t.interpolation == transforms.InterpolationMode.BILINEAR
18841890

@@ -2233,7 +2239,13 @@ def test_random_affine():
22332239
assert t.fill == 10
22342240

22352241
# assert changed type warning
2236-
with pytest.warns(UserWarning, match=r"Argument interpolation should be of type InterpolationMode"):
2242+
with pytest.warns(
2243+
UserWarning,
2244+
match=re.escape(
2245+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
2246+
"Please use InterpolationMode enum."
2247+
),
2248+
):
22372249
t = transforms.RandomAffine(10, interpolation=2)
22382250
assert t.interpolation == transforms.InterpolationMode.BILINEAR
22392251

torchvision/transforms/functional.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ def resize(
392392
:class:`torchvision.transforms.InterpolationMode`.
393393
Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``,
394394
``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported.
395-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
395+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
396+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
396397
max_size (int, optional): The maximum allowed for the longer edge of
397398
the resized image: if the longer edge of the image is greater
398399
than ``max_size`` after being resized according to ``size``, then
@@ -414,8 +415,8 @@ def resize(
414415
# Backward compatibility with integer value
415416
if isinstance(interpolation, int):
416417
warnings.warn(
417-
"Argument interpolation should be of type InterpolationMode instead of int. "
418-
"Please, use InterpolationMode enum."
418+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
419+
"Please use InterpolationMode enum."
419420
)
420421
interpolation = _interpolation_modes_from_int(interpolation)
421422

@@ -572,8 +573,8 @@ def resized_crop(
572573
:class:`torchvision.transforms.InterpolationMode`.
573574
Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``,
574575
``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported.
575-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
576-
576+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
577+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
577578
Returns:
578579
PIL Image or Tensor: Cropped image.
579580
"""
@@ -652,7 +653,8 @@ def perspective(
652653
interpolation (InterpolationMode): Desired interpolation enum defined by
653654
:class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
654655
If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
655-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
656+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
657+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
656658
fill (sequence or number, optional): Pixel fill value for the area outside the transformed
657659
image. If given a number, the value is used for all bands respectively.
658660
@@ -671,8 +673,8 @@ def perspective(
671673
# Backward compatibility with integer value
672674
if isinstance(interpolation, int):
673675
warnings.warn(
674-
"Argument interpolation should be of type InterpolationMode instead of int. "
675-
"Please, use InterpolationMode enum."
676+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
677+
"Please use InterpolationMode enum."
676678
)
677679
interpolation = _interpolation_modes_from_int(interpolation)
678680

@@ -1012,7 +1014,8 @@ def rotate(
10121014
interpolation (InterpolationMode): Desired interpolation enum defined by
10131015
:class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
10141016
If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
1015-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
1017+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
1018+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
10161019
expand (bool, optional): Optional expansion flag.
10171020
If true, expands the output image to make it large enough to hold the entire rotated image.
10181021
If false or omitted, make the output image the same size as the input image.
@@ -1048,8 +1051,8 @@ def rotate(
10481051
# Backward compatibility with integer value
10491052
if isinstance(interpolation, int):
10501053
warnings.warn(
1051-
"Argument interpolation should be of type InterpolationMode instead of int. "
1052-
"Please, use InterpolationMode enum."
1054+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
1055+
"Please use InterpolationMode enum."
10531056
)
10541057
interpolation = _interpolation_modes_from_int(interpolation)
10551058

@@ -1105,7 +1108,8 @@ def affine(
11051108
interpolation (InterpolationMode): Desired interpolation enum defined by
11061109
:class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
11071110
If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
1108-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
1111+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
1112+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
11091113
fill (sequence or number, optional): Pixel fill value for the area outside the transformed
11101114
image. If given a number, the value is used for all bands respectively.
11111115
@@ -1137,8 +1141,8 @@ def affine(
11371141
# Backward compatibility with integer value
11381142
if isinstance(interpolation, int):
11391143
warnings.warn(
1140-
"Argument interpolation should be of type InterpolationMode instead of int. "
1141-
"Please, use InterpolationMode enum."
1144+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
1145+
"Please use InterpolationMode enum."
11421146
)
11431147
interpolation = _interpolation_modes_from_int(interpolation)
11441148

torchvision/transforms/transforms.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ class Resize(torch.nn.Module):
297297
:class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
298298
If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and
299299
``InterpolationMode.BICUBIC`` are supported.
300-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
300+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
301+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
301302
max_size (int, optional): The maximum allowed for the longer edge of
302303
the resized image: if the longer edge of the image is greater
303304
than ``max_size`` after being resized according to ``size``, then
@@ -329,8 +330,8 @@ def __init__(self, size, interpolation=InterpolationMode.BILINEAR, max_size=None
329330
# Backward compatibility with integer value
330331
if isinstance(interpolation, int):
331332
warnings.warn(
332-
"Argument interpolation should be of type InterpolationMode instead of int. "
333-
"Please, use InterpolationMode enum."
333+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
334+
"Please use InterpolationMode enum."
334335
)
335336
interpolation = _interpolation_modes_from_int(interpolation)
336337

@@ -754,7 +755,8 @@ class RandomPerspective(torch.nn.Module):
754755
interpolation (InterpolationMode): Desired interpolation enum defined by
755756
:class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
756757
If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
757-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
758+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
759+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
758760
fill (sequence or number): Pixel fill value for the area outside the transformed
759761
image. Default is ``0``. If given a number, the value is used for all bands respectively.
760762
"""
@@ -767,8 +769,8 @@ def __init__(self, distortion_scale=0.5, p=0.5, interpolation=InterpolationMode.
767769
# Backward compatibility with integer value
768770
if isinstance(interpolation, int):
769771
warnings.warn(
770-
"Argument interpolation should be of type InterpolationMode instead of int. "
771-
"Please, use InterpolationMode enum."
772+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
773+
"Please use InterpolationMode enum."
772774
)
773775
interpolation = _interpolation_modes_from_int(interpolation)
774776

@@ -868,8 +870,8 @@ class RandomResizedCrop(torch.nn.Module):
868870
:class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
869871
If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and
870872
``InterpolationMode.BICUBIC`` are supported.
871-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
872-
873+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
874+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
873875
"""
874876

875877
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
887889
# Backward compatibility with integer value
888890
if isinstance(interpolation, int):
889891
warnings.warn(
890-
"Argument interpolation should be of type InterpolationMode instead of int. "
891-
"Please, use InterpolationMode enum."
892+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
893+
"Please use InterpolationMode enum."
892894
)
893895
interpolation = _interpolation_modes_from_int(interpolation)
894896

@@ -1267,7 +1269,8 @@ class RandomRotation(torch.nn.Module):
12671269
interpolation (InterpolationMode): Desired interpolation enum defined by
12681270
:class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
12691271
If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
1270-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
1272+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
1273+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
12711274
expand (bool, optional): Optional expansion flag.
12721275
If true, expands the output to make it large enough to hold the entire rotated image.
12731276
If false or omitted, make the output image the same size as the input image.
@@ -1300,8 +1303,8 @@ def __init__(
13001303
# Backward compatibility with integer value
13011304
if isinstance(interpolation, int):
13021305
warnings.warn(
1303-
"Argument interpolation should be of type InterpolationMode instead of int. "
1304-
"Please, use InterpolationMode enum."
1306+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
1307+
"Please use InterpolationMode enum."
13051308
)
13061309
interpolation = _interpolation_modes_from_int(interpolation)
13071310

@@ -1388,7 +1391,8 @@ class RandomAffine(torch.nn.Module):
13881391
interpolation (InterpolationMode): Desired interpolation enum defined by
13891392
:class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
13901393
If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
1391-
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still acceptable.
1394+
For backward compatibility integer values (e.g. ``PIL.Image[.Resampling].NEAREST``) are still accepted,
1395+
but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.
13921396
fill (sequence or number): Pixel fill value for the area outside the transformed
13931397
image. Default is ``0``. If given a number, the value is used for all bands respectively.
13941398
fillcolor (sequence or number, optional):
@@ -1429,8 +1433,8 @@ def __init__(
14291433
# Backward compatibility with integer value
14301434
if isinstance(interpolation, int):
14311435
warnings.warn(
1432-
"Argument interpolation should be of type InterpolationMode instead of int. "
1433-
"Please, use InterpolationMode enum."
1436+
"Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. "
1437+
"Please use InterpolationMode enum."
14341438
)
14351439
interpolation = _interpolation_modes_from_int(interpolation)
14361440

@@ -1727,9 +1731,7 @@ def forward(self, img):
17271731

17281732
# cast self.value to script acceptable type
17291733
if isinstance(self.value, (int, float)):
1730-
value = [
1731-
self.value,
1732-
]
1734+
value = [self.value]
17331735
elif isinstance(self.value, str):
17341736
value = None
17351737
elif isinstance(self.value, tuple):

0 commit comments

Comments
 (0)