Example #1
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)
Example #2
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_get_correct_pairwise_similarity_based_on_iou(self):
     corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]])
     corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0],
                             [0.0, 0.0, 20.0, 20.0]])
     exp_output = [[2.0 / 16.0, 0, 6.0 / 400.0],
                   [1.0 / 16.0, 0.0, 5.0 / 400.0]]
     boxes1 = box_list.BoxList(corners1)
     boxes2 = box_list.BoxList(corners2)
     iou_similarity_calculator = region_similarity_calculator.IouSimilarity(
     )
     iou_similarity = iou_similarity_calculator.compare(boxes1, boxes2)
     with self.test_session() as sess:
         iou_output = sess.run(iou_similarity)
         self.assertAllClose(iou_output, exp_output)
Example #4
0
    def setUp(self):
        """Set up mock SSD model.

        Here we set up a simple mock SSD model that will always predict 4
        detections that happen to always be exactly the anchors that are set up
        in the above MockAnchorGenerator.  Because we let max_detections=5,
        we will also always end up with an extra padded row in the detection
        results.
        """
        is_training = False
        self._num_classes = 1
        mock_anchor_generator = MockAnchorGenerator2x2()
        mock_box_predictor = test_utils.MockBoxPredictor(
            is_training, self._num_classes)
        mock_box_coder = test_utils.MockBoxCoder()
        fake_feature_extractor = FakeSSDFeatureExtractor()
        mock_matcher = test_utils.MockMatcher()
        region_similarity_calculator = sim_calc.IouSimilarity()

        def image_resizer_fn(image):
            return tf.identity(image)

        classification_loss = losses.WeightedSigmoidClassificationLoss(
            anchorwise_output=True)
        localization_loss = losses.WeightedSmoothL1LocalizationLoss(
            anchorwise_output=True)
        non_max_suppression_fn = functools.partial(
            post_processing.batch_multiclass_non_max_suppression,
            score_thresh=-20.0,
            iou_thresh=1.0,
            max_size_per_class=5,
            max_total_size=5)
        classification_loss_weight = 1.0
        localization_loss_weight = 1.0
        normalize_loss_by_num_matches = False

        # This hard example miner is expected to be a no-op.
        hard_example_miner = losses.HardExampleMiner(num_hard_examples=None,
                                                     iou_threshold=1.0)

        self._num_anchors = 4
        self._code_size = 4
        self._model = ssd_meta_arch.SSDMetaArch(
            is_training, mock_anchor_generator, mock_box_predictor,
            mock_box_coder, fake_feature_extractor, mock_matcher,
            region_similarity_calculator, image_resizer_fn,
            non_max_suppression_fn, tf.identity, classification_loss,
            localization_loss, classification_loss_weight,
            localization_loss_weight, normalize_loss_by_num_matches,
            hard_example_miner)
    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)
Example #6
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)
Example #7
0
    def __init__(self):
        similarity_calc = region_similarity_calculator.IouSimilarity()
        matcher = argmax_matcher.ArgMaxMatcher(
            matched_threshold=ssd_constants.MATCH_THRESHOLD,
            unmatched_threshold=ssd_constants.MATCH_THRESHOLD,
            negatives_lower_than_unmatched=True,
            force_match_for_each_row=True)

        box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder(
            scale_factors=ssd_constants.BOX_CODER_SCALES)

        self.default_boxes = DefaultBoxes()('ltrb')
        self.default_boxes = box_list.BoxList(
            tf.convert_to_tensor(self.default_boxes))
        self.assigner = target_assigner.TargetAssigner(similarity_calc,
                                                       matcher, box_coder)
Example #8
0
 def graph_fn(anchor_means, groundtruth_box_corners,
              groundtruth_keypoints):
   similarity_calc = region_similarity_calculator.IouSimilarity()
   matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                          unmatched_threshold=0.5)
   box_coder = keypoint_box_coder.KeypointBoxCoder(
       num_keypoints=6, scale_factors=[10.0, 10.0, 5.0, 5.0])
   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)
   groundtruth_boxlist.add_field(fields.BoxListFields.keypoints,
                                 groundtruth_keypoints)
   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)
