Skip to content

Commit 1a88baa

Browse files
jerrywgzqingqing01
authored andcommitted
add rpn_target_assign api test (#13013)
* Add unit test for rpn_target_assign API.
1 parent 03b1e4b commit 1a88baa

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

paddle/fluid/API.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ paddle.fluid.layers.target_assign ArgSpec(args=['input', 'matched_indices', 'neg
301301
paddle.fluid.layers.detection_output ArgSpec(args=['loc', 'scores', 'prior_box', 'prior_box_var', 'background_label', 'nms_threshold', 'nms_top_k', 'keep_top_k', 'score_threshold', 'nms_eta'], varargs=None, keywords=None, defaults=(0, 0.3, 400, 200, 0.01, 1.0))
302302
paddle.fluid.layers.ssd_loss ArgSpec(args=['location', 'confidence', 'gt_box', 'gt_label', 'prior_box', 'prior_box_var', 'background_label', 'overlap_threshold', 'neg_pos_ratio', 'neg_overlap', 'loc_loss_weight', 'conf_loss_weight', 'match_type', 'mining_type', 'normalize', 'sample_size'], varargs=None, keywords=None, defaults=(None, 0, 0.5, 3.0, 0.5, 1.0, 1.0, 'per_prediction', 'max_negative', True, None))
303303
paddle.fluid.layers.detection_map ArgSpec(args=['detect_res', 'label', 'class_num', 'background_label', 'overlap_threshold', 'evaluate_difficult', 'has_state', 'input_states', 'out_states', 'ap_version'], varargs=None, keywords=None, defaults=(0, 0.3, True, None, None, None, 'integral'))
304-
paddle.fluid.layers.rpn_target_assign ArgSpec(args=['loc', 'scores', 'anchor_box', 'gt_box', 'rpn_batch_size_per_im', 'fg_fraction', 'rpn_positive_overlap', 'rpn_negative_overlap'], varargs=None, keywords=None, defaults=(256, 0.25, 0.7, 0.3))
304+
paddle.fluid.layers.rpn_target_assign ArgSpec(args=['loc', 'scores', 'anchor_box', 'anchor_var', 'gt_box', 'rpn_batch_size_per_im', 'fg_fraction', 'rpn_positive_overlap', 'rpn_negative_overlap'], varargs=None, keywords=None, defaults=(256, 0.25, 0.7, 0.3))
305305
paddle.fluid.layers.anchor_generator ArgSpec(args=['input', 'anchor_sizes', 'aspect_ratios', 'variance', 'stride', 'offset', 'name'], varargs=None, keywords=None, defaults=(None, None, [0.1, 0.1, 0.2, 0.2], None, 0.5, None))
306306
paddle.fluid.layers.generate_proposal_labels ArgSpec(args=['rpn_rois', 'gt_classes', 'gt_boxes', 'im_scales', 'batch_size_per_im', 'fg_fraction', 'fg_thresh', 'bg_thresh_hi', 'bg_thresh_lo', 'bbox_reg_weights', 'class_nums'], varargs=None, keywords=None, defaults=(256, 0.25, 0.25, 0.5, 0.0, [0.1, 0.1, 0.2, 0.2], None))
307307
paddle.fluid.layers.generate_proposals ArgSpec(args=['scores', 'bbox_deltas', 'im_info', 'anchors', 'variances', 'pre_nms_top_n', 'post_nms_top_n', 'nms_thresh', 'min_size', 'eta', 'name'], varargs=None, keywords=None, defaults=(6000, 1000, 0.5, 0.1, 1.0, None))

python/paddle/fluid/layers/detection.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
def rpn_target_assign(loc,
5959
scores,
6060
anchor_box,
61+
anchor_var,
6162
gt_box,
6263
rpn_batch_size_per_im=256,
6364
fg_fraction=0.25,
@@ -96,6 +97,8 @@ class number, M is number of bounding boxes. For each category
9697
if the input is image feature map, they are close to the origin
9798
of the coordinate system. [xmax, ymax] is the right bottom
9899
coordinate of the anchor box.
100+
anchor_var(Variable): A 2-D Tensor with shape [M,4] holds expanded
101+
variances of anchors.
99102
gt_box (Variable): The ground-truth boudding boxes (bboxes) are a 2D
100103
LoDTensor with shape [Ng, 4], Ng is the total number of ground-truth
101104
bboxes of mini-batch input.
@@ -145,30 +148,29 @@ class number, M is number of bounding boxes. For each category
145148
# 1. Compute the regression target bboxes
146149
target_bbox = box_coder(
147150
prior_box=anchor_box,
151+
prior_box_var=anchor_var,
148152
target_box=gt_box,
149153
code_type='encode_center_size',
150154
box_normalized=False)
151-
152155
# 2. Compute overlaps between the prior boxes and the gt boxes overlaps
153156
iou = iou_similarity(x=gt_box, y=anchor_box)
154-
155157
# 3. Assign target label to anchors
156158
loc_index = helper.create_tmp_variable(dtype=anchor_box.dtype)
157159
score_index = helper.create_tmp_variable(dtype=anchor_box.dtype)
158160
target_label = helper.create_tmp_variable(dtype=anchor_box.dtype)
159161
helper.append_op(
160162
type="rpn_target_assign",
161-
inputs={'Overlap': iou, },
163+
inputs={'DistMat': iou},
162164
outputs={
163165
'LocationIndex': loc_index,
164166
'ScoreIndex': score_index,
165-
'TargetLabel': target_label,
167+
'TargetLabel': target_label
166168
},
167169
attrs={
168170
'rpn_batch_size_per_im': rpn_batch_size_per_im,
169171
'rpn_positive_overlap': rpn_positive_overlap,
170172
'rpn_negative_overlap': rpn_negative_overlap,
171-
'fg_fraction': fg_fraction,
173+
'fg_fraction': fg_fraction
172174
})
173175

174176
# 4. Reshape and gather the target entry
@@ -181,7 +183,7 @@ class number, M is number of bounding boxes. For each category
181183
predicted_location = nn.gather(loc, loc_index)
182184
target_label = nn.gather(target_label, score_index)
183185
target_bbox = nn.gather(target_bbox, loc_index)
184-
return predicted_scores, predicted_loc, target_label, target_bbox
186+
return predicted_scores, predicted_location, target_label, target_bbox
185187

186188

187189
def detection_output(loc,

python/paddle/fluid/tests/test_detection.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,59 @@ def test_detection_map(self):
250250
print(str(program))
251251

252252

253+
class TestRpnTargetAssign(unittest.TestCase):
254+
def test_rpn_target_assign(self):
255+
program = Program()
256+
with program_guard(program):
257+
loc_shape = [10, 50, 4]
258+
score_shape = [10, 50, 2]
259+
anchor_shape = [50, 4]
260+
261+
loc = layers.data(
262+
name='loc',
263+
shape=loc_shape,
264+
append_batch_size=False,
265+
dtype='float32')
266+
scores = layers.data(
267+
name='scores',
268+
shape=score_shape,
269+
append_batch_size=False,
270+
dtype='float32')
271+
anchor_box = layers.data(
272+
name='anchor_box',
273+
shape=anchor_shape,
274+
append_batch_size=False,
275+
dtype='float32')
276+
anchor_var = layers.data(
277+
name='anchor_var',
278+
shape=anchor_shape,
279+
append_batch_size=False,
280+
dtype='float32')
281+
gt_box = layers.data(
282+
name='gt_box', shape=[4], lod_level=1, dtype='float32')
283+
284+
predicted_scores, predicted_location, target_label, target_bbox = layers.rpn_target_assign(
285+
loc=loc,
286+
scores=scores,
287+
anchor_box=anchor_box,
288+
anchor_var=anchor_var,
289+
gt_box=gt_box,
290+
rpn_batch_size_per_im=256,
291+
fg_fraction=0.25,
292+
rpn_positive_overlap=0.7,
293+
rpn_negative_overlap=0.3)
294+
295+
self.assertIsNotNone(predicted_scores)
296+
self.assertIsNotNone(predicted_location)
297+
self.assertIsNotNone(target_label)
298+
self.assertIsNotNone(target_bbox)
299+
assert predicted_scores.shape[1] == 2
300+
assert predicted_location.shape[1] == 4
301+
assert predicted_location.shape[1] == target_bbox.shape[1]
302+
303+
print(str(program))
304+
305+
253306
class TestGenerateProposals(unittest.TestCase):
254307
def test_generate_proposals(self):
255308
data_shape = [20, 64, 64]

0 commit comments

Comments
 (0)