好久没有写博客了,想接着之前pytorch训练好模型一直往下写,如何使用c++调用pytorch训练的pytorch模型。pytorch 1.0已经提供cpp接口了然后我就在mac上捣鼓了一下,发现还是有坑的。我把安装流程记录下来。(顺便求个关注和赞)
1.安装pytorch
比较喜欢使用anaconda进行包管理,首先我创建一个虚拟环境,python使用3.6,虚拟环境名称cpp:
conda create -n cpp python=3.6
下面激活进入到虚拟环境:
source activate cpp
安装pytorch包:
pip install torch==1.0
pip install torchvision
好了,现在python环境已经搭建完毕(如果感觉你使用pip源慢的话,可以自行替换成清华、阿里、豆瓣的,速度都非常快,教程网上非常多),这边肯定都是轻车熟路没有坑。
2.编译cpp环境
编译cpp环境,发现有坑。
1)下载libtorch包
主要参考官网文档
Installing C++ Distributions of PyTorchpytorch.org官网文档提到的下载libtorch使用的指令:
wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
实际这个libtorch是根据不同系统需要不同的包的。最好的方法是去官网主页按照自己需求下载。
2)定义CMakeLists.txt,按照说明来就可以。我为了测试一个项目,参考一下这个链接里的代码
iamhankai/cpp-pytorchgithub.com3)编译
cmake 结束后就是make,这边就是有坑。
接着2)步,需要注释掉example-app.cpp里的#include<torch/Tensor.h>,估计原作者没注意到这里。
当在build文件夹下面make的时候可能会出现如下指令:
Scanning dependencies of target example-app
[ 50%] Building CXX object CMakeFiles/example-app.dir/example-app.cpp.o
In file included from /Users/***/Desktop/cpp/libtorch/include/torch/csrc/jit/script/tree.h:7:0,
from /Users/***/Desktop/cpp/libtorch/include/torch/csrc/jit/script/error_report.h:3,
from /Users/***/Desktop/cpp/libtorch/include/torch/csrc/jit/script/compiler.h:7,
from /Users/***/Desktop/cpp/libtorch/include/torch/csrc/api/include/torch/jit.h:3,
from /Users/***/Desktop/cpp/libtorch/include/torch/csrc/api/include/torch/all.h:5,
from /Users/***/Desktop/cpp/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
from /Users/***/Desktop/cpp/test/example-app.cpp:8:
/Users/***/Desktop/cpp/libtorch/include/torch/csrc/jit/script/lexer.h: In static member function ‘static double torch::jit::script::SharedParserData::strtod_c(const char*, char**)’:
/Users/***/Desktop/cpp/libtorch/include/torch/csrc/jit/script/lexer.h:169:12: error: ‘locale_t’ does not name a type
static locale_t loc = newlocale(LC_ALL_MASK, "C", nullptr);
^
/Users/***/Desktop/cpp/libtorch/include/torch/csrc/jit/script/lexer.h:170:31: error: ‘loc’ was not declared in this scope
return strtod_l(str, end, loc);
^
/Users/***/Desktop/cpp/libtorch/include/torch/csrc/jit/script/lexer.h:170:34: error: ‘strtod_l’ was not declared in this scope
return strtod_l(str, end, loc);
^
make[2]: *** [CMakeFiles/example-app.dir/example-app.cpp.o] Error 1
make[1]: *** [CMakeFiles/example-app.dir/all] Error 2
当时报错以后我觉得可能是我系统问题,如果你没遇到这个问题,继续往下走。我当时解决这个问题是把mac升级并且xcode修复解决了这个问题。
3.加载模型运行
这一步运行指令:
./example-app ../model.pt ../dog.png ../synset_words.txt
报错:
dyld: Library not loaded: @rpath/libiomp5.dylib
Referenced from: /Users/**/Desktop/cpp/libtorch/lib/libcaffe2.dylib
Reason: image not found
Abort trap: 6
遇到这个问题,网上搜到解决方法(PyTorch C++ 使用笔记),安装mkl-dnn,懒得自己编译的,可以直接从intel/mkl-dnn下载,解压后把lib目录下libmklml.dylib,libiomp5.dylib 复制到libtorch/lib下,然后就可以开心的使用cpp加载pytorch模型了。
不过我测试一下了发现cpp调用模型时间比pytorch还慢???预知是否如此,大家可以按照步骤自己操作一下试试吧。