Exemple #1
0
 def test_raise_error_on_empty_config(self):
     losses_text_proto = """
   localization_loss {
     weighted_l2 {
     }
   }
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     with self.assertRaises(ValueError):
         losses_builder.build(losses_proto)
Exemple #2
0
 def test_build_all_loss_parameters(self):
     losses_text_proto = """
   localization_loss {
     weighted_l2 {
     }
   }
   classification_loss {
     weighted_softmax {
     }
   }
   hard_example_miner {
   }
   classification_weight: 0.8
   localization_weight: 0.2
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     (classification_loss, localization_loss, classification_weight,
      localization_weight,
      hard_example_miner) = losses_builder.build(losses_proto)
     self.assertTrue(isinstance(hard_example_miner,
                                losses.HardExampleMiner))
     self.assertTrue(
         isinstance(classification_loss,
                    losses.WeightedSoftmaxClassificationLoss))
     self.assertTrue(
         isinstance(localization_loss, losses.WeightedL2LocalizationLoss))
     self.assertAlmostEqual(classification_weight, 0.8)
     self.assertAlmostEqual(localization_weight, 0.2)
Exemple #3
0
 def test_build_hard_example_miner_with_non_default_values(self):
     losses_text_proto = """
   localization_loss {
     weighted_l2 {
     }
   }
   classification_loss {
     weighted_softmax {
     }
   }
   hard_example_miner {
     num_hard_examples: 32
     iou_threshold: 0.5
     loss_type: LOCALIZATION
     max_negatives_per_positive: 10
     min_negatives_per_image: 3
   }
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     _, _, _, _, hard_example_miner = losses_builder.build(losses_proto)
     self.assertTrue(isinstance(hard_example_miner,
                                losses.HardExampleMiner))
     self.assertEqual(hard_example_miner._num_hard_examples, 32)
     self.assertAlmostEqual(hard_example_miner._iou_threshold, 0.5)
     self.assertEqual(hard_example_miner._max_negatives_per_positive, 10)
     self.assertEqual(hard_example_miner._min_negatives_per_image, 3)
Exemple #4
0
 def test_raise_error_when_both_focal_loss_and_hard_example_miner(self):
     losses_text_proto = """
   localization_loss {
     weighted_l2 {
     }
   }
   classification_loss {
     weighted_sigmoid_focal {
     }
   }
   hard_example_miner {
   }
   classification_weight: 0.8
   localization_weight: 0.2
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     with self.assertRaises(ValueError):
         losses_builder.build(losses_proto)
def _build_ssd_model(ssd_config, is_training):
  """Builds an SSD detection model based on the model config.

  Args:
    ssd_config: A ssd.proto object containing the config for the desired
      SSDMetaArch.
    is_training: True if this model is being built for training purposes.

  Returns:
    SSDMetaArch based on the config.
  Raises:
    ValueError: If ssd_config.type is not recognized (i.e. not registered in
      model_class_map).
  """
  num_classes = ssd_config.num_classes

  # Feature extractor
  feature_extractor = _build_ssd_feature_extractor(ssd_config.feature_extractor,
                                                   is_training)

  box_coder = box_coder_builder.build(ssd_config.box_coder)
  matcher = matcher_builder.build(ssd_config.matcher)
  region_similarity_calculator = sim_calc.build(
      ssd_config.similarity_calculator)
  ssd_box_predictor = box_predictor_builder.build(hyperparams_builder.build,
                                                  ssd_config.box_predictor,
                                                  is_training, num_classes)
  anchor_generator = anchor_generator_builder.build(
      ssd_config.anchor_generator)
  image_resizer_fn = image_resizer_builder.build(ssd_config.image_resizer)
  non_max_suppression_fn, score_conversion_fn = post_processing_builder.build(
      ssd_config.post_processing)
  (classification_loss, localization_loss, classification_weight,
   localization_weight,
   hard_example_miner) = losses_builder.build(ssd_config.loss)
  normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches

  return ssd_meta_arch.SSDMetaArch(
      is_training,
      anchor_generator,
      ssd_box_predictor,
      box_coder,
      feature_extractor,
      matcher,
      region_similarity_calculator,
      image_resizer_fn,
      non_max_suppression_fn,
      score_conversion_fn,
      classification_loss,
      localization_loss,
      classification_weight,
      localization_weight,
      normalize_loss_by_num_matches,
      hard_example_miner)
