PyTorch Model zoo and pretrained models not loading in Colab

username: bluesky314
cooked: <p>I am trying to load models from the model zoo in Google Colab and keep getting the errors:</p>
<p><code>AttributeError: module 'torch.jit' has no attribute 'unused'</code></p>
<p>I was trying to load <a href="https://pytorch.org/hub/pytorch_vision_deeplabv3_resnet101/" rel="nofollow noopener">https://pytorch.org/hub/pytorch_vision_deeplabv3_resnet101/</a> and used the offical link to open in Colab yet getting this error. Even if I try to import any model from torchvision I get the same error. How do I fix this to use a pretrained model?</p>
<p>I tried somethings suggested elsewhere to update pytorch to nightly and the one on google colab is already nightly and 1.2.0 so I dont know what more to do.</p>

username: bluesky314
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  <a class="mention" href="/u/apaszke">@apaszke</a> any suggestions?</p>

username: ptrblck
cooked: <p>Thanks for raising this issue!<br>
It seems the currently linked model cannot be imported using the latest stable release (<code>1.2.0</code>).<br>
However, after installing the nightly build, the model was loaded successfully in my colab notebook.<br>
Try to run the following lines:</p>
<pre><code class="lang-python">!pip uninstall torch -y
!pip install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cu100/torch_nightly.html
# Restart of runtime might be necessary 
import torch
model = torch.hub.load('pytorch/vision', 'deeplabv3_resnet101', pretrained=True)
model.eval()
</code></pre>

username: bluesky314
cooked: <p>Thanks! It worked. When will we see more models? Segmentation has only 2!</p>

username: ptrblck
cooked: <p>Good to hear it’s working!<br>
I’m not sure which models are currently being developed, but if you are missing some important model, we might assist you in contributing it to the hub. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: SHRAMAN_PRAMANICK
cooked: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>, I was facing the same problem during importing torchvision, and after installing nightly build, the problem was solved. I can import any pre-trained model using your code snippet,  but I also need to use other useful functions like torchvision.transforms, torchvision.utils etc. How can I load them using nightly build? Also, if there any modified way to import torchvision now?</p>

username: ptrblck
cooked: <p>You should be able to import all submodules from <code>torchvision</code>. Which error are you getting?</p>

username: SHRAMAN_PRAMANICK
cooked: <p>I am getting this error while importing torchvision:</p>
<pre><code class="lang-auto">AttributeError                            Traceback (most recent call last)
&lt;ipython-input-159-07ce7fca6932&gt; in &lt;module&gt;
      5 from torch.utils.data import Dataset, DataLoader
      6 from PIL import Image
----&gt; 7 import torchvision
      8 
      9 

~/.cache/torch/hub/pytorch_vision_master/torchvision/__init__.py in &lt;module&gt;
      3 from .extension import _HAS_OPS
      4 
----&gt; 5 from torchvision import models
      6 from torchvision import datasets
      7 from torchvision import ops

~/.cache/torch/hub/pytorch_vision_master/torchvision/models/__init__.py in &lt;module&gt;
     10 from .shufflenetv2 import *
     11 from . import segmentation
---&gt; 12 from . import detection
     13 from . import video
     14 from . import quantization

~/.cache/torch/hub/pytorch_vision_master/torchvision/models/detection/__init__.py in &lt;module&gt;
----&gt; 1 from .faster_rcnn import *
      2 from .mask_rcnn import *
      3 from .keypoint_rcnn import *

~/.cache/torch/hub/pytorch_vision_master/torchvision/models/detection/faster_rcnn.py in &lt;module&gt;
      5 import torch.nn.functional as F
      6 
----&gt; 7 from torchvision.ops import misc as misc_nn_ops
      8 from torchvision.ops import MultiScaleRoIAlign
      9 

~/.cache/torch/hub/pytorch_vision_master/torchvision/ops/__init__.py in &lt;module&gt;
----&gt; 1 from .boxes import nms, box_iou
      2 from .new_empty_tensor import _new_empty_tensor
      3 from .deform_conv import deform_conv2d, DeformConv2d
      4 from .roi_align import roi_align, RoIAlign
      5 from .roi_pool import roi_pool, RoIPool

~/.cache/torch/hub/pytorch_vision_master/torchvision/ops/boxes.py in &lt;module&gt;
     40 
     41 
---&gt; 42 @torch.jit._script_if_tracing
     43 def batched_nms(
     44     boxes: Tensor,

AttributeError: module 'torch.jit' has no attribute '_script_if_tracing'
</code></pre>
<p>P.S I am working in a virtual environment in a remote server. In google colab, I could import torchvision. What may be the reason?</p>

username: ptrblck
cooked: <p>This is most likely caused by diverged <code>torchvision</code> and PyTorch versions.<br>
Based on the error message it seems you might be using a newer <code>torchvision</code> installation with an older PyTorch version, which doesn’t provide the <code>torch.jit._script_if_tracing</code> context manager.</p>
<p>Could you update both libs to the stable or nightly versions and rerun the code?</p>

username: SHRAMAN_PRAMANICK
cooked: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a>, downgrading torchvision to 0.5.0 worked!</p>

username: ptrblck
cooked: <p>Good to hear it’s working now!<br>
I would recommend to rather update PyTorch than downgrade <code>torchvision</code> to get the latest features and bug fixes. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>