Add tests for Tversky loss
@@ -255,6 +255,48 @@ class DiceTestNoSquare(tf.test.TestCase):
self.assertAlmostEqual(one_minus_dice_score.eval(), 1.0)
+class TverskyTest(tf.test.TestCase):
+ def test_tversky_index(self):
+ with self.test_session():
+ predicted = tf.constant(
+ [[0, 10], [10, 0], [10, 0], [10, 0]],
+ dtype=tf.float32, name='predicted')
+ labels = tf.constant([1, 0, 0, 0], dtype=tf.int64, name='labels')
+ predicted, labels = [tf.expand_dims(x, axis=0) for x in (predicted, labels)]
+
+ test_loss_func = LossFunction(2, loss_type='Tversky')
+ one_minus_dice_score = test_loss_func(predicted, labels)
+ self.assertAllClose(one_minus_dice_score.eval(), 0.0, atol=1e-4)
+
+ def test_tversky_index_weights(self):
+ with self.test_session():
+ weights = tf.constant([[1, 1, 0, 0]], dtype=tf.float32,
+ name='weights')
+ predicted = tf.constant(
+ [[0, 10], [10, 0], [10, 0], [10, 0]],
+ dtype=tf.float32, name='predicted')
+ labels = tf.constant([[1, 0, 0, 0]], dtype=tf.int64, name='labels')
+ predicted, labels = [tf.expand_dims(x, axis=0) for x in (predicted, labels)]
+
+ test_loss_func = LossFunction(2,
+ loss_type='Tversky')
+ one_minus_dice_score = test_loss_func(predicted, labels,
+ weight_map=weights)
+ self.assertAllClose(one_minus_dice_score.eval(), 0.0, atol=1e-4)
+
+ def test_wrong_prediction(self):
+ with self.test_session():
+ predicted = tf.constant(
+ [[0, 100]],
+ dtype=tf.float32, name='predicted')
+ labels = tf.constant([0], dtype=tf.int64, name='labels')
+ predicted, labels = [tf.expand_dims(x, axis=0) for x in (predicted, labels)]
+
+ test_loss_func = LossFunction(2, loss_type='Tversky')
+ one_minus_dice_score = test_loss_func(predicted, labels)
+ self.assertAlmostEqual(one_minus_dice_score.eval(), 1.0)
+
+
class DiceDenseTest(tf.test.TestCase):
def test_dice_dense_score(self):
with self.test_session():
