(原)ubuntu中C++调用libotrch
转载请注明出处:
https://www.cnblogs.com/darkknightzh/p/11479240.html
参考网址:
https://pytorch.org/tutorials/advanced/cpp_export.html
https://github.com/pytorch/pytorch/issues/15476#issuecomment-478293447
主要目的是在ubuntu中使用C++调用libotrch,从而调用pytorch训练得到的模型。
cuda 10.0
python环境:anaconda,python3.6.8
1. 按照官方教程https://pytorch.org/tutorials/advanced/cpp_export.html编写对应文件
CMakeLists.txt和example-app.cpp
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(custom_ops) find_package(Torch REQUIRED) add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)
example-app.cpp:
#include <torch/script.h> // One-stop header. #include <iostream>
#include <memory> int main(int argc, const char *argv[])
{
if (argc != )
{
std::cerr << "usage: example-app <path-to-exported-script-module>\n";
return -;
} torch::jit::script::Module module;
try
{
// Deserialize the ScriptModule from a file using torch::jit::load().
module = torch::jit::load(argv[]);
}
catch (const c10::Error &e)
{
std::cerr << "error loading the model\n";
return -;
} std::cout << "ok\n";
}
2. 生成makefile,编译:
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch ..
make
3. 运行程序:
./example-app <path_to_model>/traced_resnet_model.pt
输出ok,证明成功了。
理论上应该成功,然而实际上。。。
1. 修改后的cmake命令(修改2后,此处可以不修改)
cmake -DCMAKE_PREFIX_PATH=/home/xxx/libtorch/libtorch -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.0 -DCUDA_NVCC_EXECUTABLE=/usr/local/cuda-10.0/bin -DCUDA_INCLUDE_DIRS=/usr/local/cuda-10.0/include ..
2. 按照https://github.com/pytorch/pytorch/issues/15476#issuecomment-478293447,修改cuda库的相关路径,修改后的libtorch/libtorch/share/cmake/Caffe2/Caffe2Targets.cmake:
set_target_properties(torch PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "_THP_CORE;AT_PARALLEL_OPENMP=1"
INTERFACE_COMPILE_OPTIONS "-Wall;-Wextra;-Wno-unused-parameter;-Wno-missing-field-initializers;-Wno-write-strings;-Wno-unknown-pragmas;-Wno-missing-braces;-fopenmp"
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include;${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "protobuf::libprotobuf;c10;Threads::Threads;caffe2::mkl;caffe2::mkldnn;torch::cudart;c10_cuda;/usr/local/cuda-10.0/targets/x86_64-linux/lib/libnvToolsExt.so;/usr/local/cuda-10.0/targets/x86_64-linux/lib/libcudart.so;caffe2::cufft;caffe2::curand;caffe2::cudnn;/usr/local/cuda-10.0/targets/x86_64-linux/lib/libculibos.a;dl;/usr/local/cuda-10.0/targets/x86_64-linux/lib/libculibos.a;caffe2::cublas"
)
说明:实际上只需要修改2就可以了。修改之后,可以编译成功。如果修改了1,不修改2,编译时还是找不到cuda相关的那些库(原因是,cuda10的这些库不在默认的路径下,导致编译失败。。。)
(原)ubuntu中C++调用libotrch的更多相关文章
- (原)ubuntu中安装kate
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6339642.html 参考网址: https://kate-editor.org/build-it/# ...
- (原+转)Eclipse中Android调用OpenCv
大部分都是参考下面的网址,如果感觉看起来不舒服,可以直接查看原网址.最后遇到了一点问题: Description Resource Path Location Type E:/~\cod ...
- (原)caffe在ubuntu中设置GPU的ID号及使用多个GPU
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5884892.html 参考网址: http://caffe.berkeleyvision.org/tu ...
- (原)ubuntu中安装tensorflow
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6592052.html 参考网址: https://www.tensorflow.org/install ...
- ubuntu中的Wine详解
什么是wine?(转自百度百科,具体看百科) wine,是一款优秀的Linux系统平台下的模拟器软件,用来将Windows系统下的软件在Linux系统下稳定运行,该软件更新频繁,日臻完善,可以运行许多 ...
- 【转】ubuntu中的Wine详解
原文网址:http://blog.csdn.net/iwtwiioi/article/details/10530561 什么是wine?(转自百度百科,具体看百科) wine,是一款优秀的Linux系 ...
- [转]ubuntu中查找软件的安装位置
原博客地址:http://www.cnblogs.com/zhuyatao/p/4060559.html ubuntu中的软件可通过图形界面的软件中心安装,也可以通过命令行apt-get instal ...
- 【转】在Ubuntu中安装HBase
原博客出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/ 感谢! Posted: Apr 3, 2014 Tags: Hado ...
- 在Ubuntu中设置中文输入法
在Ubuntu中设置中文输入法 */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola,&quo ...
随机推荐
- JS通过指定大小来压缩图片
安装: npm i image-conversion --save 引入: <script src="https://cdn.jsdelivr.net/gh/WangYuLue/ima ...
- 知识点总结 REACT
1.react中如何创建一个组件 ES6:class 组件名 extends Component{} ES5:var App=React.createClass({}) 2.render函数什么时候会 ...
- 第02组 Beta冲刺(2/4)
队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:了解整个游戏的流程 GitHub签入记录 接下来的计划:继续完成游戏 还剩下哪些任务:完成游戏 燃尽图 遇到的困难:没有美术比较好的 ...
- 拒绝后门程序-Alibabaprotect和AliPaladin
详细参考帖子及评论区:流氓进程AlibabaProtect的删除[程序员吧]_百度贴吧 首先打开服务找到AlibabaProtect,然后找到他的位置(C:\Program Files (x86)\A ...
- 分布式事物解决方案-TCC
分布式框架下,如何保证事物一致性一直是一个热门话题.当然事物一致性解决方案有很多种(请参考:分布式事物一致性设计思路),我们今天主要介绍TCC方案解决的思路.以下是参与设计讨论的一种解决思路,大家有问 ...
- What programming language is best for a bioinformatics beginner?
probably Unix Shell scripts, Perl, or Python and R can be the best options. ---------- 1-python 2-R ...
- 用欧拉计划学Rust语言(第7~12题)
最近想学习Libra数字货币的MOVE语言,发现它是用Rust编写的,所以先补一下Rust的基础知识.学习了一段时间,发现Rust的学习曲线非常陡峭,不过仍有快速入门的办法. 学习任何一项技能最怕没有 ...
- 1+x证书《Web前端开发》等级考试样题
Web前端开发初级理论考试样题2019 http://blog.zh66.club/index.php/archives/149/ Web前端开发初级实操考试样题2019 http://blog.zh ...
- Github问题:fatal: unable to access 'https://github.com/LIU-HONGYANG/Algorithm.git/': The requested URL returned error: 403
在向服务器push之后,出现如下问题: The requested URL returned error: 403 解决路径如下: 参考文章: https://stackoverflow.com/qu ...
- SpringBoot 系列教程自动配置选择生效
191214-SpringBoot 系列教程自动配置选择生效 写了这么久的 Spring 系列博文,发现了一个问题,之前所有的文章都是围绕的让一个东西生效:那么有没有反其道而行之的呢? 我们知道可以通 ...