Pytorch模型转C++
-
首先,在pytorch官网下载libtorch对应系统版本的压缩包并解压。
-
打开vs,编译器选择ISO C++14标准
- 设置附加包含目录
- 设置附加库目录
-
设置附加依赖项
需要把所有lib加进来,可以在lib文件夹下输入下面命令
ls *.lib
查看文件夹下的所有lib文件
- 修改两个地方:
- 第一项:
- 第二项:
-
其他问题:
-
如果报错说找不到XXX.dll,可以将lib文件夹里对应的XXX.dll放到项目文件夹可执行文件.exe所在目录下
-
libtorch版本经过更迭,测试代码应改写为:
#include <torch/script.h> using torch::jit::script::Module; Module module = torch::jit::load("<path to .pt>") vector<torch::jit::IValue> inputs; inputs.push_back(torch::rand({1,1,80})); at::Tensor output = module.forward(inputs).toTensor();
-