Load模型参数
使用多GPU训练的模型在save时参数字典会键会添加model.关键字,在CPU或者单GPU使用时会引发错误Unexpected key(s) in state_dict: “module.features. …”.,Expected “.features…”。在保存时用如下语句可解决:
torch.save(model.module.state_dict(), model_out_path)
若是直接保存的,CPU使用时Load方法:
pretrain = torch.load(model_path, map_location=lambda storage, loc: storage)
# model_path 是模型的完整路径
from collections import OrderedDict
new_state_dict = OrderedDict