通过c++加载python训练好的模型,libtorch
torch::jit::script::Module model_;
model_ = torch::jit::load(model);
terminate called after throwing an instance of 'c10::Error'
what(): PytorchStreamReader failed locating file constants.pkl: file not found
Exception raised from valid at ../caffe2/serialize/inline_container.cc:158 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11::basic_string
frame #2: caffe2::serialize::PyTorchStreamReader::valid(char const*, char const*) + 0x3ca (0x7f5bdacea09a in ../lib/libtorch/libtorch_cpu.so)
正确加载
这个错误不像是链接错误
PytorchStreamReader failed locating file constants.pkl: file not found 这句话应该是pt文件受损了
1、下载libtorch时,需要选择跟python中使用的版本一致
2、换个训练好的权重文件
3、把pt文件删掉重新保存一下
有一个可能得原因是这个 pt文件里只有权重,而没有网络结构。 所以 只能用 torch.load去加载,不能用torch.jit.load