Import error while using torch.hub.load

# passing weights param as string
model = torch.hub.load("pytorch/vision", "resnet18", weights= ResNet18_Weights.IMAGENET1K_V1)
File ~/.cache/torch/hub/pytorch_vision_main/hubconf.py:4, in <module>
      1 # Optional list of dependencies required by the package
      2 dependencies = ["torch"]
----> 4 from torchvision.models import get_model_weights, get_weight
      5 from torchvision.models.alexnet import alexnet
      6 from torchvision.models.convnext import convnext_base, convnext_large, convnext_small, convnext_tiny

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

Could you update to the latest release, as it seems to be a known issue which was already fixed as described here?

Hello @ptrblck I use the latest torch and torchvision, pip freeze shown as followings:

torch==1.12.1
torchaudio==0.12.1
torchvision==0.13.1

the error remains, I also tried the link you mentioned switch torchvision==0.13.0, still, the error is shown below

----> 4 from torchvision.models import get_model_weights, get_weight
      5 from torchvision.models.alexnet import alexnet
      6 from torchvision.models.convnext import convnext_base, convnext_large, convnext_small, convnext_tiny

ImportError: cannot import name 'get_model_weights' from 'torchvision.models'
 (/Users/sniu/.venv/tf/lib/python3.8/site-packages/torchvision/models/__init__.py)

do you still have any insights?

I guess you might have multiple torchvision installs in your current environment and the latest one might not be picked up. Could you check the torchvision.__version__ and post the code which raises the error, please?

Thank you, I use ipython

In [2]: import torchvision
In [3]: torchvision.__version__
Out[3]: '0.13.0'

and checked my pip freeze | grep torchvision, the terminal shows as:
torchvision==0.13.0
I still encounter this ‘get_model_weights’ error, which is very weird. I also used another linux machine with different torch version, install a pytorch in a new conda env, pip freeze shows as followings:

torchvision==0.11.2+cu113

and in python I confirmed the torchvision.version is ‘0.11.2+cu113’. Same error exists when I do torch.hub.load

I still don’t know which command is causing the error, but using torch.hub.load works for torchvision==0.13.0 and ==0.13.1:

>>> import torch
>>> torch.__version__
'1.12.0+cu116'
>>> import torchvision
>>> torchvision.__version__
'0.13.0+cu116'
>>> model = torch.hub.load("pytorch/vision", "resnet18", pretrained=True)
>>> print(type(model))
<class 'torchvision.models.resnet.ResNet'>
>>> import torch
>>> torch.__version__
'1.12.1+cu116'
>>> import torchvision
>>> torchvision.__version__
'0.13.1+cu116'
>>> import torch.hub
>>> model = torch.hub.load("pytorch/vision", "resnet18", pretrained=True)
>>> print(type(model))
<class 'torchvision.models.resnet.ResNet'>

Hello, I got error with torchvision==0.13.1. Any idea how to fix it. I create conda env with Python 3.9

>>> import torch
i>>> import torchvision
>>> torch.__version__
'1.12.1+cu102'
>>> torchvision.__version__
'0.13.1+cu102'
>>> import torch.hub
>>> model = torch.hub.load("pytorch/vision", "resnet18", pretrained=True)
Using cache found in /home/user/.cache/torch/hub/pytorch_vision_main
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/anaconda3/envs/pip_torchvs/lib/python3.9/site-packages/torch/hub.py", line 540, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "/home/user/anaconda3/envs/pip_torchvs/lib/python3.9/site-packages/torch/hub.py", line 566, in _load_local
    hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
  File "/home/user/anaconda3/envs/pip_torchvs/lib/python3.9/site-packages/torch/hub.py", line 89, in _import_module
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/user/.cache/torch/hub/pytorch_vision_main/hubconf.py", line 4, in <module>
    from torchvision.models import get_model_weights, get_weight
ImportError: cannot import name 'get_model_weights' from 'torchvision.models' (/home/user/anaconda3/envs/pip_torchvs/lib/python3.9/site-packages/torchvision/models/__init__.py)

You could try to delete your cache folder and rerun the code. I don’t know how old the checkpoint stored there is and if there was a BC-breaking change in torchvision. Let me know if this works.

I am getting this exact same error with torchvision 0.13.1+cu116 and torch 1.12.1+cu116 - tried all tips but downgrading :slight_smile:

1 Like