From 2b668cec77fc4ea4eca75b88b336540cf46e106f Mon Sep 17 00:00:00 2001 From: alex <57749489+aboccag@users.noreply.github.com> Date: Tue, 4 Aug 2026 08:55:54 +0200 Subject: [PATCH] Fix the RCNN export_onnx path under PIR RPNHead carries a __shared__ config key `export_onnx`. With it set, the RCNN family emits a straight-line, batch-size-1 graph -- a Python loop over FPN levels, then concat/topk/gather -- instead of the batch loop with TensorArray accumulation. That is what makes an ONNX export of a two-stage detector possible at all, and it had simply never been run under PIR. Two one-liners, each restoring consistency with code a few lines away in the same function: * rpn_head: `paddle.shape(onnx_topk_rois)[0]` is a 0-d tensor under PIR, and the caller indexes rois_num[0] -- IndexError: list index out of range. The non-ONNX branch three lines up already slices [0:1]. * post_process: concat rejects the 0-d scalars that indexing scale_factor now yields -- "The axis is expected to be in range of [0, 0)". The non-ONNX branch already unsqueezes them, and carries a TODO(PIR) comment saying exactly this. Without these, faster_rcnn and friends export a graph containing while + TensorArray + slice_array_dense, which no ONNX exporter can follow. --- ppdet/modeling/post_process.py | 3 +++ ppdet/modeling/proposal_generator/rpn_head.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ppdet/modeling/post_process.py b/ppdet/modeling/post_process.py index 78aeacf1c74..a66ba76a8e7 100644 --- a/ppdet/modeling/post_process.py +++ b/ppdet/modeling/post_process.py @@ -160,6 +160,9 @@ def get_pred(self, bboxes, bbox_num, im_shape, scale_factor): else: # simplify the computation for bs=1 when exporting onnx scale_y, scale_x = scale_factor[0][0], scale_factor[0][1] + # TODO(PIR): indexing yields 0-d tensors which concat rejects. + scale_y = paddle.unsqueeze(scale_y, 0) + scale_x = paddle.unsqueeze(scale_x, 0) scale = paddle.concat( [scale_x, scale_y, scale_x, scale_y]).unsqueeze(0) self.origin_shape_list = paddle.expand(origin_shape, diff --git a/ppdet/modeling/proposal_generator/rpn_head.py b/ppdet/modeling/proposal_generator/rpn_head.py index 456a9322fcd..b334437f046 100644 --- a/ppdet/modeling/proposal_generator/rpn_head.py +++ b/ppdet/modeling/proposal_generator/rpn_head.py @@ -242,7 +242,7 @@ def _gen_proposal(self, scores, bbox_deltas, anchors, inputs): if self.export_onnx: output_rois = [onnx_topk_rois] - output_rois_num = paddle.shape(onnx_topk_rois)[0] + output_rois_num = paddle.shape(onnx_topk_rois)[0:1] else: output_rois = bs_rois_collect output_rois_num = bs_rois_num_collect