Problem with loading models from torch.hub pytorch/vision in pytest

gorodnitskiy: <p>Hi!</p>
<p>I’m trying to load models from “pytorch/vision” repo through torch.hub, but each time I get such error:<br>
Downloading: “<a href="https://github.com/pytorch/vision/zipball/main" rel="noopener nofollow ugc">https://github.com/pytorch/vision/zipball/main</a>” to …/…/…/.cache/torch/hub/main.zip …/…/…/.cache/torch/hub/pytorch_vision_main/torchvision/io/image.py:13: UserWarning: Failed to load image Python extension: warn(f"Failed to load image Python extension: {e}")</p>
<p>Internet connection works fine. Also, I’ve tried few VPNs.</p>
<p>I use the latest torch and torchvision versions:<br>
torch==1.13.1<br>
torchvision==0.14.1</p>
<p>Python 3.10.8<br>
OS: MacOS</p>
<p>Also, I’ve tried to remove “.cache/torch/hub/” directory and re-run script - it doesn’t work.</p>
<p>Code example:</p>
<pre><code class="lang-auto">import torch

model = torch.hub.load("pytorch/vision", "regnet_x_400mf")
</code></pre>
<p>Is it a bug or maybe I do something wrong?</p>

ptrblck: <p>The warning is unrelated to the download and raised by <code>torchvision</code> as it cannot import the built-in image extension.<br>
Are you seeing any issues in the actual model or were you only concerned about seeing this warning?</p>

gorodnitskiy: <p>Hi! <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>Thanks for response!</p>
<p>I found a problem.</p>
<p>This code works well as early:</p>
<pre><code class="lang-auto">model = torch.hub.load("pytorch/vision", "regnet_x_400mf")
model.eval()

tensor = torch.randn((2, 3, 224, 224))
output = model.forward(tensor)
print(output.size())
</code></pre>
<p>But when I’m trying to run it under pytest - it falls with import error:</p>
<pre><code class="lang-auto">def test_torch_hub_load():
    model = torch.hub.load("pytorch/vision", "regnet_x_400mf")
    model.eval()

    tensor = torch.randn((2, 3, 224, 224))
    output = model.forward(tensor)
    print(output.size())
</code></pre>
<p>Error:</p>
<pre><code class="lang-auto">___________________________________________________________________________ test_torch_hub_load ___________________________________________________________________________

    def test_torch_hub_load():
&gt;       model = torch.hub.load("pytorch/vision", "regnet_x_400mf")

tests/test_models.py:45: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../lib/python3.10/site-packages/torch/hub.py:542: in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
../../lib/python3.10/site-packages/torch/hub.py:569: in _load_local
    hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
../../lib/python3.10/site-packages/torch/hub.py:90: in _import_module
    spec.loader.exec_module(module)
&lt;frozen importlib._bootstrap_external&gt;:883: in exec_module
    ???
&lt;frozen importlib._bootstrap&gt;:241: in _call_with_frames_removed
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    # Optional list of dependencies required by the package
    dependencies = ["torch"]
    
    from torchvision.models import get_model_weights, get_weight
    from torchvision.models.alexnet import alexnet
    from torchvision.models.convnext import convnext_base, convnext_large, convnext_small, convnext_tiny
    from torchvision.models.densenet import densenet121, densenet161, densenet169, densenet201
    from torchvision.models.efficientnet import (
        efficientnet_b0,
        efficientnet_b1,
        efficientnet_b2,
        efficientnet_b3,
        efficientnet_b4,
        efficientnet_b5,
        efficientnet_b6,
        efficientnet_b7,
        efficientnet_v2_l,
        efficientnet_v2_m,
        efficientnet_v2_s,
    )
    from torchvision.models.googlenet import googlenet
    from torchvision.models.inception import inception_v3
    from torchvision.models.maxvit import maxvit_t
    from torchvision.models.mnasnet import mnasnet0_5, mnasnet0_75, mnasnet1_0, mnasnet1_3
    from torchvision.models.mobilenetv2 import mobilenet_v2
    from torchvision.models.mobilenetv3 import mobilenet_v3_large, mobilenet_v3_small
    from torchvision.models.optical_flow import raft_large, raft_small
    from torchvision.models.regnet import (
        regnet_x_16gf,
        regnet_x_1_6gf,
        regnet_x_32gf,
        regnet_x_3_2gf,
        regnet_x_400mf,
        regnet_x_800mf,
        regnet_x_8gf,
        regnet_y_128gf,
        regnet_y_16gf,
        regnet_y_1_6gf,
        regnet_y_32gf,
        regnet_y_3_2gf,
        regnet_y_400mf,
        regnet_y_800mf,
        regnet_y_8gf,
    )
    from torchvision.models.resnet import (
        resnet101,
        resnet152,
        resnet18,
        resnet34,
        resnet50,
        resnext101_32x8d,
        resnext101_64x4d,
        resnext50_32x4d,
        wide_resnet101_2,
        wide_resnet50_2,
    )
    from torchvision.models.segmentation import (
        deeplabv3_mobilenet_v3_large,
        deeplabv3_resnet101,
        deeplabv3_resnet50,
        fcn_resnet101,
        fcn_resnet50,
        lraspp_mobilenet_v3_large,
    )
    from torchvision.models.shufflenetv2 import (
        shufflenet_v2_x0_5,
        shufflenet_v2_x1_0,
        shufflenet_v2_x1_5,
        shufflenet_v2_x2_0,
    )
    from torchvision.models.squeezenet import squeezenet1_0, squeezenet1_1
    from torchvision.models.swin_transformer import swin_b, swin_s, swin_t, swin_v2_b, swin_v2_s, swin_v2_t
    from torchvision.models.vgg import vgg11, vgg11_bn, vgg13, vgg13_bn, vgg16, vgg16_bn, vgg19, vgg19_bn
