本文主要参考:
(1)
Ubuntu18下编译安装torchvision详细流程—C++API_Flag_ing的博客-CSDN博客_编译torchvision
(二)libtorch和torchvision的编译安装_陈建渠的博客-CSDN博客_torchvision编译
(3)在Win10上编译安装libtorch+torchvision_阿白的博客-CSDN博客_在win10上编译libtorch
一、简介
最近,我使用libtorch来部署yolov5网络模型。 我用的是大步行道的yolort。 关键部分是将后处理nms与后面的模型部分一起导入,并使用torchvision自带的gpu版本的nms。 因此c++端使用官方的nms.h等文件,但是这个文件需要安装torchvision,而torchvision并没有包含在官方的LibTorch包中,需要自己下载源码编译安装。 在安装过程中,遇到了一些问题,在此记录一下。
2.编译安装步骤
1.下载torchvision源码包
vision,特定于计算机视觉的数据集、转换和模型,下载vision的源码_GitHub_帮酷
这里需要注意的一件事是安装的 torchvision 必须与您的 LibTorch 版本相对应。 从GitHub主页的标签中下载对应版本的torchvision。 这个对应关系在官方GitHub上的解释如下:
我目前使用的libtorch是1.9版本,所以我按照pytorch类比,torchvision的版本是0.10.0
2、详细安装步骤
(1)解压源码包并重命名为vision(为了方便)
(2)新建torchvision文件夹
(3)在vision下新建build文件夹并输入
(4)安装pybind
需要CMake,最好是最新版本。 安装CMake后,打开cmd命令行并输入以下命令。 注意路径。
git clone https://github.com/pybind/pybind11.git
cd pybind11
mkdir build
cd build
cmake -DPYBIND11_TEST=OFF -DCMAKE_INSTALL_PREFIX="wheretoinstallpybind11" ..
cmake --install .
如果cmake报错! 看这个:因为c++编译器是VS默认使用的32位编译器,所以如果使用的python是64位的话,就会报错。 解决办法就是将cmake这句替换成下面的命令windows 编译源码包,然后安装即可。 (其实就是指定64位编译器,至于VS的具体版本,要根据自己安装的版本来替换,错误信息上有提示)
(5)安装libjpeg和zlib
接下来,使用 conda 命令行在环境中安装两个包:
conda install -c conda-forge libjpeg-turbo
conda install -c anaconda zlib
这一步是因为torchvision的CMakeLists.txt中有这几行:
find_package(Python3 COMPONENTS Development)
find_package(Torch REQUIRED)
find_package(PNG REQUIRED)
find_package(JPEG REQUIRED)
这意味着这里不仅需要libtorch,还需要PNG和JPEG。 如果不安装这两个东西的话windows 编译源码包,前面会报错。
(6)构建火炬愿景
cmake -DCMAKE_PREFIX_PATH="D:programfilelibtorch;C:wheretoinstallpybind11" .. -DCMAKE_INSTALL_PREFIX="D:programfiletorchvision_0100torchvision" -DCMAKE_BUILD_TYPE=Release -DWITH_CUDA=ON ..
注意:因为我要安装GPU版本,所以如果要安装CPU版本,不能在Release前的命令行中添加-DWITH_CUDA=ON参数
完成后,构建文件夹应该生成这些内容:
(7)cmake工程完善后的VS编译安装
这一步和linux下是等价的
这里我在vs2019下编译生成:
步骤1:双击torchvision.sln
step2:修正编译模式并设置启动项
上面cmake完善工程时我们选择的是Release模式,所以这里vs编译要修正为Release模式,并将ALL_BUILD设置为启动工程。右键ALL_BUILD>Generate
我查看了build编译安装后的文件夹下,可能只有Release
有用的文件夹:
另外,我们认为我们不应该只需要这个torchvision.dll和torchvision.lib。 因为我发现就torchvision源码包自带的例子来说,需要.h文件:
让我们测试一下这个例子:
步骤3:精简编译torchvision库和测试代码示例
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(hello-world)
# The first thing do is to tell cmake to find the TorchVision library.
# The package pulls in all the necessary torch libraries,
# so there is no need to also add `find_package(Torch)` here.
find_package(TorchVision REQUIRED)
add_executable(hello-world main.cpp)
# We now need to link the TorchVision library to our executable.
# We can do that by using the TorchVision::TorchVision target,
# which also adds all the necessary torch dependencies.
target_compile_features(hello-world PUBLIC cxx_range_for)
target_link_libraries(hello-world TorchVision::TorchVision)
set_property(TARGET hello-world PROPERTY CXX_STANDARD 14)
主要.cpp:
#include
#include
#include
#include
int main()
{
auto model = vision::models::ResNet18();
model->eval();
// Create a random input tensor and run it through the model.
auto in = torch::rand({1, 3, 10, 10});
auto out = model->forward(in);
std::cout <to(torch::kCUDA);
auto gpu_in = in.to(torch::kCUDA);
auto gpu_out = model->forward(gpu_in);
std::cout << gpu_out.sizes();
}
}
自述文件.rst:
Hello World!
============
This is a minimal example of getting TorchVision to work in C++ with CMake.
In order to successfully compile this example, make sure you have both ``LibTorch`` and
``TorchVision`` installed.
Once both dependencies are sorted, we can start the CMake fun:
1) Create a ``build`` directory inside the current one.
2) from within the ``build`` directory, run the following commands:
- | ``cmake -DCMAKE_PREFIX_PATH=";" ..``
| where ```` and ```` are the paths to the libtorch and torchvision installations.
- ``cmake --build .``
| That's it!
| You should now have a ``hello-world`` executable in your ``build`` folder.
Running it will output a (fairly long) tensor of random values to your terminal.
在这个测试用例中,我最终没有使用cmake。
我最终自己创建了一个新的解决方案,然后配置相关属性:
附加依赖项:
火炬视觉库
c10.lib
动能库
C:Program FilesNVIDIA CorporationNvToolsExt\libx64nvToolsExt64_1.lib
cudart_static.lib
caffe2_nvrtc.lib
c10_cuda.lib
火炬库
torch_cuda.lib
torch_cuda_cu.lib
D:programfilelibtorchlibtorch_cuda_cpp.lib
torch_cpu.lib
-包括:?warp_size@cuda@at@@YAHXZ
袖口库
curand.lib
cublas.lib
cudnn.lib
-包括:?searchsorted_cuda@native@at@@YA?AVTensor@2@AEBV32@0_N1@Z
内核32.lib
user32.lib
gdi32库
温斯普尔库
shell32.lib
ole32库
oleaut32.lib
uuid库
comdlg32.lib
advapi32.lib
运行结果如下:
最后值得注意的是:
我们已经完成了编译。 事实上,我们后续的项目只需要两个文件夹:
(1)在构建编译安装后的文件夹下的Release文件夹中有用。
(2)由于很多项目都需要在torchvsion包中包含一些.h头文件,所以不难想象,一定会有一些带有头文件的文件夹。 如下:
我刚刚复制了这两个文件夹并重命名它们:
这样就可以明确哪些include应该放在include目录下,哪个lib应该放在library目录下。
而且我这次编译后得到的文件是可以移植到任何其他机器上的。