FastDeploy C++ GPU本地编译(Windows)

由于官方已经编译好的opencv版本较旧,所以本地编译。

除了指定opencv路径以外(不指定会自动下载,版本较旧),其他所有按官方步骤进行

FastDeploy/build_on_win_with_gui.md at release/1.0.5 · PaddlePaddle/FastDeploy (github.com)

 如果想openvino、onnxruntime也用自己指定的版本,已经尝试过在CMake GUI中指定路径但没有编译生成(可能是cmake有问题,如果您编译成功,欢迎留言分享)。可以尝试如下方法:

可以先不指定路径,CMake GUI上Configure、Generate、OpenProject,然后VS自动打开后,ALL_BUILD处鼠标右键——生成,等待……等待……编译完成。

“自己指定的build路径\third_libs\install”路径里会出现openvino、onnxruntime的文件夹,将自己下载的版本对应文件替换进去,再次编译。

下载地址:Releases · microsoft/onnxruntime (github.com)

Releases · openvinotoolkit/openvino (github.com)

---------------------------------------------------------------------- 进入正题 --------------------------------------------------------------------

下载并配置编译环境,参考我的博客 深度学习 - 随笔分类 - 夕西行 - 博客园 (cnblogs.com)

版本:

  • TensorRT-8.5.3.1.Windows10.x86_64.cuda-11.8.cudnn8.6
  • cuda_11.8.0_522.06_windows
  • cudnn-windows-x86_64-8.6.0.163_cuda11-archive
  • zlib x64
  • OpenCV 4.7.0

1、下载源码 Releases · PaddlePaddle/FastDeploy (github.com)

选择最新的release

2、CMake GUI编译Release版本(官方只提供了Release版)

 点击Configure,选择vs2019,x64,确定后进行如下设置:

只编译Release版本:

勾选:

  • ENABLE_ORT_BACKEND
  • ENABLE_PADDLE_BACKEND
  • ENABLE_TRT_BACKEND
  • ENABLE_OPENVINO_BACKEND
  • ENABLE_VISION
  • ENABLE_TEXT

选择路径:

  • opencv路径,
  • 设置最终编译完毕后的库路径(手动新建),
  • 设置cuda,
  • 设置tensorrt,
  • 勾选GPU,

3、以上设置完毕后,点击Configure,多次点击不出现红色提示后,点击Generate,Open Project。

VS自动打开后,ALL_BUILD处鼠标右键——生成,等待……等待……编译完成。

等待编译完毕后,INSTALL处鼠标右键——生成,库就出现在之前设置的路径里了。

在库文件夹中新建bin,双击 fastdeploy_init.bat,将所有的dll放入bin中。

bin添加到系统环境变量。

【实战】

vs新建infer控制台程序

#include "fastdeploy/vision.h"

int main() {
    namespace vision = fastdeploy::vision;
    auto model = vision::detection::PPYOLOE("ppyoloe_crn_l_300e_coco/model.pdmodel",
        "ppyoloe_crn_l_300e_coco/model.pdiparams",
        "ppyoloe_crn_l_300e_coco/infer_cfg.yml");
    auto im = cv::imread("000000014439.jpg");

    vision::DetectionResult res;
    model.Predict(im, &res);

    auto vis_im = vision::VisDetection(im, res, 0.5);
    cv::imwrite("vis_image.jpg", vis_im);
    return 0;
}

【CPU的配置文件props】

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ImportGroup Label="PropertySheets" />
    <PropertyGroup Label="UserMacros" />
    <PropertyGroup />
    <ItemDefinitionGroup>
        <ClCompile>
            <AdditionalIncludeDirectories>C:\opencv\build\include;C:\opencv\build\include\opencv2;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
            <AdditionalIncludeDirectories>C:\fastdeploy-win-x64-cpu-1.0.5\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
        </ClCompile>
        <Link>
            <AdditionalLibraryDirectories>C:\opencv\build\x64\vc16\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
            <AdditionalDependencies>opencv_world470.lib;%(AdditionalDependencies)</AdditionalDependencies>
            <AdditionalLibraryDirectories>C:\fastdeploy-win-x64-cpu-1.0.5\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
            <AdditionalDependencies>fastdeploy.lib;%(AdditionalDependencies)</AdditionalDependencies>
        </Link>
    </ItemDefinitionGroup>
</Project>

【GPU的配置文件props】

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ImportGroup Label="PropertySheets" />
    <PropertyGroup Label="UserMacros" />
    <PropertyGroup />
    <ItemDefinitionGroup>
        <ClCompile>
            <AdditionalIncludeDirectories>C:\opencv\build\include;C:\opencv\build\include\opencv2;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
            <AdditionalIncludeDirectories>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
            <AdditionalIncludeDirectories>C:\fastdeploy-win-x64-gpu-1.0.5\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
        </ClCompile>
        <Link>
            <AdditionalLibraryDirectories>C:\opencv\build\x64\vc16\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
            <AdditionalDependencies>opencv_world470.lib;%(AdditionalDependencies)</AdditionalDependencies>
            <AdditionalLibraryDirectories>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\lib\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
            <AdditionalDependencies>cublasLt.lib;cuda.lib;cudadevrt.lib;cudart.lib;cudart_static.lib;cudnn.lib;cudnn_adv_infer.lib;cudnn_adv_infer64_8.lib;cudnn_adv_train.lib;cudnn_adv_train64_8.lib;cudnn_cnn_infer.lib;cudnn_cnn_infer64_8.lib;cudnn_cnn_train.lib;cudnn_cnn_train64_8.lib;cudnn_ops_infer.lib;cudnn_ops_infer64_8.lib;cudnn_ops_train.lib;cudnn_ops_train64_8.lib;cudnn64_8.lib;cufft.lib;cufftw.lib;cufilt.lib;curand.lib;cusolver.lib;cusolverMg.lib;cusparse.lib;nppc.lib;nppial.lib;nppicc.lib;nppidei.lib;nppif.lib;nppig.lib;nppim.lib;nppist.lib;nppisu.lib;nppitc.lib;npps.lib;nvblas.lib;nvjpeg.lib;nvml.lib;nvptxcompiler_static.lib;nvrtc.lib;nvrtc_static.lib;nvrtc-builtins_static.lib;OpenCL.lib;%(AdditionalDependencies)</AdditionalDependencies>
            <AdditionalLibraryDirectories>C:\fastdeploy-win-x64-gpu-1.0.5\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
            <AdditionalDependencies>fastdeploy.lib;%(AdditionalDependencies)</AdditionalDependencies>
        </Link>
        <CudaCompile>
              <CodeGeneration>compute_61,sm_61</CodeGeneration>
              <TargetMachinePlatform>64</TargetMachinePlatform>
        </CudaCompile>
    </ItemDefinitionGroup>
</Project>

 

有的电脑运行时会出现如下错误:

原因是编译得到的 yaml-cpp.dll,即使已经添加到系统环境变量,exe依然识别不到它。所以将它放到 C:\Windows\System32 或者 放到exe所在目录。

有的电脑程序无法运行:

可能是C盘有多余的其他版本的dll,如C:\Windows\SysWOW64\或者System32中onnxruntime.dll,删除之。

【模型转换】

onnx格式的模型可以使用官方转换工具,转为paddle。GitHub - PaddlePaddle/X2Paddle: Deep learning model converter for PaddlePaddle. (『飞桨』深度学习模型转换工具)

官方提供的工具 FastDeploy/README.md at develop · PaddlePaddle/FastDeploy · GitHub

posted @ 2023-03-28 23:01  夕西行  阅读(1140)  评论(0编辑  收藏  举报