Using torchvision.models with fastai

I’m trying to use a torchvision.models model in my image segmentation project but can’t seem to get it working. Specifically I’m trying to use the deeplabv3_resnet101 model in my code since deeplabv3 is a segmentation specific model. I’ve gotten it working using the included resnet34 model within fastai:

learn = unet_learner(data, models.resnet34, metrics=accuracy)

but if I try to switch out resnet34 for deeplabv3_resnet101 I run into an error:

import torchvision.models as models
dlv3 = models.segmentation.deeplabv3_resnet101(pretrained=False, progress=True)
learn = unet_learner(data, dlv3, metrics=accuracy)

error seen:

AttributeError                            Traceback (most recent call last)
<ipython-input-22-bad08e2542b6> in <module>
----> 1 learn = unet_learner(data, dlv3, metrics=accuracy)
      2 lr_find(learn)
      3 learn.recorder.plot()
      4 lr=1e-2
      5 learn.fit_one_cycle(1, slice(lr))

~/.local/lib/python3.7/site-packages/fastai/vision/ in unet_learner(data, arch, pretrained, blur_final, norm_type, split_on, blur, self_attention, y_range, last_cross, bottle, cut, **learn_kwargs)
    113     "Build Unet learner from `data` and `arch`."
    114     meta = cnn_config(arch)
--> 115     body = create_body(arch, pretrained, cut)
    116     try:    size = data.train_ds[0][0].size
    117     except: size = next(iter(data.train_dl))[0].shape[-2:]

~/.local/lib/python3.7/site-packages/fastai/vision/ in create_body(arch, pretrained, cut)
     54 def create_body(arch:Callable, pretrained:bool=True, cut:Optional[Union[int, Callable]]=None):
     55     "Cut off the body of a typically pretrained `model` at `cut` (int) or cut the model as specified by `cut(model)` (function)."
---> 56     model = arch(pretrained)
     57     cut = ifnone(cut, cnn_config(arch)['cut'])
     58     if cut is None:

~/.local/lib/python3.7/site-packages/torch/nn/modules/ in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--> 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

~/.local/lib/python3.7/site-packages/torchvision/models/segmentation/ in forward(self, x)
     15     def forward(self, x):
---> 16         input_shape = x.shape[-2:]
     17         # contract: features is a dict of tensors
     18         features = self.backbone(x)

AttributeError: 'bool' object has no attribute 'shape'

I’m pretty new to pytorch and fastai so it might be something pretty simple, but any guidance would be helpful to solve this issue.

1 Like

I’d start by exploring what unet_learner is doing and what it needs. For a TLDR:

  1. You’ll need to make a body from your model (which is essentially all but the last layer)
  2. Pass this body into what the final unet_learner model call wants to create the full thing and whatever other parameters it wants.

So you’ll be exploring using Learner basically at the end of the day. Feel free to ping if you run into issues or get confused while exploring :slight_smile:

unet_learner does not work with other models:

The last update was from December reporting the error is still there:


Thanks @ilovescience!

Note: there is a DenseNet example in that thread to perhaps help you get started, but you need thorough knowledge of the architecture to work it well :slight_smile:

1 Like