在pytorch导入模型文件时报错:
RuntimeError: Error(s) in loading state_dict for DataParallel:
Unexpected running stats buffer(s) "module.norm1.norm_func.running_mean" and "module.norm1.norm_func.running_var" for InstanceNorm2d with track_running_stats=False. If state_dict is a checkpoint saved before 0.4.0, this may be expected because InstanceNorm2d does not track running stats by default since 0.4.0. Please remove these keys from state_dict. If the running stats are actually needed, instead set track_running_stats=True in InstanceNorm2d to enable them. See the documentation of InstanceNorm2d for details.
...
Unexpected running stats buffer(s) "module.res5.norm1.norm_func.running_mean" and "module.res5.norm1.norm_func.running_var" for InstanceNorm2d with track_running_stats=False. If state_dict is a checkpoint saved before 0.4.0, this may be expected because InstanceNorm2d does not track running stats by default since 0.4.0. Please remove these keys from state_dict. If the running stats are actually needed, instead set track_running_stats=True in InstanceNorm2d to enable them. See the documentation of InstanceNorm2d for details.
...
Process finished with exit code 0
根据提示是因为所导入的模型是用pytorch 0.4.0之前的版本生成的,而现在所用的是pytorch 1.0,查了一下module中load_state_dict函数:
估计是关键字随着版本的更改而有所更改,
解决的方法是:
将上面的语句改为
model_dict = torch.load(args.test_weight_path)
model_dict_clone = model_dict.copy()
for key, value in model_dict_clone.items():
if key.endswith(('running_mean', 'running_var')):
del model_dict[key]
Gnet.load_state_dict(model_dict,False)