pytorch加载模型报错Missing key(s) in state_dict: “classifier00.0.fc.weight“, “classifier00.0.fc.bias“
程序员文章站
2022-05-27 11:29:29
...
报错:
Traceback (most recent call last):
File "/home/user1/load_model_test.py", line 23, in <module>
learner.load_state(conf, 'ir_se50.pth', model_only=True, from_save_folder=True)
File "/home/user1/Learner.py", line 84, in load_state
self.model.load_state_dict(torch.load(save_path/'model_{}'.format(fixed_str)))
File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 777, in load_state_dict
self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for Backbone:
Missing key(s) in state_dict: "classifier00.0.fc.weight", "classifier00.0.fc.bias", "classifier00.0.bn.weight", "classifier00.0.bn.bias", "classifier00.0.bn.running_mean", "classifier00.0.bn.running_var", "classifier00.1.weight", "classifier00.1.bias", "classifier01.0.fc.weight", "classifier01.0.fc.bias", "classifier01.0.bn.weight", "classifier01.0.bn.bias", "classifier01.0.bn.running_mean", "classifier01.0.bn.running_var", "classifier01.1.weight", "classifier01.1.bias", "classifier02.0.fc.weight", "classifier02.0.fc.bias", "classifier02.0.bn.weight", "classifier02.0.bn.bias", "classifier02.0.bn.running_mean", "classifier02.0.bn.running_var", "classifier02.1.weight", "classifier02.1.bias", "classifier03.0.fc.weight", "classifier03.0.fc.bias", "classifier03.0.bn.weight", "classifier03.0.bn.bias", "classifier03.0.bn.running_mean", "classifier03.0.bn.running_var", "classifier03.1.weight", "classifier03.1.bias", "classifier04.0.fc.weight", "classifier04.0.fc.bias", "classifier04.0.bn.weight", "classifier04.0.bn.bias", "classifier04.0.bn.running_mean", "classifier04.0.bn.running_var", "classifier04.1.weight", "classifier04.1.bias", "classifier05.0.fc.weight", "classifier05.0.fc.bias", "classifier05.0.bn.weight", "classifier05.0.bn.bias", "classifier05.0.bn.running_mean", "classifier05.0.bn.running_var", "classifier05.1.weight", "classifier05.1.bias", "classifier06.0.fc.weight", "classifier06.0.fc.bias", "classifier06.0.bn.weight", "classifier06.0.bn.bias", "classifier06.0.bn.running_mean", "classifier06.0.bn.running_var", "classifier06.1.weight", "classifier06.1.bias", "classifier07.0.fc.weight", "classifier07.0.fc.bias", "classifier07.0.bn.weight", "classifier07.0.bn.bias", "classifier07.0.bn.running_mean", "classifier07.0.bn.running_var", "classifier07.1.weight", "classifier07.1.bias", "classifier08.0.fc.weight", "classifier08.0.fc.bias", "classifier08.0.bn.weight", "classifier08.0.bn.bias", "classifier08.0.bn.running_mean", "classifier08.0.bn.running_var", "classifier08.1.weight", "classifier08.1.bias", "classifier09.0.fc.weight", "classifier09.0.fc.bias", "classifier09.0.bn.weight", "classifier09.0.bn.bias", "classifier09.0.bn.running_mean", "classifier09.0.bn.running_var", "classifier09.1.weight", "classifier09.1.bias", "classifier10.0.fc.weight", "classifier10.0.fc.bias", "classifier10.0.bn.weight", "classifier10.0.bn.bias", "classifier10.0.bn.running_mean", "classifier10.0.bn.running_var", "classifier10.1.weight", "classifier10.1.bias", "classifier11.0.fc.weight", "classifier11.0.fc.bias", "classifier11.0.bn.weight", "classifier11.0.bn.bias", "classifier11.0.bn.running_mean", "classifier11.0.bn.running_var", "classifier11.1.weight", "classifier11.1.bias", "classifier12.0.fc.weight", "classifier12.0.fc.bias", "classifier12.0.bn.weight", "classifier12.0.bn.bias", "classifier12.0.bn.running_mean", "classifier12.0.bn.running_var", "classifier12.1.weight", "classifier12.1.bias", "classifier13.0.fc.weight", "classifier13.0.fc.bias", "classifier13.0.bn.weight", "classifier13.0.bn.bias", "classifier13.0.bn.running_mean", "classifier13.0.bn.running_var", "classifier13.1.weight", "classifier13.1.bias", "classifier14.0.fc.weight", "classifier14.0.fc.bias", "classifier14.0.bn.weight", "classifier14.0.bn.bias", "classifier14.0.bn.running_mean", "classifier14.0.bn.running_var", "classifier14.1.weight", "classifier14.1.bias", "classifier15.0.fc.weight", "classifier15.0.fc.bias", "classifier15.0.bn.weight", "classifier15.0.bn.bias", "classifier15.0.bn.running_mean", "classifier15.0.bn.running_var", "classifier15.1.weight", "classifier15.1.bias", "classifier16.0.fc.weight", "classifier16.0.fc.bias", "classifier16.0.bn.weight", "classifier16.0.bn.bias", "classifier16.0.bn.running_mean", "classifier16.0.bn.running_var", "classifier16.1.weight", "classifier16.1.bias", "classifier17.0.fc.weight", "classifier17.0.fc.bias", "classifier17.0.bn.weight", "classifier17.0.bn.bias", "classifier17.0.bn.running_mean", "classifier17.0.bn.running_var", "classifier17.1.weight", "classifier17.1.bias", "classifier18.0.fc.weight", "classifier18.0.fc.bias", "classifier18.0.bn.weight", "classifier18.0.bn.bias", "classifier18.0.bn.running_mean", "classifier18.0.bn.running_var", "classifier18.1.weight", "classifier18.1.bias", "classifier19.0.fc.weight", "classifier19.0.fc.bias", "classifier19.0.bn.weight", "classifier19.0.bn.bias", "classifier19.0.bn.running_mean", "classifier19.0.bn.running_var", "classifier19.1.weight", "classifier19.1.bias", "classifier20.0.fc.weight", "classifier20.0.fc.bias", "classifier20.0.bn.weight", "classifier20.0.bn.bias", "classifier20.0.bn.running_mean", "classifier20.0.bn.running_var", "classifier20.1.weight", "classifier20.1.bias", "classifier21.0.fc.weight", "classifier21.0.fc.bias", "classifier21.0.bn.weight", "classifier21.0.bn.bias", "classifier21.0.bn.running_mean", "classifier21.0.bn.running_var", "classifier21.1.weight", "classifier21.1.bias", "classifier22.0.fc.weight", "classifier22.0.fc.bias", "classifier22.0.bn.weight", "classifier22.0.bn.bias", "classifier22.0.bn.running_mean", "classifier22.0.bn.running_var", "classifier22.1.weight", "classifier22.1.bias", "classifier23.0.fc.weight", "classifier23.0.fc.bias", "classifier23.0.bn.weight", "classifier23.0.bn.bias", "classifier23.0.bn.running_mean", "classifier23.0.bn.running_var", "classifier23.1.weight", "classifier23.1.bias", "classifier24.0.fc.weight", "classifier24.0.fc.bias", "classifier24.0.bn.weight", "classifier24.0.bn.bias", "classifier24.0.bn.running_mean", "classifier24.0.bn.running_var", "classifier24.1.weight", "classifier24.1.bias", "classifier25.0.fc.weight", "classifier25.0.fc.bias", "classifier25.0.bn.weight", "classifier25.0.bn.bias", "classifier25.0.bn.running_mean", "classifier25.0.bn.running_var", "classifier25.1.weight", "classifier25.1.bias", "classifier26.0.fc.weight", "classifier26.0.fc.bias", "classifier26.0.bn.weight", "classifier26.0.bn.bias", "classifier26.0.bn.running_mean", "classifier26.0.bn.running_var", "classifier26.1.weight", "classifier26.1.bias", "classifier27.0.fc.weight", "classifier27.0.fc.bias", "classifier27.0.bn.weight", "classifier27.0.bn.bias", "classifier27.0.bn.running_mean", "classifier27.0.bn.running_var", "classifier27.1.weight", "classifier27.1.bias", "classifier28.0.fc.weight", "classifier28.0.fc.bias", "classifier28.0.bn.weight", "classifier28.0.bn.bias", "classifier28.0.bn.running_mean", "classifier28.0.bn.running_var", "classifier28.1.weight", "classifier28.1.bias", "classifier29.0.fc.weight", "classifier29.0.fc.bias", "classifier29.0.bn.weight", "classifier29.0.bn.bias", "classifier29.0.bn.running_mean", "classifier29.0.bn.running_var", "classifier29.1.weight", "classifier29.1.bias", "classifier30.0.fc.weight", "classifier30.0.fc.bias", "classifier30.0.bn.weight", "classifier30.0.bn.bias", "classifier30.0.bn.running_mean", "classifier30.0.bn.running_var", "classifier30.1.weight", "classifier30.1.bias", "classifier31.0.fc.weight", "classifier31.0.fc.bias", "classifier31.0.bn.weight", "classifier31.0.bn.bias", "classifier31.0.bn.running_mean", "classifier31.0.bn.running_var", "classifier31.1.weight", "classifier31.1.bias", "classifier32.0.fc.weight", "classifier32.0.fc.bias", "classifier32.0.bn.weight", "classifier32.0.bn.bias", "classifier32.0.bn.running_mean", "classifier32.0.bn.running_var", "classifier32.1.weight", "classifier32.1.bias", "classifier33.0.fc.weight", "classifier33.0.fc.bias", "classifier33.0.bn.weight", "classifier33.0.bn.bias", "classifier33.0.bn.running_mean", "classifier33.0.bn.running_var", "classifier33.1.weight", "classifier33.1.bias", "classifier34.0.fc.weight", "classifier34.0.fc.bias", "classifier34.0.bn.weight", "classifier34.0.bn.bias", "classifier34.0.bn.running_mean", "classifier34.0.bn.running_var", "classifier34.1.weight", "classifier34.1.bias", "classifier35.0.fc.weight", "classifier35.0.fc.bias", "classifier35.0.bn.weight", "classifier35.0.bn.bias", "classifier35.0.bn.running_mean", "classifier35.0.bn.running_var", "classifier35.1.weight", "classifier35.1.bias", "classifier36.0.fc.weight", "classifier36.0.fc.bias", "classifier36.0.bn.weight", "classifier36.0.bn.bias", "classifier36.0.bn.running_mean", "classifier36.0.bn.running_var", "classifier36.1.weight", "classifier36.1.bias", "classifier37.0.fc.weight", "classifier37.0.fc.bias", "classifier37.0.bn.weight", "classifier37.0.bn.bias", "classifier37.0.bn.running_mean", "classifier37.0.bn.running_var", "classifier37.1.weight", "classifier37.1.bias", "classifier38.0.fc.weight", "classifier38.0.fc.bias", "classifier38.0.bn.weight", "classifier38.0.bn.bias", "classifier38.0.bn.running_mean", "classifier38.0.bn.running_var", "classifier38.1.weight", "classifier38.1.bias", "classifier39.0.fc.weight", "classifier39.0.fc.bias", "classifier39.0.bn.weight", "classifier39.0.bn.bias", "classifier39.0.bn.running_mean", "classifier39.0.bn.running_var", "classifier39.1.weight", "classifier39.1.bias".
Process finished with exit code 1
原因:可能是你的网络结构不对。比如你修改了原来的网络结构,加入了一些fc层。但是你加载的参数还是与原来没有fc的网络结构对应的。这时候就会出错。
解决:将网络结构改回原来的,和权重参数对应起来。
下一篇: 跨域名设置cookie或获取cookie