From db64393c0d9908645fcb661769485d0e8ac9b2c5 Mon Sep 17 00:00:00 2001 From: Jakub Kaczmarzyk Date: Mon, 13 Jun 2022 01:30:57 -0400 Subject: [PATCH] use `Image.Resampling` namespace for PIL mapping (#1256) * use `Image.Resampling` namespace for PIL mapping PIL shows a deprecation warning when accessing resampling constants via the `Image` namespace. The suggested namespace is `Image.Resampling`. This commit updates `_pil_interpolation_to_str` to use the `Image.Resampling` namespace. ``` /tmp/ipykernel_11959/698124036.py:2: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.NEAREST or Dither.NONE instead. Image.NEAREST: 'nearest', /tmp/ipykernel_11959/698124036.py:3: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead. Image.BILINEAR: 'bilinear', /tmp/ipykernel_11959/698124036.py:4: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead. Image.BICUBIC: 'bicubic', /tmp/ipykernel_11959/698124036.py:5: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BOX instead. Image.BOX: 'box', /tmp/ipykernel_11959/698124036.py:6: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.HAMMING instead. Image.HAMMING: 'hamming', /tmp/ipykernel_11959/698124036.py:7: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead. Image.LANCZOS: 'lanczos', ``` * use new pillow resampling enum only if it exists --- timm/data/transforms.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/timm/data/transforms.py b/timm/data/transforms.py index 45c078f3..3eb3bc32 100644 --- a/timm/data/transforms.py +++ b/timm/data/transforms.py @@ -35,14 +35,28 @@ class ToTensor: return torch.from_numpy(np_img).to(dtype=self.dtype) -_pil_interpolation_to_str = { - Image.NEAREST: 'nearest', - Image.BILINEAR: 'bilinear', - Image.BICUBIC: 'bicubic', - Image.BOX: 'box', - Image.HAMMING: 'hamming', - Image.LANCZOS: 'lanczos', -} +# Pillow is deprecating the top-level resampling attributes (e.g., Image.BILINEAR) in +# favor of the Image.Resampling enum. The top-level resampling attributes will be +# removed in Pillow 10. +if hasattr(Image, "Resampling"): + _pil_interpolation_to_str = { + Image.Resampling.NEAREST: 'nearest', + Image.Resampling.BILINEAR: 'bilinear', + Image.Resampling.BICUBIC: 'bicubic', + Image.Resampling.BOX: 'box', + Image.Resampling.HAMMING: 'hamming', + Image.Resampling.LANCZOS: 'lanczos', + } +else: + _pil_interpolation_to_str = { + Image.NEAREST: 'nearest', + Image.BILINEAR: 'bilinear', + Image.BICUBIC: 'bicubic', + Image.BOX: 'box', + Image.HAMMING: 'hamming', + Image.LANCZOS: 'lanczos', + } + _str_to_pil_interpolation = {b: a for a, b in _pil_interpolation_to_str.items()} @@ -181,5 +195,3 @@ class RandomResizedCropAndInterpolation: format_string += ', ratio={0}'.format(tuple(round(r, 4) for r in self.ratio)) format_string += ', interpolation={0})'.format(interpolate_str) return format_string - -