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: https://github.com/Cadene/pretrained-models.pytorch

Answered here --> https://forums.fast.ai/t/lesson-5-advanced-discussion/30865/7?u=shar1

Great thanks!

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

2 Likes

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.

2 Likes

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:
https://forums.fast.ai/t/lesson-5-advanced-discussion/30865/40?u=hwasiti

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, 
pretrained='imagenet')

learn = fastai.vision.learner.create_cnn(
    data = data,
    arch=model_f, 
    cut=-2) #cut parameter is different for each model

Hope, it will help!

2 Likes

[ 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