Add pretrained pytorch models to my project

Hello! I am new with pytorch and fastai.
Does anybody know how to add some custom pretrained models to my fastai 1.0.28 code?
These for example:

Answered here -->

Great thanks!

But it doesn’t work and fails with error:
TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not bool


Could you make it run yet? Would be interesting to know.

1 Like

Try this:

def get_model(pretrained=False, **kwargs):
        model = se_resnet50(**kwargs) #pass parameters here. se_resnet50 as an example
        return model

Create one function get_model, which returns the model. Pass get_model in create_cnn.


It did not work for me. I am on fastai v1.39

Here is my minimal code that I tried on the standard resnet50 before trying more fancy cadene models.

How did you do it with se_resnet? Did you use cadene library?

1 Like

Update: I could make create_cnn work with custom model. Here is my code:

1 Like

Hello, all!
I have 1.0.30 version and what I do:

import pretrainedmodels
def model_f(pretrained=True, **kwargs):
    return pretrainedmodels.se_resnext50_32x4d(num_classes=1000, 

learn =
    data = data,
    cut=-2) #cut parameter is different for each model

Hope, it will help!


[ EDIT ] : this issue is solved (see thread [ SOLVED ] Get prediction from the ImageNet model without creating a databunch).

Hello @myltykritik.

I’m searching an answer in the forum about how to use a pretrained model without changing anything. For example, I would like to use resnet34 with the 1000 classes of ImageNet.

It is easy to get the weights (for example, model = models.resnet34(pretrained=True)), but how to use it after to get a prediction through model.predict(img)? I got the following error.

AttributeError: 'ResNet' object has no attribute 'predict'

I guess I need to build a learner with create_cnn(data, model) for example but for that I need a databunch data with my 1000 classes… and I do not want to download the 138 GB of Imagenet training dataset :slight_smile: Any advice?

Here is my solution to import cadene models based on your solution: A function that returns a function. Does anyone knows a better way to import cadene pretrained models?

I don’t want to deal with the cut=-2 parameter.

1 Like