In pytorch how can you load pretrained orderdict weights into a model?

I have an orderdict and it contains all the pretrained weights and layer names. I would like to turn this orderdict back into a model class so can call it and run some transfer learning tests. I tried to directly convert it by using a dictionary comprehension with nn.ParameterDict but I got the error 'parameter name can\'t contain "."' So should I remove all the “.” in the key’s naming conventions? Eg layer1.weight to layer1_weight? What is the easiest way to do this?

    def _load_model(self,directory,strict_loading=True,**kwargs):
        print('Loading pretrained weights')
        if isinstance(directory,dict):
            state_dict = directory
            state_dict = torch.load(directory, map_location=lambda storage, loc: storage)
        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            if k[:7] == 'module.':
                name = k[7:] # remove `module.`
                name = k
            new_state_dict[name] = v

My loading function :slight_smile:
But in short you have something called

Hi Juan I tried and googled your method already and it does not work in this context. If you look at the screenshot the keys are named differently in this pth.tar file. So when I try to use your method load_state_dict crashes with error because of the [k:7] slice. This was one of the reasons I wanted to use nn.ParameterDict in the first place because the normal method fails. Thanks for the help anyways.

Edited: I think I am completely confused. I need to rebuild this person’s model from scratch then I can load this pth.tar file isn’t it? Does the pth.tar file format contain the entire model itself or just the weights?

When you load a model, pytorch match keys by name. It means your weights have to match model names, otherwise this msg will appear. This means in your weights there exists keys which does not exist in the model and vice versa.
Another option could be rename model layers but that is more difficult.
Appear of the names the amount of parameters sizes etcetera have to match too.

The problem is not about the function but the key names of your weights.

Ok thanks I think I understand now.