Example #1
0
    def test_assign_multidimensional_class_targets_rbox(self):
        similarity_calc = region_similarity_calculator.IouSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                               unmatched_threshold=0.3)
        box_coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder()
        unmatched_cls_target = tf.constant([[0, 0], [0, 0]], tf.float32)
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc,
            matcher,
            box_coder,
            unmatched_cls_target=unmatched_cls_target)

        anchors = tf.constant([[0.0, 0.0, 0.5, 0.5, 0.0],
                               [0.5, 0.5, 1.0, 0.8,
                                0.0], [0, 0.5, .5, 1.0, 0.0],
                               [.75, 0, 1.0, .25, 0.0]])
        anchors_rbox = rbox_list.RBoxList(anchors)

        box_corners = [[0.0, 0.0, 0.5, 0.5, 0.0], [0.5, 0.5, 0.9, 0.9, 0.0],
                       [.75, 0, .95, .27, 0.0]]
        rboxes = rbox_list.RBoxList(tf.constant(box_corners))

        groundtruth_labels = tf.constant(
            [[[0, 1], [1, 0]], [[1, 0], [0, 1]], [[0, 1], [1, .5]]],
            tf.float32)

        exp_cls_targets = [[[0, 1], [1, 0]], [[1, 0], [0, 1]], [[0, 0], [0,
                                                                         0]],
                           [[0, 1], [1, .5]]]
        exp_cls_weights = [1, 1, 1, 1]
        exp_reg_targets = [[0, 0, 0, 0, 0], [0, 0, -0.105361, 0.117783, 0],
                           [0, 0, 0, 0, 0], [0, 0, -0.0512933, .0769611, 0]]
        exp_reg_weights = [1, 1, 0, 1]
        exp_matching_anchors = [0, 1, 3]

        result = target_assigner.assign(anchors_rbox, rboxes,
                                        groundtruth_labels)
        (cls_targets, cls_weights, reg_targets, reg_weights, match) = result
        with self.test_session() as sess:
            (cls_targets_out, cls_weights_out, reg_targets_out, reg_weights_out, matching_anchors_out) = \
                sess.run([cls_targets, cls_weights, reg_targets, reg_weights, match.matched_column_indices()])

            self.assertAllClose(cls_targets_out, exp_cls_targets)
            self.assertAllClose(cls_weights_out, exp_cls_weights)
            self.assertAllClose(reg_targets_out, exp_reg_targets)
            self.assertAllClose(reg_weights_out, exp_reg_weights)
            self.assertAllClose(matching_anchors_out, exp_matching_anchors)
            self.assertEquals(cls_targets_out.dtype, np.float32)
            self.assertEquals(cls_weights_out.dtype, np.float32)
            self.assertEquals(reg_targets_out.dtype, np.float32)
            self.assertEquals(reg_weights_out.dtype, np.float32)
            self.assertEquals(matching_anchors_out.dtype, np.int32)
    def test_get_correct_boxes_after_decoding(self):
        anchors = [[15.0, 12.0, 30.0, 18.0, 0.15], [0.1, 0.0, 0.7, 0.9, 0.1]]
        rel_codes = [[-0.154833, -0.151374, -0.405465, -0.182322, -0.05],
                     [0.127882, 0.121649, -0.336472, -0.810930, 0.1]]
        expected_boxes = [[10.0, 10.0, 20.0, 15.0, 0.07146],
                          [0.2, 0.1, 0.5, 0.4, 0.25708]]

        anchors = rbox_list.RBoxList(tf.constant(anchors))
        coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder()
        boxes = coder.decode(rel_codes, anchors)
        with self.test_session() as sess:
            boxes_out, = sess.run([boxes.get()])
            self.assertAllClose(boxes_out, expected_boxes)
