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

(Thiago) #1

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: https://github.com/cydonia999/VGGFace2-pytorch, 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:

0 Likes

(Akash ) #2

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:

0 Likes

(Akash ) #3

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.

0 Likes

(Thiago) #4

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:
https://github.com/cydonia999/VGGFace2-pytorch/issues/2

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

0 Likes

(Thiago) #5

I also tried something like this:

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

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:
https://github.com/cydonia999/VGGFace2-pytorch/blob/master/models/resnet.py

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

0 Likes

(Zachary Mueller) #6

See this post here, should help:

0 Likes