Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named ‘torchvision.models.utils‘

When I use the environment of pytorch=1.10.0, torchvision=0.11.1 to run the code, I run to the statement from torchvision.models.utils import load_state_dict_from_url. The following error will appear when:

>>> from torchvision.models.utils import load_state_dict_from_url
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torchvision.models.utils'
like image 951
Oscar Rangel Avatar asked Mar 11 '26 12:03

Oscar Rangel


1 Answers

After consulting torchvision's code repository, there is a solution:

Note that this syntax is only for higher versions of PyTorch.

The original code from .utils import load_state_dict_from_url is not applicable. you cannot import load_state_dict_from_url from .utils.

change .utils to torch.hub can fix the problem.

from torch.hub import load_state_dict_from_url

This worked for me.

like image 108
Oscar Rangel Avatar answered Mar 13 '26 01:03

Oscar Rangel