Skip to content

Commit d38b869

Browse files
authored
Fix the input dimension for multiclass_nms_op. (#8232)
1 parent e800597 commit d38b869

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

paddle/fluid/operators/multiclass_nms_op.cc

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@ class MultiClassNMSOp : public framework::OperatorWithKernel {
3838
auto box_dims = ctx->GetInputDim("BBoxes");
3939
auto score_dims = ctx->GetInputDim("Scores");
4040

41-
PADDLE_ENFORCE_EQ(box_dims.size(), 2,
42-
"The rank of Input(BBoxes) must be 2.");
41+
PADDLE_ENFORCE_EQ(box_dims.size(), 3,
42+
"The rank of Input(BBoxes) must be 3.");
4343
PADDLE_ENFORCE_EQ(score_dims.size(), 3,
4444
"The rank of Input(Scores) must be 3.");
45-
PADDLE_ENFORCE_EQ(box_dims[1], 4,
45+
PADDLE_ENFORCE_EQ(box_dims[2], 4,
4646
"The 2nd dimension of Input(BBoxes) must be 4, "
4747
"represents the layout of coordinate "
4848
"[xmin, ymin, xmax, ymax]");
49-
PADDLE_ENFORCE_EQ(box_dims[0], score_dims[2],
49+
PADDLE_ENFORCE_EQ(box_dims[1], score_dims[2],
5050
"The 1st dimensiong of Input(BBoxes) must be equal to "
5151
"3rd dimension of Input(Scores), which represents the "
5252
"predicted bboxes.");
5353

5454
// Here the box_dims[0] is not the real dimension of output.
5555
// It will be rewritten in the computing kernel.
56-
ctx->SetOutputDim("Out", {box_dims[0], 6});
56+
ctx->SetOutputDim("Out", {box_dims[1], 6});
5757
}
5858

5959
protected:
@@ -260,15 +260,20 @@ class MultiClassNMSKernel : public framework::OpKernel<T> {
260260
int64_t batch_size = score_dims[0];
261261
int64_t class_num = score_dims[1];
262262
int64_t predict_dim = score_dims[2];
263+
int64_t box_dim = boxes->dims()[2];
263264

264265
std::vector<std::map<int, std::vector<int>>> all_indices;
265266
std::vector<size_t> batch_starts = {0};
266267
for (int64_t i = 0; i < batch_size; ++i) {
267268
Tensor ins_score = scores->Slice(i, i + 1);
268269
ins_score.Resize({class_num, predict_dim});
270+
271+
Tensor ins_boxes = boxes->Slice(i, i + 1);
272+
ins_boxes.Resize({predict_dim, box_dim});
273+
269274
std::map<int, std::vector<int>> indices;
270275
int num_nmsed_out = 0;
271-
MultiClassNMS(ctx, ins_score, *boxes, indices, num_nmsed_out);
276+
MultiClassNMS(ctx, ins_score, ins_boxes, indices, num_nmsed_out);
272277
all_indices.push_back(indices);
273278
batch_starts.push_back(batch_starts.back() + num_nmsed_out);
274279
}
@@ -282,11 +287,15 @@ class MultiClassNMSKernel : public framework::OpKernel<T> {
282287
for (int64_t i = 0; i < batch_size; ++i) {
283288
Tensor ins_score = scores->Slice(i, i + 1);
284289
ins_score.Resize({class_num, predict_dim});
290+
291+
Tensor ins_boxes = boxes->Slice(i, i + 1);
292+
ins_boxes.Resize({predict_dim, box_dim});
293+
285294
int64_t s = batch_starts[i];
286295
int64_t e = batch_starts[i + 1];
287296
if (e > s) {
288297
Tensor out = outs->Slice(s, e);
289-
MultiClassOutput(ins_score, *boxes, all_indices[i], &out);
298+
MultiClassOutput(ins_score, ins_boxes, all_indices[i], &out);
290299
}
291300
}
292301
}
@@ -303,9 +312,9 @@ class MultiClassNMSOpMaker : public framework::OpProtoAndCheckerMaker {
303312
MultiClassNMSOpMaker(OpProto* proto, OpAttrChecker* op_checker)
304313
: OpProtoAndCheckerMaker(proto, op_checker) {
305314
AddInput("BBoxes",
306-
"(Tensor) A 2-D Tensor with shape [M, 4] represents the "
307-
"predicted locations of M bounding bboxes. Each bounding box "
308-
"has four coordinate values and the layout is "
315+
"(Tensor) A 3-D Tensor with shape [N, M, 4] represents the "
316+
"predicted locations of M bounding bboxes, N is the batch size. "
317+
"Each bounding box has four coordinate values and the layout is "
309318
"[xmin, ymin, xmax, ymax].");
310319
AddInput("Scores",
311320
"(Tensor) A 3-D Tensor with shape [N, C, M] represents the "

python/paddle/v2/fluid/tests/test_multiclass_nms_op.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ def batched_multiclass_nms(boxes, scores, background, score_threshold,
137137
det_outs = []
138138
lod = [0]
139139
for n in range(batch_size):
140-
nmsed_outs, nmsed_num = multiclass_nms(boxes, scores[n], background,
140+
nmsed_outs, nmsed_num = multiclass_nms(boxes[n], scores[n], background,
141141
score_threshold, nms_threshold,
142142
nms_top_k, keep_top_k)
143143
lod.append(lod[-1] + nmsed_num)
144144
if nmsed_num == 0: continue
145145

146146
for c, indices in nmsed_outs.iteritems():
147147
for idx in indices:
148-
xmin, ymin, xmax, ymax = boxes[idx][:]
148+
xmin, ymin, xmax, ymax = boxes[n][idx][:]
149149
det_outs.append([c, scores[n][c][idx], xmin, ymin, xmax, ymax])
150150

151151
return det_outs, lod
@@ -179,9 +179,9 @@ def softmax(x):
179179
scores = np.reshape(scores, (N, M, C))
180180
scores = np.transpose(scores, (0, 2, 1))
181181

182-
boxes = np.random.random((M, BOX_SIZE)).astype('float32')
183-
boxes[:, 0:2] = boxes[:, 0:2] * 0.5
184-
boxes[:, 2:4] = boxes[:, 2:4] * 0.5 + 0.5
182+
boxes = np.random.random((N, M, BOX_SIZE)).astype('float32')
183+
boxes[:, :, 0:2] = boxes[:, :, 0:2] * 0.5
184+
boxes[:, :, 2:4] = boxes[:, :, 2:4] * 0.5 + 0.5
185185

186186
nmsed_outs, lod = batched_multiclass_nms(boxes, scores, background,
187187
score_threshold, nms_threshold,

0 commit comments

Comments
 (0)