背景:
在多个GPU上数据并行训练,出现断电导致训练停止,重新加载模型训练时出现以下报错:
RuntimeError: Error(s) in loading state_dict for DataParallel:
Missing key(s) in state_dict: "module.fc0.weight", "module.fc0.bias", "module.fc0_bath.weight", "module.fc0_bath.bias", "module.fc0_bath.running_mean",
Unexpected key(s) in state_dict: "fc0.weight", "fc0.bias", "fc0_bath.weight", "fc0_bath.bias", "fc0_bath.running_mean",
经过网上查找资料,发现和其他人报错相反,他们miss fc0,unexpected module.fc0,通过以下代码查看保存model 的key:
import torch
path = ''
checkpoint = torch.load(path)
for key, value in checkpoint['model_state_dict'].items():
print(key)
发现模型里没有module,所以思路应该是先加载模型,再放到多个GPU上面。
tips:
加载模型后,要先放在一个gpu上,也就是to(device),然后再net = nn.DataParallel(net)
问题解决!