More CNN archs in fastai v1

In an image classification project, I have tried various CNN architectures with fastai v1. I’d like to share my note here, as it may take some time to figure out what cut and split parameters to use and where to get the pretained models.

This notebook shows the structures of some archs and how to use them with fastai v1. It includes ResNet, ResNeXt, SENet, Densenet, Inception V4, WRN, Xception, VGG, etc. Some pretrained models are from torchvision, some are from Cadene. The notebook also shows what the model is like after cutting and splitting into layer groups.

I hope it helps!

Although the fastai v1’s flexible API allows people to customize their own models, it may be more convenient to use if the library has default cut and split parameters for various models. Actually v0.7 has more models included and I saw people asking about it in some other posts.
I’d be happy to submit a PR if we want to include some of the models into the library.



This looks great! Feel free to suggest a PR with the cut and split for those new models so that we can have them work with create_cnn!


Hi @sgugger ,
I’m glad you like it! I have sent a PR to include all models in torchvision, except for inception_v3. The torchvision inception_v3 code has used functions like F.max_pool2d in between the model’s modules in forward, but the cut model code in fastai only take its children, these functions won’t be included, so it doesn’t work for now. It’s not hard to fix if we write our own code for Inception, but Cadene already has working ones for Inception.

Some other useful models in the notebook, such as ResNeXt, SENet, etc, are from Cadene pretrained models, I’m not sure we want to add a dependency just for this so I didn’t include them. Let me know whether we’d like to add these models or skip them for now. In v0.7, we had our own implementations for some of them (e.g. SENet).



Your notebook is really spectacular. Thank you!

@PPPW I don’t think that pretrainedmodels has to be a dependency. You could add the code to fastai and throw an error message when a model from pretrainedmodels is used, but the dependency ist not installed.

Thank you very much!!! This is what I am trying to understand for past few days.

Now the final piece is gathered, I can start working on the cut and split with create_body and create_head.

Really appreciated your post :+1:

Hi @sotte, model_meta uses the model function as key, do you mean inside the model function we add a check to see whether the user has pretrainedmodel installed or not? Good idea, thanks!

@others, I’m glad the notebook helps, someone asked me to add more examples, I have added a few such as NASNet, PNASNet, etc.

1 Like

@PPPW yes, exactly. It could look something like this (not tested):

# in
    import pretrainedmodels

    def resnext101_32x4d(pretrained=False):
        pretrained = 'imagenet' if pretrained else None
        model = pretrainedmodels.resnext101_32x4d(pretrained=pretrained)
        all_layers = list(model.children())
        return nn.Sequential(*all_layers[0], *all_layers[1:])

    model_meta[resnext101_32x4d] = {'cut': -2, 'split': lambda m: (m[0][6], m[1]) }

except ImportError:
    print("Can't import pretrainedmodels")

Look good, it should work. We can create a file in the models folder and put all those models there, to prevent the file getting too long.

name ‘arch_summary’ is not defined

Hi @Karl_Mason,

arch_summary is a function defined in (it’s not a fastai function), you can import it from that file

1 Like

There’s another repo (pytorchcv) which provides a much more comprehensive list of models. Moreover, the models’ implementation is much easier to be used with fastai, comparing with Cadene. I have provided some simple examples here.


@PPPW sorry to bother you but how do i import that function from that file and where is the file?

Hi @xaerin, the “” is under this folder. At the beginning of the notebook, everything in this file is imported by from utils import *.

Ohh nice! Thank you so much

@PPPW would you please show how to import the file correctly? I’ve followed the installation steps as specified by [] but with no success when running, eg: arch_summary(resnet50)

Here’s a screenshot from my notebook:

Please advise. Thank you!

Hi @rdass, in the second last line of your first block, you have from utils import *. The is in this folder, it’s just a simple py file with some functions, it’s not part of any library. Do you have that file in the directory of your notebook?

Hi @PPPW sorry for the really late reply. I just revisited this problem and it’s been resolved now. How would you go about permanently adding the file to my path? Would you recommend following this link?