Example #9
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)
Example #10
0
    def test_get_correct_pairwise_similarity_based_on_iou_with_crowd(self):
        corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]])
        corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0],
                                [0.0, 0.0, 20.0, 20.0]])
        exp_output = [[2.0 / 16.0, 0, 6.0 / 400.0],
                      [1.0 / 12.0, 0.0, 5.0 / 400.0]]
        boxes1 = box_list.BoxList(corners1)
        boxes2 = box_list.BoxList(corners2)

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

        iou_similarity_calculator = region_similarity_calculator.IouSimilarity(
        )
        iou_similarity = iou_similarity_calculator.compare(boxes1, boxes2)
        with self.test_session() as sess:
            iou_output = sess.run(iou_similarity)
            self.assertAllClose(iou_output, exp_output)
Example #11
0
def compute_ssd_targets(
    gt_boxes_list: List[tf.Tensor], gt_labels_list: List[tf.Tensor],
    default_boxes: BoxList, box_coder, unmatched_class_target: tf.Tensor
) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor]:
    """
    """
    assigner = target_assigner.TargetAssigner(
        region_similarity_calculator.IouSimilarity(),
        hungarian_matcher.HungarianBipartiteMatcher(), box_coder)
    cls_targets = []
    cls_weights = []
    reg_targets = []
    reg_weights = []
    matched = []
    for (gtb_arr, gtl_arr) in zip(gt_boxes_list, gt_labels_list):
        result = assigner.assign(anchors=default_boxes,
                                 groundtruth_boxes=BoxList(
                                     tf.constant(gtb_arr, dtype=tf.float32)),
                                 groundtruth_labels=gtl_arr,
                                 unmatched_class_label=unmatched_class_target)
        cls_targets.append(result[0])
        cls_weights.append(result[1])
        reg_targets.append(result[2])
        reg_weights.append(result[3])
        matched.append(result[4])
    # By the way, one default box is matched to at most one gt box,
    #  but one gt box can have multiple default boxes matched.
    #  Thus, we produce a mapping from default boxes to gt boxes
    #  which is not one-to-one or onto.

    cls_weights = tf.stack(cls_weights, axis=0)
    # This assertion will pass every time. I don't know
    #  what the point of returning an array of all 1s is.
    #  Maybe some of the other assigner classes return
    #  more informative weights.
    # assert tf.math.reduce_all(cls_weights == 1.)
    cls_targets = tf.stack(cls_targets, axis=0)
    reg_targets = tf.stack(reg_targets, axis=0)
    reg_weights = tf.stack(reg_weights, axis=0)
    # An entry in [0,num_gt_boxes) indicates a match
    # An entry in {-2,-1} indicates a nonmatch
    matched = tf.stack(matched, axis=0)

    return (cls_targets, cls_weights, reg_targets, reg_weights, matched)
Example #12
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)
Example #13
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)
def build(region_similarity_calculator_config):
    """Builds region similarity calculator based on the configuration.

  Builds one of [IouSimilarity, IoaSimilarity, NegSqDistSimilarity] objects. See
  core/region_similarity_calculator.proto for details.

  Args:
    region_similarity_calculator_config: RegionSimilarityCalculator
      configuration proto.

  Returns:
    region_similarity_calculator: RegionSimilarityCalculator object.

  Raises:
    ValueError: On unknown region similarity calculator.
  """

    if not isinstance(
            region_similarity_calculator_config,
            region_similarity_calculator_pb2.RegionSimilarityCalculator):
        raise ValueError(
            'region_similarity_calculator_config not of type '
            'region_similarity_calculator_pb2.RegionsSimilarityCalculator')

    similarity_calculator = region_similarity_calculator_config.WhichOneof(
        'region_similarity')
    if similarity_calculator == 'iou_similarity':
        return region_similarity_calculator.IouSimilarity()
    if similarity_calculator == 'ioa_similarity':
        return region_similarity_calculator.IoaSimilarity()
    if similarity_calculator == 'neg_sq_dist_similarity':
        return region_similarity_calculator.NegSqDistSimilarity()
    if similarity_calculator == 'thresholded_iou_similarity':
        return region_similarity_calculator.ThresholdedIouSimilarity(
            region_similarity_calculator_config.thresholded_iou_similarity.
            iou_threshold)

    raise ValueError('Unknown region similarity calculator.')
