How to Use other Pretrained Models not included in fastai/torchvision

Hi All,

In Fastai V1, we were able to use cadene models with cnn_learner. Does the current version support those models ?
I am trying to use xception Architecture:

import pretrainedmodels as ptm
model = create_cnn_model(ptm.xception, 1, None,True) # Copied from  docs

I am getting the following error trace:

KeyError Traceback (most recent call last)
<ipython-input-23-0dafcba3f64b> in <module>
----> 8 model = create_cnn_model(ptm.xception, 1, None, pretrained=True)
      9 # learn = cnn_learner(dls, model, metrics=accuracy)

~/fastai2/fastai2/vision/ in create_cnn_model(arch, n_out, cut, pretrained, n_in, init, custom_head, concat_pool, **kwargs)
     99                      concat_pool=True, **kwargs):
    100     "Create custom convnet architecture using `arch`, `n_in` and `n_out`"
--> 101     body = create_body(arch, n_in, pretrained, cut)
    102     if custom_head is None:
    103         nf = num_features_model(nn.Sequential(*body.children())) * (2 if concat_pool else 1)

~/fastai2/fastai2/vision/ in create_body(arch, n_in, pretrained, cut)
     64 def create_body(arch, n_in=3, pretrained=True, cut=None):
     65     "Cut off the body of a typically pretrained `arch` as determined by `cut`"
---> 66     model = arch(pretrained=pretrained)
     67     _update_first_layer(model, n_in, pretrained)
     68     #cut = ifnone(cut, cnn_config(arch)['cut'])

~/miniconda3/envs/fastai2/lib/python3.7/site-packages/pretrainedmodels/models/ in xception(num_classes, pretrained)
    216     model = Xception(num_classes=num_classes)
    217     if pretrained:
--> 218         settings = pretrained_settings['xception'][pretrained]
    219         assert num_classes == settings['num_classes'], \
    220             "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)

KeyError: True

When i try to debug the code, cadene seems to use string for pretrained parameter (with the value of ‘imagenet’) while fastai tries to pass the value as pretrained = True. I tried overriding manually but still i am not able to get to work. Has someone able to get past this issue? Kindly help

P.S : I am trying to use it along with create_cnn_model --> internally calls create_body

I have an example of this here using efficientnet (also not in the v2 library). Read through the notebook and tell me if you have any questions :slight_smile:


Let me check. Thanks a lot :slight_smile:

Hi @muellerzr do you know where to find an exhaustive list of all pretrained models / arch from the latest fastai library?

from import *
from pprint import pprint


Here’s a snippet of the output from above:

    <function resnet18 at 0x7fa1a2104710>: {
        'cut': -2,
        'split': <function _resnet_split at 0x7fa1a23e77a0>,
        'stats': ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    <function resnet34 at 0x7fa1a21047a0>: {
        'cut': -2,
        'split': <function _resnet_split at 0x7fa1a23e77a0>,
        'stats': ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    <function resnet50 at 0x7fa1a2104830>: {
        'cut': -2,
        'split': <function _resnet_split at 0x7fa1a23e77a0>,
        'stats': ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    <function resnet101 at 0x7fa1a21048c0>: {
        'cut': -2,
        'split': <function _resnet_split at 0x7fa1a23e77a0>,
        'stats': ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    <function resnet152 at 0x7fa1a2104950>: {
        'cut': -2,
        'split': <function _resnet_split at 0x7fa1a23e77a0>,
        'stats': ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

The keys of model_meta are the functions that you would pass into the arch argument of cnn_learner. Not all, but a lot of them are pretrained models (mostly from torchvision) with built in support for fastai style splitting for discriminative learning rates.

Note that the split and cut that you see in model_meta are what’s called internally by cnn_learner. If you’re using models that aren’t supported inherently in fastai, you’d pass in a custom cut and/or split into cnn_learner. Here is one such example.