Unexpected key(s) in state_dict: “dense_block1.denselayer1.norm.1
程序员文章站
2022-05-27 10:32:29
...
Unexpected key(s) in state_dict: "dense_block1.denselayer1.norm.1
from torchvision.models import densenet121
from collections import OrderedDict
model = densenet121(pretrained=False)
state_dict =torch.load(model_weight_path)
# 初始化一个空 dict
new_state_dict = OrderedDict()
# 修改 key
for k, v in state_dict.items():
if 'denseblock' in k:
param = k.split(".")
k = ".".join(param[:-3] + [param[-3]+param[-2]] + [param[-1]])
new_state_dict[k] = v
model.load_state_dict(new_state_dict)
我的解决方法:
# 初始化一个空 dict
new_state_dict = OrderedDict()
# 修改 key
for k, v in state_dict.items():
k=k.replace('module.', '')
if 'dense_block' in k:
if "norm" in k or "conv.1" in k or "conv.2" in k:
param = k.split(".")
k = ".".join(param[:-3] + [param[-3]+param[-2]] + [param[-1]])
new_state_dict[k] = v
else:
new_state_dict[k] = v
上一篇: 解决Pytorch下报错Missing key(s) in state_dict: "resnet.conv1.0.weight",和 Unexpected key(s) in state_dict
下一篇: 检测键盘输入
推荐阅读
-
pytorch加载模型报错Missing key(s) in state_dict: “classifier00.0.fc.weight“, “classifier00.0.fc.bias“
-
unexpected key "module.conv1_1.weight" in state_dict
-
unexpected key "module.encoder.embedding.weight" in state_dict解决方案
-
Missing key(s) in state_dict: “module.resnet50.conv1.weight“
-
Missing key(s) in state_dict: “features.conv1.weight“
-
KeyError: ‘unexpected key “module.encoder.embedding.weight” in state_dict’
-
unexpected key “model.conv1.weight” in state_dict
-
解决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