Пример #1
0
    def test_raises_error_on_incompatible_groundtruth_boxes_and_labels(self):
        similarity_calc = region_similarity_calculator.NegSqDistSimilarity()
        matcher = bipartite_matcher.GreedyBipartiteMatcher()
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
        unmatched_class_label = tf.constant([1, 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], [0.5, 0.5, 1.0, 0.8],
                                   [0, 0.5, .5, 1.0], [.75, 0, 1.0, .25]])
        priors = box_list.BoxList(prior_means)

        box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.8],
                       [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, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0],
             [0, 0, 0, 1, 0, 0, 0]], tf.float32)
        with self.assertRaisesRegexp(ValueError, 'Unequal shapes'):
            target_assigner.assign(priors,
                                   boxes,
                                   groundtruth_labels,
                                   unmatched_class_label=unmatched_class_label,
                                   num_valid_rows=3)
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
        ])
    raise ValueError('Empty box coder.')
Пример #3
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)
Пример #4
0
def build(box_coder_config):

    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.')
Пример #5
0
    def test_raises_error_on_incompatible_groundtruth_boxes_and_labels(self):
        similarity_calc = region_similarity_calculator.NegSqDistSimilarity()
        matcher = bipartite_matcher.GreedyBipartiteMatcher()
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
        unmatched_cls_target = tf.constant([1, 0, 0, 0, 0, 0, 0], tf.float32)
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc,
            matcher,
            box_coder,
            unmatched_cls_target=unmatched_cls_target)

        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8],
                                   [0, 0.5, .5, 1.0], [.75, 0, 1.0, .25]])
        prior_stddevs = tf.constant(4 * [4 * [.1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.8],
                       [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, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0],
             [0, 0, 0, 1, 0, 0, 0]], tf.float32)
        result = target_assigner.assign(priors,
                                        boxes,
                                        groundtruth_labels,
                                        num_valid_rows=3)
        (cls_targets, cls_weights, reg_targets, reg_weights, _) = result
        with self.test_session() as sess:
            with self.assertRaisesWithPredicateMatch(
                    tf.errors.InvalidArgumentError,
                    'Groundtruth boxes and labels have incompatible shapes!'):
                sess.run([cls_targets, cls_weights, reg_targets, reg_weights])
    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()
        unmatched_cls_target = tf.constant([[0, 0], [0, 0], [0, 0]],
                                           tf.float32)
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc,
            matcher,
            box_coder,
            unmatched_cls_target=unmatched_cls_target)

        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5]])
        prior_stddevs = tf.constant([[1.0, 1.0, 1.0, 1.0]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        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,
                                   num_valid_rows=3)
    def test_assign_multiclass_unequal_class_weights(self):
        similarity_calc = region_similarity_calculator.NegSqDistSimilarity()
        matcher = bipartite_matcher.GreedyBipartiteMatcher()
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
        unmatched_cls_target = tf.constant([1, 0, 0, 0, 0, 0, 0], tf.float32)
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc, matcher, box_coder,
            positive_class_weight=1.0, negative_class_weight=0.5,
            unmatched_cls_target=unmatched_cls_target)

        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5],
                                   [0.5, 0.5, 1.0, 0.8],
                                   [0, 0.5, .5, 1.0],
                                   [.75, 0, 1.0, .25]])
        prior_stddevs = tf.constant(4 * [4 * [.1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        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, 0, 0, 0, 0, 0],
                                          [0, 0, 0, 0, 0, 1, 0],
                                          [0, 0, 0, 1, 0, 0, 0]], tf.float32)

        exp_cls_weights = [1, 1, .5, 1]
        result = target_assigner.assign(priors, boxes, groundtruth_labels,
                                        num_valid_rows=3)
        (_, cls_weights, _, _, _) = result
        with self.test_session() as sess:
            cls_weights_out = sess.run(cls_weights)
            self.assertAllClose(cls_weights_out, exp_cls_weights)
