def test_transform_data_types(self): for dtype in _DTYPES: image = tf.constant([[1, 2], [3, 4]], dtype=dtype) with self.test_session(use_gpu=True): self.assertAllEqual( np.array([[4, 4], [4, 4]]).astype(dtype.as_numpy_dtype()), transform_ops.transform(image, [1] * 8))
def test_extreme_projective_transform(self): for dtype in _DTYPES: image = tf.constant( [[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1]], dtype=dtype) transformation = tf.constant([1, 0, 0, 0, 1, 0, -1, 0], tf.dtypes.float32) image_transformed = transform_ops.transform(image, transformation) self.assertAllEqual( [[1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]], image_transformed)
def _test_grad(self, shape_to_test): with self.cached_session(): test_image_shape = shape_to_test test_image = np.random.randn(*test_image_shape) test_image_tensor = tf.constant(test_image, shape=test_image_shape) test_transform = transform_ops.angles_to_projective_transforms( np.pi / 2, 4, 4) output_shape = test_image_shape output = transform_ops.transform(test_image_tensor, test_transform) left_err = gradient_checker.compute_gradient_error( test_image_tensor, test_image_shape, output, output_shape, x_init_value=test_image) self.assertLess(left_err, 1e-10)
def test_compose(self): for dtype in _DTYPES: image = tf.constant( [[1, 1, 1, 0], [1, 0, 0, 0], [1, 1, 1, 0], [0, 0, 0, 0]], dtype=dtype) # Rotate counter-clockwise by pi / 2. rotation = transform_ops.angles_to_projective_transforms( np.pi / 2, 4, 4) # Translate right by 1 (the transformation matrix is always inverted, # hence the -1). translation = tf.constant([1, 0, -1, 0, 1, 0, 0, 0], dtype=tf.dtypes.float32) composed = transform_ops.compose_transforms(rotation, translation) image_transformed = transform_ops.transform(image, composed) self.assertAllEqual( [[0, 0, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1], [0, 1, 1, 1]], image_transformed)
def _test_grad_different_shape(self, input_shape, output_shape): with self.cached_session(): test_image_shape = input_shape test_image = np.random.randn(*test_image_shape) test_image_tensor = tf.constant(test_image, shape=test_image_shape) test_transform = transform_ops.angles_to_projective_transforms( np.pi / 2, 4, 4) if len(output_shape) == 2: resize_shape = output_shape elif len(output_shape) == 3: resize_shape = output_shape[0:2] elif len(output_shape) == 4: resize_shape = output_shape[1:3] output = transform_ops.transform(images=test_image_tensor, transforms=test_transform, output_shape=resize_shape) left_err = gradient_checker.compute_gradient_error( test_image_tensor, test_image_shape, output, output_shape, x_init_value=test_image) self.assertLess(left_err, 1e-10)
def test_transform_static_output_shape(self): image = tf.constant([[1., 2.], [3., 4.]]) result = transform_ops.transform(image, tf.random.uniform([8], -1, 1), output_shape=tf.constant([3, 5])) self.assertAllEqual([3, 5], result.shape)
def test_transform_eager(self): image = tf.constant([[1., 2.], [3., 4.]]) self.assertAllEqual(np.array([[4, 4], [4, 4]]), transform_ops.transform(image, [1] * 8))