def test_downsample_jacobian_random(self): """Tests the Jacobian for random inputs.""" downsample = lambda image: pyramid.downsample(image, num_levels=_NUM_LEVELS) tensor_shape = np.random.randint(1, 5, size=4).tolist() image_random_init = np.random.uniform(size=tensor_shape) for level in range(_NUM_LEVELS): self.assert_jacobian_is_correct_fn( lambda x, level=level: downsample(x)[level], [image_random_init])
def test_downsample_jacobian_random(self): """Tests the Jacobian for random inputs.""" downsample = lambda image: pyramid.downsample(image, num_levels=4) tensor_shape = np.random.randint(1, 5, size=(4)).tolist() image_random_init = np.random.uniform(size=tensor_shape) image_random = tf.convert_to_tensor(value=image_random_init) downsample_random = downsample(image_random) for level in downsample_random: self.assert_jacobian_is_correct(image_random, image_random_init, level)
def test_downsample_preset(self, image_high, image_low): """Tests that the downsample works as expected.""" downsample = lambda image: pyramid.downsample(image, num_levels=1) image_high = tf.expand_dims(tf.expand_dims(image_high, axis=-1), axis=0) image_low = tf.expand_dims(tf.expand_dims(image_low, axis=-1), axis=0) pyr = downsample(image_high) with self.subTest(name="image_high"): self.assertAllClose(image_high, pyr[0]) with self.subTest(name="image_low"): self.assertAllClose(image_low, pyr[1])
def test_downsample_jacobian_random(self): """Tests the Jacobian for random inputs.""" downsample = lambda image: pyramid.downsample(image, num_levels=_NUM_LEVELS) tensor_shape = np.random.randint(1, 5, size=4).tolist() image_random_init = np.random.uniform(size=tensor_shape) for level in range(_NUM_LEVELS): # We skip testing level = 0, which returns the image as is. In graph mode, # the gradient calculation fails when there are no nodes in the graph. if level == 0 and not tf.executing_eagerly(): continue self.assert_jacobian_is_correct_fn( lambda x, level=level: downsample(x)[level], [image_random_init])
def test_downsample_exception_raised(self, error_msg, *shape): """Tests that the shape exceptions are properly raised.""" downsample = lambda image: pyramid.downsample(image, num_levels=_NUM_LEVELS) self.assert_exception_is_raised(downsample, error_msg, shape)
def test_downsample_exception_not_raised(self, *shape): """Tests that the shape exceptions are not raised.""" downsample = lambda image: pyramid.downsample(image, num_levels=_NUM_LEVELS) self.assert_exception_is_not_raised(downsample, shape)