From 1a88baae228a561c8347ee2be31716d38de63840 Mon Sep 17 00:00:00 2001 From: jerrywgz Date: Thu, 30 Aug 2018 21:45:17 +0800 Subject: [PATCH] add rpn_target_assign api test (#13013) * Add unit test for rpn_target_assign API. --- paddle/fluid/API.spec | 2 +- python/paddle/fluid/layers/detection.py | 14 +++--- python/paddle/fluid/tests/test_detection.py | 53 +++++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 46726ab4945..ff443e82ae7 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -301,7 +301,7 @@ paddle.fluid.layers.target_assign ArgSpec(args=['input', 'matched_indices', 'neg 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)) 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)) 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')) -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)) +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)) 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)) 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)) 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)) diff --git a/python/paddle/fluid/layers/detection.py b/python/paddle/fluid/layers/detection.py index 8bb161495ba..5757b2798e4 100644 --- a/python/paddle/fluid/layers/detection.py +++ b/python/paddle/fluid/layers/detection.py @@ -58,6 +58,7 @@ for _OP in set(__auto__): def rpn_target_assign(loc, scores, anchor_box, + anchor_var, gt_box, rpn_batch_size_per_im=256, fg_fraction=0.25, @@ -96,6 +97,8 @@ def rpn_target_assign(loc, if the input is image feature map, they are close to the origin of the coordinate system. [xmax, ymax] is the right bottom coordinate of the anchor box. + anchor_var(Variable): A 2-D Tensor with shape [M,4] holds expanded + variances of anchors. gt_box (Variable): The ground-truth boudding boxes (bboxes) are a 2D LoDTensor with shape [Ng, 4], Ng is the total number of ground-truth bboxes of mini-batch input. @@ -145,30 +148,29 @@ def rpn_target_assign(loc, # 1. Compute the regression target bboxes target_bbox = box_coder( prior_box=anchor_box, + prior_box_var=anchor_var, target_box=gt_box, code_type='encode_center_size', box_normalized=False) - # 2. Compute overlaps between the prior boxes and the gt boxes overlaps iou = iou_similarity(x=gt_box, y=anchor_box) - # 3. Assign target label to anchors loc_index = helper.create_tmp_variable(dtype=anchor_box.dtype) score_index = helper.create_tmp_variable(dtype=anchor_box.dtype) target_label = helper.create_tmp_variable(dtype=anchor_box.dtype) helper.append_op( type="rpn_target_assign", - inputs={'Overlap': iou, }, + inputs={'DistMat': iou}, outputs={ 'LocationIndex': loc_index, 'ScoreIndex': score_index, - 'TargetLabel': target_label, + 'TargetLabel': target_label }, attrs={ 'rpn_batch_size_per_im': rpn_batch_size_per_im, 'rpn_positive_overlap': rpn_positive_overlap, 'rpn_negative_overlap': rpn_negative_overlap, - 'fg_fraction': fg_fraction, + 'fg_fraction': fg_fraction }) # 4. Reshape and gather the target entry @@ -181,7 +183,7 @@ def rpn_target_assign(loc, predicted_location = nn.gather(loc, loc_index) target_label = nn.gather(target_label, score_index) target_bbox = nn.gather(target_bbox, loc_index) - return predicted_scores, predicted_loc, target_label, target_bbox + return predicted_scores, predicted_location, target_label, target_bbox def detection_output(loc, diff --git a/python/paddle/fluid/tests/test_detection.py b/python/paddle/fluid/tests/test_detection.py index 9cbd8b68666..ec0bf3ff8d6 100644 --- a/python/paddle/fluid/tests/test_detection.py +++ b/python/paddle/fluid/tests/test_detection.py @@ -250,6 +250,59 @@ class TestDetectionMAP(unittest.TestCase): print(str(program)) +class TestRpnTargetAssign(unittest.TestCase): + def test_rpn_target_assign(self): + program = Program() + with program_guard(program): + loc_shape = [10, 50, 4] + score_shape = [10, 50, 2] + anchor_shape = [50, 4] + + loc = layers.data( + name='loc', + shape=loc_shape, + append_batch_size=False, + dtype='float32') + scores = layers.data( + name='scores', + shape=score_shape, + append_batch_size=False, + dtype='float32') + anchor_box = layers.data( + name='anchor_box', + shape=anchor_shape, + append_batch_size=False, + dtype='float32') + anchor_var = layers.data( + name='anchor_var', + shape=anchor_shape, + append_batch_size=False, + dtype='float32') + gt_box = layers.data( + name='gt_box', shape=[4], lod_level=1, dtype='float32') + + predicted_scores, predicted_location, target_label, target_bbox = layers.rpn_target_assign( + loc=loc, + scores=scores, + anchor_box=anchor_box, + anchor_var=anchor_var, + gt_box=gt_box, + rpn_batch_size_per_im=256, + fg_fraction=0.25, + rpn_positive_overlap=0.7, + rpn_negative_overlap=0.3) + + self.assertIsNotNone(predicted_scores) + self.assertIsNotNone(predicted_location) + self.assertIsNotNone(target_label) + self.assertIsNotNone(target_bbox) + assert predicted_scores.shape[1] == 2 + assert predicted_location.shape[1] == 4 + assert predicted_location.shape[1] == target_bbox.shape[1] + + print(str(program)) + + class TestGenerateProposals(unittest.TestCase): def test_generate_proposals(self): data_shape = [20, 64, 64] -- GitLab