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