一、问题描述
多个GPU 训练,保存时没有加module , 导致加载模型时报错。正确写法应该如下:
# save modelif num_gpu == 1:torch.dule.state_dict(), os.path.join(opt.outf, 'net.pth'))else:torch.save(model.state_dict(), os.path.join(opt.outf, 'net.pth'))
二、解决方法
load 模型时,删除多余的module. 可以打印下面代码中的pth , 查看问题症结
具体代码如下:
print("load pre_training weight. ")pth = torch.load(os.path.join(opt.outf, 'net.pth'))new_state_dict = OrderedDict()for k, v in pth.items():name = k[7:] # remove 'module'new_state_dict[name]=vnet.load_state_dict(new_state_dict)
参考链接:.html
本文发布于:2024-02-05 01:59:27,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170721469361994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |