PyTorch Model zoo and pretrained models not loading in Colab

I am trying to load models from the model zoo in Google Colab and keep getting the errors:

AttributeError: module 'torch.jit' has no attribute 'unused'

I was trying to load 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?

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.

@ptrblck @apaszke any suggestions?

Thanks for raising this issue!
It seems the currently linked model cannot be imported using the latest stable release (1.2.0).
However, after installing the nightly build, the model was loaded successfully in my colab notebook.
Try to run the following lines:

!pip uninstall torch -y
!pip install --pre torch torchvision -f
# Restart of runtime might be necessary 
import torch
model = torch.hub.load('pytorch/vision', 'deeplabv3_resnet101', pretrained=True)
1 Like

Thanks! It worked. When will we see more models? Segmentation has only 2!

Good to hear it’s working!
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. :slight_smile:

1 Like

Hi @ptrblck, 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?

You should be able to import all submodules from torchvision. Which error are you getting?

I am getting this error while importing torchvision:

AttributeError                            Traceback (most recent call last)
<ipython-input-159-07ce7fca6932> in <module>
      5 from import Dataset, DataLoader
      6 from PIL import Image
----> 7 import torchvision

~/.cache/torch/hub/pytorch_vision_master/torchvision/ in <module>
      3 from .extension import _HAS_OPS
----> 5 from torchvision import models
      6 from torchvision import datasets
      7 from torchvision import ops

~/.cache/torch/hub/pytorch_vision_master/torchvision/models/ in <module>
     10 from .shufflenetv2 import *
     11 from . import segmentation
---> 12 from . import detection
     13 from . import video
     14 from . import quantization

~/.cache/torch/hub/pytorch_vision_master/torchvision/models/detection/ in <module>
----> 1 from .faster_rcnn import *
      2 from .mask_rcnn import *
      3 from .keypoint_rcnn import *

~/.cache/torch/hub/pytorch_vision_master/torchvision/models/detection/ in <module>
      5 import torch.nn.functional as F
----> 7 from torchvision.ops import misc as misc_nn_ops
      8 from torchvision.ops import MultiScaleRoIAlign

~/.cache/torch/hub/pytorch_vision_master/torchvision/ops/ in <module>
----> 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/ in <module>
---> 42 @torch.jit._script_if_tracing
     43 def batched_nms(
     44     boxes: Tensor,

AttributeError: module 'torch.jit' has no attribute '_script_if_tracing'

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?

This is most likely caused by diverged torchvision and PyTorch versions.
Based on the error message it seems you might be using a newer torchvision installation with an older PyTorch version, which doesn’t provide the torch.jit._script_if_tracing context manager.

Could you update both libs to the stable or nightly versions and rerun the code?

Thanks @ptrblck, downgrading torchvision to 0.5.0 worked!

Good to hear it’s working now!
I would recommend to rather update PyTorch than downgrade torchvision to get the latest features and bug fixes. :wink: