def test_return_correct_matches_with_top_k_anchor_matcher(self):
        similarity = np.array([[
            0.0, 0.26110054, 0.5278733, 0.14728792, 0.9657118, 0.32553806,
            0.29333659, 0.17396439, 0.44223007, 0.91810811
        ],
                               [
                                   0.0, 0.84968435, 0.15998602, 0.07703288,
                                   0.06165979, 0.70155799, 0.50643075,
                                   0.06866041, 0.91013184, 0.79128903
                               ],
                               [
                                   0.31, 0.36826426, 0.75763793, 0.27401928,
                                   0.46727863, 0.85160311, 0.26166277,
                                   0.45111642, 0.05999146, 0.88036637
                               ]])

        matcher = top_k_anchor_matcher.TopKAnchorMatcher(number_of_top_k=1)
        expected_match_results = [0, 1, 2, -1, 0, 2, 1, 2, 1, 0]

        sim = tf.constant(similarity, dtype=tf.float32)
        match = matcher.match(sim)
        match_results = match.match_results

        init_op = tf.global_variables_initializer()

        with self.test_session() as sess:
            sess.run(init_op)
            res_match_results = sess.run(match_results)

        self.assertAllEqual(res_match_results, expected_match_results)
    def test_return_correct_matches_with_default_thresholds(self):
        similarity = np.array([[
            0.29310609, 0.26110054, 0.5278733, 0.14728792, 0.9657118,
            0.32553806, 0.29333659, 0.17396439, 0.44223007, 0.91810811
        ],
                               [
                                   0.19628839, 0.64968435, 0.15998602,
                                   0.07703288, 0.06165979, 0.70155799,
                                   0.50643075, 0.06866041, 0.91013184,
                                   0.89128903
                               ],
                               [
                                   0.03621593, 0.36826426, 0.75763793,
                                   0.27401928, 0.46727863, 0.85160311,
                                   0.26166277, 0.45111642, 0.05999146,
                                   0.08036637
                               ]])

        matcher = top_k_anchor_matcher.TopKAnchorMatcher()
        expected_match_results = [-1, 1, 2, -1, 0, 2, -2, -2, 1, 0]
        expected_matched_cols = np.array([1, 2, 4, 5, 8, 9])

        sim = tf.constant(similarity, dtype=tf.float32)
        match = matcher.match(sim)
        matched_cols = match.matched_column_indices()
        match_results = match.match_results

        with self.test_session() as sess:
            res_matched_cols = sess.run(matched_cols)
            res_match_results = sess.run(match_results)

        self.assertAllEqual(res_matched_cols, expected_matched_cols)
        self.assertAllEqual(res_match_results, expected_match_results)
    def test_return_correct_matches_with_empty_rows(self):

        matcher = top_k_anchor_matcher.TopKAnchorMatcher()
        expected_match_results = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
        sim = 0.2 * tf.ones([0, 10])
        match = matcher.match(sim)
        unmatched_cols = match.unmatched_column_indices()
        match_results = match.match_results

        with self.test_session() as sess:
            res_unmatched_cols = sess.run(unmatched_cols)
            res_match_results = sess.run(match_results)

        self.assertAllEqual(res_unmatched_cols, np.arange(10))
        self.assertAllEqual(res_match_results, expected_match_results)
Ejemplo n.º 4
0
def build(matcher_config):
  """Builds a matcher object based on the matcher config.

  Args:
    matcher_config: A matcher.proto object containing the config for the desired
      Matcher.

  Returns:
    Matcher based on the config.

  Raises:
    ValueError: On empty matcher proto.
  """
  if not isinstance(matcher_config, matcher_pb2.Matcher):
    raise ValueError('matcher_config not of type matcher_pb2.Matcher.')
  if matcher_config.WhichOneof('matcher_oneof') == 'argmax_matcher':
    matcher = matcher_config.argmax_matcher
    matched_threshold = unmatched_threshold = None
    if not matcher.ignore_thresholds:
      matched_threshold = matcher.matched_threshold
      unmatched_threshold = matcher.unmatched_threshold
    return argmax_matcher.ArgMaxMatcher(
        matched_threshold=matched_threshold,
        unmatched_threshold=unmatched_threshold,
        negatives_lower_than_unmatched=matcher.negatives_lower_than_unmatched,
        force_match_for_each_row=matcher.force_match_for_each_row)
  if matcher_config.WhichOneof('matcher_oneof') == 'bipartite_matcher':
    return bipartite_matcher.GreedyBipartiteMatcher()
  if matcher_config.WhichOneof('matcher_oneof') == 'top_k_anchor_matcher':
    matcher = matcher_config.top_k_anchor_matcher
    number_of_top_k = matcher.number_of_top_k
    unmatched_threshold = matcher.unmatched_threshold
    return top_k_anchor_matcher.TopKAnchorMatcher(
        number_of_top_k=number_of_top_k,
        unmatched_threshold=unmatched_threshold)
  raise ValueError('Empty matcher.')
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':
        print("GGGGGGGGGGGGGGGGGGGGGGGG")
        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':
        print("gggggggggggggggggggggggg")
        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 == 'FasterRCNN' and stage == 'proposal51':
        print("FFFFFFFFFFFFFFFFFFFFFFF")
        similarity_calc = sim_calc.IouSimilarity()
        matcher = top_k_anchor_matcher.TopKAnchorMatcher(
            number_of_top_k=5, unmatched_threshold=0.3)
        box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder(
            scale_factors=[10.0, 10.0, 5.0, 5.0])

    elif reference == 'FasterRCNN' and stage == 'detection51':
        print("fffffffffffffffffffffffffff")
        similarity_calc = sim_calc.IouSimilarity()
        # Uses all proposals with IOU < 0.5 as candidate negatives.
        matcher = top_k_anchor_matcher.TopKAnchorMatcher(
            number_of_top_k=1, unmatched_threshold=0.5)
        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,
                          positive_class_weight=positive_class_weight,
                          negative_class_weight=negative_class_weight,
                          unmatched_cls_target=unmatched_cls_target)