Example #15
0
def build(region_similarity_calculator_config):

    if not isinstance(
            region_similarity_calculator_config,
            region_similarity_calculator_pb2.RegionSimilarityCalculator):
        raise ValueError(
            'region_similarity_calculator_config not of type '
            'region_similarity_calculator_pb2.RegionsSimilarityCalculator')

    similarity_calculator = region_similarity_calculator_config.WhichOneof(
        'region_similarity')
    if similarity_calculator == 'iou_similarity':
        return region_similarity_calculator.IouSimilarity()
    if similarity_calculator == 'ioa_similarity':
        return region_similarity_calculator.IoaSimilarity()
    if similarity_calculator == 'neg_sq_dist_similarity':
        return region_similarity_calculator.NegSqDistSimilarity()
    if similarity_calculator == 'thresholded_iou_similarity':
        return region_similarity_calculator.ThresholdedIouSimilarity(
            region_similarity_calculator_config.thresholded_iou_similarity.
            iou_threshold)

    raise ValueError('Unknown region similarity calculator.')
Example #16
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)
    def _create_model(self,
                      apply_hard_mining=True,
                      normalize_loc_loss_by_codesize=False,
                      add_background_class=True):
        is_training = False
        num_classes = 1
        mock_anchor_generator = MockAnchorGenerator2x2()
        mock_box_predictor = test_utils.MockBoxPredictor(
            is_training, num_classes)
        mock_box_coder = test_utils.MockBoxCoder()
        fake_feature_extractor = FakeSSDFeatureExtractor()
        mock_matcher = test_utils.MockMatcher()
        region_similarity_calculator = sim_calc.IouSimilarity()
        encode_background_as_zeros = False

        def image_resizer_fn(image):
            return [tf.identity(image), tf.shape(image)]

        classification_loss = losses.WeightedSigmoidClassificationLoss()
        localization_loss = losses.WeightedSmoothL1LocalizationLoss()
        non_max_suppression_fn = functools.partial(
            post_processing.batch_multiclass_non_max_suppression,
            score_thresh=-20.0,
            iou_thresh=1.0,
            max_size_per_class=5,
            max_total_size=5)
        classification_loss_weight = 1.0
        localization_loss_weight = 1.0
        negative_class_weight = 1.0
        normalize_loss_by_num_matches = False

        hard_example_miner = None
        if apply_hard_mining:
            # This hard example miner is expected to be a no-op.
            hard_example_miner = losses.HardExampleMiner(
                num_hard_examples=None, iou_threshold=1.0)

        code_size = 4
        model = ssd_meta_arch.SSDMetaArch(
            is_training,
            mock_anchor_generator,
            mock_box_predictor,
            mock_box_coder,
            fake_feature_extractor,
            mock_matcher,
            region_similarity_calculator,
            encode_background_as_zeros,
            negative_class_weight,
            image_resizer_fn,
            non_max_suppression_fn,
            tf.identity,
            classification_loss,
            localization_loss,
            classification_loss_weight,
            localization_loss_weight,
            normalize_loss_by_num_matches,
            hard_example_miner,
            add_summaries=False,
            normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize,
            freeze_batchnorm=False,
            inplace_batchnorm_update=False,
            add_background_class=add_background_class)
        return model, num_classes, mock_anchor_generator.num_anchors(
        ), code_size
