def test_similar_vs_different_inputs(self): loss = losses.PerceptualLoss() pure_bright = tf.ones((3, 256, 256, 3)) * tf.constant([0.9, 0.7, 0.7]) pure_dark = tf.ones((3, 256, 256, 3)) * tf.constant([0.5, 0.2, 0.2]) speckles = tf.random.uniform((3, 256, 256, 3)) self.assertAllGreater(loss(pure_bright, speckles), loss(pure_bright, pure_dark))
def test_composite_loss_equals_sum_of_components(self): composite = losses.CompositeLoss() mae = tf.keras.losses.MAE vgg = losses.PerceptualLoss() composite.add_loss(mae, 1.0) composite.add_loss(vgg, 2.0) y_true = tf.random.uniform((1, 192, 256, 3)) y_pred = tf.random.uniform((1, 192, 256, 3)) loss_value = composite(y_true, y_pred) mae_loss_value = tf.math.reduce_mean(mae(y_true, y_pred)) vgg_loss_value = vgg(y_true, y_pred) self.assertAllClose(loss_value, mae_loss_value * 1.0 + vgg_loss_value * 2.0)
def test_different_inputs(self): loss = losses.PerceptualLoss() image_1 = tf.zeros((2, 192, 256, 3)) image_2 = tf.random.uniform((2, 192, 256, 3)) self.assertAllGreater(loss(image_1, image_2), 1.0)
def test_identical_inputs(self): loss = losses.PerceptualLoss() images = tf.random.uniform((2, 192, 256, 3)) self.assertAllClose(loss(images, images), 0.0)