mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-25 09:31:38 +08:00
[Model] Add tinypose single && pipeline model (#177)
* Add tinypose model * Add PPTinypose python API * Fix picodet preprocess bug && Add Tinypose examples * Update tinypose example code * Update ppseg preprocess if condition * Update ppseg backend support type * Update permute.h * Update README.md * Update code with comments * Move files dir * Delete premute.cc * Add single model pptinypose * Delete pptinypose old code in ppdet * Code format * Add ppdet + pptinypose pipeline model * Fix bug for posedetpipeline * Change Frontend to ModelFormat * Change Frontend to ModelFormat in __init__.py * Add python posedetpipeline/ * Update pptinypose example dir name * Update README.md * Update README.md * Update README.md * Update README.md * Create keypointdetection_result.md * Create README.md * Create README.md * Create README.md * Update README.md * Update README.md * Create README.md * Fix det_keypoint_unite_infer.py bug * Create README.md * Update PP-Tinypose by comment * Update by comment * Add pipeline directory * Add pptinypose dir * Update pptinypose to align accuracy * Addd warpAffine processor * Update GetCpuMat to GetOpenCVMat * Add comment for pptinypose && pipline * Update docs/main_page.md * Add README.md for pptinypose * Add README for det_keypoint_unite * Remove ENABLE_PIPELINE option * Remove ENABLE_PIPELINE option * Change pptinypose default backend * PP-TinyPose Pipeline support multi PP-Detection models * Update pp-tinypose comment * Update by comments * Add single test example Co-authored-by: Jason <jiangjiajun@baidu.com>
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
PROJECT(infer_demo C CXX)
|
||||
CMAKE_MINIMUM_REQUIRED (VERSION 3.12)
|
||||
|
||||
# 指定下载解压后的fastdeploy库路径
|
||||
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
|
||||
|
||||
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
|
||||
|
||||
# 添加FastDeploy依赖头文件
|
||||
include_directories(${FASTDEPLOY_INCS})
|
||||
|
||||
add_executable(infer_demo ${PROJECT_SOURCE_DIR}/det_keypoint_unite_infer.cc)
|
||||
# 添加FastDeploy库依赖
|
||||
target_link_libraries(infer_demo ${FASTDEPLOY_LIBS})
|
||||
@@ -0,0 +1,84 @@
|
||||
# PP-PicoDet + PP-TinyPose (Pipeline) C++部署示例
|
||||
|
||||
本目录下提供`det_keypoint_unite_infer.cc`快速完成多人模型配置 PP-PicoDet + PP-TinyPose 在CPU/GPU,以及GPU上通过TensorRT加速部署的`单图多人关键点检测`示例。执行如下脚本即可完成
|
||||
>> **注意**: PP-TinyPose单模型独立部署,请参考[PP-TinyPose 单模型](../../tiny_pose/cpp/README.md)
|
||||
|
||||
在部署前,需确认以下两个步骤
|
||||
|
||||
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
|
||||
- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
|
||||
|
||||
|
||||
以Linux上推理为例,在本目录执行如下命令即可完成编译测试
|
||||
|
||||
```bash
|
||||
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-gpu-0.3.0.tgz
|
||||
tar xvf fastdeploy-linux-x64-gpu-0.3.0.tgz
|
||||
cd fastdeploy-linux-x64-gpu-0.3.0/examples/vision/keypointdetection/tiny_pose/cpp/
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/../../../../../../../fastdeploy-linux-x64-gpu-0.3.0
|
||||
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
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/000000018491.jpg
|
||||
|
||||
# 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上TensorRT推理
|
||||
./infer_demo PP_PicoDet_V2_S_Pedestrian_320x320_infer PP_TinyPose_256x192_infer 000000018491.jpg 2
|
||||
```
|
||||
|
||||
运行完成可视化结果如下图所示
|
||||
<div align="center">
|
||||
<img src="https://user-images.githubusercontent.com/16222477/196393343-eeb6b68f-0bc6-4927-871f-5ac610da7293.jpeg", width=359px, height=423px />
|
||||
</div>
|
||||
|
||||
以上命令只适用于Linux或MacOS, Windows下SDK的使用方式请参考:
|
||||
- [如何在Windows中使用FastDeploy C++ SDK](../../../../../docs/cn/faq/use_sdk_on_windows.md)
|
||||
|
||||
## PP-TinyPose C++接口
|
||||
|
||||
### PP-TinyPose类
|
||||
|
||||
```c++
|
||||
fastdeploy::pipeline::PPTinyPose(
|
||||
fastdeploy::vision::detection::PPYOLOE* det_model,
|
||||
fastdeploy::vision::keypointdetection::PPTinyPose* pptinypose_model)
|
||||
```
|
||||
|
||||
PPTinyPose Pipeline模型加载和初始化。
|
||||
|
||||
**参数**
|
||||
|
||||
> * **model_det_modelfile**(fastdeploy::vision::detection): 初始化后的检测模型,参考[PP-TinyPose](../../tiny_pose/README.md)
|
||||
> * **pptinypose_model**(fastdeploy::vision::keypointdetection): 初始化后的检测模型[Detection](../../../detection/paddledetection/README.md),暂时只提供PaddleDetection系列
|
||||
|
||||
#### Predict函数
|
||||
|
||||
> ```c++
|
||||
> PPTinyPose::Predict(cv::Mat* im, KeyPointDetectionResult* result)
|
||||
> ```
|
||||
>
|
||||
> 模型预测接口,输入图像直接输出关键点检测结果。
|
||||
>
|
||||
> **参数**
|
||||
>
|
||||
> > * **im**: 输入图像,注意需为HWC,BGR格式
|
||||
> > * **result**: 关键点检测结果,包括关键点的坐标以及关键点对应的概率值, KeyPointDetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/)
|
||||
|
||||
### 类成员属性
|
||||
#### 后处理参数
|
||||
> > * **detection_model_score_threshold**(bool):
|
||||
输入PP-TinyPose模型前,Detectin模型过滤检测框的分数阈值
|
||||
|
||||
- [模型介绍](../../)
|
||||
- [Python部署](../python)
|
||||
- [视觉模型预测结果](../../../../../docs/api/vision_results/)
|
||||
- [如何切换模型推理后端引擎](../../../../../docs/cn/faq/how_to_change_backend.md)
|
||||
@@ -0,0 +1,196 @@
|
||||
// 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"
|
||||
#include "fastdeploy/pipeline.h"
|
||||
|
||||
#ifdef WIN32
|
||||
const char sep = '\\';
|
||||
#else
|
||||
const char sep = '/';
|
||||
#endif
|
||||
|
||||
void CpuInfer(const std::string& det_model_dir,
|
||||
const std::string& tinypose_model_dir,
|
||||
const std::string& image_file) {
|
||||
auto det_model_file = det_model_dir + sep + "model.pdmodel";
|
||||
auto det_params_file = det_model_dir + sep + "model.pdiparams";
|
||||
auto det_config_file = det_model_dir + sep + "infer_cfg.yml";
|
||||
auto det_model = fastdeploy::vision::detection::PicoDet(
|
||||
det_model_file, det_params_file, det_config_file);
|
||||
if (!det_model.Initialized()) {
|
||||
std::cerr << "Detection Model Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto tinypose_model_file = tinypose_model_dir + sep + "model.pdmodel";
|
||||
auto tinypose_params_file = tinypose_model_dir + sep + "model.pdiparams";
|
||||
auto tinypose_config_file = tinypose_model_dir + sep + "infer_cfg.yml";
|
||||
auto tinypose_model = fastdeploy::vision::keypointdetection::PPTinyPose(
|
||||
tinypose_model_file, tinypose_params_file, tinypose_config_file);
|
||||
if (!tinypose_model.Initialized()) {
|
||||
std::cerr << "TinyPose Model Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
fastdeploy::vision::KeyPointDetectionResult res;
|
||||
|
||||
auto pipeline =fastdeploy::pipeline::PPTinyPose(&det_model, &tinypose_model);
|
||||
pipeline.detection_model_score_threshold = 0.5;
|
||||
if (!pipeline.Predict(&im, &res)) {
|
||||
std::cerr << "TinyPose Prediction Failed." << std::endl;
|
||||
return;
|
||||
} else {
|
||||
std::cout << "TinyPose Prediction Done!" << std::endl;
|
||||
}
|
||||
// 输出预测框结果
|
||||
std::cout << res.Str() << std::endl;
|
||||
|
||||
// 可视化预测结果
|
||||
auto vis_im =
|
||||
fastdeploy::vision::VisKeypointDetection(im, res, 0.2);
|
||||
cv::imwrite("vis_result.jpg", vis_im);
|
||||
std::cout << "TinyPose visualized result saved in ./vis_result.jpg"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void GpuInfer(const std::string& det_model_dir,
|
||||
const std::string& tinypose_model_dir,
|
||||
const std::string& image_file) {
|
||||
auto option = fastdeploy::RuntimeOption();
|
||||
option.UseGpu();
|
||||
auto det_model_file = det_model_dir + sep + "model.pdmodel";
|
||||
auto det_params_file = det_model_dir + sep + "model.pdiparams";
|
||||
auto det_config_file = det_model_dir + sep + "infer_cfg.yml";
|
||||
auto det_model = fastdeploy::vision::detection::PicoDet(
|
||||
det_model_file, det_params_file, det_config_file, option);
|
||||
if (!det_model.Initialized()) {
|
||||
std::cerr << "Detection Model Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto tinypose_model_file = tinypose_model_dir + sep + "model.pdmodel";
|
||||
auto tinypose_params_file = tinypose_model_dir + sep + "model.pdiparams";
|
||||
auto tinypose_config_file = tinypose_model_dir + sep + "infer_cfg.yml";
|
||||
auto tinypose_model = fastdeploy::vision::keypointdetection::PPTinyPose(
|
||||
tinypose_model_file, tinypose_params_file, tinypose_config_file, option);
|
||||
if (!tinypose_model.Initialized()) {
|
||||
std::cerr << "TinyPose Model Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
fastdeploy::vision::KeyPointDetectionResult res;
|
||||
|
||||
auto pipeline =
|
||||
fastdeploy::pipeline::PPTinyPose(
|
||||
&det_model, &tinypose_model);
|
||||
pipeline.detection_model_score_threshold = 0.5;
|
||||
if (!pipeline.Predict(&im, &res)) {
|
||||
std::cerr << "TinyPose Prediction Failed." << std::endl;
|
||||
return;
|
||||
} else {
|
||||
std::cout << "TinyPose Prediction Done!" << std::endl;
|
||||
}
|
||||
// 输出预测框结果
|
||||
std::cout << res.Str() << std::endl;
|
||||
|
||||
// 可视化预测结果
|
||||
auto vis_im =
|
||||
fastdeploy::vision::VisKeypointDetection(im, res, 0.2);
|
||||
cv::imwrite("vis_result.jpg", vis_im);
|
||||
std::cout << "TinyPose visualized result saved in ./vis_result.jpg"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void TrtInfer(const std::string& det_model_dir,
|
||||
const std::string& tinypose_model_dir,
|
||||
const std::string& image_file) {
|
||||
auto det_model_file = det_model_dir + sep + "model.pdmodel";
|
||||
auto det_params_file = det_model_dir + sep + "model.pdiparams";
|
||||
auto det_config_file = det_model_dir + sep + "infer_cfg.yml";
|
||||
|
||||
auto det_option = fastdeploy::RuntimeOption();
|
||||
det_option.UseGpu();
|
||||
det_option.UseTrtBackend();
|
||||
det_option.SetTrtInputShape("image", {1, 3, 320, 320});
|
||||
det_option.SetTrtInputShape("scale_factor", {1, 2});
|
||||
auto det_model = fastdeploy::vision::detection::PicoDet(
|
||||
det_model_file, det_params_file, det_config_file, det_option);
|
||||
if (!det_model.Initialized()) {
|
||||
std::cerr << "Detection Model Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto tinypose_model_file = tinypose_model_dir + sep + "model.pdmodel";
|
||||
auto tinypose_params_file = tinypose_model_dir + sep + "model.pdiparams";
|
||||
auto tinypose_config_file = tinypose_model_dir + sep + "infer_cfg.yml";
|
||||
auto tinypose_option = fastdeploy::RuntimeOption();
|
||||
tinypose_option.UseGpu();
|
||||
tinypose_option.UseTrtBackend();
|
||||
auto tinypose_model = fastdeploy::vision::keypointdetection::PPTinyPose(
|
||||
tinypose_model_file, tinypose_params_file, tinypose_config_file,
|
||||
tinypose_option);
|
||||
if (!tinypose_model.Initialized()) {
|
||||
std::cerr << "TinyPose Model Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
fastdeploy::vision::KeyPointDetectionResult res;
|
||||
|
||||
auto pipeline =
|
||||
fastdeploy::pipeline::PPTinyPose(
|
||||
&det_model, &tinypose_model);
|
||||
pipeline.detection_model_score_threshold = 0.5;
|
||||
if (!pipeline.Predict(&im, &res)) {
|
||||
std::cerr << "TinyPose Prediction Failed." << std::endl;
|
||||
return;
|
||||
} else {
|
||||
std::cout << "TinyPose Prediction Done!" << std::endl;
|
||||
}
|
||||
// 输出预测关键点结果
|
||||
std::cout << res.Str() << std::endl;
|
||||
|
||||
// 可视化预测结果
|
||||
auto vis_im =
|
||||
fastdeploy::vision::VisKeypointDetection(im, res, 0.2);
|
||||
cv::imwrite("vis_result.jpg", vis_im);
|
||||
std::cout << "TinyPose visualized result saved in ./vis_result.jpg"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 5) {
|
||||
std::cout << "Usage: infer_demo path/to/detection_model_dir "
|
||||
"path/to/pptinypose_model_dir path/to/image run_option, "
|
||||
"e.g ./infer_model ./picodet_model_dir ./pptinypose_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."
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (std::atoi(argv[4]) == 0) {
|
||||
CpuInfer(argv[1], argv[2], argv[3]);
|
||||
} else if (std::atoi(argv[4]) == 1) {
|
||||
GpuInfer(argv[1], argv[2], argv[3]);
|
||||
} else if (std::atoi(argv[4]) == 2) {
|
||||
TrtInfer(argv[1], argv[2], argv[3]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user