PaddlePaddle / PaddleDetection

Object Detection toolkit based on PaddlePaddle. It supports object detection, instance segmentation, multiple object tracking and real-time multi-person keypoint detection.
Apache License 2.0
12.82k stars 2.89k forks source link

导出的 onnx 模型 shape 问题 #8838

Closed jrsebastian closed 8 months ago

jrsebastian commented 8 months ago

问题确认 Search before asking

请提出你的问题 Please ask your question

我根据 教程 导出了 ppyoloe_plus_crn_s_80e_coco.onnx,并且加了TestReader.inputs_def.image_shape=[3,640,640],但是得到的 shape 是这样的: image 请问用什么命令可以导出正常的 shape(如 image.shape=(1,3,640,640))? 我的命令如下:

$python tools/export_model.py -c configs/ppyoloe/ppyoloe_plus_crn_s_80e_coco.yml -o weights=../ppyoloe_plus_crn_s_80e_coco.pdparams exclude_nms=True use_gpu=false TestReader.inputs_def.image_shape=[3,640,640]

$paddle2onnx --model_dir output_inference/ppyoloe_plus_crn_s_80e_coco --model_filename model.pdmodel --params_filename model.pdiparams --opset_version 11 --save_file ppyoloe_plus_crn_s_80e_coco.onnx
laugh12321 commented 8 months ago

Hello everyone!

I would like to introduce my open-source project - TensoRT-YOLO, a tool for deploying YOLO Series (Support PP-YOLOE and PP-YOLOE+) with Efficient NMS in TensorRT.

Key Features

lyuwenyu commented 8 months ago

batch那个维度是动态的 改成 [1 3 640 640]试一下

jrsebastian commented 8 months ago

batch那个维度是动态的 改成 [1 3 640 640]试一下

@lyuwenyu 谢谢。目前通过这个命令已经转成了:

python -m paddle2onnx.optimize --input_model ppyoloe_plus_crn_s_80e_coco.onnx --output_model ppyoloe_plus_crn_s_80e_coco.onnx --input_shape_dict "{'image':[1,3,640,640],'scale_factor':[1,2],'tmp_17':[1,8400,4],'concat_14.tmp_0':[1,80,8400]}"

但是例子中给的 TestReader.inputs_def.image_shape=[3,640,640] 的作用是啥?为什么没用呢

laugh12321 commented 8 months ago

默认只能导出batch为1,可以参考TensoRT-YOLO这个项目,支持多batch导出,且添加EfficientNMS插件,相关代码如下。

# Ensure the required modules are imported within the function scope
from paddle2onnx.command import c_paddle_to_onnx
import paddle2onnx.paddle2onnx_cpp2py_export as c_p2o

model_dir = Path(model_dir)

# Validate model directory
assert model_dir.exists() and model_dir.is_dir(), f"Invalid model directory: {model_dir}"

# Define input shape dictionary
input_shape_dict = {
  'image': [batch_size, 3, *imgsz], 
  'scale_factor': [batch_size, 2]
}

# Export the model to ONNX
c_paddle_to_onnx(
  model_file=str(model_dir / model_filename),
  params_file=str(model_dir / params_filename),
  save_file=onnx_path,
  opset_version=opset,
  export_fp16_model=half,
  auto_upgrade_opset=True,
  enable_onnx_checker=True
)

# Convert Static Shape
c_p2o.optimize(onnx_path, onnx_path, input_shape_dict)
jrsebastian commented 8 months ago

默认只能导出batch为1,可以参考TensoRT-YOLO这个项目,支持多batch导出

其实我想要的结果就是 batch 为1 ,而不是 batch 是动态的,看上去只能通过指定 input_shape_dict 来固定 shape ?TestReader.inputs_def.image_shape=[3,640,640] 默认导出的是动态 batch 吗?

laugh12321 commented 8 months ago

我记得是这样的。