Skip to content

Update PaddleClas and PaddleSeg doc #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
10d7bcf
Update README.md
felixhjh Aug 11, 2022
64e191a
Update README.md
felixhjh Aug 11, 2022
43ae0ba
Update README.md
felixhjh Aug 11, 2022
1328d44
Create README.md
felixhjh Aug 11, 2022
8999d86
Update README.md
felixhjh Aug 11, 2022
19eeeb4
Update README.md
felixhjh Aug 12, 2022
a6def20
Update README.md
felixhjh Aug 12, 2022
b586546
Update README.md
felixhjh Aug 12, 2022
560ea16
Add evaluation calculate time and fix some bugs
felixhjh Aug 12, 2022
55e0587
Merge branch 'PaddlePaddle:develop' into develop
felixhjh Aug 12, 2022
04fffdd
Merge branch 'develop' of https://github.com/felixhjh/FastDeploy into…
felixhjh Aug 12, 2022
016f7cb
Update classification __init__
felixhjh Aug 12, 2022
84b94b6
Merge branch 'develop' into develop
jiangjiajun Aug 12, 2022
74aaa2a
Move to ppseg
felixhjh Aug 12, 2022
1105435
Merge branch 'develop' of https://github.com/felixhjh/FastDeploy into…
felixhjh Aug 12, 2022
bb8920c
Add segmentation doc
felixhjh Aug 12, 2022
a25213f
Add PaddleClas infer.py
felixhjh Aug 12, 2022
9cf20b2
Update PaddleClas infer.py
felixhjh Aug 12, 2022
a4c83d6
Merge branch 'develop' into develop
felixhjh Aug 12, 2022
df1a368
Delete .infer.py.swp
felixhjh Aug 12, 2022
c3dfdd1
Add PaddleClas infer.cc
felixhjh Aug 12, 2022
f920c96
Merge branch 'develop' of https://github.com/felixhjh/FastDeploy into…
felixhjh Aug 12, 2022
74890ba
Update README.md
felixhjh Aug 12, 2022
5fe7ef1
Update README.md
felixhjh Aug 12, 2022
d766ce1
Update README.md
felixhjh Aug 12, 2022
b463a0d
Merge branch 'develop' into develop
felixhjh Aug 12, 2022
34b7fd6
Update infer.py
felixhjh Aug 12, 2022
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
57 changes: 28 additions & 29 deletions examples/vision/classification/paddleclas/cpp/README.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,75 @@
# YOLOv7 C++部署示例
# PaddleClas C++部署示例

本目录下提供`infer.cc`快速完成YOLOv7在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。
本目录下提供`infer.cc`快速完成PaddleClas系列模型在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。

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

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

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

```
#下载SDK,编译模型examples代码(SDK中包含了examples代码)
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/classification/paddleclas/cpp
mkdir build
cd build
wget https://xxx.tgz
tar xvf fastdeploy-linux-x64-0.2.0.tgz
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/../../../../../../fastdeploy-linux-x64-gpu-0.2.0
make -j

#下载官方转换好的yolov7模型文件和测试图片
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov7.onnx
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000087038.jpg
# 下载ResNet50_vd模型文件和测试图片
wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz
tar -xvf ResNet50_vd_infer.tgz
wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg


# CPU推理
./infer_demo yolov7.onnx 000000087038.jpg 0
./infer_demo ResNet50_vd_infer ILSVRC2012_val_00000010.jpeg 0
# GPU推理
./infer_demo yolov7.onnx 000000087038.jpg 1
./infer_demo ResNet50_vd_infer ILSVRC2012_val_00000010.jpeg 1
# GPU上TensorRT推理
./infer_demo yolov7.onnx 000000087038.jpg 2
./infer_demo ResNet50_vd_infer ILSVRC2012_val_00000010.jpeg 2
```

## YOLOv7 C++接口
## PaddleClas C++接口

### YOLOv7类
### PaddleClas类

```
fastdeploy::vision::detection::YOLOv7(
fastdeploy::vision::classification::PaddleClasModel(
const string& model_file,
const string& params_file = "",
const string& params_file,
const string& config_file,
const RuntimeOption& runtime_option = RuntimeOption(),
const Frontend& model_format = Frontend::ONNX)
const Frontend& model_format = Frontend::PADDLE)
```

