How can I load pre-trained weights from a different task into fastai models?

Hi there,

I am currently working on a binary classification task of detecting if a facial image has some expressions of pain or not. Instead of using fastai models pre-trained on ImageNet, I would like to use some weights pre-trained on a task similar to mine, like some face detection dataset.

I came across this project:, which already provides the weights for resnet50. But I couldn’t figure it out how to use them in the fastai workflow.

I tought this was a fairly common task, but I couldn’t find anything about it in the docs or in any tutorial.

Can someone help me with that or at least point me towards a resource in the topic?

Thanks in advance! :slight_smile:

hey i just had a look at the download and its a .pkl file.
when u export models natively from fastai u get .pkl files too and u can load them by using “load_learner”

you can try

learn = load_learner(path, ’ resnet50_ft_weight.pkl’)

let me know if this works :slight_smile:

Also if the above works, you wont be able to use discriminative learning rates. if u want to use that, then create another learn object thats a resnet50 and set layer_groups of loaded learn to that of the temporary learn object u just created.

Thanks for the help @akashgshastri!

When I run:

learn = load_learner(path, "resnet50_ft_weight.pkl")

I’m getting the following error:

RuntimeError: Invalid magic number; corrupt file?

Which appears to be the same one shared by other users in their repo:

But I haven’t figured it out yet how to solve it.

I also tried something like this:

with open(path, 'rb') as f:
    obj =
    weights = {key: torch.from_numpy(arr) for key, arr in pickle.loads(obj, encoding='latin1').items()}
learner = cnn_learner(data, models.resnet50, pretrained=False)

But this gives me this error:

RuntimeError: Error(s) in loading state_dict for Sequential:
	Missing key(s) in state_dict:

Followed by a long list of incompatible keys…

It seems there is some kind of incompatibility between the models.
They do have an implementation of Resnet though:

I guess I’m gonna try to use that! But not sure how to integrate that with fastai workflow still…

1 Like

See this post here, should help:

1 Like