When i test the torch.hub.load some error

my code is

import torch
repo = 'pytorch/vision'
model = torch.hub.load(repo, 'resnet50', pretrained=True)

the error is
ImportError: cannot import name 'get_weight' from 'torchvision.models' (C:\ProgramData\Anaconda3\lib\site-packages\torchvision\models\__init__.py)

what’s wrong
is the program updated?

I cannot reproduce the error and get a valid model using the latest PyTorch and torchvision nightly binaries. Are you mixing different PyTorch and torchvision installations?

i think so , this error looks like the package version is wrong, but i install the use conda, maybe i should check it.

i have checked my version on conda, the PyTorch is 1.11.0 and the torchvision is 0.12.0
should i change it?

1 Like

I can reproduce it in these versions and it seems that the latest hubconf.py file is being downloaded, which has recently (3 days ago) added the get_weight import.
I don’t quite understand the workflow as I would have assumed the packaged hubconf.py would be used, but I see that the one from the vision/main is being downloaded to the .cache directory.
Even after removing the import the script will fail with imports of unknown models which were not available in these stable releases.

CC @pmeier is this behavior expected?

1 Like

that’s true , i don’t know how to fix it

As a workaround you could install the latest nightly releases which I would assume should work.

1 Like

Hi! I tried this but have not had any luck.

model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=True)

Using Torchvision :: Anaconda.org to update to the nightly build does not change my versions of PyTorch or Torchvision

Both are what are listed above - torchvision 0.12.0 and PyTorch 1.11.0.