Пример #8
0
 def create_target_assigner(self, iou_threshold):
     similarity_calc = region_similarity_calculator.IouSimilarity()
     matcher = argmax_matcher.ArgMaxMatcher(
         matched_threshold=iou_threshold, unmatched_threshold=iou_threshold)
     box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
     assigner = target_assigner.TargetAssigner(similarity_calc, matcher,
                                               box_coder)
     return assigner
Пример #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)
 def _get_agnostic_target_assigner(self):
     similarity_calc = region_similarity_calculator.NegSqDistSimilarity()
     matcher = bipartite_matcher.GreedyBipartiteMatcher()
     box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
     return targetassigner.TargetAssigner(
         similarity_calc, matcher, box_coder,
         positive_class_weight=1.0,
         negative_class_weight=1.0,
         unmatched_cls_target=None)
    def test_assign_multidimensional_class_targets(self):
        similarity_calc = region_similarity_calculator.NegSqDistSimilarity()
        matcher = bipartite_matcher.GreedyBipartiteMatcher()
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
        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)

        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5],
                                   [0.5, 0.5, 1.0, 0.8],
                                   [0, 0.5, .5, 1.0],
                                   [.75, 0, 1.0, .25]])
        prior_stddevs = tf.constant(4 * [4 * [.1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        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]],
                                          [[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, -1, 1],
                           [0, 0, 0, 0],
                           [0, 0, -.5, .2]]
        exp_reg_weights = [1, 1, 0, 1]
        exp_matching_anchors = [0, 1, 3]

        result = target_assigner.assign(priors, boxes, groundtruth_labels,
                                        num_valid_rows=3)
        (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)
Пример #12
0
 def _get_multi_class_target_assigner(self, num_classes):
   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_cls_target = tf.constant([1] + num_classes * [0], tf.float32)
   return targetassigner.TargetAssigner(
       similarity_calc, matcher, box_coder,
       unmatched_cls_target=unmatched_cls_target)
Пример #13
0
 def _get_multi_dimensional_target_assigner(self, target_dimensions):
   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()
   unmatched_cls_target = tf.constant(np.zeros(target_dimensions),
                                      tf.float32)
   return targetassigner.TargetAssigner(
       similarity_calc, matcher, box_coder,
       unmatched_cls_target=unmatched_cls_target)
 def _get_multi_class_target_assigner(self, num_classes):
     similarity_calc = region_similarity_calculator.NegSqDistSimilarity()
     matcher = bipartite_matcher.GreedyBipartiteMatcher()
     box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
     unmatched_cls_target = tf.constant([1] + num_classes * [0], 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)
Пример #15
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)
  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)
  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)
Пример #18
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, unmatched_cls_target=None)
   anchors_boxlist = box_list.BoxList(anchor_means)
   groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
   result = target_assigner.assign(anchors_boxlist, groundtruth_boxlist)
   (cls_targets, cls_weights, reg_targets, reg_weights, _) = result
   return (cls_targets, cls_weights, reg_targets, reg_weights)
