From 4161328eb528a396449a8be124aa203969ebc066 Mon Sep 17 00:00:00 2001 From: wanghaox Date: Mon, 26 Feb 2018 16:06:22 +0800 Subject: [PATCH] add warrper for detection map operator --- python/paddle/fluid/layers/detection.py | 40 +++++++++++++++++++++ python/paddle/fluid/tests/test_detection.py | 38 ++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/python/paddle/fluid/layers/detection.py b/python/paddle/fluid/layers/detection.py index 5ae4da1ea..2c2f80b4b 100644 --- a/python/paddle/fluid/layers/detection.py +++ b/python/paddle/fluid/layers/detection.py @@ -16,6 +16,7 @@ All layers just related to the detection neural network. """ from layer_function_generator import generate_layer_fn +from layer_function_generator import autodoc from ..layer_helper import LayerHelper import tensor import ops @@ -28,6 +29,7 @@ __all__ = [ 'target_assign', 'detection_output', 'ssd_loss', + 'detection_map', ] __auto__ = [ @@ -132,6 +134,44 @@ def detection_output(scores, return nmsed_outs +@autodoc() +def detection_map(detect_res, + label, + pos_count=None, + true_pos=None, + false_pos=None, + overlap_threshold=0.3, + evaluate_difficult=True, + ap_type='integral'): + helper = LayerHelper("detection_map", **locals()) + + map_out = helper.create_tmp_variable(dtype='float32') + accum_pos_count_out = helper.create_tmp_variable(dtype='int32') + accum_true_pos_out = helper.create_tmp_variable(dtype='float32') + accum_false_pos_out = helper.create_tmp_variable(dtype='float32') + helper.append_op( + type="detection_map", + inputs={ + 'Label': label, + 'DetectRes': detect_res, + 'PosCount': pos_count, + 'TruePos': true_pos, + 'FalsePos': false_pos + }, + outputs={ + 'MAP': map_out, + 'AccumPosCount': accum_pos_count_out, + 'AccumTruePos': accum_true_pos_out, + 'AccumFalsePos': accum_false_pos_out + }, + attrs={ + 'overlap_threshold': overlap_threshold, + 'evaluate_difficult': evaluate_difficult, + 'ap_type': ap_type + }) + return map_out, accum_pos_count_out, accum_true_pos_out, accum_false_pos_out + + def bipartite_match(dist_matrix, name=None): """ **Bipartite matchint operator** diff --git a/python/paddle/fluid/tests/test_detection.py b/python/paddle/fluid/tests/test_detection.py index 1dc6d107d..fc2578649 100644 --- a/python/paddle/fluid/tests/test_detection.py +++ b/python/paddle/fluid/tests/test_detection.py @@ -145,5 +145,43 @@ class TestMultiBoxHead(unittest.TestCase): return mbox_locs, mbox_confs, box, var +class TestDetectionMAP(unittest.TestCase): + def test_detection_map(self): + program = Program() + with program_guard(program): + detect_res = layers.data( + name='detect_res', + shape=[10, 6], + append_batch_size=False, + dtype='float32') + label = layers.data( + name='label', + shape=[10, 6], + append_batch_size=False, + dtype='float32') + + map_out, accum_pos_count_out, accum_true_pos_out, accum_false_pos_out = layers.detection_map( + detect_res=detect_res, label=label) + self.assertIsNotNone(map_out) + self.assertIsNotNone(accum_pos_count_out) + self.assertIsNotNone(accum_true_pos_out) + self.assertIsNotNone(accum_false_pos_out) + self.assertEqual(map_out.shape, (1, )) + map_out, accum_pos_count_out2, accum_true_pos_out2, accum_false_pos_out2 = layers.detection_map( + detect_res=detect_res, label=label) + self.assertIsNotNone(map_out) + self.assertIsNotNone(accum_pos_count_out2) + self.assertIsNotNone(accum_true_pos_out2) + self.assertIsNotNone(accum_false_pos_out2) + self.assertEqual(map_out.shape, (1, )) + self.assertEqual(accum_pos_count_out.shape, + accum_pos_count_out2.shape) + self.assertEqual(accum_true_pos_out.shape, + accum_true_pos_out2.shape) + self.assertEqual(accum_false_pos_out.shape, + accum_false_pos_out2.shape) + print(str(program)) + + if __name__ == '__main__': unittest.main() -- GitLab