Example #1
0
    def testEasyExamplesProduceSmallLossComparedToSigmoidXEntropy(self):
        prediction_tensor = tf.constant(
            [[[_logit(0.97)], [_logit(0.90)], [_logit(0.73)], [_logit(0.27)],
              [_logit(0.09)], [_logit(0.03)]]], tf.float32)
        target_tensor = tf.constant([[[1], [1], [1], [0], [0], [0]]],
                                    tf.float32)
        weights = tf.constant([[1, 1, 1, 1, 1, 1]], tf.float32)
        focal_loss_op = losses.SigmoidFocalClassificationLoss(gamma=2.0,
                                                              alpha=None)
        sigmoid_loss_op = losses.WeightedSigmoidClassificationLoss()
        focal_loss = tf.reduce_sum(focal_loss_op(prediction_tensor,
                                                 target_tensor,
                                                 weights=weights),
                                   axis=2)
        sigmoid_loss = tf.reduce_sum(sigmoid_loss_op(prediction_tensor,
                                                     target_tensor,
                                                     weights=weights),
                                     axis=2)

        with self.test_session() as sess:
            sigmoid_loss, focal_loss = sess.run([sigmoid_loss, focal_loss])
            order_of_ratio = np.power(
                10, np.floor(np.log10(sigmoid_loss / focal_loss)))
            self.assertAllClose(order_of_ratio,
                                [[1000, 100, 10, 10, 100, 1000]])
Example #2
0
def _build_classification_loss(loss_config):
    """Builds a classification loss based on the loss config.

    Args:
      loss_config: A losses_pb2.ClassificationLoss object.

    Returns:
      Loss based on the config.

    Raises:
      ValueError: On invalid loss_config.
    """
    if not isinstance(loss_config, losses_pb2.ClassificationLoss):
        raise ValueError('loss_config not of type losses_pb2.ClassificationLoss.')

    loss_type = loss_config.WhichOneof('classification_loss')

    if loss_type == 'weighted_sigmoid':
        config = loss_config.weighted_sigmoid
        return losses.WeightedSigmoidClassificationLoss(
            anchorwise_output=config.anchorwise_output)

    if loss_type == 'weighted_softmax':
        config = loss_config.weighted_softmax
        return losses.WeightedSoftmaxClassificationLoss(
            anchorwise_output=config.anchorwise_output)

    if loss_type == 'bootstrapped_sigmoid':
        config = loss_config.bootstrapped_sigmoid
        return losses.BootstrappedSigmoidClassificationLoss(
            alpha=config.alpha,
            bootstrap_type=('hard' if config.hard_bootstrap else 'soft'),
            anchorwise_output=config.anchorwise_output)

    raise ValueError('Empty loss config.')
Example #3
0
    def testIgnorePositiveExampleLossViaAlphaMultiplier(self):
        prediction_tensor = tf.constant(
            [[[_logit(0.55)], [_logit(0.52)], [_logit(0.50)], [_logit(0.48)],
              [_logit(0.45)]]], tf.float32)
        target_tensor = tf.constant([[[1], [1], [1], [0], [0]]], tf.float32)
        weights = tf.constant([[1, 1, 1, 1, 1]], tf.float32)
        focal_loss_op = losses.SigmoidFocalClassificationLoss(gamma=2.0,
                                                              alpha=0.0)
        sigmoid_loss_op = losses.WeightedSigmoidClassificationLoss()
        focal_loss = tf.reduce_sum(focal_loss_op(prediction_tensor,
                                                 target_tensor,
                                                 weights=weights),
                                   axis=2)
        sigmoid_loss = tf.reduce_sum(sigmoid_loss_op(prediction_tensor,
                                                     target_tensor,
                                                     weights=weights),
                                     axis=2)

        with self.test_session() as sess:
            sigmoid_loss, focal_loss = sess.run([sigmoid_loss, focal_loss])
            self.assertAllClose(focal_loss[0][:3], [0., 0., 0.])
            order_of_ratio = np.power(
                10,
                np.floor(np.log10(sigmoid_loss[0][3:] / focal_loss[0][3:])))
            self.assertAllClose(order_of_ratio, [1., 1.])
def build_faster_rcnn_classification_loss(loss_config):
    """Builds a classification loss for Faster RCNN based on the loss config.

  Args:
    loss_config: A losses_pb2.ClassificationLoss object.

  Returns:
    Loss based on the config.

  Raises:
    ValueError: On invalid loss_config.
  """
    if not isinstance(loss_config, losses_pb2.ClassificationLoss):
        raise ValueError(
            'loss_config not of type losses_pb2.ClassificationLoss.')

    loss_type = loss_config.WhichOneof('classification_loss')

    if loss_type == 'weighted_sigmoid':
        return losses.WeightedSigmoidClassificationLoss()
    if loss_type == 'weighted_softmax':
        config = loss_config.weighted_softmax
        return losses.WeightedSoftmaxClassificationLoss(
            logit_scale=config.logit_scale)

    # By default, Faster RCNN second stage classifier uses Softmax loss
    # with anchor-wise outputs.
    config = loss_config.weighted_softmax
    return losses.WeightedSoftmaxClassificationLoss(
        logit_scale=config.logit_scale)
Example #5
0
    def testReturnsCorrectLossWithClassIndices(self):
        prediction_tensor = tf.constant(
            [[[-100, 100, -100, 100], [100, -100, -100, -100],
              [100, 0, -100, 100], [-100, -100, 100, -100]],
             [[-100, 0, 100, 100], [-100, 100, -100, 100],
              [100, 100, 100, 100], [0, 0, -1, 100]]], tf.float32)
        target_tensor = tf.constant(
            [[[0, 1, 0, 0], [1, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]],
             [[0, 0, 1, 0], [0, 1, 0, 0], [1, 1, 1, 0], [1, 0, 0, 0]]],
            tf.float32)
        weights = tf.constant([[1, 1, 1, 1], [1, 1, 1, 0]], tf.float32)
        # Ignores the last class.
        class_indices = tf.constant([0, 1, 2], tf.int32)
        loss_op = losses.WeightedSigmoidClassificationLoss()
        loss = loss_op(prediction_tensor,
                       target_tensor,
                       weights=weights,
                       class_indices=class_indices)
        loss = tf.reduce_sum(loss, axis=2)

        exp_loss = np.matrix([[0, 0, -math.log(.5), 0],
                              [-math.log(.5), 0, 0, 0]])
        with self.test_session() as sess:
            loss_output = sess.run(loss)
            self.assertAllClose(loss_output, exp_loss)
Example #6
0
  def testSameAsSigmoidXEntropyWithNoAlphaAndZeroGamma(self):
    prediction_tensor = tf.constant([[[-100, 100, -100],
                                      [100, -100, -100],
                                      [100, 0, -100],
                                      [-100, -100, 100]],
                                     [[-100, 0, 100],
                                      [-100, 100, -100],
                                      [100, 100, 100],
                                      [0, 0, -1]]], tf.float32)
    target_tensor = tf.constant([[[0, 1, 0],
                                  [1, 0, 0],
                                  [1, 0, 0],
                                  [0, 0, 1]],
                                 [[0, 0, 1],
                                  [0, 1, 0],
                                  [1, 1, 1],
                                  [1, 0, 0]]], tf.float32)
    weights = tf.constant([[1, 1, 1, 1],
                           [1, 1, 1, 0]], tf.float32)
    focal_loss_op = losses.SigmoidFocalClassificationLoss(alpha=None, gamma=0.0)
    sigmoid_loss_op = losses.WeightedSigmoidClassificationLoss()
    focal_loss = focal_loss_op(prediction_tensor, target_tensor,
                               weights=weights)
    sigmoid_loss = sigmoid_loss_op(prediction_tensor, target_tensor,
                                   weights=weights)

    with self.test_session() as sess:
      sigmoid_loss, focal_loss = sess.run([sigmoid_loss, focal_loss])
      self.assertAllClose(sigmoid_loss, focal_loss)
Example #7
0
  def testNonAnchorWiseOutputComparableToSigmoidXEntropy(self):
    prediction_tensor = tf.constant([[[_logit(0.55)],
                                      [_logit(0.52)],
                                      [_logit(0.50)],
                                      [_logit(0.48)],
                                      [_logit(0.45)]]], tf.float32)
    target_tensor = tf.constant([[[1],
                                  [1],
                                  [1],
                                  [0],
                                  [0]]], tf.float32)
    weights = tf.constant([[1, 1, 1, 1, 1]], tf.float32)
    focal_loss_op = losses.SigmoidFocalClassificationLoss(gamma=2.0, alpha=None)
    sigmoid_loss_op = losses.WeightedSigmoidClassificationLoss()
    focal_loss = tf.reduce_sum(focal_loss_op(prediction_tensor, target_tensor,
                                             weights=weights))
    sigmoid_loss = tf.reduce_sum(sigmoid_loss_op(prediction_tensor,
                                                 target_tensor,
                                                 weights=weights))

    with self.test_session() as sess:
      sigmoid_loss, focal_loss = sess.run([sigmoid_loss, focal_loss])
      order_of_ratio = np.power(10,
                                np.floor(np.log10(sigmoid_loss / focal_loss)))
      self.assertAlmostEqual(order_of_ratio, 1.)
Example #8
0
  def testReturnsCorrectLoss(self):
    prediction_tensor = tf.constant([[[-100, 100, -100],
                                      [100, -100, -100],
                                      [100, 0, -100],
                                      [-100, -100, 100]],
                                     [[-100, 0, 100],
                                      [-100, 100, -100],
                                      [100, 100, 100],
                                      [0, 0, -1]]], tf.float32)
    target_tensor = tf.constant([[[0, 1, 0],
                                  [1, 0, 0],
                                  [1, 0, 0],
                                  [0, 0, 1]],
                                 [[0, 0, 1],
                                  [0, 1, 0],
                                  [1, 1, 1],
                                  [1, 0, 0]]], tf.float32)
    weights = tf.constant([[1, 1, 1, 1],
                           [1, 1, 1, 0]], tf.float32)
    loss_op = losses.WeightedSigmoidClassificationLoss()
    loss = loss_op(prediction_tensor, target_tensor, weights=weights)
    loss = tf.reduce_sum(loss)

    exp_loss = -2 * math.log(.5)
    with self.test_session() as sess:
      loss_output = sess.run(loss)
      self.assertAllClose(loss_output, exp_loss)