Exemple #6
0
 def test_do_not_build_hard_example_miner_by_default(self):
     losses_text_proto = """
   localization_loss {
     weighted_l2 {
     }
   }
   classification_loss {
     weighted_softmax {
     }
   }
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     _, _, _, _, hard_example_miner = losses_builder.build(losses_proto)
     self.assertEqual(hard_example_miner, None)
Exemple #7
0
 def test_build_weighted_iou_localization_loss(self):
     losses_text_proto = """
   localization_loss {
     weighted_iou {
     }
   }
   classification_loss {
     weighted_softmax {
     }
   }
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     _, localization_loss, _, _, _ = losses_builder.build(losses_proto)
     self.assertTrue(
         isinstance(localization_loss, losses.WeightedIOULocalizationLoss))
Exemple #8
0
 def test_build_weighted_sigmoid_classification_loss(self):
     losses_text_proto = """
   classification_loss {
     weighted_sigmoid {
     }
   }
   localization_loss {
     weighted_l2 {
     }
   }
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     classification_loss, _, _, _, _ = losses_builder.build(losses_proto)
     self.assertTrue(
         isinstance(classification_loss,
                    losses.WeightedSigmoidClassificationLoss))
Exemple #9
0
 def test_build_weighted_softmax_classification_loss_with_logit_scale(self):
     losses_text_proto = """
   classification_loss {
     weighted_softmax {
       logit_scale: 2.0
     }
   }
   localization_loss {
     weighted_l2 {
     }
   }
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     classification_loss, _, _, _, _ = losses_builder.build(losses_proto)
     self.assertTrue(
         isinstance(classification_loss,
                    losses.WeightedSoftmaxClassificationLoss))
Exemple #10
0
 def test_build_weighted_sigmoid_focal_classification_loss(self):
     losses_text_proto = """
   classification_loss {
     weighted_sigmoid_focal {
     }
   }
   localization_loss {
     weighted_l2 {
     }
   }
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     classification_loss, _, _, _, _ = losses_builder.build(losses_proto)
     self.assertTrue(
         isinstance(classification_loss,
                    losses.SigmoidFocalClassificationLoss))
     self.assertAlmostEqual(classification_loss._alpha, None)
     self.assertAlmostEqual(classification_loss._gamma, 2.0)
Exemple #11
0
 def test_build_hard_example_miner_for_localization_loss(self):
     losses_text_proto = """
   localization_loss {
     weighted_l2 {
     }
   }
   classification_loss {
     weighted_softmax {
     }
   }
   hard_example_miner {
     loss_type: LOCALIZATION
   }
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     _, _, _, _, hard_example_miner = losses_builder.build(losses_proto)
     self.assertTrue(isinstance(hard_example_miner,
                                losses.HardExampleMiner))
     self.assertEqual(hard_example_miner._loss_type, 'loc')
Exemple #12
0
 def test_anchorwise_output(self):
     losses_text_proto = """
   classification_loss {
     weighted_sigmoid {
       anchorwise_output: true
     }
   }
   localization_loss {
     weighted_l2 {
     }
   }
 """
     losses_proto = losses_pb2.Loss()
     text_format.Merge(losses_text_proto, losses_proto)
     classification_loss, _, _, _, _ = losses_builder.build(losses_proto)
     self.assertTrue(
         isinstance(classification_loss,
                    losses.WeightedSigmoidClassificationLoss))
     predictions = tf.constant([[[0.0, 1.0, 0.0], [0.0, 0.5, 0.5]]])
     targets = tf.constant([[[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]])
     weights = tf.constant([[1.0, 1.0]])
     loss = classification_loss(predictions, targets, weights=weights)
     self.assertEqual(loss.shape, [1, 2])