-
Libtorch编译
2019-08-06 14:07:31关于libtorch的问题 libtorch的编译主要由下面两篇博文解决: https://www.cnblogs.com/cheungxiongwei/p/10689483.html ... libtorch编译完成后,写了一个cpp测试torch... -
libtorch编译C++版本
2020-12-05 12:15:33libtorch编译C++版本 一. 下载pytorch源码 git clone https://github.com/pytorch/pytorch.git cd pytorch git submodule sync git submodule update --init --recursive 二. 编译 1.安装依赖 # first: 安装cuda与...libtorch编译C++版本
一. 下载pytorch源码
git clone https://github.com/pytorch/pytorch.git cd pytorch git submodule sync git submodule update --init --recursive
下面是上传至百度网盘的源码,pytorch-1.8.0
链接:https://pan.baidu.com/s/1lxh7ueDsrnHqLaf5_oP2gg 提取码:8kp8
二. 编译
1.安装依赖
# first: 安装cuda与cudnn,下载cuda10.0对应.run文件与对应的cudnn7.6.5 sh cuda_10.0.130_410.48_linux.run --no-opengl-libs ldconfig /usr/local/cuda-10.0/lib64 tar -xvzf cudnn-10.0-linux-x64-v7.6.5.32.tgz cp cuda/include/* /usr/local/cuda-10.0/include/ cp cuda/lib64/* /usr/local/cuda-10.0/lib64/ chmod +x /usr/local/cuda-10.0/include/cudnn.h chmod +x /usr/local/cuda-10.0/lib64/libcudnn* nvcc -V # second:安装gcc g++ apt install apt install software-properties-common add-apt-repository ppa:ubuntu-toolchain-r/test apt-get update apt-get install gcc-7 apt-get install g++-7 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 100 update-alternatives --config gcc update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 100 update-alternatives --config g++ gcc --version g++ --version
2.编译libtorch
cd pytorch mkdir release cd release cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/opt/libtorch -D BUILD_CAFFE2_MOBILE=OFF -D BUILD_PYTHON=OFF -D BUILD_CAFFE2_OPS=OFF -D BUILD_TEST=OFF -D USE_CUDA=ON -D USE_CUDNN=ON -D USE_OPENCV=ON -D USE_TBB=OFF .. make -j${nproc} make install
三. 使用
python training
import torch import io class MyModule(torch.nn.Module): def forward(self, x): return x + 10 m = torch.jit.script(MyModule()) # Save to file torch.jit.save(m, 'scriptmodule.pt') # This line is equivalent to the previous m.save("scriptmodule.pt") # Save to io.BytesIO buffer buffer = io.BytesIO() torch.jit.save(m, buffer) # Save with extra files extra_files = {'foo.txt': b'bar'} torch.jit.save(m, 'scriptmodule.pt', _extra_files=extra_files)
c++ inference
#include <torch/script.h> // One-stop header. #include <iostream> #include <memory> int main(int argc, const char* argv[]) { if (argc != 2) { std::cerr << "usage: example-app <path-to-exported-script-module>\n"; return -1; } // Deserialize the ScriptModule from a file using torch::jit::load(). // libtorch verison 1.7.0 torch::jit::script::Module module = torch::jit::load(argv[1]); std::cout << "ok\n"; //设置Device torch::DeviceType device_type; //设置Device类型 device_type = torch::kCUDA; //torch::kCUDA and torch::kCPU torch::Device device(device_type, 0); //把模型和数据都送到Device中去(数据和模型必须都在同一个device,结果也是) module.to(device); // Create a vector of inputs. std::vector<torch::jit::IValue> inputs; inputs.push_back(torch::ones({1, 3, 224, 224}).to(device)); // Execute the model and turn its output into a tensor. at::Tensor output = module.forward(inputs).toTensor(); std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n'; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.0 FATAL_ERROR) project(custom_ops) set(CMAKE_CXX_STANDARD 11) set(Torch_DIR /home/xxx/下载/libtorch/share/cmake/Torch) #TorchConfig.cmake上级目录 find_package(Torch REQUIRED) set(OpenCV_DIR /opt/opencv440/lib/cmake/opencv4/) # OpenCVConfig.cmake上级目录 find_package(OpenCV REQUIRED) include_directories(${OpenCV_LIBS}) include_directories(${TORCH_INCLUDE_DIRS}) add_executable(example-app example-app.cpp) target_link_libraries(example-app ${OpenCV_LIBS} ${TORCH_LIBRARIES}) # 加入opencv/libTorch的库文件路径
-
libtorch编译时只使用单核cpu的问题
2020-08-14 09:44:11在ubuntu18.04虚拟机下面,从源码编译libtorch,发现编译速度超级慢,一看cpu只使用了一个核,应该是配置的时候参数有点问题,找了一下发现,应该是有个参数读出来的不太对,可能是因为虚拟机配置读取的问题,如下图...在ubuntu18.04虚拟机下面,从源码编译libtorch,发现编译速度超级慢,一看cpu只使用了一个核,应该是配置的时候参数有点问题,找了一下发现,应该是有个参数读出来的不太对,可能是因为虚拟机配置读取的问题,文件路径是pytorch/scripts/build_local.sh,最后一行的参数修改下,如下图
估计是这个CAFFE_MAKE_NCPUS的数据错了,如下,直接把变量改成4或者8,自己根据自己的cpu核数来指定就行。
改了之后快多了
-
Libtorch C++编译 使用问题整理
2020-11-19 14:19:36#libtorch C++ GPU版本编译问题 1.首先进行cuda版本编译,不建议手动在VS中创建项目然后进行,包含头include以及链接lib库,建议使用cmake进行编译。 按照如下的目录结构创建CMakeLists.txt文件 CMakeLists.txt内容...LibTorch问题整理
#C++ tensor cannot move from cpu to gpu
#libtorch C++ GPU版本编译问题
1.首先进行cuda版本编译,不建议手动在VS中创建项目然后进行,包含头include以及链接lib库,建议使用cmake进行编译。
按照如下的目录结构创建CMakeLists.txt文件
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 14)
example_app.cpp中可先创建一个main的简单函数
#include <iostream> #include "torch/torch.h" #include "torch/jit.h" int main() { std::cout << "Hello, World!" << std::endl; auto a = torch::tensor({{1, 2}, {3, 4}}); std::cout << a << std::endl; return 0; }
cd build cmake -DCMAKE_PREFIX_PATH=/media/DATA2/libtorch ..
/media/DATA2/libtorch 指向libtorch cuda版库的路径,注意最后结尾的…不可少
编译后build内容大致如下
2.VS打开工程,torch::cuda::is_available() 一直返回false,网上很多说cuda版本与显卡驱动版本不兼容,可以检查下自己电脑上的cuda版本与显卡驱动是否兼容?自己本地显卡支持CUDA版本为10.1,本地安装的也是CUDA10.1。
另外太低的CUDA版本可能不支持libtorch。本地配置libtorch1.5,libtorch1.6,cuda10.1版本,仍然找不到可用的显卡。升级libtorch到最新的libtorch1.7后问题解决,具体问题没有深度分析,估计是libtorch库开源时间不久,仍存在一些小的兼容性问题。auto cuda_available = torch::cuda::is_available(); std::cout << cuda_available << std::endl;
3.配置好需要的环境后,可能还存在找不到显卡问题,需要在VS连接器中加入 /INCLUDE:"?warp_size@cuda@at@@YAHXZ",该指令应该是指定编译程序需要cuda的一些库。
4.如何将tensor从cpu移动到gpu上,C++ Tensor not work move form cpu to gpu using opeator to 在Python中直接to操作即可完成数据CPU到GPU,或者GPU到CPU,但是在C++中遇到使用to结果数据移动不成功。
本人遇到的问题是模型从CPU 到GPU成功,例如:model.to(“cuda:0”)成功,但是输入数据input_tensor.to(“cuda:0”)不成功,改为input_tensor = input_tensor.to(“cuda:0”)之后成功,在判断input_tensor是在显卡显存空间。torch::Tensor input_tensor = torch::rand({ 3,4 }); std::cout << input_tensor.device() << std::endl; #输出cpu torch::Device device(torch::kCUDA, 0); torch::Tensor a = input_tensor.to(device) std::cout << input_tensor.device() << std::endl; #输出cpu std::cout << a.device() << std::endl; #输出cuda:0
-
深度学习--第1篇(续): Ununtu16.04源码编译libtorch(GUDA版本)环境配置
2020-10-25 15:28:31Ubuntu16.04+libtorch编译1.参考博客2.准备工作3.Libtorch编译安装3.1 下载Pytorch源码3.2 下载libtorch库3.3 CMakeLists编写4.低版本兼容问题(权重加载) 1.参考博客 Pytorch+libtorch编译 libtorch源码编译 2.... -
windwos源码编译libtorch(win10+32bit+libtorch)
2020-04-06 16:54:50win10编译32位libtorch 文章目录win10编译32位libtorch前言1. 安装Anaconda2. 安装Visual Studio 20173. 获取pytorch源码4. 设置环境变量1. 打开anaconda prompt2. 添加环境变量3. 编译好后libtorch的位置4. 测试... -
ubuntu 源码编译libtorch
2020-04-29 11:06:00有一点儿感悟就是: ...首先我跑的是pytorch 1.3版本的,conda安装好的,现在需要源码编译。按照官网的流程,需要先安装依赖包。 我是切换到pytorch1.3的conda虚拟环境进行的:这里是官网的教程: 其中,git clon... -
编译libtorch/pytorch
2019-11-07 15:59:48vs2017 64位 git clone --recursive https://github.com/pytorch/pytorch.git ...,生成build.ninja编译FLAGS是/MT,否则是/MD ...Release模式,编译时更新torch.pdb,这一步很慢,让人受不了。。。 -
Libtorch部署编译报错问题:non-scalar type, ‘->‘ has non-pointer type等
2020-11-05 15:28:25使用libtorch在C++中部署遇到了问题,大多是关于torch的API接口问题,因为使用网上大多的示例是很久之前的,而现在torch的很多API接口已经发生了改变,所以才会在编译的时候出现各种接口问题报错。 1. CMake error: ... -
Pytorch C++ libtorch的使用方法
2019-09-27 14:12:20pytorch C++ libtorch的,版本方面可以自己进行源码编译(有很多依赖),也可以从github上下载已经编译好的版本,官方使用教程给的libtorch编译时gcc版本较低,不支持CXX11所以,可以下载支持CXX11版本的libtorch ... -
win10 下源码编译Libtorch
2020-09-05 19:45:11问题需要编写pytorch的libtorch库,记录一下大致步骤与问题; 下载源码:从官方克隆最新的代码的时候要加入recursive这个参数,因为Pytorch本身需要很多的第三方库参与编译:git clone --recursive ... -
Linux下源码编译libtorch
2020-09-15 21:52:46参考官网:https://github.com/pytorch/pytorch/tree/v1.5.1 -
ubuntu18.04下libtorch的example的编译应用
2020-05-20 16:29:41搜索了好多方法,搞了很久,demo都编译不过,各种一堆错误,后来还是找到官网搞定的。 https://pytorch.org/cppdocs/installing.html 1 下载libtorch 2 解压之后变成libtorch文件夹 3 新建example-app ... -
ubuntu 18 编译libtorch 文件部署demo
2020-03-17 13:34:51编译好opencv, 下载好对应的libtorch 到pytorch官网上并解压到任意文件夹 编译所需文件: 建议文件夹:test_two , mkdir build , 将以下2个文件放入build main.cpp makelists.txt 一张图像 7.jpg main.cpp内容: ... -
libtorch,TenSorRt跨平台编译一二
2020-12-24 13:21:521.3.0时,需要支持c++14的编译器编译 2. 输入 模型pt文件(模型文件需要转化,pt->wts),linux下的cmakelist工程文件 详情见yolov3工程 3. cudnn安装 cudnn下载之后不用安装,解压即可。 下载解压缩后,将其... -
Tx2编译torch及libtorch
2019-07-05 13:39:27torch编译 适用于TX2 JetPack4.2,系统内置Ubuntu版本18.04,GCC版本7.4.0,CUDA版本10.0,cudNN版本7.3.1,OpenCV版本3.3.1 pytorchV1.1.0 python2.7 wget ... -
使用cmake和vs2019进行编译libtorch过程
2020-12-29 08:57:451. 从官网下载已经编译好的LibTorch库 官网地址:(https://pytorch.org/) 根据自己的环境可以下载GPU版和CPU版。 2. 使用cmake命令创建项目,失败! 安装使用可参考官网文档:...