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 transform_fn(x): x.set_shape(input_shape) transform = transform_ops.angles_to_projective_transforms( np.pi / 2, 4, 4) return transform_ops.transform(images=x, transforms=transform, output_shape=resize_shape)
def test_compose_rotate(dtype): 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.float32) composed = transform_ops.compose_transforms([rotation, translation]) image_transformed = transform_ops.transform(image, composed) np.testing.assert_equal( image_transformed.numpy(), [[0, 0, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1], [0, 1, 1, 1]], )
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)