FastDeploy在Windows平台上使用C++开发介绍

2023-10-15 12:59

本文主要是介绍FastDeploy在Windows平台上使用C++开发介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 FastDeploy库环境准备

1.1 预编译库安装

FastDeploy提供各平台预编译库,供开发者直接下载安装使用。当然FastDeploy编译也非常容易,开发者也可根据自身需求编译FastDeploy。

本文分为两部分:

  • 1.GPU部署环境
  • 2.CPU部署环境

1.1.1 GPU部署环境

环境要求
  • CUDA >= 11.2
  • cuDNN >= 8.0
  • python >= 3.6
  • OS: Linux(x64)/Windows 10(x64)

支持CPU和Nvidia GPU的部署,默认集成Paddle Inference、ONNX Runtime、OpenVINO以及TensorRT推理后端,Vision视觉模型模块,Text文本NLP模型模块

版本信息:
Paddle Inference:2.4-dev5
ONNXRuntime:1.12.0
OpenVINO:2022.2.0.dev20220829
TensorRT:8.5.2.2

C++ SDK安装

Release版本

平台文件说明
Linux x64fastdeploy-linux-x64-gpu-1.0.7.tgzg++ 8.2, CUDA 11.2, cuDNN 8.2编译产出
Windows x64fastdeploy-win-x64-gpu-1.0.7.zipVisual Studio 16 2019, CUDA 11.2, cuDNN 8.2编译产出

Develop版本(Nightly build)

平台文件说明
Linux x64fastdeploy-linux-x64-gpu-0.0.0.tgzg++ 8.2, CUDA 11.2, cuDNN 8.2编译产出
Windows x64fastdeploy-win-x64-gpu-0.0.0.zipVisual Studio 16 2019, CUDA 11.2, cuDNN 8.2编译产出

需要注意的是,使用的FastDeploy脚本的版本要与下载的版本一致,如果是SDK是Release建议FastDeploy库也选用Release。

1.1.2 CPU部署环境

1.2 自行编译C++ SDK

详见链接:打开学习
建议使用编译好的库,效率较高,少花时间。

以win10下编译简单讲解一下
Windows编译需要满足条件

  • Windows 10/11 x64
  • Visual Studio 2019
  • cuda >= 11.2
  • cudnn >= 8.2

注意:安装CUDA时需要勾选Visual Studio Integration, 或者手动将C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\extras\visual_studio_integration\MSBuildExtensions\文件夹下的4个文件复制到C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations\文件夹。否则执行cmake命令时可能会遇到No CUDA toolset found报错。

在Windows菜单中,找到x64 Native Tools Command Prompt for VS 2019打开,执行如下命令

