当前位置:   article > 正文

深度学习--第1篇(续): Ununtu16.04源码编译libtorch(GUDA版本)环境配置_libtorch 国内镜像源

libtorch 国内镜像源

1.参考博客

Pytorch+libtorch编译
libtorch源码编译

2.准备工作

笔者采用Anaconda的虚拟环境下安装Pytorch 1.5.0版本,具体版本如下:torchvision=0.6.0,python=3.6,CUDA=10.1, cudnn=7.6.5
上述的需要预先安装完成

具体安装步骤可参考上一篇博客:Pytorch安装

3.Libtorch编译安装

3.1 下载Pytorch源码

# 1.切换到conda的虚拟环境下
source activate wind

# 2 由于pytorch的官网下载速度慢,可以参考笔者的另一篇,添加清华的镜像源下载
[清华镜像源安装方法](https://blog.csdn.net/qq_37568167/article/details/105620960)

# 3.安装依赖库, 需要注意 cmake版本采用3.5.1,如果版本过高会存在编译不通过
conda install numpy ninja pyyaml mkl mkl-include setuptools cffi typing_extensions future six requests dataclasses

# 4.安装对CUDA的支持包, 对应自己版本的cuda,笔者的时10.1 
conda install  magma-cuda101  # or [ magma-cuda102 | magma-cuda100 | magma-cuda92 ] 
# conda install -c pytorch magma-cuda101  

# 5. 编译Pytorch
# 由于官网的下载较慢,笔者采用国内gitee上进行下载, 下载时间大约需要1.5个小时
git clone https://gitee.com/wind_x/pytorch.git

# 6. 下载完成后,进行获取其中的子模块
cd pytorch
git submodule sync
git submodule update --init --recursive

# 7. 选择自己需要的Pytorch版本,需要与anaconda安装的pytorch版本对应,笔者的是1.5.0版本
git tag -l
git checkout v1.5.0

# 8.编译pytorch, 需要大约2个小时;安装扔需要2个小时,共计4个小时
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
mkdir build
cd build
cmake ..
make -j4
sudo make install

# 9.编译成功后,会生成libtorch.so动态链接库,存储位置
动态链接库: /usr/local/lib/libtorch.so
功能包: /usr/local/share/cmake/Torch
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

3.2 下载libtorch库

注意: libtorch版本向上兼容,即高版本的pytorch和低版本的libtorch(例如笔者:Pytorch=1.5.0, libtorch=1.3.0), 一定要选择下载c++11的预编译版本,使用ABI编译的版本

由于笔者采用的时pytorch=1.5.0, 且 cuda=10.1, 因此下载libtorch<=1.5.0的库,但是cuda需要对应。 具体的libtorch下载地址:libtorch=1.3.0, cuda=10.1

https://download.pytorch.org/libtorch/cu101/libtorch-cxx11-abi-shared-with-deps-1.3.0.zip

其中加粗部分可以根据自己的需求进行更换,主要是cuda版本号pytorch版本号 (libtorch1.6.0版本之前的都可以)

# 解压,解压后得到的libtorch文件夹便是可以直接使用的
unzip libtorch-cxx11-abi-shared-with-deps-1.3.0.zip
  • 1
  • 2

3.3 CMakeLists编写

# 使用libtorch时,需要在CMakeLists.txt中添加
# 表示的时通过3.3节libtorch下载解压后的文件夹位置
set(CMAKE_PREFIX_PATH "/home/lenovo/libtorch")
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)

# 寻找torch包时可以放在其他的下面
find_package(Torch REQUIRED)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

较为完整的CMakeLists.txt文件

cmake_minimum_required(VERSION 3.0.2)
project(CNN_SLAM)

set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g")

## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  roscpp
  rospy
  sensor_msgs
  std_msgs
)

## System dependencies are found with CMake's conventions
find_package(OpenCV 3.0 REQUIRED)
find_package(Eigen3 REQUIRED)

catkin_package(

)


include_directories(
  # include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${EIGEN_INCLUDE_DIRS}
)

set(CMAKE_PREFIX_PATH "/home/lenovo/libtorch")
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)
find_package(Torch REQUIRED)
include_directories(include/)

## Declare a C++ library
add_library(${PROJECT_NAME} SHARED
  src/cnn_slam.cpp
)

target_link_libraries(${PROJECT_NAME} 
${catkin_LIBRARIES} 
${OpenCV_LIBS}
${EIGEN_LIBS}
${TORCH_LIBRARIES}
)


## Declare a C++ executable
add_executable(CNN_SLAM_node CNN_SLAM_node.cpp)

target_link_libraries(CNN_SLAM_node ${PROJECT_NAME})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

4.低版本兼容问题(权重加载)

# 对于v1.1.0
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load("./model.pt");

# >1.1.0
torch::jit::script::Module module = torch::jit::load("./model.pt");
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/943106
推荐阅读
相关标签
  

闽ICP备14008679号