def _test_grad_different_shape(self, input_shape, output_shape): with self.test_session(): test_image_shape = input_shape test_image = np.random.randn(*test_image_shape) test_image_tensor = constant_op.constant(test_image, shape=test_image_shape) test_transform = image_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 = image_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_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 = constant_op.constant( test_image, shape=test_image_shape) test_transform = image_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 = image_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 rotate(images, angles, interpolation="NEAREST"): """A variant of tensorflow.contrib.image.python.ops.image_ops.rotate that only works on 4D tensors, without checking image rank. when the shape of images is unknown, len(image_or_images.get_shape()) will raise error. Rotate image(s) by the passed angle(s) in radians. Args: images: A tensor of shape (num_images, num_rows, num_columns, num_channels) (NHWC), (num_rows, num_columns, num_channels) (HWC), or (num_rows, num_columns) (HW). angles: A scalar angle to rotate all images by, or (if images has rank 4) a vector of length num_images, with an angle for each image in the batch. interpolation: Interpolation mode. Supported values: "NEAREST", "BILINEAR". Returns: Image(s) with the same type and shape as `images`, rotated by the given angle(s). Empty space due to the rotation will be filled with zeros. Raises: TypeError: If `image` is an invalid type. """ images = ops.convert_to_tensor(images, name="images") image_height = math_ops.cast(array_ops.shape(images)[1], dtypes.float32)[None] image_width = math_ops.cast(array_ops.shape(images)[2], dtypes.float32)[None] output = transform_4d(images, angles_to_projective_transforms( angles, image_height, image_width), interpolation=interpolation) return output
def _test_grad(self, shape_to_test): with self.test_session(): test_image_shape = shape_to_test test_image = np.random.randn(*test_image_shape) test_image_tensor = constant_op.constant(test_image, shape=test_image_shape) test_transform = image_ops.angles_to_projective_transforms(np.pi / 2, 4, 4) test_transform_shape = test_transform.shape output_shape = test_image_shape output = image_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_grad(self, shape_to_test): with self.test_session(): test_image_shape = shape_to_test test_image = np.random.randn(*test_image_shape) test_image_tensor = constant_op.constant(test_image, shape=test_image_shape) test_transform = image_ops.angles_to_projective_transforms( np.pi / 2, 4, 4) output_shape = test_image_shape output = image_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: with self.test_session(): image = constant_op.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 = image_ops.angles_to_projective_transforms( np.pi / 2, 4, 4) # Translate right by 1 (the transformation matrix is always inverted, # hence the -1). translation = constant_op.constant([1, 0, -1, 0, 1, 0, 0, 0], dtype=dtypes.float32) composed = image_ops.compose_transforms(rotation, translation) image_transformed = image_ops.transform(image, composed) self.assertAllEqual( image_transformed.eval(), [[0, 0, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1], [0, 1, 1, 1]])
def test_compose(self): for dtype in _DTYPES: with self.cached_session(): image = constant_op.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 = image_ops.angles_to_projective_transforms(np.pi / 2, 4, 4) # Translate right by 1 (the transformation matrix is always inverted, # hence the -1). translation = constant_op.constant([1, 0, -1, 0, 1, 0, 0, 0], dtype=dtypes.float32) composed = image_ops.compose_transforms(rotation, translation) image_transformed = image_ops.transform(image, composed) self.assertAllEqual(image_transformed.eval(), [[0, 0, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1], [0, 1, 1, 1]])