# 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'
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?
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
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.