Пример #19
0
 def _get_multi_class_target_with_confidence_assigner(self, num_classes):
     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()
     unmatched_cls_target = tf.constant([1.0 / num_classes] * num_classes,
                                        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_assign_with_ignored_matches(self):
        # Note: test is very similar to above. The third box matched with an IOU
        # of 0.35, which is between the matched and unmatched threshold. This means
        # That like above the expected classification targets are [1, 1, 0].
        # Unlike above, the third target is ignored and therefore expected
        # classification weights are [1, 1, 0].
        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()
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc, matcher, box_coder)

        prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5],
                                   [0.5, 0.5, 1.0, 0.8],
                                   [0.0, 0.5, .9, 1.0]])
        prior_stddevs = tf.constant(3 * [4 * [.1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        box_corners = [[0.0, 0.0, 0.5, 0.5],
                       [0.5, 0.5, 0.9, 0.9]]
        boxes = box_list.BoxList(tf.constant(box_corners))
        exp_cls_targets = [[1], [1], [0]]
        exp_cls_weights = [1, 1, 0]
        exp_reg_targets = [[0, 0, 0, 0],
                           [0, 0, -1, 1],
                           [0, 0, 0, 0]]
        exp_reg_weights = [1, 1, 0]
        exp_matching_anchors = [0, 1]

        result = target_assigner.assign(priors, boxes)
        (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)
Пример #21
0
    def test_assign_crowd(self):
        similarity_calc = region_similarity_calculator.IouSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.7,
                                               unmatched_threshold=0.6,
                                               force_match_for_each_row=True)
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc, matcher, box_coder, unmatched_cls_target=None)

        prior_means = tf.constant([[0.5, 0.5, 1.0, 0.8], [0, 0.5, .5, 1.0],
                                   [0.0, 0.0, 0.5, 0.5]])
        prior_stddevs = tf.constant(3 * [4 * [.1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        box_corners = [[0.0, 0.0, 0.5, 0.5], [0, 0.5, .5, 1.0],
                       [0.5, 0.5, 0.9, 0.9]]
        boxes = box_list.BoxList(tf.constant(box_corners))
        exp_cls_targets = [[1], [1], [1]]
        exp_cls_weights = [1, 1, 1]
        exp_reg_targets = [[0, 0, -1, 1], [0, 0, 0, 0], [0, 0, 0, 0]]
        exp_reg_weights = [1, 1, 1]
        exp_matching_anchors = [1]

        # # crowd
        # crowd = tf.constant([True, False, False], dtype=tf.bool)
        # boxes.add_field(fields.BoxListFields.crowd, crowd)
        #
        # # ignore
        # ignore = tf.constant([False, False, True], dtype=tf.bool)
        # boxes.add_field(fields.BoxListFields.ignore, ignore)

        result = target_assigner.assign(priors, boxes)
        (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, match_results_out, matching_anchors_out = \
                sess.run([cls_targets, cls_weights, reg_targets, reg_weights, match.match_results, 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)
Пример #22
0
 def graph_fn(anchor_means, anchor_stddevs, groundtruth_box_corners,
              groundtruth_labels):
   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()
   unmatched_cls_target = tf.constant([0, 0, 0], tf.float32)
   anchors_boxlist = box_list.BoxList(anchor_means)
   anchors_boxlist.add_field('stddev', anchor_stddevs)
   groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
   target_assigner = targetassigner.TargetAssigner(
       similarity_calc, matcher, box_coder,
       unmatched_cls_target=unmatched_cls_target)
   result = target_assigner.assign(anchors_boxlist, groundtruth_boxlist,
                                   groundtruth_labels)
   (cls_targets, cls_weights, reg_targets, reg_weights, _) = result
   return (cls_targets, cls_weights, reg_targets, reg_weights)
Пример #23
0
    def __init__(self, categories, iou_threshold=0.5):
        """Constructor.

    Args:
      categories: A list of dicts, each of which has the following keys -
        'id': (required) an integer id uniquely identifying this category.
        'name': (required) string representing category name e.g., 'cat', 'dog'.
      iou_threshold: Threshold above which to consider a box as matched during
        evaluation.
    """
        super(CalibrationDetectionEvaluator, self).__init__(categories)

        # Constructing target_assigner to match detections to groundtruth.
        similarity_calc = region_similarity_calculator.IouSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(
            matched_threshold=iou_threshold, unmatched_threshold=iou_threshold)
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
        self._target_assigner = target_assigner.TargetAssigner(
            similarity_calc, matcher, box_coder)
Пример #24
0
    def test_categorize_crowd_ignore(self):
        similarity_calc = region_similarity_calculator.IouSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.7,
                                               unmatched_threshold=0.6,
                                               force_match_for_each_row=True)
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc, matcher, box_coder, unmatched_cls_target=None)

        box_corners = [[0.0, 0.0, 0.1, 0.1], [0.1, 0.1, 0.2, 0.2],
                       [0.2, 0.2, 0.3, 0.3], [0.3, 0.3, 0.4, 0.4],
                       [0.4, 0.4, 0.5, 0.5], [0.5, 0.5, 0.6, 0.6]]

        boxes = box_list.BoxList(tf.constant(box_corners))

        crowd = tf.constant([False, True, False, False, True, False],
                            dtype=tf.bool)
        boxes.add_field(fields.BoxListFields.crowd, crowd)

        ignore = tf.constant([False, False, True, False, True, True],
                             dtype=tf.bool)
        boxes.add_field(fields.BoxListFields.ignore, ignore)

        gt_boxes, crowd_boxes, ignore_boxes = target_assigner.categorize_crowd_ignore(
            boxes)
        gt_boxes_tensor = gt_boxes.get()
        crowd_boxes_tensor = crowd_boxes.get()
        ignore_boxes_tensor = ignore_boxes.get()

        exp_gt_boxes = [[0.0, 0.0, 0.1, 0.1], [0.3, 0.3, 0.4, 0.4]]
        exp_crowd_boxes = [[0.1, 0.1, 0.2, 0.2], [0.4, 0.4, 0.5, 0.5]]
        exp_ignore_boxes = [[0.2, 0.2, 0.3, 0.3], [0.4, 0.4, 0.5, 0.5],
                            [0.5, 0.5, 0.6, 0.6]]

        with self.test_session() as sess:
            gt_boxes_out, crowd_boxes_out, ignore_boxes_out = \
                sess.run([gt_boxes_tensor, crowd_boxes_tensor, ignore_boxes_tensor])

            self.assertAllClose(gt_boxes_out, exp_gt_boxes)
            self.assertAllClose(crowd_boxes_out, exp_crowd_boxes)
            self.assertAllClose(ignore_boxes_out, exp_ignore_boxes)
Пример #25
0
        def graph_fn(anchor_means, groundtruth_box_corners,
                     groundtruth_labels):
            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,
                weight_regression_loss_by_score=True)

            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)
            (_, cls_weights, _, reg_weights, _) = result
            return (cls_weights, reg_weights)
Пример #26
0
    def test_assign_agnostic(self):
        similarity_calc = region_similarity_calculator.IoaSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(0.5)
        box_coder = mean_stddev_box_coder.MeanStddevBoxCoder()
        target_assigner = targetassigner.TargetAssigner(
            similarity_calc, matcher, box_coder, unmatched_cls_target=None)

        prior_means = tf.constant([[0.5, 0.5, 1.0, 0.8], [0, 0.5, .5, 1.0],
                                   [0.0, 0.0, 0.5, 0.5]])
        prior_stddevs = tf.constant(3 * [4 * [.1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        box_corners = [[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.9, 0.9]]
        boxes = box_list.BoxList(tf.constant(box_corners))
        exp_cls_targets = [[1], [0], [1]]
        exp_cls_weights = [1, 1, 1]
        exp_reg_targets = [[0, 0, -1, 1], [0, 0, 0, 0], [0, 0, 0, 0]]
        exp_reg_weights = [1, 0, 1]
        exp_matching_anchors = [0, 2]

        result = target_assigner.assign(priors, boxes)
        (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)
 def graph_fn(boxes, anchors):
     anchors = box_list.BoxList(anchors)
     boxes = box_list.BoxList(boxes)
     coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
     rel_codes = coder.encode(boxes, anchors)
     return rel_codes
 def graph_fn(rel_codes, anchors):
     anchors = box_list.BoxList(anchors)
     coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1)
     decoded_boxes = coder.decode(rel_codes, anchors).get()
     return decoded_boxes