&gt;   from torchvision.models.video import (
        mc3_18,
        mvit_v1_b,
        mvit_v2_s,
        r2plus1d_18,
        r3d_18,
        s3d,
        swin3d_b,
        swin3d_s,
        swin3d_t,
    )
E   ImportError: cannot import name 'swin3d_b' from 'torchvision.models.video' (../../lib/python3.10/site-packages/torchvision/models/video/__init__.py)

../../.cache/torch/hub/pytorch_vision_main/hubconf.py:74: ImportError
</code></pre>
<p>However, when I’m removing torchvision, it works fine in both cases.</p>
<p>In general it’s not a problem for me, just weird behaviour. Hope it could be useful for someone. Thanks!</p>

Marcus_Wong: <p>torch : 1.13.1+cu117<br>
torchvision : 0.14.1+cu117<br>
Python: 3.10.6<br>
Env: WSL2</p>
<p>The same error after running below :</p>
<pre><code class="lang-auto">import torch
from torch import hub
# resnet18_model = hub.load('pytorch/vision:master', 
#                           'resnet18',
#                           pretrained=True)
from torchvision import models
resnet18_model = hub.load('pytorch/vision:main', 
                          'resnet18',
                          weights=models.ResNet18_Weights.DEFAULT)
</code></pre>
<p>Error:</p>
<pre><code class="lang-auto">Using cache found in /root/.cache/torch/hub/pytorch_vision_main
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[46], line 7
      3 # resnet18_model = hub.load('pytorch/vision:master', 
      4 #                           'resnet18',
      5 #                           pretrained=True)
      6 from torchvision import models
----&gt; 7 resnet18_model = hub.load('pytorch/vision:main', 
      8                           'resnet18',
      9                           weights=models.ResNet18_Weights.DEFAULT)

File /usr/local/lib/python3.10/dist-packages/torch/hub.py:542, in load(repo_or_dir, model, source, trust_repo, force_reload, verbose, skip_validation, *args, **kwargs)
    538 if source == 'github':
    539     repo_or_dir = _get_cache_or_reload(repo_or_dir, force_reload, trust_repo, "load",
    540                                        verbose=verbose, skip_validation=skip_validation)
--&gt; 542 model = _load_local(repo_or_dir, model, *args, **kwargs)
    543 return model

File /usr/local/lib/python3.10/dist-packages/torch/hub.py:569, in _load_local(hubconf_dir, model, *args, **kwargs)
    566 sys.path.insert(0, hubconf_dir)
    568 hubconf_path = os.path.join(hubconf_dir, MODULE_HUBCONF)
--&gt; 569 hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
    571 entry = _load_entry_from_hubconf(hub_module, model)
    572 model = entry(*args, **kwargs)

File /usr/local/lib/python3.10/dist-packages/torch/hub.py:90, in _import_module(name, path)
     88 module = importlib.util.module_from_spec(spec)
     89 assert isinstance(spec.loader, Loader)
---&gt; 90 spec.loader.exec_module(module)
     91 return module

File &lt;frozen importlib._bootstrap_external&gt;:883, in exec_module(self, module)

File &lt;frozen importlib._bootstrap&gt;:241, in _call_with_frames_removed(f, *args, **kwds)

File ~/.cache/torch/hub/pytorch_vision_main/hubconf.py:74
     72 from torchvision.models.swin_transformer import swin_b, swin_s, swin_t, swin_v2_b, swin_v2_s, swin_v2_t
     73 from torchvision.models.vgg import vgg11, vgg11_bn, vgg13, vgg13_bn, vgg16, vgg16_bn, vgg19, vgg19_bn
---&gt; 74 from torchvision.models.video import (
     75     mc3_18,
     76     mvit_v1_b,
     77     mvit_v2_s,
     78     r2plus1d_18,
     79     r3d_18,
     80     s3d,
     81     swin3d_b,
     82     swin3d_s,
     83     swin3d_t,
     84 )
     85 from torchvision.models.vision_transformer import vit_b_16, vit_b_32, vit_h_14, vit_l_16, vit_l_32

ImportError: cannot import name 'swin3d_b' from 'torchvision.models.video' (/usr/local/lib/python3.10/dist-packages/torchvision/models/video/__init__.py)
</code></pre>

ptrblck: <p>The <code>swin</code> models were added ~2 months ago in <a href="https://github.com/pytorch/vision/commit/372f4faeeb89d644705891be411499fc750d571c">this PR</a> by <a class="mention" href="/u/oke-aditya">@oke-aditya</a>.<br>
Aditya, is PyTorch supposed to pull the latest <code>hubconf.py</code> even in older <code>torchvision</code> versions?<br>
If so, new models might need to be guarded to avoid import errors.<br>
CC <a class="mention" href="/u/pmeier">@pmeier</a></p>

pmeier: <p><a class="mention" href="/u/marcus_wong">@Marcus_Wong</a> I’ve replied in the issue you opened: <a href="https://github.com/pytorch/vision/issues/7397" class="inline-onebox" rel="noopener nofollow ugc">Cannot import name 'swin3d_b' from 'torchvision.models.video' · Issue #7397 · pytorch/vision · GitHub</a></p>

Marcus_Wong: <p><a class="mention" href="/u/pmeier">@pmeier</a> thanks for your detail explain.</p>