Skip to content

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

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

Open
adamjstewart opened this issue Jan 1, 2024 · 8 comments

Comments

@adamjstewart
Copy link
Contributor

🐛 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
Copy link
Contributor

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
Copy link
Member

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
Copy link
Contributor Author

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

@NicolasHug
Copy link
Member

NicolasHug commented Jul 25, 2024

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
Copy link
Contributor Author

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
Copy link
Member

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

(also let me know about #8406 (comment) please :) )

@adamjstewart
Copy link
Contributor Author

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

@NicolasHug
Copy link
Member

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:

  • in the root __init__ file we can remove it from there, but there's the other ones:
  • in torchvision.datasets for the optical flow datasets
  • in torchvision.transforms.v2 for the Jpeg() transform.

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...

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

No branches or pull requests

3 participants