git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy
mkdir build && cd build
cmake .. -G "Visual Studio 16 2019" -A x64 ^-DENABLE_ORT_BACKEND=ON ^-DENABLE_PADDLE_BACKEND=ON ^-DENABLE_OPENVINO_BACKEND=ON ^-DENABLE_TRT_BACKEND=OFF ^-DENABLE_VISION=ON ^-DENABLE_TEXT=OFF ^-DWITH_GPU=OFF ^-DTRT_DIRECTORY=D:\Paddle\TensorRT-8.4.1.5 ^-DCUDA_DIRECTORY=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6 ^-DCMAKE_INSTALL_PREFIX=D:\Paddle\compiled_fastdeploy
% nuget restore  (please execute it when WITH_CSHARPAPI=ON to prepare dependencies in C#)
msbuild fastdeploy.sln /m /p:Configuration=Release /p:Platform=x64
msbuild INSTALL.vcxproj /m /p:Configuration=Release /p:Platform=x64
git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy
mkdir build && cd build
cmake .. -G "Visual Studio 16 2019" -A x64 ^-DENABLE_ORT_BACKEND=OFF ^-DENABLE_PADDLE_BACKEND=OFF ^-DENABLE_OPENVINO_BACKEND=ON ^-DENABLE_TRT_BACKEND=OFF ^-DENABLE_VISION=ON ^-DENABLE_TEXT=OFF ^-DWITH_GPU=OFF ^-DOPENCV_DIRECTORY=D:\bushu\opencv\build ^-DCMAKE_INSTALL_PREFIX=D:\fastdeploy_cpumsbuild fastdeploy.sln /m /p:Configuration=Release /p:Platform=x64
msbuild INSTALL.vcxproj /m /p:Configuration=Release /p:Platform=x64

编译完成后,即在CMAKE_INSTALL_PREFIX指定的目录下生成C++推理库

如您使用CMake GUI可参考文档Windows使用CMakeGUI + Visual Studio 2019 IDE编译

2 下载FastDeploy代码

FastDeploy库里面包含了许多代码示例,还有文档的讲解,强烈建议您下载。

git clone https://github.com/PaddlePaddle/FastDeploy
# 如果上面网站速度比较慢,也可以使用国内的Gitee下载
git clone https://gitee.com/paddlepaddle/FastDeploy

本示例中使用的是分割代码,C++代码的目录如下:

FastDeploy/ examples / vision / segmentation / paddleseg / semantic_segmentation / cpu-gpu /

这段代码一会需要拷贝到visual studio编译器中,提前准备好。

3 深度学习模型

您可以自己训练深度学习模型,这里使用预训练好的模型,模型下载地址为:

https://bj.bcebos.com/paddlehub/fastdeploy/PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer.tgz

测试图片下载地址为:

https://paddleseg.bj.bcebos.com/dygraph/demo/cityscapes_demo.png

创建Visual studio工程

下载并安装Visual Studio 2019,点击新建,选择C++控制台应用,点击下一步

在这里插入图片描述
填入项目名称,选择项目的位置,点击创建
在这里插入图片描述
在上面状态栏将项目设置为Release,选择x64平台。
在这里插入图片描述
删除代码,将FastDeploy/ examples / vision / segmentation / paddleseg / semantic_segmentation / cpu-gpu /infer.cc中的代码拷贝到编辑器中,代码如下:

// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.#include "fastdeploy/vision.h"#ifdef WIN32
const char sep = '\\';
#else
const char sep = '/';
#endifvoid CpuInfer(const std::string& model_dir, const std::string& image_file) {auto model_file = model_dir + sep + "model.pdmodel";auto params_file = model_dir + sep + "model.pdiparams";auto config_file = model_dir + sep + "deploy.yaml";auto option = fastdeploy::RuntimeOption();option.UseCpu();auto model = fastdeploy::vision::segmentation::PaddleSegModel(model_file, params_file, config_file, option);if (!model.Initialized()) {std::cerr << "Failed to initialize." << std::endl;return;}auto im = cv::imread(image_file);fastdeploy::vision::SegmentationResult res;if (!model.Predict(im, &res)) {std::cerr << "Failed to predict." << std::endl;return;}std::cout << res.Str() << std::endl;auto vis_im = fastdeploy::vision::VisSegmentation(im, res, 0.5);cv::imwrite("vis_result.jpg", vis_im);std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}void GpuInfer(const std::string& model_dir, const std::string& image_file) {auto model_file = model_dir + sep + "model.pdmodel";auto params_file = model_dir + sep + "model.pdiparams";auto config_file = model_dir + sep + "deploy.yaml";auto option = fastdeploy::RuntimeOption();option.UseGpu();auto model = fastdeploy::vision::segmentation::PaddleSegModel(model_file, params_file, config_file, option);if (!model.Initialized()) {std::cerr << "Failed to initialize." << std::endl;return;}auto im = cv::imread(image_file);fastdeploy::vision::SegmentationResult res;if (!model.Predict(im, &res)) {std::cerr << "Failed to predict." << std::endl;return;}std::cout << res.Str() << std::endl;auto vis_im = fastdeploy::vision::VisSegmentation(im, res, 0.5);cv::imwrite("vis_result.jpg", vis_im);std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}void TrtInfer(const std::string& model_dir, const std::string& image_file) {auto model_file = model_dir + sep + "model.pdmodel";auto params_file = model_dir + sep + "model.pdiparams";auto config_file = model_dir + sep + "deploy.yaml";auto option = fastdeploy::RuntimeOption();option.UseGpu();option.UseTrtBackend();// If use original Tensorrt, not Paddle-TensorRT,// comment the following two linesoption.EnablePaddleToTrt();option.EnablePaddleTrtCollectShape();option.SetTrtInputShape("x", {1, 3, 256, 256}, {1, 3, 1024, 1024},{1, 3, 2048, 2048});auto model = fastdeploy::vision::segmentation::PaddleSegModel(model_file, params_file, config_file, option);if (!model.Initialized()) {std::cerr << "Failed to initialize." << std::endl;return;}auto im = cv::imread(image_file);fastdeploy::vision::SegmentationResult res;if (!model.Predict(im, &res)) {std::cerr << "Failed to predict." << std::endl;return;}std::cout << res.Str() << std::endl;auto vis_im = fastdeploy::vision::VisSegmentation(im, res, 0.5);cv::imwrite("vis_result.jpg", vis_im);std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}int main(int argc, char* argv[]) {if (argc < 4) {std::cout<< "Usage: infer_demo path/to/model_dir path/to/image run_option, ""e.g ./infer_model ./ppseg_model_dir ./test.jpeg 0"<< std::endl;std::cout << "The data type of run_option is int, 0: run with cpu; 1: run ""with gpu; 2: run with gpu and use tensorrt backend; 3: run ""with kunlunxin."<< std::endl;return -1;}if (std::atoi(argv[3]) == 0) {CpuInfer(argv[1], argv[2]);} else if (std::atoi(argv[3]) == 1) {GpuInfer(argv[1], argv[2]);} else if (std::atoi(argv[3]) == 2) {TrtInfer(argv[1], argv[2]);}return 0;
}

设置

在这里插入图片描述

  1. 附件包含目录

在弹出来的属性页中选择:C/C++ —> 常规 —> 附加包含目录,然后在添加 fastdeploy 、 opencv 以及cuda的头文件路径。如:

