Missing key(s) in state_dict: “module.resnet50.conv1.weight“
程序员文章站
2022-05-27 10:33:11
...
在使用训练好的模型验证时,报以下错误:
self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for DataParallel:
Missing key(s) in state_dict: "module.resnet50.conv1.weight", "module.resnet50.bn1.weight", "module.resnet50.bn1.bias", "module.resnet50.bn1.running_mean", "module.resnet50.bn1.running_var", "module.resnet50.block1.0.conv1.weight", "module.resnet50.block1.0.bn1.weight", "module.resnet50.block1.0.bn1.bias", "module.resnet50.block1.0.bn1.running_mean", "module.resnet50.block1.0.bn1.running_var", "module.resnet50.block1.0.conv2.weight", "module.resnet50.block1.0.bn2.weight", "module.resnet50.block1.0.bn2.bias", "module.resnet50.block1.0.bn2.running_mean",……
Unexpected key(s) in state_dict: "resnet50.conv1.weight", "resnet50.bn1.weight", "resnet50.bn1.bias", "resnet50.bn1.running_mean", "resnet50.bn1.running_var", "resnet50.bn1.num_batches_tracked", "resnet50.block1.0.conv1.weight", "resnet50.block1.0.bn1.weight", "resnet50.block1.0.bn1.bias", "resnet50.block1.0.bn1.running_mean", "resnet50.block1.0.bn1.running_var", "resnet50.block1.0.bn1.num_batches_tracked", "resnet50.block1.0.conv2.weight", "resnet50.block1.0.bn2.weight", "resnet50.block1.0.bn2.bias", "resnet50.block1.0.bn2.running_mean", "resnet50.block1.0.bn2.running_var", "resnet50.block1.0.bn2.num_batches_tracked", "resnet50.block1.0.conv3.weight", "resnet50.block1.0.bn3.weight", "resnet50.block1.0.bn3.bias", "resnet50.block1.0.bn3.running_mean", "resnet50.block1.0.bn3.running_var", "resnet50.block1.0.bn3.num_batches_tracked", "resnet50.block1.0.shortcut.0.weight",
踩坑:
百度时查到此种解决方案,虽然不报错了,也可以运行了。
但是!!!!!!准确率只有0.01,哭了。明明训练时显示有90%多的准确率呀。
model.load_state_dict(state_dict,False)
解决办法
对于此种错误的原因,
网上大部分都说是训练的使用使用了nn.DataParallel ,而验证时未使用导致的。
但是我的代码里明明用到了多GPU的。
后来发现时加载顺序问题,应该先加载模型再调用nn.DataParallel!!!!!!
model.load_state_dict(torch.load(save_path))
model = nn.DataParallel(model)
推荐阅读
-
pytorch加载模型报错Missing key(s) in state_dict: “classifier00.0.fc.weight“, “classifier00.0.fc.bias“
-
Missing key(s) in state_dict: “module.resnet50.conv1.weight“
-
Missing key(s) in state_dict: “features.conv1.weight“
-
解决Pytorch下报错Missing key(s) in state_dict: "resnet.conv1.0.weight",和 Unexpected key(s) in state_dict
-
Unexpected key(s) in state_dict: “dense_block1.denselayer1.norm.1
-
Error(s) in loading state_dict for DataParallel: Missing key(s) in state_dict: “module.conv0.weight
-
PyTorch 错误 导出onnx提示 Unexpected key(s) in state_dict: “module.classifier.0.weight
-
pytorch加载模型报错Unexpected key(s) in state_dict: module.conv1.weight, module.bn1