Ejemplo n.º 1
0
 def testHardMiningNMS(self):
   location_losses = tf.constant([[100, 90, 80, 0],
                                  [0, 1, 2, 3]], tf.float32)
   cls_losses = tf.constant([[0, 10, 50, 110],
                             [9, 6, 3, 0]], tf.float32)
   box_corners = tf.constant([[0.1, 0.1, 0.9, 0.9],
                              [0.9, 0.9, 0.99, 0.99],
                              [0.1, 0.1, 0.9, 0.9],
                              [0.1, 0.1, 0.9, 0.9]], tf.float32)
   decoded_boxlist_list = []
   decoded_boxlist_list.append(box_list.BoxList(box_corners))
   decoded_boxlist_list.append(box_list.BoxList(box_corners))
   loss_op = losses.HardExampleMiner(num_hard_examples=2,
                                     iou_threshold=0.5,
                                     loss_type='cls',
                                     cls_loss_weight=1,
                                     loc_loss_weight=1)
   (loc_loss, cls_loss) = loss_op(location_losses, cls_losses,
                                  decoded_boxlist_list)
   exp_loc_loss = 0 + 90 + 0 + 1
   exp_cls_loss = 110 + 10 + 9 + 6
   with self.test_session() as sess:
     loc_loss_output = sess.run(loc_loss)
     self.assertAllClose(loc_loss_output, exp_loc_loss)
     cls_loss_output = sess.run(cls_loss)
     self.assertAllClose(cls_loss_output, exp_cls_loss)
Ejemplo n.º 2
0
 def testHardMiningWithSingleLossType(self):
   location_losses = tf.constant([[100, 90, 80, 0],
                                  [0, 1, 2, 3]], tf.float32)
   cls_losses = tf.constant([[0, 10, 50, 110],
                             [9, 6, 3, 0]], tf.float32)
   box_corners = tf.constant([[0.1, 0.1, 0.9, 0.9],
                              [0.1, 0.1, 0.9, 0.9],
                              [0.1, 0.1, 0.9, 0.9],
                              [0.1, 0.1, 0.9, 0.9]], tf.float32)
   decoded_boxlist_list = []
   decoded_boxlist_list.append(box_list.BoxList(box_corners))
   decoded_boxlist_list.append(box_list.BoxList(box_corners))
   # Uses only location loss to select hard examples
   loss_op = losses.HardExampleMiner(num_hard_examples=1,
                                     iou_threshold=0.0,
                                     loss_type='loc',
                                     cls_loss_weight=1,
                                     loc_loss_weight=1)
   (loc_loss, cls_loss) = loss_op(location_losses, cls_losses,
                                  decoded_boxlist_list)
   exp_loc_loss = 100 + 3
   exp_cls_loss = 0 + 0
   with self.test_session() as sess:
     loc_loss_output = sess.run(loc_loss)
     self.assertAllClose(loc_loss_output, exp_loc_loss)
     cls_loss_output = sess.run(cls_loss)
     self.assertAllClose(cls_loss_output, exp_cls_loss)
Ejemplo n.º 3
0
  def testEnforceNegativesPerPositiveRatioWithMinNegativesPerImage(self):
    location_losses = tf.constant([[100, 90, 80, 0, 1, 2,
                                    3, 10, 20, 100, 20, 3]], tf.float32)
    cls_losses = tf.constant([[0, 0, 100, 0, 90, 70,
                               0, 60, 0, 17, 13, 0]], tf.float32)
    box_corners = tf.constant([[0.0, 0.0, 0.2, 0.1],
                               [0.0, 0.0, 0.2, 0.1],
                               [0.0, 0.0, 0.2, 0.1],
                               [0.0, 0.0, 0.2, 0.1],
                               [0.0, 0.0, 0.5, 0.1],
                               [0.0, 0.0, 0.6, 0.1],
                               [0.0, 0.0, 0.2, 0.1],
                               [0.0, 0.0, 0.8, 0.1],
                               [0.0, 0.0, 0.2, 0.1],
                               [0.0, 0.0, 1.0, 0.1],
                               [0.0, 0.0, 1.1, 0.1],
                               [0.0, 0.0, 0.2, 0.1]], tf.float32)
    match_results = tf.constant([-1] * 12)
    match_list = [matcher.Match(match_results)]
    decoded_boxlist_list = []
    decoded_boxlist_list.append(box_list.BoxList(box_corners))

    min_negatives_per_image_list = [0, 1, 2, 4, 5, 6]
    exp_loc_loss_list = [0,
                         80,
                         80 + 1,
                         80 + 1 + 2 + 10,
                         80 + 1 + 2 + 10 + 100,
                         80 + 1 + 2 + 10 + 100 + 20]
    exp_cls_loss_list = [0,
                         100,
                         100 + 90,
                         100 + 90 + 70 + 60,
                         100 + 90 + 70 + 60 + 17,
                         100 + 90 + 70 + 60 + 17 + 13]

    for min_negatives_per_image, exp_loc_loss, exp_cls_loss in zip(
        min_negatives_per_image_list, exp_loc_loss_list, exp_cls_loss_list):
      loss_op = losses.HardExampleMiner(
          num_hard_examples=None, iou_threshold=0.9999, loss_type='cls',
          cls_loss_weight=1, loc_loss_weight=1,
          max_negatives_per_positive=3,
          min_negatives_per_image=min_negatives_per_image)
      (loc_loss, cls_loss) = loss_op(location_losses, cls_losses,
                                     decoded_boxlist_list, match_list)
      with self.test_session() as sess:
        loc_loss_output = sess.run(loc_loss)
        self.assertAllClose(loc_loss_output, exp_loc_loss)
        cls_loss_output = sess.run(cls_loss)
        self.assertAllClose(cls_loss_output, exp_cls_loss)
Ejemplo n.º 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)
Ejemplo n.º 5
0
def build_hard_example_miner(config, classification_weight,
                             localization_weight):
    """Builds hard example miner based on the config.

  Args:
    config: A losses_pb2.HardExampleMiner object.
    classification_weight: Classification loss weight.
    localization_weight: Localization loss weight.

  Returns:
    Hard example miner.

  """
    loss_type = None
    if config.loss_type == losses_pb2.HardExampleMiner.BOTH:
        loss_type = 'both'
    if config.loss_type == losses_pb2.HardExampleMiner.CLASSIFICATION:
        loss_type = 'cls'
    if config.loss_type == losses_pb2.HardExampleMiner.LOCALIZATION:
        loss_type = 'loc'

    max_negatives_per_positive = None
    num_hard_examples = None
    if config.max_negatives_per_positive > 0:
        max_negatives_per_positive = config.max_negatives_per_positive
    if config.num_hard_examples > 0:
        num_hard_examples = config.num_hard_examples
    hard_example_miner = losses.HardExampleMiner(
        num_hard_examples=num_hard_examples,
        iou_threshold=config.iou_threshold,
        loss_type=loss_type,
        cls_loss_weight=classification_weight,
        loc_loss_weight=localization_weight,
        max_negatives_per_positive=max_negatives_per_positive,
        min_negatives_per_image=config.min_negatives_per_image)
    return hard_example_miner