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:
- Created a year ago
- Comments:6 (4 by maintainers)
@NicolasHug @datumbox I found where was my bug, previously I was doing:
Which did not specify torchvision version number. The issue was resolved after I changed the code to:
My assumption is that
torchvision
was already imported before the call totorch.hub.load()
? In which case:torchvision
module is already present insys.modules
main
branch of the repo according to the stderr (but to make sure, maybe useforce_reload=True
)hubconf.py
file, where this line is foundfrom torchvision.models import get_model_weights, get_weight
sys.modules
), this line will importtorchvision
from the local directory (i.e. the downloadedmain
branch), whereget_model_weights
andget_weight
can be foundtorchvision
is already present insys.modules
, this local import doesnāt happen, and python tries to loadget_[model]weight
from the 0.13 torchvision package, where they donāt exist.