my environment
# packages in environment at D:\Anaconda\envs\fastai:
#
# Name                    Version                   Build  Channel
anyio                     3.6.1                    pypi_0    pypi
argon2-cffi               21.3.0             pyhd3eb1b0_0
argon2-cffi-bindings      21.2.0          py310h2bbff1b_0
asttokens                 2.0.5              pyhd3eb1b0_0
attrs                     21.4.0             pyhd3eb1b0_0
babel                     2.10.3                   pypi_0    pypi
backcall                  0.2.0              pyhd3eb1b0_0
beautifulsoup4            4.11.1          py310haa95532_0
blas                      1.0                         mkl
bleach                    4.1.0              pyhd3eb1b0_0
blosc                     1.21.0               h19a0ad4_0
bottleneck                1.3.4           py310h9128911_0
brotli                    1.0.9                ha925a31_2
brotlipy                  0.7.0           py310h2bbff1b_1002
bzip2                     1.0.8                he774522_0
ca-certificates           2022.4.26            haa95532_0
catalogue                 2.0.7           py310haa95532_0
certifi                   2022.6.15       py310haa95532_0
cffi                      1.15.0          py310h2bbff1b_1
cfitsio                   3.470                he774522_6
charls                    2.2.0                h6c2663c_0
charset-normalizer        2.0.4              pyhd3eb1b0_0
click                     8.0.4           py310haa95532_0
cloudpickle               2.0.0              pyhd3eb1b0_0
colorama                  0.4.4              pyhd3eb1b0_0
cryptography              37.0.1          py310h21b164f_0
cudatoolkit               11.3.1               h59b6b97_2
cycler                    0.11.0             pyhd3eb1b0_0
cymem                     2.0.6           py310hd77b12b_0
cython-blis               0.7.7           py310h9128911_0
cytoolz                   0.11.0          py310h2bbff1b_0
dask-core                 2022.5.0        py310haa95532_0
debugpy                   1.5.1           py310hd77b12b_0
decorator                 5.1.1              pyhd3eb1b0_0
defusedxml                0.7.1              pyhd3eb1b0_0
entrypoints               0.4             py310haa95532_0
executing                 0.8.3              pyhd3eb1b0_0
fastai                    2.6.3                      py_0    fastai
fastbook                  0.0.24                     py_0    fastai
fastcore                  1.4.4                      py_0    fastai
fastdownload              0.0.6                      py_0    fastai
fastprogress              1.0.2                      py_0    fastai
fonttools                 4.25.0             pyhd3eb1b0_0
freetype                  2.10.4               hd328e21_0
fsspec                    2022.3.0        py310haa95532_0
giflib                    5.2.1                h62dcd97_0
graphviz                  2.38                 hfd603c8_2
icc_rt                    2019.0.0             h0cc432a_1
icu                       58.2                 ha925a31_3
idna                      3.3                pyhd3eb1b0_0
imagecodecs               2021.8.26       py310h6cc35ac_0
imageio                   2.9.0              pyhd3eb1b0_0
intel-openmp              2021.4.0          haa95532_3556
ipykernel                 6.9.1           py310haa95532_0
ipython                   8.3.0           py310haa95532_0
ipython_genutils          0.2.0              pyhd3eb1b0_1
ipywidgets                7.6.5              pyhd3eb1b0_1
jedi                      0.18.1          py310haa95532_1
jinja2                    3.0.3              pyhd3eb1b0_0
joblib                    1.1.0              pyhd3eb1b0_0
jpeg                      9e                   h2bbff1b_0
json5                     0.9.8                    pypi_0    pypi
jsonschema                4.4.0           py310haa95532_0
jupyter-server            1.17.1                   pypi_0    pypi
jupyter_client            7.2.2           py310haa95532_0
jupyter_core              4.10.0          py310haa95532_0
jupyterlab-server         2.14.0                   pypi_0    pypi
jupyterlab_pygments       0.1.2                      py_0
jupyterlab_widgets        1.0.0              pyhd3eb1b0_1
kiwisolver                1.4.2           py310hd77b12b_0
langcodes                 3.3.0              pyhd3eb1b0_0
lcms2                     2.12                 h83e58a3_0
lerc                      3.0                  hd77b12b_0
libaec                    1.0.4                h33f27b4_1
libdeflate                1.8                  h2bbff1b_5
libffi                    3.4.2                h604cdb4_1
libpng                    1.6.37               h2a8f88b_0
libtiff                   4.2.0                hd0e1b90_0
libuv                     1.40.0               he774522_0
libwebp                   1.2.2                h2bbff1b_0
libzopfli                 1.0.3                ha925a31_0
locket                    1.0.0           py310haa95532_0
lz4-c                     1.9.3                h2bbff1b_1
markupsafe                2.0.1           py310h2bbff1b_0
matplotlib                3.5.1           py310haa95532_1
matplotlib-base           3.5.1           py310hd77b12b_1
matplotlib-inline         0.1.2              pyhd3eb1b0_2
mistune                   0.8.4           py310h2bbff1b_1000
mkl                       2021.4.0           haa95532_640
mkl-service               2.4.0           py310h2bbff1b_0
mkl_fft                   1.3.1           py310ha0764ea_0
mkl_random                1.2.2           py310h4ed8f06_0
munkres                   1.1.4                      py_0
murmurhash                1.0.7           py310hd77b12b_0
nbclient                  0.5.13          py310haa95532_0
nbconvert                 6.5.0                    pypi_0    pypi
nbformat                  5.3.0           py310haa95532_0
nest-asyncio              1.5.5           py310haa95532_0
networkx                  2.7.1              pyhd3eb1b0_0
notebook                  6.4.11          py310haa95532_0
numexpr                   2.8.1           py310hb57aa6b_1
numpy                     1.22.3          py310h6d2d95c_0
numpy-base                1.22.3          py310h206c741_0
openjpeg                  2.4.0                h4fc8c34_0
openssl                   1.1.1o               h2bbff1b_0
packaging                 21.3               pyhd3eb1b0_0
pandas                    1.4.2           py310hd77b12b_0
pandocfilters             1.5.0              pyhd3eb1b0_0
parso                     0.8.3              pyhd3eb1b0_0
partd                     1.2.0              pyhd3eb1b0_1
pathy                     0.6.1           py310haa95532_0
pickleshare               0.7.5           pyhd3eb1b0_1003
pillow                    9.0.1           py310hdc2b20a_0
pip                       21.2.4          py310haa95532_0
preshed                   3.0.6           py310h6c2663c_0
prometheus_client         0.13.1             pyhd3eb1b0_0
prompt-toolkit            3.0.20             pyhd3eb1b0_0
pure_eval                 0.2.2              pyhd3eb1b0_0
pycparser                 2.21               pyhd3eb1b0_0
pydantic                  1.9.0           py310h2bbff1b_1
pygments                  2.11.2             pyhd3eb1b0_0
pyopenssl                 22.0.0             pyhd3eb1b0_0
pyparsing                 3.0.4              pyhd3eb1b0_0
pyqt                      5.9.2           py310hd77b12b_6
pyrsistent                0.18.0          py310h2bbff1b_0
pysocks                   1.7.1           py310haa95532_0
python                    3.10.4               hbb2ffb3_0
python-dateutil           2.8.2              pyhd3eb1b0_0
python-fastjsonschema     2.15.1             pyhd3eb1b0_0
python-graphviz           0.16               pyhd3eb1b0_1
pytorch                   1.11.0          py3.10_cuda11.3_cudnn8_0    pytorch
pytorch-mutex             1.0                        cuda    pytorch
pytz                      2021.3             pyhd3eb1b0_0
pywavelets                1.3.0           py310h2bbff1b_0
pywin32                   302             py310h2bbff1b_2
pywinpty                  2.0.2           py310h5da7b33_0
pyyaml                    6.0             py310h2bbff1b_0
pyzmq                     22.3.0          py310hd77b12b_2
qt                        5.9.7            vc14h73c81de_0
requests                  2.27.1             pyhd3eb1b0_0
scikit-image              0.19.2          py310h4ed8f06_0
scikit-learn              1.0.2           py310h4ed8f06_0
scipy                     1.7.3           py310h6d2d95c_0
send2trash                1.8.0              pyhd3eb1b0_1
sentencepiece             0.1.96                   pypi_0    pypi
setuptools                61.2.0          py310haa95532_0
shellingham               1.3.1              pyhd3eb1b0_0
sip                       4.19.13         py310hd77b12b_0
six                       1.16.0             pyhd3eb1b0_1
smart_open                5.2.1           py310haa95532_0
snappy                    1.1.9                h6c2663c_0
sniffio                   1.2.0                    pypi_0    pypi
soupsieve                 2.3.1              pyhd3eb1b0_0
spacy                     3.3.0           py310hef0f399_0
spacy-legacy              3.0.9           py310haa95532_0
spacy-loggers             1.0.1              pyhd3eb1b0_0
sqlite                    3.38.3               h2bbff1b_0
srsly                     2.4.3           py310hd77b12b_0
stack_data                0.2.0              pyhd3eb1b0_0
terminado                 0.13.1          py310haa95532_0
testpath                  0.5.0              pyhd3eb1b0_0
thinc                     8.0.15          py310hf497b98_0
threadpoolctl             2.2.0              pyh0d69192_0
tifffile                  2021.7.2           pyhd3eb1b0_2
tinycss2                  1.1.1                    pypi_0    pypi
tk                        8.6.12               h2bbff1b_0
toolz                     0.11.2             pyhd3eb1b0_0
torchaudio                0.11.0              py310_cu113    pytorch
torchvision               0.12.0              py310_cu113    pytorch
tornado                   6.1             py310h2bbff1b_0
tqdm                      4.64.0          py310haa95532_0
traitlets                 5.1.1              pyhd3eb1b0_0
typer                     0.4.1           py310haa95532_0
typing-extensions         4.1.1                hd3eb1b0_0
typing_extensions         4.1.1              pyh06a4308_0
tzdata                    2022a                hda174b7_0
urllib3                   1.26.9          py310haa95532_0
vc                        14.2                 h21ff451_1
voila                     0.3.5                    pypi_0    pypi
vs2015_runtime            14.27.29016          h5e58377_2
wasabi                    0.9.1           py310haa95532_0
wcwidth                   0.2.5              pyhd3eb1b0_0
webencodings              0.5.1           py310haa95532_1
websocket-client          1.3.3                    pypi_0    pypi
websockets                10.3                     pypi_0    pypi
wheel                     0.37.1             pyhd3eb1b0_0
widgetsnbextension        3.5.2           py310haa95532_0
win_inet_pton             1.1.0           py310haa95532_0
wincertstore              0.2             py310haa95532_2
winpty                    0.4.3                         4
xz                        5.2.5                h8cc25b3_1
yaml                      0.2.5                he774522_0
zfp                       0.5.5                hd77b12b_6
zlib                      1.2.12               h8cc25b3_2
zstd                      1.4.9                h19a0ad4_0
----> 4 from torchvision.models import get_weight
      5 from torchvision.models.alexnet import alexnet
      6 from torchvision.models.convnext import convnext_tiny, convnext_small, convnext_base, convnext_large

ImportError: cannot import name 'get_weight' from 'torchvision.models' (d:\Anaconda\envs\fastai2\lib\site-packages\torchvision\models\__init__.py)

PyTorch nightly did not change anything, All requested packages already installed. Torchvision nightly only added nbconvert and updated certifi

Edit: Nevermind - sort of. I found out that it does work in Jupyter Notebook in Chrome in that environment - however in VS Code using the same environment it fails with the given error message.

Most likely your VS Code setup is using another environment and is thus failing while the Jupyter notebook uses the newer (and fixed) one.

I made sure they were both using the same environment, and it was a fresh environment so there was no chance that each was using a slightly different version. For the moment I can use Jupyter Notebooks I guess. And it works fine on other models, I just can’t load specific models like ResNet18.

model = torch.hub.load('mateuszbuda/brain-segmentation-pytorch', 'unet', in_channels=1, out_channels=1, init_features=8, pretrained=False)

for example works in either.

Hi. Torchvision models have changed since torchvision 0.13 released few days ago.
Can you try with torchvision 0.13?