Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading the pretrained weights into pytorch #67

Open
josh-wende opened this issue Nov 18, 2021 · 1 comment
Open

Loading the pretrained weights into pytorch #67

josh-wende opened this issue Nov 18, 2021 · 1 comment

Comments

@josh-wende
Copy link

Hi,

I'm trying to use the pretrained weights provided in the readme, but am having some trouble. I loaded model_final.pth as a state_dict, and then tried to load it into a new instance of KeypointDetector, but this hit an error because the expected keys for a KeypointDetector state_dict and the given dictionary keys do not match up. What am I doing wrong?

Thanks,
Josh

@gch
Copy link

gch commented Jan 3, 2022

It seems like the codebase may have changed since the pretrained weights were saved. I found the following (manually changed) key names gets things to work:

    pretrained = torch.load('/mnt/drive/MyDrive/Datasets/SMOKE/model_final.pth')['model']
    for k1, k2 in zip(sorted(pretrained.keys()), sorted(model.state_dict().keys())):
        assert(k1.replace('module.', '') == k2)
        model.state_dict()[k2][:] = pretrained[k1]

EDIT: reading the code further, there's just some extra information that's saved when the training script serializes the model (training debug data, etc.). It looks like

def load_state_dict(model, loaded_state_dict):
provides an existing helper function that loads from the slightly customized serialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants