pytorch / vision

Datasets, Transforms and Models specific to Computer Vision
https://pytorch.org/vision
BSD 3-Clause "New" or "Revised" License
16.05k stars 6.93k forks source link

torchvision.io: don't warn about missing image libraries unless necessary #8192

Open adamjstewart opened 9 months ago

adamjstewart commented 9 months ago

🐛 Describe the bug

If no image libraries are installed, the following will warn:

>>> import torchvision
UserWarning: Failed to load image Python extension: 

I propose we only warn when importing torchvision.io or when using a specific function that requires one of these image libraries.

Related to #7151

Versions

Collecting environment information...
Traceback (most recent call last):
  File "/Users/Adam/Downloads/collect_env.py", line 624, in <module>
    main()
  File "/Users/Adam/Downloads/collect_env.py", line 607, in main
    output = get_pretty_env_info()
             ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Adam/Downloads/collect_env.py", line 602, in get_pretty_env_info
    return pretty_str(get_env_info())
                      ^^^^^^^^^^^^^^
  File "/Users/Adam/Downloads/collect_env.py", line 438, in get_env_info
    pip_version, pip_list_output = get_pip_packages(run_lambda)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Adam/Downloads/collect_env.py", line 410, in get_pip_packages
    out = run_with_pip([sys.executable, '-mpip'])
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Adam/Downloads/collect_env.py", line 405, in run_with_pip
    for line in out.splitlines()
                ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'splitlines'
Bhavay-2001 commented 7 months ago

Hi @NicolasHug, I would like to work on this issue if it is for beginners. Can you suggest me how may I proceed? Thanks

NicolasHug commented 2 months ago

I recently re-worked our extension compilation logic and the image.so extension is now always built, unconditionally. It doesn't depend anymore on the existence of libjpeg or libpng (because we vendor libgif, which is always present, so we always build).

Because of this recent change, I'm having a hard time imagining a scenario where this warning is emitted and undesirable. @adamjstewart can you remind me exact the scenario where you got this warning? Was it when building from source in specific conditions? When installing the stable version?

adamjstewart commented 2 months ago

It was when building from source, stable releases, but no libjpeg/libpng installed on the OS.

NicolasHug commented 2 months ago

Thanks for confirming. I understand why this was causing an undesirable warning in the past. Now that we always build image.so, I suspect this might have become a non-issue, i.e. the extension should be available and load just fine without a warning (and only contain the gif decoder).

adamjstewart commented 2 months ago

I'm fine with closing this if you want, although I'm also hoping for an option to build with external giflib and disable that in the future.

NicolasHug commented 2 months ago

Do you mean to dynamically link against giflib instead of statically? If so, why is that the case?

(also let me know about https://github.com/pytorch/vision/pull/8406#discussion_r1691444762 please :) )

adamjstewart commented 2 months ago

Replied to that thread, I mean building with a non-vendored copy of giflib.

NicolasHug commented 1 month ago

I've been looking into this a bit more. I tried using a lazy importing strategy like in https://peps.python.org/pep-0562/, so that the problematic image.so load check is only done when when io or io.image get imported. Unfortunately, this doesn't really work because these modules are loaded from different places when importing torchvision:

So, I'm afraid the only way to make this warning lazy is to only raise it when the functions are called. Which adds an extra check to each call...