How to use the Senet in fastai library?

I found the senet has been added to fastai library, but i don’t konw how to use it.
the original method is not available:
arch = senet154
learn = ConvLearner.pretrained(arch, data)
@jeremy
what should i do?
thanks!

I am having the same problem. If I understand correctly, senet model is taken from https://github.com/Cadene/pretrained-models.pytorch. The notebook on nasnet https://github.com/fastai/fastai/blob/master/courses/dl1/nasnet.ipynb shows how to use it for nasnet, but I am not sure how to get the model features number for senet.

@charming were you able to get it running? @radek posted on imaterialist that he used se_resnext for his experiments. So he might be able to help

IIRC, for se_resnext the constructor from pretrained-models.pytorch (an awesome repo btw!) was giving errors when asked to create a different number of activations for the last layer. Replacing the last layer via reconstructing the model from layers through nn.Sequential also didn’t work I believe - there is likely quite a bit happening in the forward method of the architecture.

I went the easy and quick route, that is I added an fc layer on top of the architecture, going from 1000 activations trained on imagenet to the 228 I needed, trained the layer with the model frozen and subsequently unfroze the entire model and trained that. The model still performed well (3rd best model IIRC behind resnet152 and resnet152 with a fully convolutional head) but most likely one could get much more from it if the architecture was properly broken down into parts to work with discriminative learning rates. I think one still should be able to train with discriminative learning rates even without properly cutting up the arch if that should prove problematic but I have not looked into that.

1 Like

@radek thanks for your reply. Unfortunately, I couldn’t get it running. Could you share your code? For the moment what I have done is this

from fastai.models.senet import *
model = se_resnext50_32x4d(pretrained='imagenet').cuda()
data = data
opt = torch.optim.Adam(model.parameters())
fit(model, data, 1, opt, crit=nn.CrossEntropy(), metrics=[accuracy])

While this works, I would prefer to use the fastai learner so that I can experiment with the cyclic learning rates and use the best_save_name functionality.

Cheers.

1 Like

The ingredient that you seem to be missing is using ConvLearner.from_model_data. You can pass in the architecture and a model data object and if the above code runs for you, it should work as well (this is as opposed to using ConvLearner.pretrained).

Let me know how it goes.

@radek @charming I have written a notebook on how to use architectures like senet which have been taken from https://github.com/Cadene/pretrained-models.pytorch and incorporate into the fastai library.

Special thanks to @radek for giving me a hint on using ConvLearner.from_model_data.

Link to the notebook : https://github.com/TheShadow29/FAI-notes/blob/master/notebooks/Using-Pretrained-Pytorch-Models.ipynb

I have also posted this here: Using Pretrained Models from Cadene repository

3 Likes

Thanks!

How should we modify the model so that we can use differential learning rates ?

I would guess it has something to do with layergroups. Have a look at this recent discussion lesson7-CAM: mismatch between # of lrs and of layer groups.

To be more specific, one would need to pass an array of learning rates equal to the number of layer groups. So first check the number of layergroups, and for each layergroup there should be a learning rate.

Hope that helps. I will perhaps try to add this to the notebook as well in some days.

2 Likes

i find the way to use Senet in ConvLearner.pretrained:


from fastai.models.senet import *

def senext_50(pre): return se_resnext50_32x4d(pretrained = ‘imagenet’ if pre else None)

learn = ConvLearner.pretrained(senext_50, data, xtra_cut=2)


In this way we can use differential learning rate

Thanks, Any recommendation on how to use se_resnext50 with custom Unet head from Carvana lesson?
Seems, I am unable to cut the model, as “model_meta” does not have entries for this model.

And even if I gave arbitrary numbers for cut,lr_cut, I am getting errored out at “get_base()” function

“TypeError: conv2d(): argument ‘input’ (position 1) must be Tensor, not bool”

I believe model meta is just a dictionary. It captures the number of parameters for the linear layer for a particular network. Can you share your code?

def senext_50(pre): return se_resnext50_32x4d(pretrained = ‘imagenet’ if pre else None)
f = resnext50
cut,lr_cut = model_meta[f]
m_base = get_base()

Following the technique in the Lesson 14, carvana image segmentation problem

This works for cutting layers and setting differential learning rate groups:

def senext_50(pre): return se_resnext50_32x4d(pretrained = ‘imagenet’ if pre else None)
model_meta = { senext_50 : [5, 3] }
learn = ConvLearner.pretrained(senext_50, md)

See an example in here.

1 Like

ModuleNotFoundError: No module named ‘fastai.models’

ModuleNotFoundError: No module named ‘fastai.models’:worried: