[Pytorch] When using torch.load() to load a model, an error occurs, indicating that the loaded model is in zip format.

question

Save the optimal model on pytorch version 1.6.0. When downloading the model to other devices, it is found that the model has been converted from .pt to zip compressed package format or an error occurs when it is loaded directly on other versions of pytorch.

Solution

torch官网上指出
The 1.6 release of PyTorch switched torch.save to use a new zipfile-based file format. torch.load still retains the ability to load files in the old format. If for any reason you want torch.save to use the old format, pass the kwarg _use_new_zipfile_serialization=False.
Insert image description here

# 保存模型时
torch.save(model.state_dict(), model_cp,_use_new_zipfile_serialization=False)

reference

https://www.freesion.com/article/87611412928/

Guess you like

Origin blog.csdn.net/qq_44846512/article/details/118677603