def test_build_bootstrapped_sigmoid_classification_loss(self): losses_text_proto = """ classification_loss { bootstrapped_sigmoid { alpha: 0.5 } } 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.BootstrappedSigmoidClassificationLoss))
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))
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 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)
def test_build_weighted_smooth_l1_localization_loss_non_default_delta( self): losses_text_proto = """ localization_loss { weighted_smooth_l1 { delta: 0.1 } } 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.WeightedSmoothL1LocalizationLoss)) self.assertAlmostEqual(localization_loss._delta, 0.1)
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')
def test_anchorwise_output(self): losses_text_proto = """ localization_loss { weighted_smooth_l1 { } } 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.WeightedSmoothL1LocalizationLoss)) predictions = tf.constant([[[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]]]) targets = tf.constant([[[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]]]) weights = tf.constant([[1.0, 1.0]]) loss = localization_loss(predictions, targets, weights=weights) self.assertEqual(loss.shape, [1, 2])
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, 3])