Skip to content

Commit 9eefd2c

Browse files
authored
Modify some infer-shape about detection operators in compile-time. (#14483)
* Modify some infer-shape in compile-time.
1 parent cf685f3 commit 9eefd2c

File tree

3 files changed

+43
-42
lines changed

3 files changed

+43
-42
lines changed

paddle/fluid/operators/detection/box_coder_op.cc

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,30 @@ class BoxCoderOp : public framework::OperatorWithKernel {
3030
auto prior_box_dims = ctx->GetInputDim("PriorBox");
3131
auto target_box_dims = ctx->GetInputDim("TargetBox");
3232

33-
PADDLE_ENFORCE_EQ(prior_box_dims.size(), 2,
34-
"The rank of Input of PriorBoxVar must be 2");
35-
PADDLE_ENFORCE_EQ(prior_box_dims[1], 4, "The shape of PriorBox is [N, 4]");
36-
if (ctx->HasInput("PriorBoxVar")) {
37-
auto prior_box_var_dims = ctx->GetInputDim("PriorBoxVar");
38-
PADDLE_ENFORCE_EQ(prior_box_dims, prior_box_var_dims);
33+
if (ctx->IsRuntime()) {
34+
PADDLE_ENFORCE_EQ(prior_box_dims.size(), 2,
35+
"The rank of Input of PriorBoxVar must be 2");
36+
PADDLE_ENFORCE_EQ(prior_box_dims[1], 4,
37+
"The shape of PriorBox is [N, 4]");
38+
if (ctx->HasInput("PriorBoxVar")) {
39+
auto prior_box_var_dims = ctx->GetInputDim("PriorBoxVar");
40+
PADDLE_ENFORCE_EQ(prior_box_dims, prior_box_var_dims);
41+
}
42+
43+
auto code_type =
44+
GetBoxCodeType(ctx->Attrs().Get<std::string>("code_type"));
45+
if (code_type == BoxCodeType::kEncodeCenterSize) {
46+
PADDLE_ENFORCE_EQ(target_box_dims.size(), 2,
47+
"The rank of Input of TargetBox must be 2");
48+
PADDLE_ENFORCE_EQ(target_box_dims[1], 4,
49+
"The shape of TargetBox is [M, 4]");
50+
} else if (code_type == BoxCodeType::kDecodeCenterSize) {
51+
PADDLE_ENFORCE_EQ(target_box_dims.size(), 3,
52+
"The rank of Input of TargetBox must be 3");
53+
PADDLE_ENFORCE_EQ(target_box_dims[1], prior_box_dims[0]);
54+
PADDLE_ENFORCE_EQ(target_box_dims[2], prior_box_dims[1]);
55+
}
3956
}
40-
41-
auto code_type = GetBoxCodeType(ctx->Attrs().Get<std::string>("code_type"));
42-
if (code_type == BoxCodeType::kEncodeCenterSize) {
43-
PADDLE_ENFORCE_EQ(target_box_dims.size(), 2,
44-
"The rank of Input of TargetBox must be 2");
45-
PADDLE_ENFORCE_EQ(target_box_dims[1], 4,
46-
"The shape of TargetBox is [M, 4]");
47-
} else if (code_type == BoxCodeType::kDecodeCenterSize) {
48-
PADDLE_ENFORCE_EQ(target_box_dims.size(), 3,
49-
"The rank of Input of TargetBox must be 3");
50-
PADDLE_ENFORCE_EQ(target_box_dims[1], prior_box_dims[0]);
51-
PADDLE_ENFORCE_EQ(target_box_dims[2], prior_box_dims[1]);
52-
}
53-
5457
ctx->SetOutputDim(
5558
"OutputBox",
5659
framework::make_ddim({target_box_dims[0], prior_box_dims[0], 4}));

paddle/fluid/operators/detection/multiclass_nms_op.cc

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,26 @@ class MultiClassNMSOp : public framework::OperatorWithKernel {
3636
auto box_dims = ctx->GetInputDim("BBoxes");
3737
auto score_dims = ctx->GetInputDim("Scores");
3838

39-
PADDLE_ENFORCE_EQ(box_dims.size(), 3,
40-
"The rank of Input(BBoxes) must be 3.");
41-
PADDLE_ENFORCE_EQ(score_dims.size(), 3,
42-
"The rank of Input(Scores) must be 3.");
43-
PADDLE_ENFORCE(box_dims[2] == 4 || box_dims[2] == 8 || box_dims[2] == 16 ||
44-
box_dims[2] == 24 || box_dims[2] == 32,
45-
"The 2nd dimension of Input(BBoxes) must be 4 or 8, "
46-
"represents the layout of coordinate "
47-
"[xmin, ymin, xmax, ymax] or "
48-
"4 points: [x1, y1, x2, y2, x3, y3, x4, y4] or "
49-
"8 points: [xi, yi] i= 1,2,...,8 or "
50-
"12 points: [xi, yi] i= 1,2,...,12 or "
51-
"16 points: [xi, yi] i= 1,2,...,16");
52-
PADDLE_ENFORCE_EQ(box_dims[1], score_dims[2],
53-
"The 1st dimensiong of Input(BBoxes) must be equal to "
54-
"3rd dimension of Input(Scores), which represents the "
55-
"predicted bboxes.");
56-
39+
if (ctx->IsRuntime()) {
40+
PADDLE_ENFORCE_EQ(box_dims.size(), 3,
41+
"The rank of Input(BBoxes) must be 3.");
42+
PADDLE_ENFORCE_EQ(score_dims.size(), 3,
43+
"The rank of Input(Scores) must be 3.");
44+
PADDLE_ENFORCE(box_dims[2] == 4 || box_dims[2] == 8 ||
45+
box_dims[2] == 16 || box_dims[2] == 24 ||
46+
box_dims[2] == 32,
47+
"The 2nd dimension of Input(BBoxes) must be 4 or 8, "
48+
"represents the layout of coordinate "
49+
"[xmin, ymin, xmax, ymax] or "
50+
"4 points: [x1, y1, x2, y2, x3, y3, x4, y4] or "
51+
"8 points: [xi, yi] i= 1,2,...,8 or "
52+
"12 points: [xi, yi] i= 1,2,...,12 or "
53+
"16 points: [xi, yi] i= 1,2,...,16");
54+
PADDLE_ENFORCE_EQ(box_dims[1], score_dims[2],
55+
"The 1st dimensiong of Input(BBoxes) must be equal to "
56+
"3rd dimension of Input(Scores), which represents the "
57+
"predicted bboxes.");
58+
}
5759
// Here the box_dims[0] is not the real dimension of output.
5860
// It will be rewritten in the computing kernel.
5961
ctx->SetOutputDim("Out", {box_dims[1], box_dims[2] + 2});

python/paddle/fluid/layers/detection.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,7 @@ class number, M is number of bounding boxes. For each category
283283
prior_box_var=prior_box_var,
284284
target_box=loc,
285285
code_type='decode_center_size')
286-
compile_shape = scores.shape
287-
run_shape = nn.shape(scores)
288-
scores = nn.flatten(x=scores, axis=2)
289286
scores = nn.softmax(input=scores)
290-
scores = nn.reshape(x=scores, shape=compile_shape, actual_shape=run_shape)
291287
scores = nn.transpose(scores, perm=[0, 2, 1])
292288
scores.stop_gradient = True
293289
nmsed_outs = helper.create_variable_for_type_inference(

0 commit comments

Comments
 (0)