question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImportError: cannot import name 'get_model_weights' from 'torchvision.models'

See original GitHub issue

šŸ› Describe the bug

Can’t use torch.hub.load from torchvision==0.13.0, since hubconf.py from main branch is doing

from torchvision.models import get_model_weights, get_weight

which is different from torchvision==0.13.0 hubconf.py.

Error:

    model = torch.hub.load("pytorch/vision", self.model_name, **self.kwargs)
/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/torch/hub.py:540: in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/torch/hub.py:566: in _load_local
    hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/torch/hub.py:89: in _import_module
    spec.loader.exec_module(module)
<frozen importlib._bootstrap_external>:850: in exec_module
    ???
<frozen importlib._bootstrap>:228: in _call_with_frames_removed
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    dependencies = ["torch"]
    
>   from torchvision.models import get_model_weights, get_weight
E   ImportError: cannot import name 'get_model_weights' from 'torchvision.models' (/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/torchvision/models/__init__.py)

Relevant PR https://github.com/pytorch/vision/pull/6364

Versions

Collecting environment information… PyTorch version: 1.12.1+cu102 Is debug build: False CUDA used to build PyTorch: 10.2 ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.4 LTS (x86_64) GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0 Clang version: Could not collect CMake version: version 3.16.3 Libc version: glibc-2.31

Python version: 3.9.12 (main, Apr 18 2022, 22:40:46) [GCC 9.4.0] (64-bit runtime) Python platform: Linux-5.4.0-107-generic-x86_64-with-glibc2.31 Is CUDA available: True CUDA runtime version: 11.4.48 GPU models and configuration: GPU 0: NVIDIA GeForce RTX 2080 Ti GPU 1: NVIDIA GeForce RTX 3090

Nvidia driver version: 510.54 cuDNN version: Probably one of the following: /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.2.2 HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True

Versions of relevant libraries: [pip3] mypy==0.971 [pip3] mypy-extensions==0.4.3 [pip3] numpy==1.23.2 [pip3] torch==1.12.1 [pip3] torchvision==0.13.1 [conda] Could not collect

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
yil8commented, Aug 17, 2022

@NicolasHug @datumbox I found where was my bug, previously I was doing:

model = torch.hub.load("pytorch/vision", self.model_name, **self.kwargs)

Which did not specify torchvision version number. The issue was resolved after I changed the code to:

model = torch.hub.load("pytorch/vision:v0.13.0", self.model_name, **self.kwargs)
1reaction
NicolasHugcommented, Aug 17, 2022

My assumption is that torchvision was already imported before the call to torch.hub.load()? In which case:

  • the torchvision module is already present in sys.modules
  • torchhub properly downloads the main branch of the repo according to the stderr (but to make sure, maybe use force_reload=True)
  • torchhub tries to load that folder’s hubconf.py file, where this line is found from torchvision.models import get_model_weights, get_weight
  • In a normal scenario (i.e. without torchvision already present in sys.modules), this line will import torchvision from the local directory (i.e. the downloaded main branch), where get_model_weights and get_weight can be found
  • But since torchvision is already present in sys.modules, this local import doesn’t happen, and python tries to load get_[model]weight from the 0.13 torchvision package, where they don’t exist.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Import error while using torch.hub.load - vision - PyTorch Forums
passing weights param as string model ... convnext_tiny ImportError: cannot import name 'get_model_weights' from 'torchvision.models'.
Read more >
Cannot import name 'ResNet50_Weights' from 'torchvision ...
The problem took me some days to solve it. Before running a code containing the Pytorch models, make sure you are connected to...
Read more >
ImportError: cannot import name 'wide_resnet50_2' #46 - GitHub
I reproduced the issue, it depends if you import torchvision or not. Case 1: import torch model = torch.hub.load(Ā ...
Read more >
cannot import name 'mobilenet_v2' from 'torchvision.models ...
ImportError : cannot import name 'mobilenet_v2' from 'torchvision.models' (C:\file_path\__init__.py). The Code I tried to execute:.
Read more >
cannot import name 'mobilenet_v2' from 'torchvision.models'
This was the error I received when trying to import fastbook to study the excellent fast.ai course. I had Python 3.9.1 installed and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found