在这里插入图片描述

根据您存放的路径进行适当更改:

D:\fastdeploy-win-x64-gpu-0.0.0\include
D:\fastdeploy-win-x64-gpu-0.0.0\third_libs\install\opencv\build\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include
  1. 配置lib路径

在这里插入图片描述

添加附加库目录如下:

D:\fastdeploy-win-x64-gpu-0.0.0\third_libs\install\opencv\build\x64\vc15\lib
D:\fastdeploy-win-x64-gpu-0.0.0\lib
  1. 添加库文件
    分别将
fastdeploy.lib
opencv_world3416.lib

添加到库文件中。
在这里插入图片描述

  1. 生成解决方案
    在这里插入图片描述
    可执行文件生成目录为:工程目录\x64\Release

  2. 拷贝依赖的dll

对于版本高于0.2.1的SDK,提供了 fastdeploy_init.bat 工具来管理FastDeploy中所有的依赖库。可以通过该脚本工具查看(show)、拷贝(install) 和 设置(init and setup) SDK中所有的dll,方便用户快速完成运行时环境配置。

进入SDK的根目录,运行install命令,可以将SDK 中所有的 dll 安装到指定的目录(如exe所在的目录)。我们推荐这种方式来配置exe运行所需要的依赖库。比如,可以在SDK根目录下创建一个临时的bin目录备份所有的dll文件。以下命令中 %cd% 表示当前目录(SDK的根目录)。

# `install`为拷贝安装dll,`%cd%`表示当前目录,即SDK目录,`.\dir_to_copy`为拷贝的目录
D:\path-to-fastdeploy-sdk-dir>fastdeploy_init.bat install %cd% .\dir_to_copy

运行命令:
生成的项目地址为D:\Cplus\cjiajia\targetsdk\x64\Release
执行如下命令拷贝dll

fastdeploy_init.bat install %cd% D:\Cplus\cjiajia\targetsdk\x64\Release

在这里插入图片描述
6. 拷贝模型和测试图片

将下载好的模型和测试图片拷贝到生成的可执行文件目录。
需要注意的是,模型文件的子目录即为模型,如果直接下载可能还多了一层目录。

  1. 运行测试代码
# 运行部署示例
# CPU推理
./infer_demo PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer cityscapes_demo.png 0
# GPU推理
./infer_demo PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer cityscapes_demo.png 1
# GPU上Paddle-TensorRT推理
./infer_demo PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer cityscapes_demo.png 2

在这里插入图片描述
结果如vis_result.jpg所示。
在这里插入图片描述
8. 自定义代码
官方示例代码过于繁琐,可以使用如下自定义代码:

#include "fastdeploy/vision.h"#ifdef WIN32
const char sep = '\\';
#else
const char sep = '/';
#endifint infer(const std::string& model_dir, const std::string& image_file)
{
std::cout << "============开始测试==============" << std::endl;//模型初始化
auto model_file = model_dir + sep + "model.pdmodel";
auto params_file = model_dir + sep + "model.pdiparams";
auto config_file = model_dir + sep + "deploy.yaml";
auto option = fastdeploy::RuntimeOption();//设置后端
option.UseOpenVINOBackend();
option.SetCpuThreadNum(12);
//option.EnablePinnedMemory();   
//option.SetOpenVINOShapeInfo({ {"x",{1,3,800,800}} });// 初始化模型
auto model = fastdeploy::vision::segmentation::PaddleSegModel(model_file, params_file, config_file, option);if (!model.Initialized()) {std::cerr << "初始化失败." << std::endl;return 0;
}
else {std::cout << "初始化成功" << std::endl;
}fastdeploy::vision::SegmentationResult res;
//读取样例图片进行预热
auto im = cv::imread("./5.jpg");
for (int count = 1; count <= 20; count++)
{model.Predict(im, &res);
}//计时
auto start = std::chrono::system_clock::now();
//读取测试图片
im = cv::imread(image_file);// 预测
if (!model.Predict(im, &res)) {std::cerr << "Failed to predict." << std::endl;return 0;
}std::cout << res.Str() << std::endl;
auto vis_im = fastdeploy::vision::VisSegmentation(im, res, 0.5);
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "结果保存在 ./vis_result.jpg" << std::endl;//计时
auto end = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
double costTime = double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;printf("[%s][%d] 模型推理成功,耗费时间为: %lf s \n", __func__, __LINE__, costTime);
std::cout << "=============== 算法完成 ===============" << std::endl;}int main() {std::string model_dir = "./inference_model";  // 模型路径std::string image_file = "./5.jpg";  // 测速图片路径infer(model_dir, image_file);}

生成项目工程,需要注意的是,如果您选择重新生成解决方案,需要重新执行步骤5. 拷贝依赖的dll,因为每次重新生成工程后,plugins.xml都会被自动删除。

在终端中输入targetsdk运行代码

在这里插入图片描述

这篇关于FastDeploy在Windows平台上使用C++开发介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/217876

相关文章

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、