未验证 提交 5cd97a1c 编写于 作者: W wangguanzhong 提交者: GitHub

support multiclass nms for multi-batch, test=develop (#28154)

上级 602d2ce5
...@@ -290,6 +290,7 @@ class MultiClassNMSKernel : public framework::OpKernel<T> { ...@@ -290,6 +290,7 @@ class MultiClassNMSKernel : public framework::OpKernel<T> {
} else { } else {
sdata = scores_data + label * predict_dim; sdata = scores_data + label * predict_dim;
} }
for (size_t j = 0; j < indices.size(); ++j) { for (size_t j = 0; j < indices.size(); ++j) {
int idx = indices[j]; int idx = indices[j];
odata[count * out_dim] = label; // label odata[count * out_dim] = label; // label
...@@ -333,6 +334,7 @@ class MultiClassNMSKernel : public framework::OpKernel<T> { ...@@ -333,6 +334,7 @@ class MultiClassNMSKernel : public framework::OpKernel<T> {
Tensor boxes_slice, scores_slice; Tensor boxes_slice, scores_slice;
int n = score_size == 3 ? batch_size : boxes->lod().back().size() - 1; int n = score_size == 3 ? batch_size : boxes->lod().back().size() - 1;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
std::map<int, std::vector<int>> indices;
if (score_size == 3) { if (score_size == 3) {
scores_slice = scores->Slice(i, i + 1); scores_slice = scores->Slice(i, i + 1);
scores_slice.Resize({score_dims[1], score_dims[2]}); scores_slice.Resize({score_dims[1], score_dims[2]});
...@@ -340,10 +342,14 @@ class MultiClassNMSKernel : public framework::OpKernel<T> { ...@@ -340,10 +342,14 @@ class MultiClassNMSKernel : public framework::OpKernel<T> {
boxes_slice.Resize({score_dims[2], box_dim}); boxes_slice.Resize({score_dims[2], box_dim});
} else { } else {
auto boxes_lod = boxes->lod().back(); auto boxes_lod = boxes->lod().back();
if (boxes_lod[i] == boxes_lod[i + 1]) {
all_indices.push_back(indices);
batch_starts.push_back(batch_starts.back());
continue;
}
scores_slice = scores->Slice(boxes_lod[i], boxes_lod[i + 1]); scores_slice = scores->Slice(boxes_lod[i], boxes_lod[i + 1]);
boxes_slice = boxes->Slice(boxes_lod[i], boxes_lod[i + 1]); boxes_slice = boxes->Slice(boxes_lod[i], boxes_lod[i + 1]);
} }
std::map<int, std::vector<int>> indices;
MultiClassNMS(ctx, scores_slice, boxes_slice, score_size, &indices, MultiClassNMS(ctx, scores_slice, boxes_slice, score_size, &indices,
&num_nmsed_out); &num_nmsed_out);
all_indices.push_back(indices); all_indices.push_back(indices);
...@@ -375,12 +381,14 @@ class MultiClassNMSKernel : public framework::OpKernel<T> { ...@@ -375,12 +381,14 @@ class MultiClassNMSKernel : public framework::OpKernel<T> {
} }
} else { } else {
auto boxes_lod = boxes->lod().back(); auto boxes_lod = boxes->lod().back();
if (boxes_lod[i] == boxes_lod[i + 1]) continue;
scores_slice = scores->Slice(boxes_lod[i], boxes_lod[i + 1]); scores_slice = scores->Slice(boxes_lod[i], boxes_lod[i + 1]);
boxes_slice = boxes->Slice(boxes_lod[i], boxes_lod[i + 1]); boxes_slice = boxes->Slice(boxes_lod[i], boxes_lod[i + 1]);
if (return_index) { if (return_index) {
offset = boxes_lod[i] * score_dims[1]; offset = boxes_lod[i] * score_dims[1];
} }
} }
int64_t s = batch_starts[i]; int64_t s = batch_starts[i];
int64_t e = batch_starts[i + 1]; int64_t e = batch_starts[i + 1];
if (e > s) { if (e > s) {
......
...@@ -17,6 +17,7 @@ import unittest ...@@ -17,6 +17,7 @@ import unittest
import numpy as np import numpy as np
import copy import copy
from op_test import OpTest from op_test import OpTest
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid import Program, program_guard from paddle.fluid import Program, program_guard
...@@ -171,6 +172,9 @@ def lod_multiclass_nms(boxes, scores, background, score_threshold, ...@@ -171,6 +172,9 @@ def lod_multiclass_nms(boxes, scores, background, score_threshold,
lod = [] lod = []
head = 0 head = 0
for n in range(len(box_lod[0])): for n in range(len(box_lod[0])):
if box_lod[0][n] == 0:
lod.append(0)
continue
box = boxes[head:head + box_lod[0][n]] box = boxes[head:head + box_lod[0][n]]
score = scores[head:head + box_lod[0][n]] score = scores[head:head + box_lod[0][n]]
offset = head offset = head
...@@ -357,6 +361,53 @@ class TestMulticlassNMSLoDInput(OpTest): ...@@ -357,6 +361,53 @@ class TestMulticlassNMSLoDInput(OpTest):
self.check_output() self.check_output()
class TestMulticlassNMSNoBox(TestMulticlassNMSLoDInput):
def setUp(self):
self.set_argument()
M = 1200
C = 21
BOX_SIZE = 4
box_lod = [[0, 1200, 0]]
background = 0
nms_threshold = 0.3
nms_top_k = 400
keep_top_k = 200
score_threshold = self.score_threshold
normalized = False
scores = np.random.random((M, C)).astype('float32')
scores = np.apply_along_axis(softmax, 1, scores)
boxes = np.random.random((M, C, BOX_SIZE)).astype('float32')
boxes[:, :, 0] = boxes[:, :, 0] * 10
boxes[:, :, 1] = boxes[:, :, 1] * 10
boxes[:, :, 2] = boxes[:, :, 2] * 10 + 10
boxes[:, :, 3] = boxes[:, :, 3] * 10 + 10
det_outs, lod = lod_multiclass_nms(
boxes, scores, background, score_threshold, nms_threshold,
nms_top_k, keep_top_k, box_lod, normalized)
det_outs = np.array(det_outs).astype('float32')
nmsed_outs = det_outs[:, :-1].astype('float32') if len(
det_outs) else det_outs
self.op_type = 'multiclass_nms'
self.inputs = {
'BBoxes': (boxes, box_lod),
'Scores': (scores, box_lod),
}
self.outputs = {'Out': (nmsed_outs, [lod])}
self.attrs = {
'background_label': 0,
'nms_threshold': nms_threshold,
'nms_top_k': nms_top_k,
'keep_top_k': keep_top_k,
'score_threshold': score_threshold,
'nms_eta': 1.0,
'normalized': normalized,
}
class TestIOU(unittest.TestCase): class TestIOU(unittest.TestCase):
def test_iou(self): def test_iou(self):
box1 = np.array([4.0, 3.0, 7.0, 5.0]).astype('float32') box1 = np.array([4.0, 3.0, 7.0, 5.0]).astype('float32')
...@@ -521,4 +572,5 @@ class TestMulticlassNMSError(unittest.TestCase): ...@@ -521,4 +572,5 @@ class TestMulticlassNMSError(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册