def test_perspective_transform_jacobian_random(self): """Tests the Jacobian of the transform function.""" tensor_shape = np.random.randint(2, 4, size=4) image_init = np.random.uniform(0.0, 1.0, size=tensor_shape.tolist()) transformation_init = np.random.uniform( 0.0, 1.0, size=(tensor_shape[0], 3, 3)) self.assert_jacobian_is_correct_fn( lambda x: transformer.perspective_transform(x, transformation_init), [image_init]) self.assert_jacobian_is_correct_fn( lambda x: transformer.perspective_transform(image_init, x), [transformation_init])
def test_perspective_transform_integer_centers_preset(self, dtype, interpolation): """Tests that we can reproduce the results of tfa_image.transform.""" image = tf.constant( ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0), (10.0, 11.0, 12.0)), dtype=dtype) scale = 3 transformation = tf.constant( ((1.0 / scale, 0.0, 0.0), (0.0, 1.0 / scale, 0.0), (0.0, 0.0, 1.0)), dtype=dtype) image_shape = tf.shape(input=image) image_resized_shape = image_shape * scale image = image[tf.newaxis, ..., tf.newaxis] transformation = transformation[tf.newaxis, ...] image_resized = tfa_image.transform( tf.cast(image, tf.float32), tf.cast( tfa_image.transform_ops.matrices_to_flat_transforms(transformation), tf.float32), interpolation=interpolation, output_shape=image_resized_shape) image_transformed = transformer.perspective_transform( image, transformation, resampling_type=transformer.ResamplingType.NEAREST if interpolation == "NEAREST" else transformer.ResamplingType.BILINEAR, pixel_type=transformer.PixelType.INTEGER, output_shape=image_resized_shape) self.assertAllClose(image_resized, image_transformed)
def test_perspective_transform_half_integer_centers_preset( self, dtype, interpolation): """Tests that we can reproduce the results of tf.image.resize.""" image = tf.constant( ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0), (10.0, 11.0, 12.0)), dtype=dtype) scale = 3 transformation = tf.constant( ((1.0 / scale, 0.0, 0.0), (0.0, 1.0 / scale, 0.0), (0.0, 0.0, 1.0)), dtype=dtype) image_shape = tf.shape(input=image) image_resized_shape = image_shape * scale image = image[tf.newaxis, ..., tf.newaxis] transformation = transformation[tf.newaxis, ...] image_resized = tf.image.resize( image, size=image_resized_shape, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR if interpolation == "NEAREST" else tf.image.ResizeMethod.BILINEAR) image_transformed = transformer.perspective_transform( image, transformation, resampling_type=transformer.ResamplingType.NEAREST if interpolation == "NEAREST" else transformer.ResamplingType.BILINEAR, border_type=transformer.BorderType.DUPLICATE, output_shape=image_resized_shape) self.assertAllClose(image_resized, image_transformed)