How to load a model when the layer names are not the same?

How to load a model when the layer names are not the same?
model7.load_state_dict(torch.load(path)["state_dict"], strict=False)
output:
_IncompatibleKeys(missing_keys=['features.26.weight', 'features.26.bias', 'features.28.weight', 'features.28.bias', 'features.30.weight', 'features.30.bias'], unexpected_keys=['features.25.weight', 'features.25.bias', 'features.27.weight', 'features.27.bias', 'features.29.weight', 'features.29.bias'])
If I have to change the name of the layers, how do I do that?

The state_dict is an instance of an OrderedDict, so you could iterate all keys and change their name as done here.

1 Like