Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Image.Resampling namespace for PIL mapping #1256

Merged
merged 2 commits into from
Jun 13, 2022

Conversation

kaczmarj
Copy link
Contributor

@kaczmarj kaczmarj commented May 11, 2022

PIL version 9.1.0 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',

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',
```
@rwightman
Copy link
Collaborator

@kaczmarj how far back in PIL versions does the new enum exist?

@kaczmarj
Copy link
Contributor Author

kaczmarj commented May 12, 2022

great question -- the new enum was added in python-pillow/Pillow#5954 (merged february 18 2022). only the most recent version of pillow (version 9.1.0) has this enum. torchvision requires pillow>=5.3.0, so this would break functionality on older pillow versions.

torchvision recently merged a pull request that used the new enums only if the pillow version was above 9.1.

what would you like to do? perhaps we could follow torchvision's example and use the enum only if the pillow version is above 9.1.

@adamjstewart
Copy link

I'm surprised to discover that timm imports numpy and pillow despite neither being listed as dependencies in setup.py. Even if these are automatically brought in by PyTorch/torchvision, they should still be explicitly listed as direct dependencies. For example, if one of these dependencies is removed (unlikely), or if the versions of pillow supported by torchvision and timm differ (already the case) then this would prevent a lot of issues.

@rwightman
Copy link
Collaborator

@adamjstewart @kaczmarj there's no difference in version of pillow supported from a compatbility standpoint, it's a deprecation warning. 9.1 is really new and there isn't even a Pillow-SIMD release of it yet(I only use that, normal Pillow is too slow to use for training).

I will put up with the warning for now, doing string parsing version checks is notoriously problematic. Checking for the existence of the new enum type would be more robust and I'd accept the PR if it used that approach.

@rwightman
Copy link
Collaborator

rwightman commented Jun 10, 2022

@adamjstewart @kaczmarj looking at the torchvision bug I just filed, there's the ans why you don't do this (string checks are hard to get right in all cases even when they appear straightforward)

@kaczmarj
Copy link
Contributor Author

@rwightman - how prescient! i'll try to submit a pr this weekend that checks for the existence of the new resampling enum and uses it only if it exists.

@rwightman
Copy link
Collaborator

@rwightman - how prescient! i'll try to submit a pr this weekend that checks for the existence of the new resampling enum and uses it only if it exists.

Awesome, thx

@adamjstewart
Copy link

@rwightman thanks, I didn't even think about that, I made the same mistake in my library, will fix that.

@adamjstewart
Copy link

@rwightman - how prescient! i'll try to submit a pr this weekend that checks for the existence of the new resampling enum and uses it only if it exists.

This will prevent things from breaking, but it won't silence the warning. Personally, I would use packaging.version.Version like pytorch-lightning does: https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/utilities/imports.py

@adamjstewart
Copy link

@rwightman corrected me, both are valid approaches.

@kaczmarj
Copy link
Contributor Author

@rwightman - i added a commit to check for Image.Resampling. if this looks good to merge, i suggest squashing and merging to ignore the commit. i would squash myself, but then i'd have to force push and that doesn't feel right.

@rwightman rwightman merged commit db64393 into huggingface:master Jun 13, 2022
@kaczmarj kaczmarj deleted the patch-1 branch June 13, 2022 13:56
@adamjstewart
Copy link

Should this also be done in timm/data/auto_augment.py?

../../../anaconda/envs/adam-py37-min/lib/python3.7/site-packages/timm/data/auto_augment.py:41
  /anaconda/envs/adam-py37-min/lib/python3.7/site-packages/timm/data/auto_augment.py:41: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead.
    _RANDOM_INTERPOLATION = (Image.BILINEAR, Image.BICUBIC)

../../../anaconda/envs/adam-py37-min/lib/python3.7/site-packages/timm/data/auto_augment.py:41
  /anaconda/envs/adam-py37-min/lib/python3.7/site-packages/timm/data/auto_augment.py:41: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead.
    _RANDOM_INTERPOLATION = (Image.BILINEAR, Image.BICUBIC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants