Exemple #1
0
    def _create_model(self,
                      apply_hard_mining=True,
                      normalize_loc_loss_by_codesize=False):
        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)
        return model, num_classes, mock_anchor_generator.num_anchors(
        ), code_size
Exemple #2
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
        conv_hyperparams = {}
        self._conv_hyperparams = conv_hyperparams
        mock_anchor_generator = MockAnchorGenerator2x2()
        mock_box_predictor = test_utils.MockBoxPredictor(
            is_training, self._num_classes)
        mock_class_predictor = test_utils.MockClassPredictor(
            is_training, 7, self._conv_hyperparams, True, 0.5, 5, 0.0, False)
        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)

        classification_loss_in_image_level = losses.WeightedSigmoidClassificationLossInImageLevel(
        )

        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
        classification_loss_in_image_level_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_class_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_in_image_level,
            classification_loss_weight, localization_loss_weight,
            classification_loss_in_image_level_weight,
            normalize_loss_by_num_matches, hard_example_miner)
Exemple #3
0
def _build_localization_loss(loss_config):
  """Builds a localization loss based on the loss config.

  Args:
    loss_config: A losses_pb2.LocalizationLoss object.

  Returns:
    Loss based on the config.

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

  loss_type = loss_config.WhichOneof('localization_loss')

  if loss_type == 'weighted_l2':
    return losses.WeightedL2LocalizationLoss()

  if loss_type == 'weighted_smooth_l1':
    return losses.WeightedSmoothL1LocalizationLoss(
        loss_config.weighted_smooth_l1.delta)

  if loss_type == 'weighted_iou':
    return losses.WeightedIOULocalizationLoss()

  raise ValueError('Empty loss config.')
Exemple #4
0
 def build(self):
     super(FasterRCNNFirstStageLoss, self).build()
     self._proposal_target_assigner = (
         target_assigner.create_target_assigner('FasterRCNN', 'proposal'))
     self._sampler = (
         balanced_positive_negative_sampler.BalancedPositiveNegativeSampler(
             positive_fraction=self.positive_balance_fraction,
             is_static=False))
     self._localization_loss = od_losses.WeightedSmoothL1LocalizationLoss()
     self._objectness_loss = od_losses.WeightedSoftmaxClassificationLoss()
     return self
Exemple #5
0
    def testReturnsCorrectLoss(self):
        batch_size = 2
        num_anchors = 3
        code_size = 4
        prediction_tensor = tf.constant(
            [[[2.5, 0, .4, 0], [0, 0, 0, 0], [0, 2.5, 0, .4]],
             [[3.5, 0, 0, 0], [0, .4, 0, .9], [0, 0, 1.5, 0]]], tf.float32)
        target_tensor = tf.zeros([batch_size, num_anchors, code_size])
        weights = tf.constant([[2, 1, 1], [0, 3, 0]], tf.float32)
        loss_op = losses.WeightedSmoothL1LocalizationLoss()
        loss = loss_op(prediction_tensor, target_tensor, weights=weights)

        exp_loss = 7.695
        with self.test_session() as sess:
            loss_output = sess.run(loss)
            self.assertAllClose(loss_output, exp_loss)
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)

    if loss_type == 'weighted_l2':
        config = loss_config.weighted_l2
        return losses.WeightedL2LocalizationLoss(
            anchorwise_output=config.anchorwise_output)

    if loss_type == 'weighted_smooth_l1':
        config = loss_config.weighted_smooth_l1
        return losses.WeightedSmoothL1LocalizationLoss(
            anchorwise_output=config.anchorwise_output)

    raise ValueError('Empty loss config.')
Exemple #7
0
def build_loss(loss_type):
    """Builds the desired type of loss

    Args:
        loss_type: loss type (e.g. 'berHu', 'smooth_l1')

    Returns:
        Class of the specified loss_type
    """

    if loss_type == 'berHu':
        return losses_custom.WeightedBerHu()

    elif loss_type == 'chamfer_dist':
        return losses_custom.ChamferDistance()

    elif loss_type == 'emd':
        return losses_custom.EarthMoversDistance()

    elif loss_type == 'smooth_l1':
        return losses.WeightedSmoothL1LocalizationLoss()

    elif loss_type == 'smooth_l1_nonzero':
        return losses_custom.WeightedNonZeroSmoothL1LocalizationLoss()

    elif loss_type == 'softmax':
        return losses.WeightedSoftmaxClassificationLoss()

    elif loss_type == 'focal':
        return losses.SigmoidFocalClassificationLoss()

    elif loss_type == 'softmax_temp':
        return losses.WeightedSoftmaxClassificationLoss(0.5)

    elif loss_type == 'sigmoid_ce':
        return losses_custom.SigmoidClassificationLoss()

    else:
        raise ValueError('Invalid loss type', loss_type)
    def __init__(self):
        super(FakeDetectionModel, self).__init__(num_classes=NUMBER_OF_CLASSES)

        conv_hyperparams_text_proto = """
      regularizer {
        l1_regularizer {
          weight: 0.0003
        }
      }
      initializer {
        truncated_normal_initializer {
          mean: 0.0
          stddev: 0.3
        }
      }
      activation: RELU_6
    """
        hyperparams_proto = hyperparams_pb2.Hyperparams()
        text_format.Merge(conv_hyperparams_text_proto, hyperparams_proto)

        self._class_predictor = class_predictor.ImageLevelConvolutionalClassPredictor(
            is_training=False,
            num_classes=NUMBER_OF_CLASSES_IN_IMAGE_LEVEL,
            conv_hyperparams=hyperparams_proto,
            use_dropout=True,
            dropout_keep_prob=0.5,
            kernel_size=5,
            class_prediction_bias_init=0.0,
            apply_sigmoid_to_scores=False)

        self._classification_loss = losses.WeightedSigmoidClassificationLoss(
            anchorwise_output=True)
        self._localization_loss = losses.WeightedSmoothL1LocalizationLoss(
            anchorwise_output=True)
        self._classification_in_image_level_loss = losses.WeightedSigmoidClassificationLossInImageLevel(
        )
  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 __init__(self):
     super(FakeDetectionModel, self).__init__(num_classes=NUMBER_OF_CLASSES)
     self._classification_loss = losses.WeightedSigmoidClassificationLoss()
     self._localization_loss = losses.WeightedSmoothL1LocalizationLoss()
Exemple #11
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