Example #18
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 _create_model(self,
                    interleaved=False,
                    apply_hard_mining=True,
                    normalize_loc_loss_by_codesize=False,
                    add_background_class=True,
                    random_example_sampling=False,
                    use_expected_classification_loss_under_sampling=False,
                    min_num_negative_samples=1,
                    desired_negative_sampling_ratio=3,
                    unroll_length=1):
    num_classes = NUM_CLASSES
    is_training = False
    mock_anchor_generator = MockAnchorGenerator2x2()
    mock_box_predictor = test_utils.MockBoxPredictor(is_training, num_classes)
    mock_box_coder = test_utils.MockBoxCoder()
    if interleaved:
      fake_feature_extractor = FakeLSTMInterleavedFeatureExtractor()
    else:
      fake_feature_extractor = FakeLSTMFeatureExtractor()
    mock_matcher = test_utils.MockMatcher()
    region_similarity_calculator = sim_calc.IouSimilarity()
    encode_background_as_zeros = False
    def image_resizer_fn(image):
      return [tf.identity(image), tf.shape(image)]

    classification_loss = losses.WeightedSigmoidClassificationLoss()
    localization_loss = losses.WeightedSmoothL1LocalizationLoss()
    non_max_suppression_fn = functools.partial(
        post_processing.batch_multiclass_non_max_suppression,
        score_thresh=-20.0,
        iou_thresh=1.0,
        max_size_per_class=5,
        max_total_size=MAX_TOTAL_NUM_BOXES)
    classification_loss_weight = 1.0
    localization_loss_weight = 1.0
    negative_class_weight = 1.0
    normalize_loss_by_num_matches = False

    hard_example_miner = None
    if apply_hard_mining:
      # This hard example miner is expected to be a no-op.
      hard_example_miner = losses.HardExampleMiner(
          num_hard_examples=None,
          iou_threshold=1.0)

    target_assigner_instance = target_assigner.TargetAssigner(
        region_similarity_calculator,
        mock_matcher,
        mock_box_coder,
        negative_class_weight=negative_class_weight)

    code_size = 4
    model = lstm_ssd_meta_arch.LSTMSSDMetaArch(
        is_training=is_training,
        anchor_generator=mock_anchor_generator,
        box_predictor=mock_box_predictor,
        box_coder=mock_box_coder,
        feature_extractor=fake_feature_extractor,
        encode_background_as_zeros=encode_background_as_zeros,
        image_resizer_fn=image_resizer_fn,
        non_max_suppression_fn=non_max_suppression_fn,
        score_conversion_fn=tf.identity,
        classification_loss=classification_loss,
        localization_loss=localization_loss,
        classification_loss_weight=classification_loss_weight,
        localization_loss_weight=localization_loss_weight,
        normalize_loss_by_num_matches=normalize_loss_by_num_matches,
        hard_example_miner=hard_example_miner,
        unroll_length=unroll_length,
        target_assigner_instance=target_assigner_instance,
        add_summaries=False)
    return model, num_classes, mock_anchor_generator.num_anchors(), code_size
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)
Example #21
0
  def _create_model(
      self,
      model_fn=ssd_meta_arch.SSDMetaArch,
      apply_hard_mining=True,
      normalize_loc_loss_by_codesize=False,
      add_background_class=True,
      random_example_sampling=False,
      expected_loss_weights=model_pb2.DetectionModel().ssd.loss.NONE,
      min_num_negative_samples=1,
      desired_negative_sampling_ratio=3,
      use_keras=False,
      predict_mask=False,
      use_static_shapes=False,
      nms_max_size_per_class=5,
      calibration_mapping_value=None,
      return_raw_detections_during_predict=False):
    is_training = False
    num_classes = 1
    mock_anchor_generator = MockAnchorGenerator2x2()
    if use_keras:
      mock_box_predictor = test_utils.MockKerasBoxPredictor(
          is_training, num_classes, add_background_class=add_background_class)
    else:
      mock_box_predictor = test_utils.MockBoxPredictor(
          is_training, num_classes, add_background_class=add_background_class)
    mock_box_coder = test_utils.MockBoxCoder()
    if use_keras:
      fake_feature_extractor = FakeSSDKerasFeatureExtractor()
    else:
      fake_feature_extractor = FakeSSDFeatureExtractor()
    mock_matcher = test_utils.MockMatcher()
    region_similarity_calculator = sim_calc.IouSimilarity()
    encode_background_as_zeros = False

    def image_resizer_fn(image):
      return [tf.identity(image), tf.shape(image)]

    classification_loss = losses.WeightedSigmoidClassificationLoss()
    localization_loss = losses.WeightedSmoothL1LocalizationLoss()
    non_max_suppression_fn = functools.partial(
        post_processing.batch_multiclass_non_max_suppression,
        score_thresh=-20.0,
        iou_thresh=1.0,
        max_size_per_class=nms_max_size_per_class,
        max_total_size=nms_max_size_per_class,
        use_static_shapes=use_static_shapes)
    score_conversion_fn = tf.identity
    calibration_config = calibration_pb2.CalibrationConfig()
    if calibration_mapping_value:
      calibration_text_proto = """
      function_approximation {
        x_y_pairs {
            x_y_pair {
              x: 0.0
              y: %f
            }
            x_y_pair {
              x: 1.0
              y: %f
            }}}""" % (calibration_mapping_value, calibration_mapping_value)
      text_format.Merge(calibration_text_proto, calibration_config)
      score_conversion_fn = (
          post_processing_builder._build_calibrated_score_converter(  # pylint: disable=protected-access
              tf.identity, calibration_config))
    classification_loss_weight = 1.0
    localization_loss_weight = 1.0
    negative_class_weight = 1.0
    normalize_loss_by_num_matches = False

    hard_example_miner = None
    if apply_hard_mining:
      # This hard example miner is expected to be a no-op.
      hard_example_miner = losses.HardExampleMiner(
          num_hard_examples=None, iou_threshold=1.0)

    random_example_sampler = None
    if random_example_sampling:
      random_example_sampler = sampler.BalancedPositiveNegativeSampler(
          positive_fraction=0.5)

    target_assigner_instance = target_assigner.TargetAssigner(
        region_similarity_calculator,
        mock_matcher,
        mock_box_coder,
        negative_class_weight=negative_class_weight)

    model_config = model_pb2.DetectionModel()
    if expected_loss_weights == model_config.ssd.loss.NONE:
      expected_loss_weights_fn = None
    else:
      raise ValueError('Not a valid value for expected_loss_weights.')

    code_size = 4

    kwargs = {}
    if predict_mask:
      kwargs.update({
          'mask_prediction_fn': test_utils.MockMaskHead(num_classes=1).predict,
      })

    model = model_fn(
        is_training=is_training,
        anchor_generator=mock_anchor_generator,
        box_predictor=mock_box_predictor,
        box_coder=mock_box_coder,
        feature_extractor=fake_feature_extractor,
        encode_background_as_zeros=encode_background_as_zeros,
        image_resizer_fn=image_resizer_fn,
        non_max_suppression_fn=non_max_suppression_fn,
        score_conversion_fn=score_conversion_fn,
        classification_loss=classification_loss,
        localization_loss=localization_loss,
        classification_loss_weight=classification_loss_weight,
        localization_loss_weight=localization_loss_weight,
        normalize_loss_by_num_matches=normalize_loss_by_num_matches,
        hard_example_miner=hard_example_miner,
        target_assigner_instance=target_assigner_instance,
        add_summaries=False,
        normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize,
        freeze_batchnorm=False,
        inplace_batchnorm_update=False,
        add_background_class=add_background_class,
        random_example_sampler=random_example_sampler,
        expected_loss_weights_fn=expected_loss_weights_fn,
        return_raw_detections_during_predict=(
            return_raw_detections_during_predict),
        **kwargs)
    return model, num_classes, mock_anchor_generator.num_anchors(), code_size
    def _create_model(self,
                      model_fn=ssd_meta_arch.SSDMetaArch,
                      apply_hard_mining=True,
                      normalize_loc_loss_by_codesize=False,
                      add_background_class=True,
                      random_example_sampling=False,
                      weight_regression_loss_by_score=False,
                      use_expected_classification_loss_under_sampling=False,
                      min_num_negative_samples=1,
                      desired_negative_sampling_ratio=3,
                      use_keras=False,
                      predict_mask=False,
                      use_static_shapes=False,
                      nms_max_size_per_class=5):
        is_training = False
        num_classes = 1
        mock_anchor_generator = MockAnchorGenerator2x2()
        if use_keras:
            mock_box_predictor = test_utils.MockKerasBoxPredictor(
                is_training,
                num_classes,
                add_background_class=add_background_class,
                predict_mask=predict_mask)
        else:
            mock_box_predictor = test_utils.MockBoxPredictor(
                is_training,
                num_classes,
                add_background_class=add_background_class,
                predict_mask=predict_mask)
        mock_box_coder = test_utils.MockBoxCoder()
        if use_keras:
            fake_feature_extractor = FakeSSDKerasFeatureExtractor()
        else:
            fake_feature_extractor = FakeSSDFeatureExtractor()
        mock_matcher = test_utils.MockMatcher()
        region_similarity_calculator = sim_calc.IouSimilarity()
        encode_background_as_zeros = False

        def image_resizer_fn(image):
            return [tf.identity(image), tf.shape(image)]

        classification_loss = losses.WeightedSigmoidClassificationLoss()
        localization_loss = losses.WeightedSmoothL1LocalizationLoss()
        non_max_suppression_fn = functools.partial(
            post_processing.batch_multiclass_non_max_suppression,
            score_thresh=-20.0,
            iou_thresh=1.0,
            max_size_per_class=nms_max_size_per_class,
            max_total_size=nms_max_size_per_class,
            use_static_shapes=use_static_shapes)
        classification_loss_weight = 1.0
        localization_loss_weight = 1.0
        negative_class_weight = 1.0
        normalize_loss_by_num_matches = False

        hard_example_miner = None
        if apply_hard_mining:
            # This hard example miner is expected to be a no-op.
            hard_example_miner = losses.HardExampleMiner(
                num_hard_examples=None, iou_threshold=1.0)

        random_example_sampler = None
        if random_example_sampling:
            random_example_sampler = sampler.BalancedPositiveNegativeSampler(
                positive_fraction=0.5)

        target_assigner_instance = target_assigner.TargetAssigner(
            region_similarity_calculator,
            mock_matcher,
            mock_box_coder,
            negative_class_weight=negative_class_weight,
            weight_regression_loss_by_score=weight_regression_loss_by_score)

        expected_classification_loss_under_sampling = None
        if use_expected_classification_loss_under_sampling:
            expected_classification_loss_under_sampling = functools.partial(
                ops.expected_classification_loss_under_sampling,
                min_num_negative_samples=min_num_negative_samples,
                desired_negative_sampling_ratio=desired_negative_sampling_ratio
            )

        code_size = 4
        model = model_fn(
            is_training=is_training,
            anchor_generator=mock_anchor_generator,
            box_predictor=mock_box_predictor,
            box_coder=mock_box_coder,
            feature_extractor=fake_feature_extractor,
            encode_background_as_zeros=encode_background_as_zeros,
            image_resizer_fn=image_resizer_fn,
            non_max_suppression_fn=non_max_suppression_fn,
            score_conversion_fn=tf.identity,
            classification_loss=classification_loss,
            localization_loss=localization_loss,
            classification_loss_weight=classification_loss_weight,
            localization_loss_weight=localization_loss_weight,
            normalize_loss_by_num_matches=normalize_loss_by_num_matches,
            hard_example_miner=hard_example_miner,
            target_assigner_instance=target_assigner_instance,
            add_summaries=False,
            normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize,
            freeze_batchnorm=False,
            inplace_batchnorm_update=False,
            add_background_class=add_background_class,
            random_example_sampler=random_example_sampler,
            expected_classification_loss_under_sampling=
            expected_classification_loss_under_sampling)
        return model, num_classes, mock_anchor_generator.num_anchors(
        ), code_size