Example #9
0
def _build_classification_loss(loss_config):
    """Builds a classification loss based on the loss config.

  Args:
    loss_config: A losses_pb2.ClassificationLoss object.

  Returns:
    Loss based on the config.

  Raises:
    ValueError: On invalid loss_config.
  """
    if not isinstance(loss_config, losses_pb2.ClassificationLoss):
        raise ValueError(
            'loss_config not of type losses_pb2.ClassificationLoss.')

    loss_type = loss_config.WhichOneof('classification_loss')

    if loss_type == 'weighted_sigmoid':
        return losses.WeightedSigmoidClassificationLoss()

    if loss_type == 'weighted_sigmoid_focal':
        config = loss_config.weighted_sigmoid_focal
        # alpha = None
        # if config.HasField('alpha'):
        #   alpha = config.alpha
        if config.alpha > 0:
            alpha = config.alpha
        else:
            alpha = None
        return losses.SigmoidFocalClassificationLoss(gamma=config.gamma,
                                                     alpha=alpha)
    if loss_type == 'weighted_softmax_focal':
        config = loss_config.weighted_softmax_focal
        # alpha = None
        # if config.HasField('alpha'):
        #   alpha = config.alpha
        if config.alpha > 0:
            alpha = config.alpha
        else:
            alpha = None
        return losses.SoftmaxFocalClassificationLoss(gamma=config.gamma,
                                                     alpha=alpha)

    if loss_type == 'weighted_softmax':
        config = loss_config.weighted_softmax
        return losses.WeightedSoftmaxClassificationLoss(
            logit_scale=config.logit_scale)

    if loss_type == 'bootstrapped_sigmoid':
        config = loss_config.bootstrapped_sigmoid
        return losses.BootstrappedSigmoidClassificationLoss(
            alpha=config.alpha,
            bootstrap_type=('hard' if config.hard_bootstrap else 'soft'))

    raise ValueError('Empty loss config.')
Example #10
0
    def _create_model(self, apply_hard_mining=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
        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,
                                          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)
        return model, num_classes, mock_anchor_generator.num_anchors(
        ), code_size
    def setUp(self):
        """Set up mock RSSD model.

        Here we set up a simple mock RSSD 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_rbox_predictor = test_utils.MockRBoxPredictor(
            is_training, self._num_classes)
        mock_rbox_coder = test_utils.MockRBoxCoder()
        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_rbox.batch_multiclass_non_max_suppression_rbox,
            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,
                                                     box_type='rbbox')

        self._num_anchors = 4
        self._code_size = 5
        self._model = rssd_meta_arch.RSSDMetaArch(
            is_training, mock_anchor_generator, mock_rbox_predictor,
            mock_rbox_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)
Example #12
0
def _build_classification_loss(loss_config):
    """Builds a classification loss based on the loss config.

    Args:
    loss_config: A yaml.ClassificationLoss object.

    Returns:
    Loss based on the config.

    Raises:
    ValueError: On invalid loss_config.
    """
    if 'weighted_sigmoid' in loss_config:
        return losses.WeightedSigmoidClassificationLoss()

    if 'weighted_sigmoid_focal' in loss_config:
        config = loss_config.weighted_sigmoid_focal
        # alpha = None
        # if config.HasField('alpha'):
        #   alpha = config.alpha
        if config.alpha > 0:
            alpha = config.alpha
        else:
            alpha = None
        return losses.SigmoidFocalClassificationLoss(gamma=config.gamma,
                                                     alpha=alpha)
    if 'weighted_softmax_focal' in loss_config:
        config = loss_config.weighted_softmax_focal
        # alpha = None
        # if config.HasField('alpha'):
        #   alpha = config.alpha
        if config.alpha > 0:
            alpha = config.alpha
        else:
            alpha = None
        return losses.SoftmaxFocalClassificationLoss(gamma=config.gamma,
                                                     alpha=alpha)

    if 'weighted_softmax' in loss_config:
        config = loss_config.weighted_softmax
        return losses.WeightedSoftmaxClassificationLoss(
            logit_scale=config.logit_scale)

    if 'bootstrapped_sigmoid' in loss_config:
        config = loss_config.bootstrapped_sigmoid
        return losses.BootstrappedSigmoidClassificationLoss(
            alpha=config.alpha,
            bootstrap_type=('hard' if config.hard_bootstrap else 'soft'))
    else:
        raise ValueError('Empty loss config.')
 def __init__(self):
     super(FakeDetectionModel, self).__init__(num_classes=NUMBER_OF_CLASSES)
     self._classification_loss = losses.WeightedSigmoidClassificationLoss(
         anchorwise_output=True)
     self._localization_loss = losses.WeightedSmoothL1LocalizationLoss(
         anchorwise_output=True)