Efficient way to use custom models with create_cnn

I wanted to work with different models for ensembling purposes. I tried using se_resnext101, but since create_body does
arch = model(pretrained), it throws an error saying

TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not bool.
Pytorch model accepts Tensor as input defined in

def forward(self,x)

My model looks like this. I also looked at the docs (#customize model) but the instructions were same.

I am unable to use create_body because of the above reason, create_head isn’t useful either. What is the efficient way to work with custom models with create_cnn?

5 Likes

do arch = model, without “(pretrained)”, and then call create_body :slight_smile:.

But i do want to use pretrained weights. It’s not directly provided by torchvision, but weights are stored in ~/.torch/models

The method “create_body” have the argument “pretrained”, so you can control it there. Thats why you are getting an error, the method create_body expects not a model, but the method to construct the model.

So how to make create_cnn work ? create_body isn’t useful for custom models which can directly work as:
model = se_resnext101(num_classes,pretrained=True) # Can be any model
output = model(input)
This is the usual way of working with models.

In case someone else is still looking, do this

from fastai.basic learner import *
learn = Learner(data,model)

And then you can do all necessary tasks

2 Likes

Yes, I agree… For me too Learner worked, and create_cnn could not make it work. However Jeremy said create_cnn is better. He said: It’s better to use create_cnn, so that fastai will create a version you can use for transfer learning for your problem.

So it seems Learner will not use the pretrained weights and will initialize random weights. I can confirm that there is huge difference between Learner and create_cnn when I passed resnet50 pretrained in both models. The Learner starts with much more error rate and validation loss.

I have listed the different scenarios where it worked or when it did not work with create_cnn in a reply to him.

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

2 Likes

For future reference, this page helps me a lot.

7 Likes