YOLOv7模型加载和初始化,其中model_file为导出的ONNX模型格式。
PaddleClas模型加载和初始化,其中model_file, params_file为训练模型导出的Paddle inference文件,具体请参考其文档说明[模型导出](https://github.com/PaddlePaddle/PaddleClas/blob/release/2.4/docs/zh_CN/inference_deployment/export_model.md#2-%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B%E5%AF%BC%E5%87%BA)

**参数**

> * **model_file**(str): 模型文件路径
> * **params_file**(str): 参数文件路径,当模型格式为ONNX时,此参数传入空字符串即可
> * **params_file**(str): 参数文件路径
> * **config_file**(str): 推理部署配置文件
> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置
> * **model_format**(Frontend): 模型格式,默认为ONNX格式
> * **model_format**(Frontend): 模型格式,默认为Paddle格式

#### Predict函数

> ```
> YOLOv7::Predict(cv::Mat* im, DetectionResult* result,
> float conf_threshold = 0.25,
> float nms_iou_threshold = 0.5)
> PaddleClasModel::Predict(cv::Mat* im, ClassifyResult* result, int topk = 1)
> ```
>
> 模型预测接口,输入图像直接输出检测结果。
>
> **参数**
>
> > * **im**: 输入图像,注意需为HWC,BGR格式
> > * **result**: 检测结果,包括检测框,各个框的置信度, DetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/)
> > * **conf_threshold**: 检测框置信度过滤阈值
> > * **nms_iou_threshold**: NMS处理过程中iou阈值
> > * **result**: 分类结果,包括label_id,以及相应的置信度, ClassifyResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/)
> > * **topk**(int):返回预测概率最高的topk个分类结果,默认为1

### 类成员变量

> > * **size**(vector<int>): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640]

- [模型介绍](../../)
- [Python部署](../python)
Expand Down
61 changes: 38 additions & 23 deletions examples/vision/classification/paddleclas/cpp/infer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,80 +13,95 @@
// limitations under the License.

#include "fastdeploy/vision.h"
#ifdef WIN32
const char sep = '\\';
#else
const char sep = '/';
#endif

void CpuInfer(const std::string& model_file, const std::string& image_file) {
auto model = fastdeploy::vision::detection::YOLOv7(model_file);
void CpuInfer(const std::string& model_dir, const std::string& image_file) {
auto model_file = model_dir + sep + "inference.pdmodel";
auto params_file = model_dir + sep + "inference.pdiparams";
auto config_file = model_dir + sep + "inference_cls.yaml";

auto option = fastdeploy::RuntimeOption();
option.UseCpu();
auto model = fastdeploy::vision::classification::PaddleClasModel(
model_file, params_file, config_file, option);
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}

auto im = cv::imread(image_file);
auto im_bak = im.clone();

fastdeploy::vision::DetectionResult res;
fastdeploy::vision::ClassifyResult res;
if (!model.Predict(&im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}

auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res);
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
// print res
std::cout << res.Str() << std::endl;
}

void GpuInfer(const std::string& model_file, const std::string& image_file) {
void GpuInfer(const std::string& model_dir, const std::string& image_file) {
auto model_file = model_dir + sep + "inference.pdmodel";
auto params_file = model_dir + sep + "inference.pdiparams";
auto config_file = model_dir + sep + "inference_cls.yaml";
auto option = fastdeploy::RuntimeOption();
option.UseGpu();
auto model = fastdeploy::vision::detection::YOLOv7(model_file, "", option);
auto model = fastdeploy::vision::classification::PaddleClasModel(
model_file, params_file, config_file, option);
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}

auto im = cv::imread(image_file);
auto im_bak = im.clone();

fastdeploy::vision::DetectionResult res;
fastdeploy::vision::ClassifyResult res;
if (!model.Predict(&im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}

auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res);
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
// print res
std::cout << res.Str() << std::endl;
}

void TrtInfer(const std::string& model_file, const std::string& image_file) {
void TrtInfer(const std::string& model_dir, const std::string& image_file) {
auto model_file = model_dir + sep + "inference.pdmodel";
auto params_file = model_dir + sep + "inference.pdiparams";
auto config_file = model_dir + sep + "inference_cls.yaml";
auto option = fastdeploy::RuntimeOption();
option.UseGpu();
option.UseTrtBackend();
option.SetTrtInputShape("images", {1, 3, 640, 640});
auto model = fastdeploy::vision::detection::YOLOv7(model_file, "", option);
option.SetTrtInputShape("inputs", {1, 3, 224, 224}, {1, 3, 224, 224},
{1, 3, 224, 224});
auto model = fastdeploy::vision::classification::PaddleClasModel(
model_file, params_file, config_file, option);
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}

auto im = cv::imread(image_file);
auto im_bak = im.clone();

fastdeploy::vision::DetectionResult res;
fastdeploy::vision::ClassifyResult res;
if (!model.Predict(&im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}

auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res);
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
// print res
std::cout << res.Str() << std::endl;
}

int main(int argc, char* argv[]) {
if (argc < 4) {
std::cout << "Usage: infer_demo path/to/model path/to/image run_option, "
"e.g ./infer_model ./yolov7.onnx ./test.jpeg 0"
"e.g ./infer_demo ./ResNet50_vd ./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."
Expand Down
2 changes: 1 addition & 1 deletion examples/vision/classification/paddleclas/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ PaddleClas模型加载和初始化,其中model_file, params_file为训练模
> **参数**
>
> > * **input_image**(np.ndarray): 输入数据,注意需为HWC,BGR格式
> > * **topk**(int):返回预测概率最高的topk个分类结果
> > * **topk**(int):返回预测概率最高的topk个分类结果,默认为1

> **返回**
>
Expand Down