From 960de9dbaa6c8bebc38837f8167be95795fa86cd Mon Sep 17 00:00:00 2001 From: wangguanzhong Date: Wed, 19 Feb 2020 13:51:30 +0800 Subject: [PATCH] add padstride in cpp_infer and refine doc (#241) * add padstride in cpp_infer and refine doc * refine doc for export_model --- .../inference/EXPORT_MODEL.md | 15 +++++++++++++++ docs/advanced_tutorials/inference/INFERENCE.md | 1 + tools/cpp_demo.yml | 2 ++ tools/cpp_infer.py | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/docs/advanced_tutorials/inference/EXPORT_MODEL.md b/docs/advanced_tutorials/inference/EXPORT_MODEL.md index 02ead9353..8cd73a4bf 100644 --- a/docs/advanced_tutorials/inference/EXPORT_MODEL.md +++ b/docs/advanced_tutorials/inference/EXPORT_MODEL.md @@ -2,6 +2,21 @@ 训练得到一个满足要求的模型后,如果想要将该模型接入到C++预测库或者Serving服务,需要通过`tools/export_model.py`导出该模型。 +**说明:** + +- 导出模型输入为网络输入图像,即原始图片经过预处理后的图像,具体预处理方式可参考配置文件中TestReader部分。各类检测模型的输入格式分别为: + +| 模型系列名称 | 输入图像预处理方式 | 其他输入信息 | +| :---------: | ----------- | ---------- | +| YOLO | 缩放至指定大小,归一化 | im\_size: 格式为[origin\_H, origin\_W], origin为原始图像 | +| SSD | 缩放至指定大小,归一化 | im\_shape: 格式为[origin\_H, origin\_W], origin为原始图像 | +| RCNN | 归一化,等比例缩放 | 1. im\_info: 格式为[input\_H, input\_W, scale],input为输入图像,scale为```输入图像大小/原始图像大小```
2. im\_shape:格式为[origin\_H, origin\_W, 1.], origin为原始图像 | +| RCNN+FPN | 归一化,等比例缩放,对图像填充0使得长宽均为32的倍数 | 1. im\_info: 格式为[input\_H, input\_W, scale],input为输入图像,scale为```输入图像大小/原始图像大小```
2. im\_shape:格式为[origin\_H, origin\_W, 1.], origin为原始图像 | +| RetinaNet | 归一化,等比例缩放,对图像填充0使得长宽均为128的倍数 | 1. im\_info: 格式为[input\_H, input\_W, scale],input为输入图像,scale为```输入图像大小/原始图像大小```
2. im\_shape:格式为[origin\_H, origin\_W, 1.], origin为原始图像 | + +- 导出模型输出统一为NMS的输出,形状为[N, 6], 其中N为预测框的个数,6为[class_id, score, x1, y1, x2, y2]. + + ## 启动参数说明 | FLAG | 用途 | 默认值 | 备注 | diff --git a/docs/advanced_tutorials/inference/INFERENCE.md b/docs/advanced_tutorials/inference/INFERENCE.md index b15f6c9c0..3db29d930 100644 --- a/docs/advanced_tutorials/inference/INFERENCE.md +++ b/docs/advanced_tutorials/inference/INFERENCE.md @@ -25,6 +25,7 @@ python tools/cpp_infer.py --model_path=inference_model/faster_rcnn_r50_1x/ --con 1. 设置shape时必须保持与模型导出时shape大小一致; 2. `min_subgraph_size`的设置与模型arch相关,对部分arch需要调大该参数,一般设置为40适用于所有模型。适当的调小`min_subgraph_size`会对预测有加速效果,例如YOLO中该参数可设置为3。 +3. 预处理中`PadStride`为输入图片右下角填充0,默认设置stride为0,即不对输入图片做padding操作。模型中包含FPN结构时,stride应设置为32。模型为RetinaNet系列模型时,stride应设置为128. ## Paddle环境搭建 需要基于develop分支编译TensorRT版本Paddle, 在编译命令中指定TensorRT路径: diff --git a/tools/cpp_demo.yml b/tools/cpp_demo.yml index 96f111676..3d5143c9b 100644 --- a/tools/cpp_demo.yml +++ b/tools/cpp_demo.yml @@ -25,3 +25,5 @@ Preprocess: is_scale: True - type: Permute to_bgr: False +- type: PadStride + stride: 0 # set 32 on FPN and 128 on RetinaNet diff --git a/tools/cpp_infer.py b/tools/cpp_infer.py index 41f6ffe80..c7232897f 100644 --- a/tools/cpp_infer.py +++ b/tools/cpp_infer.py @@ -169,6 +169,24 @@ class Permute(object): return im +class PadStride(object): + def __init__(self, stride=0): + assert stride >= 0, "Unsupported stride: {}, the stride in PadStride must be greater or equal to 0".format( + stride) + self.coarsest_stride = stride + + def __call__(self, im): + coarsest_stride = self.coarsest_stride + if coarsest_stride == 0: + return im + im_c, im_h, im_w = im.shape + pad_h = int(np.ceil(float(im_h) / coarsest_stride) * coarsest_stride) + pad_w = int(np.ceil(float(im_w) / coarsest_stride) * coarsest_stride) + padding_im = np.zeros((im_c, pad_h, pad_w), dtype=np.float32) + padding_im[:, :im_h, :im_w] = im + return padding_im + + def Preprocess(img_path, arch, config): img = DecodeImage(img_path) orig_shape = img.shape -- GitLab