Import error while using torch.hub.load

debda018: <pre><code class="lang-auto"># passing weights param as string
model = torch.hub.load("pytorch/vision", "resnet18", weights= ResNet18_Weights.IMAGENET1K_V1)
</code></pre>
<pre><code class="lang-auto">File ~/.cache/torch/hub/pytorch_vision_main/hubconf.py:4, in &lt;module&gt;
      1 # Optional list of dependencies required by the package
      2 dependencies = ["torch"]
----&gt; 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' 
</code></pre>

ptrblck: <p>Could you update to the latest release, as it seems to be a known issue which was already fixed as described <a href="https://discuss.pytorch.org/t/when-i-test-the-torch-hub-load-some-error/152054/12">here</a>?</p>

sufengniu: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a> I use the latest torch and torchvision, pip freeze shown as followings:</p>
<pre><code class="lang-auto">torch==1.12.1
torchaudio==0.12.1
torchvision==0.13.1
</code></pre>
<p>the error remains, I also tried the link you mentioned switch torchvision==0.13.0, still, the error is shown below</p>
<pre><code class="lang-auto">----&gt; 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)
</code></pre>
<p>do you still have any insights?</p>

ptrblck: <p>I guess you might have multiple <code>torchvision</code> installs in your current environment and the latest one might not be picked up. Could you check the <code>torchvision.__version__</code> and post the code which raises the error, please?</p>

sufengniu: <aside class="quote no-group quote-modified" data-username="ptrblck" data-post="4" data-topic="159690">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>torchvision.<strong>version</strong></p>
</blockquote>
</aside>
<p>Thank you, I use ipython</p>
<pre><code class="lang-auto">In [2]: import torchvision
In [3]: torchvision.__version__
Out[3]: '0.13.0'
</code></pre>
<p>and checked my pip freeze | grep torchvision, the terminal shows as:<br>
torchvision==0.13.0<br>
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:</p>
<pre><code class="lang-auto">torchvision==0.11.2+cu113
</code></pre>
<p>and in python I confirmed the torchvision.<strong>version</strong> is ‘0.11.2+cu113’. Same error exists when I do torch.hub.load</p>

ptrblck: <p>I still don’t know which command is causing the error, but using <code>torch.hub.load</code> works for <code>torchvision==0.13.0</code> and <code>==0.13.1</code>:</p>
<pre><code class="lang-python">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.12.0+cu116'
&gt;&gt;&gt; import torchvision
&gt;&gt;&gt; torchvision.__version__
'0.13.0+cu116'
&gt;&gt;&gt; model = torch.hub.load("pytorch/vision", "resnet18", pretrained=True)
&gt;&gt;&gt; print(type(model))
&lt;class 'torchvision.models.resnet.ResNet'&gt;
</code></pre>
<pre><code class="lang-python">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.12.1+cu116'
&gt;&gt;&gt; import torchvision
&gt;&gt;&gt; torchvision.__version__
'0.13.1+cu116'
&gt;&gt;&gt; import torch.hub
&gt;&gt;&gt; model = torch.hub.load("pytorch/vision", "resnet18", pretrained=True)
&gt;&gt;&gt; print(type(model))
&lt;class 'torchvision.models.resnet.ResNet'&gt;
</code></pre>

trinhgiahuy: <p>Hello, I got error with torchvision==0.13.1. Any idea how to fix it. I create conda env with Python 3.9</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
i&gt;&gt;&gt; import torchvision
&gt;&gt;&gt; torch.__version__
'1.12.1+cu102'
&gt;&gt;&gt; torchvision.__version__
'0.13.1+cu102'
&gt;&gt;&gt; import torch.hub
&gt;&gt;&gt; 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 "&lt;stdin&gt;", line 1, in &lt;module&gt;
  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 "&lt;frozen importlib._bootstrap_external&gt;", line 850, in exec_module
  File "&lt;frozen importlib._bootstrap&gt;", line 228, in _call_with_frames_removed
  File "/home/user/.cache/torch/hub/pytorch_vision_main/hubconf.py", line 4, in &lt;module&gt;
    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)
</code></pre>

ptrblck: <p>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 <code>torchvision</code>. Let me know if this works.</p>

epichub: <p>I am getting this exact same error with torchvision 0.13.1+cu116 and torch 1.12.1+cu116 - tried all tips but downgrading <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

murat: <p>Try it as below:<br>
model = torch.hub.load(“pytorch/vision:v0.13.1”, “resnet18”, pretrained=True)</p>