Ejemplo n.º 1
0
 def _get_target_assigner(self):
     similarity_calc = region_similarity_calculator.IouSimilarity()
     matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                            unmatched_threshold=0.5)
     box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
     return targetassigner.TargetAssigner(similarity_calc, matcher,
                                          box_coder)
Ejemplo n.º 2
0
def create_target_assigner(reference, stage=None,
                           negative_class_weight=1.0, use_matmul_gather=False):
    """Factory function for creating standard target assigners.

  Args:
    reference: string referencing the type of TargetAssigner.
    stage: string denoting stage: {proposal, detection}.
    negative_class_weight: classification weight to be associated to negative
      anchors (default: 1.0)
    use_matmul_gather: whether to use matrix multiplication based gather which
      are better suited for TPUs.

  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,
                                               use_matmul_gather=use_matmul_gather)
        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,
                                               use_matmul_gather=use_matmul_gather)
        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,
                                               use_matmul_gather=use_matmul_gather)
        box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder()

    else:
        raise ValueError('No valid combination of reference and stage.')

    return TargetAssigner(similarity_calc, matcher, box_coder,
                          negative_class_weight=negative_class_weight)
Ejemplo n.º 3
0
    def testGetCorrectRelativeCodesAfterEncoding(self):
        box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.5]]
        boxes = box_list.BoxList(tf.constant(box_corners))
        expected_rel_codes = [[0.0, 0.0, 0.0, 0.0], [-5.0, -5.0, -5.0, -3.0]]
        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8]])
        priors = box_list.BoxList(prior_means)

        coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
        rel_codes = coder.encode(boxes, priors)
        with self.test_session() as sess:
            rel_codes_out = sess.run(rel_codes)
            self.assertAllClose(rel_codes_out, expected_rel_codes)
Ejemplo n.º 4
0
    def testGetCorrectBoxesAfterDecoding(self):
        rel_codes = tf.constant([[0.0, 0.0, 0.0, 0.0],
                                 [-5.0, -5.0, -5.0, -3.0]])
        expected_box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.5]]
        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8]])
        priors = box_list.BoxList(prior_means)

        coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
        decoded_boxes = coder.decode(rel_codes, priors)
        decoded_box_corners = decoded_boxes.get()
        with self.test_session() as sess:
            decoded_out = sess.run(decoded_box_corners)
            self.assertAllClose(decoded_out, expected_box_corners)
Ejemplo n.º 5
0
 def graph_fn(anchor_means, groundtruth_box_corners):
     similarity_calc = region_similarity_calculator.IouSimilarity()
     matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                            unmatched_threshold=0.3)
     box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
     target_assigner = targetassigner.TargetAssigner(
         similarity_calc, matcher, box_coder)
     anchors_boxlist = box_list.BoxList(anchor_means)
     groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
     result = target_assigner.assign(anchors_boxlist,
                                     groundtruth_boxlist,
                                     unmatched_class_label=None)
     (cls_targets, cls_weights, reg_targets, reg_weights, _) = result
     return (cls_targets, cls_weights, reg_targets, reg_weights)
Ejemplo n.º 6
0
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') == 'keypoint_box_coder':
        return keypoint_box_coder.KeypointBoxCoder(
            box_coder_config.keypoint_box_coder.num_keypoints,
            scale_factors=[
                box_coder_config.keypoint_box_coder.y_scale,
                box_coder_config.keypoint_box_coder.x_scale,
                box_coder_config.keypoint_box_coder.height_scale,
                box_coder_config.keypoint_box_coder.width_scale
            ])
    if (box_coder_config.WhichOneof('box_coder_oneof') ==
            'mean_stddev_box_coder'):
        return mean_stddev_box_coder.MeanStddevBoxCoder(
            stddev=box_coder_config.mean_stddev_box_coder.stddev)
    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
        ])
    raise ValueError('Empty box coder.')
Ejemplo n.º 7
0
        def graph_fn(anchor_means, groundtruth_box_corners, groundtruth_labels,
                     groundtruth_weights):
            similarity_calc = region_similarity_calculator.IouSimilarity()
            matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                                   unmatched_threshold=0.5)
            box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
            unmatched_class_label = tf.constant([1, 0, 0, 0, 0, 0, 0],
                                                tf.float32)
            target_assigner = targetassigner.TargetAssigner(
                similarity_calc, matcher, box_coder)

            anchors_boxlist = box_list.BoxList(anchor_means)
            groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
            result = target_assigner.assign(
                anchors_boxlist,
                groundtruth_boxlist,
                groundtruth_labels,
                unmatched_class_label=unmatched_class_label,
                groundtruth_weights=groundtruth_weights)
            (_, cls_weights, _, reg_weights, _) = result
            return (cls_weights, reg_weights)
Ejemplo n.º 8
0
    def test_raises_error_on_invalid_groundtruth_labels(self):
        similarity_calc = region_similarity_calculator.NegSqDistSimilarity()
        matcher = bipartite_matcher.GreedyBipartiteMatcher()
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=1.0)
        unmatched_class_label = tf.constant([[0, 0], [0, 0], [0, 0]],
                                            tf.float32)
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc, matcher, box_coder)

        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5]])
        priors = box_list.BoxList(prior_means)

        box_corners = [[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.9, 0.9],
                       [.75, 0, .95, .27]]
        boxes = box_list.BoxList(tf.constant(box_corners))
        groundtruth_labels = tf.constant([[[0, 1], [1, 0]]], tf.float32)

        with self.assertRaises(ValueError):
            target_assigner.assign(priors,
                                   boxes,
                                   groundtruth_labels,
                                   unmatched_class_label=unmatched_class_label)
Ejemplo n.º 9
0
def create_target_assigner(reference,
                           stage=None,
                           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}.
    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()

    else:
        raise ValueError('No valid combination of reference and stage.')

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