Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions csrc/fastdeploy/backends/paddle/paddle_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ void PaddleBackend::BuildOption(const PaddleBackendOption& option) {
config_.SetMkldnnCacheCapacity(option.mkldnn_cache_size);
}
}
if (!option.enable_log_info) {
config_.DisableGlogInfo();
}
config_.SetCpuMathLibraryNumThreads(option.cpu_thread_num);
}

Expand Down
2 changes: 2 additions & 0 deletions csrc/fastdeploy/backends/paddle/paddle_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct PaddleBackendOption {
#endif
bool enable_mkldnn = true;

bool enable_log_info = false;

int mkldnn_cache_size = 1;
int cpu_thread_num = 8;
// initialize memory size(MB) for GPU
Expand Down
51 changes: 45 additions & 6 deletions csrc/fastdeploy/fastdeploy_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/utils/unique_ptr.h"
#include "fastdeploy/utils/utils.h"

namespace fastdeploy {
Expand Down Expand Up @@ -54,12 +53,52 @@ bool FastDeployModel::InitRuntime() {
<< std::endl;
return false;
}
runtime_ = utils::make_unique<Runtime>();
if (!runtime_->Init(runtime_option)) {
return false;

bool use_gpu = (runtime_option.device == Device::GPU);
#ifndef WITH_GPU
use_gpu = false;
#endif

// whether the model is supported by the setted backend
bool is_supported = false;
if (use_gpu) {
for (auto& item : valid_gpu_backends) {
if (item == runtime_option.backend) {
is_supported = true;
break;
}
}
} else {
for (auto& item : valid_cpu_backends) {
if (item == runtime_option.backend) {
is_supported = true;
break;
}
}
}

if (is_supported) {
runtime_ = std::unique_ptr<Runtime>(new Runtime());
if (!runtime_->Init(runtime_option)) {
return false;
}
runtime_initialized_ = true;
return true;
} else {
FDWARNING << ModelName() << " is not supported with backend "
<< Str(runtime_option.backend) << "." << std::endl;
if (use_gpu) {
FDASSERT(valid_gpu_backends.size() > 0,
"There's no valid gpu backend for " + ModelName() + ".");
FDWARNING << "FastDeploy will choose " << Str(valid_gpu_backends[0])
<< " for model inference." << std::endl;
} else {
FDASSERT(valid_gpu_backends.size() > 0,
"There's no valid cpu backend for " + ModelName() + ".");
FDWARNING << "FastDeploy will choose " << Str(valid_cpu_backends[0])
<< " for model inference." << std::endl;
}
}
runtime_initialized_ = true;
return true;
}

if (runtime_option.device == Device::CPU) {
Expand Down
5 changes: 5 additions & 0 deletions csrc/fastdeploy/fastdeploy_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ void RuntimeOption::EnablePaddleMKLDNN() { pd_enable_mkldnn = true; }

void RuntimeOption::DisablePaddleMKLDNN() { pd_enable_mkldnn = false; }

void RuntimeOption::EnablePaddleLogInfo() { pd_enable_log_info = true; }

void RuntimeOption::DisablePaddleLogInfo() { pd_enable_log_info = false; }

void RuntimeOption::SetPaddleMKLDNNCacheSize(int size) {
FDASSERT(size > 0, "Parameter size must greater than 0.");
pd_mkldnn_cache_size = size;
Expand Down Expand Up @@ -272,6 +276,7 @@ void Runtime::CreatePaddleBackend() {
#ifdef ENABLE_PADDLE_BACKEND
auto pd_option = PaddleBackendOption();
pd_option.enable_mkldnn = option.pd_enable_mkldnn;
pd_option.enable_log_info = option.pd_enable_log_info;
pd_option.mkldnn_cache_size = option.pd_mkldnn_cache_size;
pd_option.use_gpu = (option.device == Device::GPU) ? true : false;
pd_option.gpu_id = option.device_id;
Expand Down
6 changes: 6 additions & 0 deletions csrc/fastdeploy/fastdeploy_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ struct FASTDEPLOY_DECL RuntimeOption {
// disable mkldnn while use paddle inference in CPU
void DisablePaddleMKLDNN();

// enable debug information of paddle backend
void EnablePaddleLogInfo();
// disable debug information of paddle backend
void DisablePaddleLogInfo();

// set size of cached shape while enable mkldnn with paddle inference backend
void SetPaddleMKLDNNCacheSize(int size);

Expand Down Expand Up @@ -108,6 +113,7 @@ struct FASTDEPLOY_DECL RuntimeOption {

// ======Only for Paddle Backend=====
bool pd_enable_mkldnn = true;
bool pd_enable_log_info = false;
int pd_mkldnn_cache_size = 1;

// ======Only for Trt Backend=======
Expand Down
2 changes: 2 additions & 0 deletions csrc/fastdeploy/pybind/fastdeploy_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void BindRuntime(pybind11::module& m) {
.def("use_trt_backend", &RuntimeOption::UseTrtBackend)
.def("enable_paddle_mkldnn", &RuntimeOption::EnablePaddleMKLDNN)
.def("disable_paddle_mkldnn", &RuntimeOption::DisablePaddleMKLDNN)
.def("enable_paddle_log_info", &RuntimeOption::EnablePaddleLogInfo)
.def("disable_paddle_log_info", &RuntimeOption::DisablePaddleLogInfo)
.def("set_paddle_mkldnn_cache_size",
&RuntimeOption::SetPaddleMKLDNNCacheSize)
.def("set_trt_input_shape", &RuntimeOption::SetTrtInputShape)
Expand Down
4 changes: 2 additions & 2 deletions csrc/fastdeploy/vision/detection/ppdet/picodet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ PicoDet::PicoDet(const std::string& model_file, const std::string& params_file,
const RuntimeOption& custom_option,
const Frontend& model_format) {
config_file_ = config_file;
valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT};
valid_cpu_backends = {Backend::ORT, Backend::PDINFER};
valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT};
runtime_option = custom_option;
runtime_option.model_format = model_format;
runtime_option.model_file = model_file;
Expand Down
4 changes: 2 additions & 2 deletions csrc/fastdeploy/vision/detection/ppdet/ppyoloe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ PPYOLOE::PPYOLOE(const std::string& model_file, const std::string& params_file,
const RuntimeOption& custom_option,
const Frontend& model_format) {
config_file_ = config_file;
valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT};
valid_cpu_backends = {Backend::ORT, Backend::PDINFER};
valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT};
runtime_option = custom_option;
runtime_option.model_format = model_format;
runtime_option.model_file = model_file;
Expand Down
4 changes: 2 additions & 2 deletions csrc/fastdeploy/vision/detection/ppdet/yolov3.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ YOLOv3::YOLOv3(const std::string& model_file, const std::string& params_file,
const RuntimeOption& custom_option,
const Frontend& model_format) {
config_file_ = config_file;
valid_cpu_backends = {Backend::PDINFER};
valid_gpu_backends = {Backend::PDINFER};
valid_cpu_backends = {Backend::ORT, Backend::PDINFER};
valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT};
runtime_option = custom_option;
runtime_option.model_format = model_format;
runtime_option.model_file = model_file;
Expand Down
12 changes: 7 additions & 5 deletions csrc/fastdeploy/vision/detection/ppdet/yolox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ namespace fastdeploy {
namespace vision {
namespace detection {

PaddleYOLOX::PaddleYOLOX(const std::string& model_file, const std::string& params_file,
const std::string& config_file, const RuntimeOption& custom_option,
const Frontend& model_format) {
PaddleYOLOX::PaddleYOLOX(const std::string& model_file,
const std::string& params_file,
const std::string& config_file,
const RuntimeOption& custom_option,
const Frontend& model_format) {
config_file_ = config_file;
valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT};
valid_cpu_backends = {Backend::ORT, Backend::PDINFER};
valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT};
runtime_option = custom_option;
runtime_option.model_format = model_format;
runtime_option.model_file = model_file;
Expand Down
6 changes: 5 additions & 1 deletion csrc/fastdeploy/vision/visualize/detection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ namespace vision {
// If need to visualize num_classes > 1000
// Please call Visualize::GetColorMap(num_classes) first
cv::Mat Visualize::VisDetection(const cv::Mat& im,
const DetectionResult& result, int line_size,
const DetectionResult& result,
float score_threshold, int line_size,
float font_size) {
auto color_map = GetColorMap();
int h = im.rows;
int w = im.cols;
auto vis_im = im.clone();
for (size_t i = 0; i < result.boxes.size(); ++i) {
if (result.scores[i] < score_threshold) {
continue;
}
cv::Rect rect(result.boxes[i][0], result.boxes[i][1],
result.boxes[i][2] - result.boxes[i][0],
result.boxes[i][3] - result.boxes[i][1]);
Expand Down
3 changes: 2 additions & 1 deletion csrc/fastdeploy/vision/visualize/visualize.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class FASTDEPLOY_DECL Visualize {
static std::vector<int> color_map_;
static const std::vector<int>& GetColorMap(int num_classes = 1000);
static cv::Mat VisDetection(const cv::Mat& im, const DetectionResult& result,
int line_size = 1, float font_size = 0.5f);
float score_threshold = 0.0, int line_size = 1,
float font_size = 0.5f);
static cv::Mat VisFaceDetection(const cv::Mat& im,
const FaceDetectionResult& result,
int line_size = 1, float font_size = 0.5f);
Expand Down
4 changes: 2 additions & 2 deletions csrc/fastdeploy/vision/visualize/visualize_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ void BindVisualize(pybind11::module& m) {
.def(pybind11::init<>())
.def_static("vis_detection",
[](pybind11::array& im_data, vision::DetectionResult& result,
int line_size, float font_size) {
float score_threshold, int line_size, float font_size) {
auto im = PyArrayToCvMat(im_data);
auto vis_im = vision::Visualize::VisDetection(
im, result, line_size, font_size);
im, result, score_threshold, line_size, font_size);
FDTensor out;
vision::Mat(vis_im).ShareWithTensor(&out);
return TensorToPyArray(out);
Expand Down
45 changes: 45 additions & 0 deletions examples/vision/detection/paddledetection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# PaddleDetection模型部署

## 模型版本说明

- [PaddleDetection Release/2.4](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4)

## 支持模型列表

目前FastDeploy支持如下模型的部署

- [PPYOLOE系列模型](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/ppyoloe)
- [PicoDet系列模型](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/picodet)
- [PPYOLO系列模型(含v2)](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/ppyolo)
- [YOLOv3系列模型](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/yolov3)
- [YOLOX系列模型](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/yolox)
- [FasterRCNN系列模型](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/faster_rcnn)

## 导出部署模型

在部署前,需要先将PaddleDetection导出成部署模型,导出步骤参考文档[导出模型](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.4/deploy/EXPORT_MODEL.md)

注意:在导出模型时不要进行NMS的去除操作,正常导出即可。

## 下载预训练模型

为了方便开发者的测试,下面提供了PaddleDetection导出的各系列模型,开发者可直接下载使用。

其中精度指标来源于PaddleDetection中对各模型的介绍,详情各参考PaddleDetection中的说明。


| 模型 | 参数大小 | 精度 | 备注 |
|:---------------------------------------------------------------- |:----- |:----- | :------ |
| [picodet_l_320_coco_lcnet](https://bj.bcebos.com/paddlehub/fastdeploy/picodet_l_320_coco_lcnet.tgz) |23MB | 42.6% |
| [ppyoloe_crn_l_300e_coco](https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz) |200MB | 51.4% |
| [ppyolo_r50vd_dcn_1x_coco](https://bj.bcebos.com/paddlehub/fastdeploy/ppyolo_r50vd_dcn_1x_coco.tgz) | 180MB | 44.8% | 暂不支持TensorRT |
| [ppyolov2_r101vd_dcn_365e_coco](https://bj.bcebos.com/paddlehub/fastdeploy/ppyolov2_r101vd_dcn_365e_coco.tgz) | 282MB | 49.7% | 暂不支持TensorRT |
| [yolov3_darknet53_270e_coco](https://bj.bcebos.com/paddlehub/fastdeploy/yolov3_darknet53_270e_coco.tgz) |237MB | 39.1% | |
| [yolox_s_300e_coco](https://bj.bcebos.com/paddlehub/fastdeploy/yolox_s_300e_coco.tgz) | 35MB | 40.4% | |
| [faster_rcnn_r50_vd_fpn_2x_coco](https://bj.bcebos.com/paddlehub/fastdeploy/faster_rcnn_r50_vd_fpn_2x_coco.tgz) | 160MB | 40.8%| 暂不支持TensorRT |


## 详细部署文档

- [Python部署](python)
- [C++部署](cpp)
28 changes: 28 additions & 0 deletions examples/vision/detection/paddledetection/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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_faster_rcnn_demo ${PROJECT_SOURCE_DIR}/infer_faster_rcnn.cc)
target_link_libraries(infer_faster_rcnn_demo ${FASTDEPLOY_LIBS})

add_executable(infer_ppyoloe_demo ${PROJECT_SOURCE_DIR}/infer_ppyoloe.cc)
target_link_libraries(infer_ppyoloe_demo ${FASTDEPLOY_LIBS})

add_executable(infer_picodet_demo ${PROJECT_SOURCE_DIR}/infer_picodet.cc)
target_link_libraries(infer_picodet_demo ${FASTDEPLOY_LIBS})

add_executable(infer_yolox_demo ${PROJECT_SOURCE_DIR}/infer_yolox.cc)
target_link_libraries(infer_yolox_demo ${FASTDEPLOY_LIBS})

add_executable(infer_yolov3_demo ${PROJECT_SOURCE_DIR}/infer_yolov3.cc)
target_link_libraries(infer_yolov3_demo ${FASTDEPLOY_LIBS})

add_executable(infer_ppyolo_demo ${PROJECT_SOURCE_DIR}/infer_ppyolo.cc)
target_link_libraries(infer_ppyolo_demo ${FASTDEPLOY_LIBS})
75 changes: 75 additions & 0 deletions examples/vision/detection/paddledetection/cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# PaddleDetection C++部署示例

本目录下提供`infer_xxx.cc`快速完成PaddleDetection模型包括PPYOLOE/PicoDet/YOLOX/YOLOv3/PPYOLO/FasterRCNN在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。

在部署前,需确认以下两个步骤

- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/quick_start/requirements.md)
- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/compile/prebuilt_libraries.md)

以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试

```
mkdir build
cd build
wget https://bj.bcebos.com/paddlehub/fastdeploy/libs/0.2.0/fastdeploy-linux-x64-gpu-0.2.0.tgz
tar xvf fastdeploy-linux-x64-gpu-0.2.0.tgz
cd fastdeploy-linux-x64-gpu-0.2.0/examples/vision/detection/paddledetection
mkdir build && cd build
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/../../../../../../fastdeploy-linux-x64-gpu-0.2.0
make -j

# 下载PPYOLOE模型文件和测试图片
wget https://bj.bcebos.com/paddlehub/fastdeploy/picodet_l_320_coco_lcnet.tgz
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000087038.jpg
tar xvf picodet_l_320_coco_lcnet.tgz


# CPU推理
./infer_ppyoloe_demo ./picodet_l_320_coco_lcnet 000000087038.jpg 0
# GPU推理
./infer_ppyoloe_demo ./picodet_l_320_coco_lcnet 000000087038.jpg 1
# GPU上TensorRT推理
./infer_ppyoloe_demo ./picodet_l_320_coco_lcnet 000000087038.jpg 2
```

## PaddleDetection C++接口

### 模型类

PaddleDetection目前支持6种模型系列,类名分别为`PPYOLOE`, `PicoDet`, `PaddleYOLOX`, `PPYOLO`, `FasterRCNN`,所有类名的构造函数和预测函数在参数上完全一致,本文档以PPYOLOE为例讲解API
```
fastdeploy::vision::detection::PPYOLOE(
const string& model_file,
const string& params_file,
const string& config_file
const RuntimeOption& runtime_option = RuntimeOption(),
const Frontend& model_format = Frontend::PADDLE)
```

PaddleDetection PPYOLOE模型加载和初始化,其中model_file为导出的ONNX模型格式。

**参数**

> * **model_file**(str): 模型文件路径
> * **params_file**(str): 参数文件路径
> * **config_file**(str): 配置文件路径,即PaddleDetection导出的部署yaml文件
> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置
> * **model_format**(Frontend): 模型格式,默认为PADDLE格式

#### Predict函数

> ```
> PPYOLOE::Predict(cv::Mat* im, DetectionResult* result)
> ```
>
> 模型预测接口,输入图像直接输出检测结果。
>
> **参数**
>
> > * **im**: 输入图像,注意需为HWC,BGR格式
> > * **result**: 检测结果,包括检测框,各个框的置信度, DetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/)

- [模型介绍](../../)
- [Python部署](../python)
- [视觉模型预测结果](../../../../../docs/api/vision_results/)
Loading