Skip to content

Commit ec33832

Browse files
authored
Merge pull request #8577 from wanghaox/python_map
add warrper for detection map operator
2 parents daa8ed3 + 52c5285 commit ec33832

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

python/paddle/fluid/layers/detection.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
from layer_function_generator import generate_layer_fn
19+
from layer_function_generator import autodoc
1920
from ..layer_helper import LayerHelper
2021
import tensor
2122
import ops
@@ -28,6 +29,7 @@
2829
'target_assign',
2930
'detection_output',
3031
'ssd_loss',
32+
'detection_map',
3133
]
3234

3335
__auto__ = [
@@ -132,6 +134,44 @@ class number, M is number of bounding boxes. For each category
132134
return nmsed_outs
133135

134136

137+
@autodoc()
138+
def detection_map(detect_res,
139+
label,
140+
pos_count=None,
141+
true_pos=None,
142+
false_pos=None,
143+
overlap_threshold=0.3,
144+
evaluate_difficult=True,
145+
ap_type='integral'):
146+
helper = LayerHelper("detection_map", **locals())
147+
148+
map_out = helper.create_tmp_variable(dtype='float32')
149+
accum_pos_count_out = helper.create_tmp_variable(dtype='int32')
150+
accum_true_pos_out = helper.create_tmp_variable(dtype='float32')
151+
accum_false_pos_out = helper.create_tmp_variable(dtype='float32')
152+
helper.append_op(
153+
type="detection_map",
154+
inputs={
155+
'Label': label,
156+
'DetectRes': detect_res,
157+
'PosCount': pos_count,
158+
'TruePos': true_pos,
159+
'FalsePos': false_pos
160+
},
161+
outputs={
162+
'MAP': map_out,
163+
'AccumPosCount': accum_pos_count_out,
164+
'AccumTruePos': accum_true_pos_out,
165+
'AccumFalsePos': accum_false_pos_out
166+
},
167+
attrs={
168+
'overlap_threshold': overlap_threshold,
169+
'evaluate_difficult': evaluate_difficult,
170+
'ap_type': ap_type
171+
})
172+
return map_out, accum_pos_count_out, accum_true_pos_out, accum_false_pos_out
173+
174+
135175
def bipartite_match(dist_matrix,
136176
match_type=None,
137177
dist_threshold=None,

python/paddle/fluid/tests/test_detection.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,43 @@ def multi_box_head_output(self, data_shape):
145145
return mbox_locs, mbox_confs, box, var
146146

147147

148+
class TestDetectionMAP(unittest.TestCase):
149+
def test_detection_map(self):
150+
program = Program()
151+
with program_guard(program):
152+
detect_res = layers.data(
153+
name='detect_res',
154+
shape=[10, 6],
155+
append_batch_size=False,
156+
dtype='float32')
157+
label = layers.data(
158+
name='label',
159+
shape=[10, 6],
160+
append_batch_size=False,
161+
dtype='float32')
162+
163+
map_out, accum_pos_count_out, accum_true_pos_out, accum_false_pos_out = layers.detection_map(
164+
detect_res=detect_res, label=label)
165+
self.assertIsNotNone(map_out)
166+
self.assertIsNotNone(accum_pos_count_out)
167+
self.assertIsNotNone(accum_true_pos_out)
168+
self.assertIsNotNone(accum_false_pos_out)
169+
self.assertEqual(map_out.shape, (1, ))
170+
map_out, accum_pos_count_out2, accum_true_pos_out2, accum_false_pos_out2 = layers.detection_map(
171+
detect_res=detect_res, label=label)
172+
self.assertIsNotNone(map_out)
173+
self.assertIsNotNone(accum_pos_count_out2)
174+
self.assertIsNotNone(accum_true_pos_out2)
175+
self.assertIsNotNone(accum_false_pos_out2)
176+
self.assertEqual(map_out.shape, (1, ))
177+
self.assertEqual(accum_pos_count_out.shape,
178+
accum_pos_count_out2.shape)
179+
self.assertEqual(accum_true_pos_out.shape,
180+
accum_true_pos_out2.shape)
181+
self.assertEqual(accum_false_pos_out.shape,
182+
accum_false_pos_out2.shape)
183+
print(str(program))
184+
185+
148186
if __name__ == '__main__':
149187
unittest.main()

0 commit comments

Comments
 (0)