Example #3
0
 def _get_multi_dimensional_target_assigner_rbox(self, target_dimensions):
     similarity_calc = region_similarity_calculator.IouSimilarity()
     matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                            unmatched_threshold=0.3)
     box_coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder()
     unmatched_cls_target = tf.constant(np.zeros(target_dimensions),
                                        tf.float32)
     return targetassigner.TargetAssigner(
         similarity_calc,
         matcher,
         box_coder,
         positive_class_weight=1.0,
         negative_class_weight=1.0,
         unmatched_cls_target=unmatched_cls_target)
    def test_get_correct_relative_codes_after_encoding(self):
        boxes = [[10.0, 10.0, 20.0, 15.0, 0.1], [0.2, 0.1, 0.5, 0.4, 0.2]]
        anchors = [[15.0, 12.0, 30.0, 18.0, 0.15], [0.1, 0.0, 0.7, 0.9, 0.1]]
        expected_rel_codes = [[
            -0.154833, -0.151374, -0.405465, -0.182322, -0.031831
        ], [0.127882, 0.121649, -0.336472, -0.810930, 0.063662]]

        boxes = rbox_list.RBoxList(tf.constant(boxes))
        anchors = rbox_list.RBoxList(tf.constant(anchors))
        coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder()
        rel_codes = coder.encode(boxes, anchors)
        with self.test_session() as sess:
            rel_codes_out, = sess.run([rel_codes])
            self.assertAllClose(rel_codes_out, expected_rel_codes)
    def test_get_correct_boxes_after_decoding_with_scaling(self):
        anchors = [[15.0, 12.0, 30.0, 18.0, 0.15], [0.1, 0.0, 0.7, 0.9, 0.1]]
        rel_codes = [[-0.309665, -0.454122, -1.621860, -0.911607, -0.3],
                     [0.2557630, 0.364945, -1.345888, -4.054651, 0.6]]
        scale_factors = [2, 3, 4, 5, 6]
        expected_boxes = [[10.0, 10.0, 20.0, 15.0, 0.07146],
                          [0.2, 0.1, 0.5, 0.4, 0.25708]]

        anchors = rbox_list.RBoxList(tf.constant(anchors))
        coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder(
            scale_factors=scale_factors)
        boxes = coder.decode(rel_codes, anchors)
        with self.test_session() as sess:
            boxes_out, = sess.run([boxes.get()])
            self.assertAllClose(boxes_out, expected_boxes)
def build(box_coder_config):
    """Builds a box coder object based on the box coder config.

    Args:
      box_coder_config: A box_coder.proto object containing the config for the
        desired box coder.

    Returns:
      BoxCoder based on the config.

    Raises:
      ValueError: On empty box coder proto.
    """
    if not isinstance(box_coder_config, box_coder_pb2.BoxCoder):
        raise ValueError(
            'box_coder_config not of type box_coder_pb2.BoxCoder.')

    if box_coder_config.WhichOneof(
            'box_coder_oneof') == 'faster_rcnn_box_coder':
        return faster_rcnn_box_coder.FasterRcnnBoxCoder(scale_factors=[
            box_coder_config.faster_rcnn_box_coder.y_scale,
            box_coder_config.faster_rcnn_box_coder.x_scale,
            box_coder_config.faster_rcnn_box_coder.height_scale,
            box_coder_config.faster_rcnn_box_coder.width_scale
        ])
    if (box_coder_config.WhichOneof('box_coder_oneof') ==
            'mean_stddev_box_coder'):
        return mean_stddev_box_coder.MeanStddevBoxCoder()
    if box_coder_config.WhichOneof('box_coder_oneof') == 'square_box_coder':
        return square_box_coder.SquareBoxCoder(scale_factors=[
            box_coder_config.square_box_coder.y_scale,
            box_coder_config.square_box_coder.x_scale,
            box_coder_config.square_box_coder.length_scale
        ])
    if box_coder_config.WhichOneof(
            'box_coder_oneof') == 'faster_rcnn_rbox_coder':
        return faster_rcnn_rbox_coder.FasterRcnnRBoxCoder(scale_factors=[
            box_coder_config.faster_rcnn_rbox_coder.y_scale,
            box_coder_config.faster_rcnn_rbox_coder.x_scale,
            box_coder_config.faster_rcnn_rbox_coder.height_scale,
            box_coder_config.faster_rcnn_rbox_coder.width_scale,
            box_coder_config.faster_rcnn_rbox_coder.angle_scale
        ])
    raise ValueError('Empty box coder.')
