欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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)