Cannot import name 'ResNet50_Weights' from 'torchvision.models.resnet'
Asked Answered
F

4

6

I was previously loading a ResNet model with the ResNet50_Weights parameter successfully, but then suddenly I started getting the following error:

Traceback (most recent call last):
  File "splitting_models.py", line 3, in <module>
    from torchvision.models.resnet import ResNet50_Weights
ImportError: cannot import name 'ResNet50_Weights' from 'torchvision.models.resnet' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torchvision/models/resnet.py)

Here is the import:

from torchvision.models import ResNet50_Weights

How could I go about fixing this?

PyTorch version: 1.2.0

TorchVision version: 0.4.0

EDIT

Upgrading using

pip install --upgrade torch torchvision

to the following versions fixed the issue:

PyTorch version: 1.12.0

TorchVision version: 0.13.0

Felishafelita answered 18/7, 2022 at 22:55 Comment(2)
Specify which version of torchvision and pytorch you are using? Try if upgrading/downgrading torchvision to a different version fixes the issue.Context
That did the trick, thank you!Felishafelita
B
3

The problem took me some days to solve it. Before running a code containing the Pytorch models, make sure you are connected to a stable network. This because for the first time when you are running a Pytorch model such resnet50, alexnet, resnet18 it downloads its' functionalities, so incase of installation error it caches its downloads and draws such error, if you try to re-run. To solve the problem locate where the cache file is, do delete it and try to re-run the code having a stable network. for my case:

C:\Users\user/.cache\torch\hub\checkpoints\resnet18-f37072fd.pth

this where the is cached file was, delete it and try to re-run having a stable network.

I hope it will help. Thanks regards,

Berth answered 20/7, 2022 at 9:46 Comment(0)
C
0

You can try upgrading/downgrading torchvision to a different version. It may fix the issue.

Context answered 20/7, 2022 at 0:49 Comment(0)
C
0

Fixed it by getting the cuda version. Already answered but just upgrading wasnt enough for me.

When checking the version

import torchvision
torchvision.__version__

I would get

'0.17.0+cpu'

Then uninstalled and installed it again and it installed the right version and was able to import ResNet18_Weights

pip3 uninstall torchvision

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

New version:

'0.17.0+cu121'

from torchvision.models import resnet18, ResNet18_Weights

Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to C:\Users\USER/.cache\torch\hub\checkpoints\resnet18-f37072fd.pth 100.0%

Crib answered 11/2, 2024 at 12:31 Comment(0)
A
-1

First, try to dd

from torchvision.models import ResNet50_Weights

Adley answered 29/10, 2024 at 7:58 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Lesialesion

© 2022 - 2025 — McMap. All rights reserved.