PaddleDetection CPU/GPU C++ 部署实战:基于 FastDeploy 的 PPYOLOE 目标检测与 PP-TinyPose 关键点检测推理示例
PaddleDetection CPU/GPU C 部署实战基于 FastDeploy 的 PPYOLOE 目标检测与 PP-TinyPose 关键点检测推理示例【免费下载链接】PaddleDetectionObject Detection toolkit based on PaddlePaddle. It supports object detection, instance segmentation, multiple object tracking and real-time multi-person keypoint detection.项目地址: https://gitcode.com/gh_mirrors/pa/PaddleDetection本指南以 PaddleDetection 仓库中的 deploy/fastdeploy/cpu-gpu/cpp/README.md 为主线完整讲解如何借助 FastDeploy 预编译库在 NVIDIA GPU、X86 CPU、飞腾 CPU、ARM CPU、Intel GPU 等硬件上以 C 方式部署 PaddleDetection 模型。读完本文你将掌握 PPYOLOE 目标检测、PP-TinyPose 关键点检测以及 PP-PicoDet PP-TinyPose 多人关键点 Pipeline 的编译、运行与源码级原理并了解如何切换 CPU、GPU、Paddle-TensorRT 三种推理后端。1. 概述FastDeploy 支持的硬件与模型系列PaddleDetection 支持利用 FastDeploy 在以下硬件上快速部署检测模型NVIDIA GPUX86 CPU飞腾 CPUARM CPUIntel GPU独立显卡 / 集成显卡FastDeploy 目前支持的模型系列包括但不限于PPYOLOE、PicoDet、PaddleYOLOX、PPYOLO、FasterRCNN、SSD、PaddleYOLOv5、PaddleYOLOv6、PaddleYOLOv7、RTMDet、CascadeRCNN、PSSDet、RetinaNet、PPYOLOESOD、FCOS、TTFNet、TOOD、GFL。这些模型的构造函数和预测函数在参数上完全一致因此只需要掌握 PPYOLOE 的调用方式即可快速迁移到其他模型。本目录deploy/fastdeploy/cpu-gpu/cpp提供两个可直接编译运行的示例源码infer.ccPPYOLOE 目标检测示例支持 CPU / GPU / GPUPaddle-TensorRT 三种运行模式pptinypose_infer.ccPP-TinyPose 关键点检测示例同样支持三种运行模式子目录 det_keypoint_unite 提供 PP-PicoDet PP-TinyPose 串联的多人关键点检测 Pipeline 示例。2. 部署环境准备在部署前需要确认软硬件环境并下载 FastDeploy 预编译部署库C SDK。安装方式为在 FastDeploy 官方发布渠道选择与操作系统、硬件匹配的fastdeploy-linux-x64-gpu-x.x.x.tgz之类的预编译包并解压随后通过 CMake 集成到工程中。版本要求FastDeploy 版本需在 1.0.4 及以上即x.x.x 1.0.4才能支持本文涉及的模型。从仓库的 CMakeLists.txt 可以看到 C 侧集成的关键点PROJECT(infer_demo C CXX) CMAKE_MINIMUM_REQUIRED (VERSION 3.10) option(FASTDEPLOY_INSTALL_DIR Path of downloaded fastdeploy sdk.) include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) include_directories(${FASTDEPLOY_INCS}) add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc) add_executable(infer_tinypose_demo ${PROJECT_SOURCE_DIR}/pptinypose_infer.cc) target_link_libraries(infer_demo ${FASTDEPLOY_LIBS}) target_link_libraries(infer_tinypose_demo ${FASTDEPLOY_LIBS})即通过-DFASTDEPLOY_INSTALL_DIR解压后的 SDK 路径传入预编译库位置SDK 自带的FastDeploy.cmake会导出FASTDEPLOY_INCS头文件路径与FASTDEPLOY_LIBS链接库两个示例程序分别编译为infer_demo与infer_tinypose_demo。3. 部署模型准备部署前需要准备推理模型有两种途径使用预导出的推理模型FastDeploy 部署概览页 deploy/fastdeploy/README.md 提供了 PaddleDetection 各系列预导出模型的下载列表如ppyoloe_crn_l_300e_coco、picodet_l_320_coco_lcnet、yolov3_darknet53_270e_coco、faster_rcnn_r50_vd_fpn_2x_coco、PP_TinyPose_256x192_infer等可直接下载使用。自行导出 PaddleDetection 部署模型参考 deploy/EXPORT_MODEL.md 使用tools/export_model.py导出。3.1 导出产物的三件套PaddleDetection 导出的模型目录包含三个文件缺一不可文件作用model.pdmodel推理模型结构文件Paddle 部署格式model.pdiparams模型权重参数文件infer_cfg.yml部署配置文件FastDeploy 会从中读取模型推理所需的预处理信息在 C 代码中三者通过model_file、params_file、config_file三个参数传入模型构造函数例如 infer.cc 中的auto model_file model_dir sep model.pdmodel; auto params_file model_dir sep model.pdiparams; auto config_file model_dir sep infer_cfg.yml; auto option fastdeploy::RuntimeOption(); option.UseCpu(); auto model fastdeploy::vision::detection::PPYOLOE(model_file, params_file, config_file, option);3.2 自行导出时的注意事项如果自行导出 PaddleDetection 推理模型需注意以下几点见 deploy/fastdeploy/README.md 的导出须知导出模型时不要进行 NMS 的去除操作正常导出即可如果用于运行原生 TensorRT 后端非 Paddle Inference 后端不要添加--trt参数导出模型时不要添加fuse_normalizeTrue参数。4. 目标检测示例PPYOLOE 在 CPU / GPU / TensorRT 上的推理4.1 编译部署示例以 Linux 上推理为例进入deploy/fastdeploy/cpu-gpu/cpp目录执行如下命令即可完成编译注意如果当前分支找不到 fastdeploy 测试代码请切换到 develop 分支# 下载 FastDeploy 预编译库选择与软硬件环境匹配的版本 wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-gpu-x.x.x.tgz tar xvf fastdeploy-linux-x64-gpu-x.x.x.tgz # 进入部署示例代码目录PaddleDetection 仓库的 deploy/fastdeploy/cpu-gpu/cpp # 编译部署示例 mkdir build cd build mv ../fastdeploy-linux-x64-gpu-x.x.x . cmake .. -DFASTDEPLOY_INSTALL_DIR${PWD}/fastdeploy-linux-x64-gpu-x.x.x make -j # 下载 PPYOLOE 模型文件也可参考 deploy/fastdeploy/README.md 选择其他预导出模型 wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz tar xvf ppyoloe_crn_l_300e_coco.tgz测试图片可直接使用仓库 demo/000000014439.jpg即原示例命令中下载的 COCO 测试图片。4.2 运行三种推理模式infer_demo的用法为./infer_demo 模型目录 图片路径 运行模式其中运行模式为整数0表示 CPU 推理1表示 GPU 推理2表示 GPU 上的 Paddle-TensorRT 推理# CPU 推理 ./infer_demo ./ppyoloe_crn_l_300e_coco 000000014439.jpg 0 # GPU 推理 ./infer_demo ./ppyoloe_crn_l_300e_coco 000000014439.jpg 1 # GPU 上 Paddle-TensorRT 推理 # 注意TensorRT 推理第一次运行有序列化模型的操作耗时较长需要耐心等待 ./infer_demo ./ppyoloe_crn_l_300e_coco 000000014439.jpg 2运行完成后程序会在终端打印检测结果DetectionResult并将可视化结果保存为./vis_result.jpg。4.3 源码解析三种后端的切换方式infer.cc 的核心是三个结构完全一致的推理函数差异仅在于RuntimeOption的配置CPU 推理CpuInferauto option fastdeploy::RuntimeOption(); option.UseCpu();GPU 推理GpuInferauto option fastdeploy::RuntimeOption(); option.UseGpu();GPU Paddle-TensorRT 推理TrtInferauto option fastdeploy::RuntimeOption(); option.UseGpu(); option.UsePaddleInferBackend(); // 如果希望使用原生 TensorRT 后端而非 Paddle-TensorRT可改用 option.UseTrtBackend() option.paddle_infer_option.enable_trt true; option.paddle_infer_option.collect_trt_shape true; option.trt_option.SetShape(image, {1, 3, 640, 640}, {1, 3, 640, 640}, {1, 3, 640, 640}); option.trt_option.SetShape(scale_factor, {1, 2}, {1, 2}, {1, 2});其中SetShape用于指定 TensorRT 动态 shape 的 min/shape/max 三个档位PPYOLOE 需要为image输入设置{1, 3, 640, 640}batch1通道3高640宽640为scale_factor输入设置{1, 2}。若实际输入尺寸不同需要相应调整。推理与可视化部分的通用流程三种模式一致auto im cv::imread(image_file); fastdeploy::vision::DetectionResult 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::VisDetection(im, res, 0.5); cv::imwrite(vis_result.jpg, vis_im);model.Initialized()用于校验模型是否初始化成功model.Predict(im, res)执行单张图片推理res.Str()输出检测框、类别与置信度fastdeploy::vision::VisDetection(im, res, 0.5)以 0.5 的置信度阈值绘制可视化结果。主函数根据第三个命令行参数分发到对应模式infer.cc。5. 关键点检测示例PP-TinyPose 推理5.1 编译与运行PP-TinyPose 示例与目标检测共用同一套 CMake 工程infer_tinypose_demo目标。编译方式同上运行命令如下# 下载 PP-TinyPose 模型文件 wget https://bj.bcebos.com/paddlehub/fastdeploy/PP_TinyPose_256x192_infer.tgz tar -xvf PP_TinyPose_256x192_infer.tgz # CPU 推理 ./infer_tinypose_demo PP_TinyPose_256x192_infer hrnet_demo.jpg 0 # GPU 推理 ./infer_tinypose_demo PP_TinyPose_256x192_infer hrnet_demo.jpg 1 # GPU 上 Paddle-TensorRT 推理第一次运行有序列化模型操作耗时较长 ./infer_tinypose_demo PP_TinyPose_256x192_infer hrnet_demo.jpg 2测试图片可使用仓库 demo/hrnet_demo.jpg即原示例命令中的hrnet_demo.jpg。运行完成后终端打印KeyPointDetectionResult可视化结果保存为./tinypose_vis_result.jpg。5.2 源码解析pptinypose_infer.cc 的骨架与目标检测示例完全一致差异在于模型类为fastdeploy::vision::keypointdetection::PPTinyPose结果类型为fastdeploy::vision::KeyPointDetectionResultTensorRT 模式下输入 shape 设置为{1, 3, 256, 192}对应 PP-TinyPose-256x192 模型tinypose_option.trt_option.SetShape(image, {1, 3, 256, 192}, {1, 3, 256, 192}, {1, 3, 256, 192});可视化函数为fastdeploy::vision::VisKeypointDetection(im, res, 0.5)。PP_TinyPose_256x192_infer对应 PaddleDetection 的 tiny_pose 系列配置详见 configs/keypoint/tiny_pose 目录。PP-TinyPose 单模型是单人关键点检测模型若需对一张图中多个行人进行关键点检测请使用下一节的 Pipeline 串联方案。6. 多人关键点检测 PipelinePP-PicoDet PP-TinyPose多人关键点检测需要先检测、再回归关键点的串联流程。子目录 det_keypoint_unite 提供了det_keypoint_unite_infer.cc用 PP-PicoDet 行人检测模型 PP-TinyPose 关键点模型完成单图多人关键点检测。6.1 编译与运行mkdir build cd build # 下载 FastDeploy 预编译库 wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz tar xvf fastdeploy-linux-x64-x.x.x.tgz cmake .. -DFASTDEPLOY_INSTALL_DIR${PWD}/fastdeploy-linux-x64-x.x.x make -j # 下载 PP-TinyPose 与 PP-PicoDet 行人检测模型 wget https://bj.bcebos.com/paddlehub/fastdeploy/PP_TinyPose_256x192_infer.tgz tar -xvf PP_TinyPose_256x192_infer.tgz wget https://bj.bcebos.com/paddlehub/fastdeploy/PP_PicoDet_V2_S_Pedestrian_320x320_infer.tgz tar -xvf PP_PicoDet_V2_S_Pedestrian_320x320_infer.tgz # CPU 推理 ./infer_demo PP_PicoDet_V2_S_Pedestrian_320x320_infer PP_TinyPose_256x192_infer 000000018491.jpg 0 # GPU 推理 ./infer_demo PP_PicoDet_V2_S_Pedestrian_320x320_infer PP_TinyPose_256x192_infer 000000018491.jpg 1 # GPU 上 Paddle-TensorRT 推理 ./infer_demo PP_PicoDet_V2_S_Pedestrian_320x320_infer PP_TinyPose_256x192_infer 000000018491.jpg 2注意此处infer_demo需要4 个命令行参数检测模型目录、关键点模型目录、图片路径、运行模式运行模式仍为0/1/2。6.2 Pipeline C 接口Pipeline 的串联接口定义如下det_keypoint_unite_infer.ccfastdeploy::pipeline::PPTinyPose( fastdeploy::vision::detection::PicoDet* det_model, fastdeploy::vision::keypointdetection::PPTinyPose* pptinypose_model)det_model是初始化后的检测模型pptinypose_model是初始化后的关键点检测模型。串联后的使用方式auto pipeline fastdeploy::pipeline::PPTinyPose(det_model, tinypose_model); pipeline.detection_model_score_threshold 0.5; if (!pipeline.Predict(im, res)) { ... }pipeline.detection_model_score_threshold控制检测阶段的置信度阈值示例中设为 0.5TensorRT 模式下PicoDet 的输入 shape 设置为{1, 3, 320, 320}并附带scale_factor {1, 2}PP-TinyPose 的输入 shape 设置为{1, 3, 256, 192}与两个模型的导出分辨率一一对应。7. PaddleDetection C 统一接口一览FastDeploy 为 PaddleDetection 系列模型提供了参数完全一致的构造函数。目标检测及实例分割模型统一签名如下model_file、params_file为导出的 Paddle 部署模型格式config_file为同时导出的部署配置 yamlfastdeploy::vision::detection::PicoDet(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::SOLOv2(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::PPYOLOE(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::PPYOLO(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::YOLOv3(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::PaddleYOLOX(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::FasterRCNN(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::MaskRCNN(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::SSD(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::PaddleYOLOv5(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::PaddleYOLOv6(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::PaddleYOLOv7(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::PaddleYOLOv8(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::CascadeRCNN(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::PSSDet(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::RetinaNet(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::PPYOLOESOD(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::FCOS(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::TOOD(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE); fastdeploy::vision::detection::GFL(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE);关键点检测模型签名fastdeploy::vision::keypointdetection::PPTinyPose(const string model_file, const string params_file, const string config_file, const RuntimeOption runtime_option RuntimeOption(), const ModelFormat model_format ModelFormat::PADDLE);两个可选参数值得注意runtime_option推理后端配置默认 CPU 后端通过RuntimeOption的UseCpu()、UseGpu()、UsePaddleInferBackend()、UseTrtBackend()等方法切换见第 4.3 节model_format模型格式默认ModelFormat::PADDLE当加载 ONNX 等其他格式的模型时传入对应枚举值。由于所有模型的构造与预测接口完全一致只需参考 PPYOLOE 的示例即可快速调用其余模型这也是 FastDeploy 在 C 部署侧保持统一体验的核心设计。8. 更多指南与常见问题8.1 更多指南FastDeploy 部署 PaddleDetection 模型概览含各系列预导出模型下载列表、模型导出步骤与导出须知Python 部署示例同一部署能力的 Python 版本infer.py、pptinypose_infer.py与det_keypoint_unite/det_keypoint_unite_infer.py分别对应本文三个 C 示例模型导出操作详见 deploy/EXPORT_MODEL.md。8.2 常见问题如何切换模型推理后端引擎FastDeploy 支持 Paddle Inference、ONNX Runtime、TensorRT、OpenVINO 等多个后端通过RuntimeOption即可切换其中 TensorRT 又分为 Paddle-TensorRTUsePaddleInferBackend()enable_trt true与原生 TensorRTUseTrtBackend()两种示例代码中默认使用前者Intel GPU独立显卡 / 集成显卡的使用需要下载或编译支持 Intel GPU 的 FastDeploy 部署库并在RuntimeOption中启用相应后端编译 CPU / GPU / Jetson 部署库当预编译库不满足需求时可参照 FastDeploy 官方文档分别编译 CPU 版、GPU 版与 Jetson 版部署库再按第 2 节方式集成Windows 平台上述命令适用于 Linux 与 macOSWindows 下使用 FastDeploy C SDK 的方式不同需按 Windows SDK 的使用说明配置环境TensorRT 首次推理较慢TensorRT 推理第一次运行时有序列化模型的操作耗时较长属正常现象后续运行会复用序列化产物。需要说明的是本文所有命令与示例均以当前仓库PaddleDetection的deploy/fastdeploy/cpu-gpu/cpp目录中的源码与文档为准具体模型的精度、参数大小等指标请以 deploy/fastdeploy/README.md 中的预导出模型列表及 PaddleDetection 各模型说明为准。【免费下载链接】PaddleDetectionObject Detection toolkit based on PaddlePaddle. It supports object detection, instance segmentation, multiple object tracking and real-time multi-person keypoint detection.项目地址: https://gitcode.com/gh_mirrors/pa/PaddleDetection创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考