[enforce fail at inline_container.cc:137] . PytorchStreamReader failed reading zip archive:解决方法

pytorch 或者libtorch报错
详细如下

terminate called after throwing an instance of 'c10::Error'
  what():  [enforce fail at inline_container.cc:137] . PytorchStreamReader failed reading zip archive: failed finding central directory
frame #0: c10::ThrowEnforceNotMet(char const*, int, char const*, std::string const&, void const*) + 0x47 (0x7ffb173b7e17 in /home/libtorch-shared-with-deps-1.2.0-cu92/libtorch/lib/libc10.so)
frame #1: caffe2::serialize::PyTorchStreamReader::valid(char const*) + 0x6b (0x7ffb1a1135bb in /home/libtorch-shared-with-deps-1.2.0-cu92/libtorch/lib/libtorch.so)
frame #2: caffe2::serialize::PyTorchStreamReader::init() + 0x9a (0x7ffb1a11706a in /home/libtorch-shared-with-deps-1.2.0-cu92/libtorch/lib/libtorch.so)
frame #3: caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader(std::unique_ptr >) + 0x53 (0x7ffb1a11a023 in /home/libtorch-shared-with-deps-1.2.0-cu92/libtorch/lib/libtorch.so)
frame #4:  + 0x3c19b32 (0x7ffb1b1f7b32 in /home/libtorch-shared-with-deps-1.2.0-cu92/libtorch/lib/libtorch.so)
frame #5: torch::jit::load(std::unique_ptr >, c10::optional, std::unordered_map, std::equal_to, std::allocator > >&) + 0x27 (0x7ffb1b1f6a87 in /home/libtorch-shared-with-deps-1.2.0-cu92/libtorch/lib/libtorch.so)
frame #6: torch::jit::load(std::string const&, c10::optional, std::unordered_map, std::equal_to, std::allocator > >&) + 0x69 (0x7ffb1b1f6dc9 in /home/libtorch-shared-with-deps-1.2.0-cu92/libtorch/lib/libtorch.so)
frame #7: main + 0x6a (0x42abaa in ./faster-rcnn-cpp)
frame #8: __libc_start_main + 0xf5 (0x7ffaed8a53d5 in /lib64/libc.so.6)
frame #9: ./faster-rcnn-cpp() [0x429699]

由于加载的模型pth或者pt文件,文件类型不同,无法加载,一般错误在这一句

torch::jit::script::Module module = torch::jit::load("reversed.pt");

需要在训练模型文件的时候,加上一个参数 _use_new_zipfile_serialization=True
在训练的时候,保存文件时

 torch.save(model, '1.pt', _use_new_zipfile_serialization=True)

或者将文件重新保存一遍

model_path = 'logs/Epoch93-Total_Loss0.1091-Val_Loss0.1511.pt'
# model
model = resnet50()[0]

model.load_state_dict(torch.load(model_path,map_location=lambda storage, loc: storage), strict=False)
torch.save(model.state_dict(),'logs/reversed.pt',_use_new_zipfile_serialization=True)

你可能感兴趣的:(linux学习记录,C++学习记录,python学习记录,c++,pytorch,linux)