用高版本的pytorch训练模型,在低版本中测试时出现如下错误:
RuntimeError: all_ep_296.pth is a zip archive (did you mean to use torch.jit.load()?)
原因是: pytorch1.6及更高版本,保存模型和低版本有区别. 如果使用torch 1.6版本进行了训练,并且在训练时没有更改参数“_use_new_zipfile_serialization=False”,此时可以在1.6中直接加载模型,然后再次使用torch.save 进行保存为非zip格式的。具体代码如下:
net = CrowdCounter(ce_weights=wts)
net.load_state_dict(torch.load(model_path)) #eg: model_path=all_ep_296.pth
torch.save(net.state_dict(),new_model_path,_use_new_zipfile_serialization=False) #eg: new_model_path=all_ep_296_new.pth
最后用得到的新的model进行测试即可.