-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Update transforms for PIL deprecation #5898
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution @kylematoba.
@pmeier @vfdev-5 Any thoughts on whether we should make the use of .Transform
conditional on the PIL version? We probably need to investigate when this was introduced. Unless we just want to pump the minimum required PIL version.
It worked on Pillow 6.0.0 for me. I couldn't get my test to work on anything earlier for unrelated reasons (so your setup.py might be out of date). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It worked on Pillow 6.0.0 for me.
I doubt that:
>>> import PIL.Image
>>> PIL.__version__
'9.0.1'
>>> PIL.Image.Transpose
AttributeError: module 'PIL.Image' has no attribute 'Transpose'
This was only introduced in python-pillow/Pillow#5954 which made it into the latest 9.1.0
release. I talked offline with @vfdev-5 about that some time ago. He wanted to pick it up, but I'm not aware of a PR regarding this yet.
Any thoughts on whether we should make the use of
.Transform
conditional on the PIL version? [...] Unless we just want to pump the minimum required PIL version.
I'm not opposed to that. Right now we require
Line 68 in f014841
pillow_ver = " >= 5.3.0, !=8.3.*" |
and this was introduced in #3641. Bumping from >=5.3.0
to >=9.1.0
is quite a difference (~ 2.5 years), but it might still be worth it to avoid cluttering our code with version checks again that were removed in #3641. cc @NicolasHug
Oh yeah, you're right. My IDE was caching the original version of PIL. I thought it was weird 😂 . I guess you can delay this until around the PIL hard change date of July 1, 2023:
In another year, it seems reasonable to bump the dep to >= 9.0.0. If you want, close this PR and I'll add a note to my calendar to look at this again in June 2023. |
Having user facing warnings for something they didn't do is not desirable. So we should handle this before the next release. If you are willing, please keep this PR open and finish it as soon as we decided which way to go:
import PIL.Image
if tuple(int(part) for part in PIL.__version__.split(".")) >= (9, 1):
FLIP_LEFT_RIGHT = PIL.Image.FLIP_LEFT_RIGHT
...
else:
FLIP_LEFT_RIGHT = PIL.Image.Transpose.FLIP_LEFT_RIGHT
...
def kernel(img):
...
img.transpose(FLIP_LEFT_RIGHT)
... In case we need these constants in multiple modules, it probably makes sense to have a from . import _pil_constants
def kernel(img):
...
img.transpose(_pil_constants.FLIP_LEFT_RIGHT)
... |
@kylematoba Thanks for the additional info and for notifying us about this. :) @pmeier Thanks for the analysis. Requiring users to be on the latest version of PIL is kind of a bummer. In principle I don't think it's a bad thing to bump the minimum version a bit but going from >=5.x to >=9.1 is quite a big jump. I don't know how many breaking changes and feature deprecations exist across 4 major versions and what effect this jump will have to users who make use of PIL directly on their software. Option 2 looks viable to me. Do we know how many constants we need to handle? @NicolasHug I would love to get your thoughts on the above. |
I agree, but we have to do something or it will only get worse. We could do this incrementally, i.e. requiring one new major PIL version per release until we hit the version we are after, but that also feels weird since we don't actually care about the versions in between.
I wrote a small script to check all deprecated constants and this is the result:
We have usages in multiple places so IMO we should go for the extra module as explained in #5898 (comment) if we don't update our requirement. |
Re minimal supported version: PIL has a deprecation policy of 2 major versions, so if we want to be a good citizen, that means the minimum PIL version we should support is probably Regardless, we'll have to support both ways (the deprecated one and the new one) until our minimum version is |
Just to be clear: Given that the current version is |
@pmeier I'm OK with that. BTW we should investigate what's the best way to keep all dependencies in one place. Bumping PIL right now involves updating 100 places across CI, building scripts and configs. |
Okay, I'll update the PR to make all instances of the deprecated constants contingent upon |
I added I extended my earlier changes to the docstrings, type annotations, and tests listed at #5898 (comment). Note the slightly confusing rename from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linting CI job is failing. Could you follow our instructions here to auto format the files you've edited? Otherwise LGTM. Thanks @kylematoba!
torchvision/transforms/functional.py
Outdated
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with this style in the docstring. Any objections?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
()
is fine, although as a nit, I think brackets []
are more common (typically in man
pages)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kylematoba Could you address this in all docstrings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, so replace instances of ()
with []
in my changes thusfar, and a second PR that just removes all of the lines about For backward compatibility integer values
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, so replace instances of
()
with[]
in my changes thusfar,
Yes.
and a second PR that just removes all of the lines about
For backward compatibility integer values
?
No. The docstring need to be changed indicating that passing int
's is actually deprecated, e.g.
vision/torchvision/transforms/functional.py
Lines 1029 to 1031 in 6274080
.. warning:: | |
This parameter was deprecated in ``0.12`` and will be removed in ``0.14``. Please use ``interpolation`` | |
instead. |
Furthermore, we need to change the warning
vision/torchvision/transforms/functional.py
Lines 415 to 419 in 6274080
if isinstance(interpolation, int): | |
warnings.warn( | |
"Argument interpolation should be of type InterpolationMode instead of int. " | |
"Please, use InterpolationMode enum." | |
) |
to include the concrete deprecation date, e.g.
vision/torchvision/transforms/functional.py
Lines 1042 to 1045 in 6274080
warnings.warn( | |
"The parameter 'resample' is deprecated since 0.12 and will be removed 0.14. " | |
"Please use 'interpolation' instead." | |
) |
For this we will be deprecating in 0.13
and remove in 0.15
.
A quick comment on the warning from a user perspective. I don't have the full context of this PIL deprecation issue so forgive me if there's anything I missed. Every run of torchrl scripts we make gives us as many warnings as the number of processes that are importing torchvision (3 warnings / process x 32 process = almost 200 lines of warnings). This gives us long logs where it's hard to find what's going on. Besides, correct me if I'm wrong, but it feels like it's a warning on which I have little agency -- it's not really that we're importing anything that will be deprecated: it's an issue in torchvision code, not ours. I think it's confusing for the users, as they're not fully informed of what's going on and who should fix what. Apart from hiding those specific warnings (my last option is to do a temporary patch in torchrl to hide specific deprecation warnings, but i'm a bit reluctant to make a PR like this), is there anything that could be done? |
The only other option is to force |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've checked CI and couldn't find a warning from PIL anywhere. Seems like we patched all places.
One small thing to do, otherwise LGTM. Thanks a lot @kylematoba!
torchvision/transforms/functional.py
Outdated
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kylematoba Could you address this in all docstrings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @kylematoba , minor comment below but LGTM
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Hey @datumbox! You merged this PR, but no labels were added. The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py |
Summary: * Update transforms for PIL deprecation * Changes agreed at #5898 * black, sort constants, version check * Format tests * Square brackets * Update torchvision/transforms/_pil_constants.py Reviewed By: YosuaMichael Differential Revision: D36281595 fbshipit-source-id: 677b0cb42dd23589197b757291d35a4b5f603c58 Co-authored-by: Nicolas Hug <contact@nicolas-hug.com> Co-authored-by: Philip Meier <github.pmeier@posteo.de> Co-authored-by: Nicolas Hug <contact@nicolas-hug.com> Co-authored-by: Vasilis Vryniotis <datumbox@users.noreply.github.com>
* Requested here #5898 (comment). * Fix tests * ufmt, not black Co-authored-by: Philip Meier <github.pmeier@posteo.de> Co-authored-by: Vasilis Vryniotis <datumbox@users.noreply.github.com>
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>
Updates torchvision transformations to the call recommended at https://github.com/python-pillow/Pillow/blob/5d070222d21138d2ead002fd33fdf5adcb708941/src/PIL/Image.py#L93 (Pillow 9.1.0 release).