Example #7
0
def create_target_assigner(reference,
                           stage=None,
                           positive_class_weight=1.0,
                           negative_class_weight=1.0,
                           unmatched_cls_target=None):
    """Factory function for creating standard target assigners.

    Args:
      reference: string referencing the type of TargetAssigner.
      stage: string denoting stage: {proposal, detection}.
      positive_class_weight: classification weight to be associated to positive
        anchors (default: 1.0)
      negative_class_weight: classification weight to be associated to negative
        anchors (default: 1.0)
      unmatched_cls_target: a float32 tensor with shape [d_1, d_2, ..., d_k]
        which is consistent with the classification target for each
        anchor (and can be empty for scalar targets).  This shape must thus be
        compatible with the groundtruth labels that are passed to the Assign
        function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]).
        If set to None, unmatched_cls_target is set to be 0 for each anchor.

    Returns:
      TargetAssigner: desired target assigner.

    Raises:
      ValueError: if combination reference+stage is invalid.
    """
    if reference == 'Multibox' and stage == 'proposal':
        similarity_calc = sim_calc.NegSqDistSimilarity()
        matcher = bipartite_matcher.GreedyBipartiteMatcher()
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()

    elif reference == 'FasterRCNN' and stage == 'proposal':
        similarity_calc = sim_calc.IouSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.7,
                                               unmatched_threshold=0.3,
                                               force_match_for_each_row=True)
        box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder(
            scale_factors=[10.0, 10.0, 5.0, 5.0])

    elif reference == 'FasterRCNN' and stage == 'detection':
        similarity_calc = sim_calc.IouSimilarity()
        # Uses all proposals with IOU < 0.5 as candidate negatives.
        matcher = argmax_matcher.ArgMaxMatcher(
            matched_threshold=0.5, negatives_lower_than_unmatched=True)
        box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder(
            scale_factors=[10.0, 10.0, 5.0, 5.0])

    elif reference == 'FastRCNN':
        similarity_calc = sim_calc.IouSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(
            matched_threshold=0.5,
            unmatched_threshold=0.1,
            force_match_for_each_row=False,
            negatives_lower_than_unmatched=False)
        box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder()

    elif reference == 'RFasterRCNN' and stage == 'proposal':
        similarity_calc = sim_calc.IouSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.6,
                                               unmatched_threshold=0.3,
                                               force_match_for_each_row=True,
                                               ignore_sim_less_than_zero=True)
        box_coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder(
            scale_factors=[10.0, 10.0, 5.0, 5.0, 5.0])

    elif reference == 'RFasterRCNN' and stage == 'detection':
        similarity_calc = sim_calc.IouSimilarity()
        # Uses all proposals with IOU < 0.5 as candidate negatives.
        matcher = argmax_matcher.ArgMaxMatcher(
            matched_threshold=0.4,
            negatives_lower_than_unmatched=True,
            ignore_sim_less_than_zero=True)
        box_coder = faster_rcnn_rbox_coder.FasterRcnnRBoxCoder(
            scale_factors=[10.0, 10.0, 5.0, 5.0, 5.0])
    else:
        raise ValueError('No valid combination of reference and stage.')

    return TargetAssigner(similarity_calc,
                          matcher,
                          box_coder,
                          positive_class_weight=positive_class_weight,
                          negative_class_weight=negative_class_weight,
                          unmatched_cls_target=unmatched_cls_target)