def testTintInvariant(self): """Tests tint invariant property.""" batch_size = 100 pred_illum_rgb = tf.constant(np.random.rand(batch_size, 3)) true_illum_rgb = tf.constant(np.random.rand(batch_size, 3)) true_scene_rgb = tf.constant(np.exp(np.random.rand(batch_size, 3))) tint = tf.constant(np.random.rand(1, 3)) self.assertAllClose( losses.anisotropic_reproduction_error(pred_illum_rgb, true_illum_rgb, true_scene_rgb), losses.anisotropic_reproduction_error( tf.multiply(tint, pred_illum_rgb), tf.multiply(tint, true_illum_rgb), true_scene_rgb))
def testAgainstRefImpl(self): batch_size = 100 pred_illum_rgb = np.random.uniform(low=sys.float_info.epsilon, size=(batch_size, 3)) pred_illum_rgb /= np.sum(pred_illum_rgb, axis=1, keepdims=True) true_illum_rgb = np.random.uniform(low=sys.float_info.epsilon, size=(batch_size, 3)) true_illum_rgb /= np.sum(true_illum_rgb, axis=1, keepdims=True) true_wb_rgb = 1.0 / true_illum_rgb true_wb_rgb /= np.expand_dims(true_wb_rgb[:, 1], axis=1) true_scene_rgb = np.random.uniform(low=sys.float_info.epsilon, size=(batch_size, 3)) input_scene_rgb = true_scene_rgb / true_wb_rgb height = 64 width = 48 rgb_stats = np.tile(input_scene_rgb[:, np.newaxis, np.newaxis, :], reps=[1, height, width, 1]) with self.test_session() as sess: actual_result = sess.run( losses.anisotropic_reproduction_loss( ops.rgb_to_uv(tf.constant(pred_illum_rgb)), ops.rgb_to_uv(tf.constant(true_illum_rgb)), tf.constant(rgb_stats))) expected_result = sess.run( losses.anisotropic_reproduction_error( tf.constant(pred_illum_rgb), tf.constant(true_illum_rgb), tf.constant(true_scene_rgb))) np.testing.assert_almost_equal(actual_result, expected_result)
def testAgainstIdenticalIluminations(self): """Tests the case where predicted ilumminants is as same as ground truth.""" batch_size = 100 pred_illum_rgb = tf.constant(np.random.rand(batch_size, 3)) true_scene_rgb = tf.constant(np.random.rand(batch_size, 3)) # The errors should drop to zeros. self.assertAllClose(losses.anisotropic_reproduction_error( pred_illum_rgb, pred_illum_rgb, true_scene_rgb), np.zeros(batch_size), atol=1e-5)
def testNonNegativeScaleInvariant(self): """Tests invariant property wrt (non-negative) scale of true_scene_rgb.""" batch_size = 100 pred_illum_rgb = tf.constant(np.random.rand(batch_size, 3)) true_illum_rgb = tf.constant(np.random.rand(batch_size, 3)) true_scene_rgb = tf.constant(np.random.rand(batch_size, 3)) scale = tf.constant(np.exp(np.random.rand(batch_size, 1))) self.assertAllClose( losses.anisotropic_reproduction_error(pred_illum_rgb, true_illum_rgb, true_scene_rgb), losses.anisotropic_reproduction_error( pred_illum_rgb, true_illum_rgb, tf.multiply(scale, true_scene_rgb))) self.assertAllClose( losses.anisotropic_reproduction_error(pred_illum_rgb, true_illum_rgb, true_scene_rgb), losses.anisotropic_reproduction_error( pred_illum_rgb, tf.multiply(scale, true_illum_rgb), true_scene_rgb)) self.assertAllClose( losses.anisotropic_reproduction_error(pred_illum_rgb, true_illum_rgb, true_scene_rgb), losses.anisotropic_reproduction_error( tf.multiply(scale, pred_illum_rgb), true_illum_rgb, true_scene_rgb))
def testZeroedWeightsSingleChannel(self): """Tests one of the true_scene_rgb is zero.""" batch_size = 100 pred_illum_rgb = np.random.rand(batch_size, 3) true_illum_rgb = np.random.rand(batch_size, 3) true_scene_rgb = np.exp(np.random.rand(batch_size, 3)) for c in range(3): zeroed_true_scene_rgb = copy.deepcopy(true_scene_rgb) zeroed_true_scene_rgb[:, c] = 0 true_illum_rgb_adjusted = copy.deepcopy(true_illum_rgb) true_illum_rgb_adjusted[:, c] = true_illum_rgb_adjusted[:, c] + 0.1 # The error should be invariant on the channel with zero weight. self.assertAllClose( losses.anisotropic_reproduction_error( tf.constant(pred_illum_rgb), tf.constant(true_illum_rgb), tf.constant(zeroed_true_scene_rgb)), losses.anisotropic_reproduction_error( tf.constant(pred_illum_rgb), tf.constant(true_illum_rgb_adjusted), tf.constant(zeroed_true_scene_rgb)))
def testConstantWeight(self): """Tests constant weights.""" batch_size = 100 pred_illum_rgb = tf.constant(np.random.rand(batch_size, 3)) true_illum_rgb = tf.constant(np.random.rand(batch_size, 3)) true_scene_rgb = tf.constant( np.exp(np.random.rand(batch_size, 1)) * np.ones(shape=(batch_size, 3))) self.assertAllClose( losses.anisotropic_reproduction_error(pred_illum_rgb, true_illum_rgb, true_scene_rgb), losses.reproduction_error(pred_illum_rgb, true_illum_rgb))
def testZeroedWeightsDualChannels(self): """Tests two of the true_scene_rgb are zero.""" batch_size = 100 pred_illum_rgb = np.random.rand(batch_size, 3) true_illum_rgb = np.random.rand(batch_size, 3) true_scene_rgb = np.exp(np.random.rand(batch_size, 3)) for c in range(3): zeroed_true_scene_rgb = np.zeros(shape=(batch_size, 3)) zeroed_true_scene_rgb[:, c] = true_scene_rgb[:, c] # The errors should drop to zeros. self.assertAllClose(losses.anisotropic_reproduction_error( tf.constant(pred_illum_rgb), tf.constant(true_illum_rgb), tf.constant(zeroed_true_scene_rgb)), np.zeros(batch_size), atol=1e-5)
def testAgainstRefImpl(self): """Tests against reference implementation.""" batch_size = 100 pred_illum_rgb = np.random.rand(batch_size, 3) true_illum_rgb = np.random.rand(batch_size, 3) true_scene_rgb = np.random.rand(batch_size, 3) expected_angle = (180. / math.pi) * np.arccos( np.clip( np.sum(_weighted_normalized(true_illum_rgb / pred_illum_rgb, true_scene_rgb) * _weighted_normalized(np.ones(shape=(1, 3)), true_scene_rgb), axis=1), -1, 1)) self.assertAllClose(losses.anisotropic_reproduction_error( tf.constant(pred_illum_rgb), tf.constant(true_illum_rgb), tf.constant(true_scene_rgb)), expected_angle, atol=1e-5)