BUG: RuntimeError: Error(s) in loading state_dict for ResnetGenerator:Missing key(s) in state_dict

BUG:

RuntimeError: Error(s) in loading state_dict for ResnetGenerator:
	Missing key(s) in state_dict: "model.9.conv_block.3.weight", "model.9.conv_block.3.bias", "model.10.conv_block.3.weight", "model.10.conv_block.3.bias", "model.11.conv_block.3.weight", "model.11.conv_block.3.bias", "model.12.conv_block.3.weight", "model.12.conv_block.3.bias", "model.13.conv_block.3.weight", "model.13.conv_block.3.bias", "model.14.conv_block.3.weight", "model.14.conv_block.3.bias", "model.15.conv_block.3.weight", "model.15.conv_block.3.bias", "model.16.conv_block.3.weight", "model.16.conv_block.3.bias", "model.17.conv_block.3.weight", "model.17.conv_block.3.bias", "model.24.weight", "model.24.bias".
	Unexpected key(s) in state_dict: "model.9.conv_block.4.weight", "model.9.conv_block.4.bias", "model.10.conv_block.4.weight", "model.10.conv_block.4.bias", "model.11.conv_block.4.weight", "model.11.conv_block.4.bias", "model.12.conv_block.4.weight", "model.12.conv_block.4.bias", "model.13.conv_block.4.weight", "model.13.conv_block.4.bias", "model.14.conv_block.4.weight", "model.14.conv_block.4.bias", "model.15.conv_block.4.weight", "model.15.conv_block.4.bias", "model.16.conv_block.4.weight", "model.16.conv_block.4.bias", "model.17.conv_block.4.weight", "model.17.conv_block.4.bias", "model.25.weight", "model.25.bias".

问题描述:

在测试阶段,首先,定义网络结构;然后,加载训练好的模型参数。但是但在加载过程中,程序运行报错:所加载的参数与模型不匹配。

解决思路:

(1) Google 搜索到的解决方法之一:

 network.load_state_dict(torch.load(save_path),strict=False)

是将load_state_dict()函数中的strict参数设置为False,设置成False, 可以允许参数与网络结构不是一一对应关系。我一开始就是这样做的,但是发现结果很离谱,完全分割不出感兴趣区域器官,全是错误分割结果。

(2) 正确的解决方法:应该将训练的时候的网路结构与测试阶段的网络结构打印出来,进行对比分析,观察在测试阶段是否缺少或者增加多余模块。

好的习惯:每次模型训练结束后,将对应训练.py,网络结构文件.py单独保存起来,并在模型的训练过程输出信息也写入.TXT,或者训练结束后,手动保存。这样有助于分析问题,找到程序在测试阶段出错的原因。

(a)通过对比右边测试阶段的网络结构与左边训练阶段的网络结构,我们可以发现,测试阶段的网络结构中存在多个位置缺少 Droupout模块。

(b)接着对比,在模型最后的输出模块,测试阶段的网络结构中填充方式与训练阶段的填充方式不一致。

在修改这些错误之后,可以得到合理的结果。

 

 

你可能感兴趣的:(BUG)