Prefix parameter names in saved model if trained by multi-GPU?
Hi, While doing inference I only use one GPU so the model failed to load the latter model file because the parameter names are not matching. I am wondering why parameter names are prepend the prefix? Can I trim the prefix and still use the model?
ming
Yes, you can just remove the prefix: <SCODE>state_dict = {k.partition('model.')[2]: v for k,v in state_dict}
<ECODE>
apaszke
Thanks for the reply! Removing prefix works. Curious why the prefix is needed? It creates inconvenience when we want to resume training a single-GPU-trained-model with multi-GPU, or pass a multi-GPU trained model to inference code that only uses one GPU? Also it looks the pretrained resnet doesn’t have ‘module.’ prefix, does it mean that they were trained on single GPU?
ming
No, they probably had the prefixes trimmed before serialization.
apaszke
<SCODE>state_dict =checkpoint['model_dict']
state_dict = {k.partition('model.')[2]: v for k,v in state_dict}
model.load_state_dict(state_dict)
<ECODE>
lunasdejavu
{k.partition(‘module.’)[2]:state_dict[k]  for k in state_dict.keys()}
Lynx_Commando
pepeportolo