Example #1
0
  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)
Example #2
0
    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)
Example #3
0
 def test_transform_data_types(self):
     for dtype in _DTYPES:
         image = constant_op.constant([[1, 2], [3, 4]], dtype=dtype)
         value = image_ops.transform(image, [1] * 8)
         with self.test_session(use_gpu=True):
             self.assertAllEqual(
                 value.eval(),
                 np.array([[4, 4], [4, 4]]).astype(dtype.as_numpy_dtype()))
Example #4
0
 def test_transform_data_types(self):
   for dtype in _DTYPES:
     image = constant_op.constant([[1, 2], [3, 4]], dtype=dtype)
     value = image_ops.transform(image, [1] * 8)
     with self.test_session(use_gpu=True):
       self.assertAllEqual(
           value.eval(),
           np.array([[4, 4], [4, 4]]).astype(dtype.as_numpy_dtype()))
Example #5
0
 def test_extreme_projective_transform(self):
     for dtype in _DTYPES:
         with self.test_session():
             image = constant_op.constant(
                 [[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1]],
                 dtype=dtype)
             transformation = constant_op.constant(
                 [1, 0, 0, 0, 1, 0, -1, 0], dtypes.float32)
             image_transformed = image_ops.transform(image, transformation)
             self.assertAllEqual(
                 image_transformed.eval(),
                 [[1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]])
Example #6
0
 def test_extreme_projective_transform(self):
   for dtype in _DTYPES:
     with self.cached_session():
       image = constant_op.constant(
           [[1, 0, 1, 0],
            [0, 1, 0, 1],
            [1, 0, 1, 0],
            [0, 1, 0, 1]], dtype=dtype)
       transformation = constant_op.constant([1, 0, 0, 0, 1, 0, -1, 0],
                                             dtypes.float32)
       image_transformed = image_ops.transform(image, transformation)
       self.assertAllEqual(image_transformed.eval(),
                           [[1, 0, 0, 0],
                            [0, 0, 0, 0],
                            [1, 0, 0, 0],
                            [0, 0, 0, 0]])
Example #7
0
  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)
Example #8
0
    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)
Example #9
0
 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]])
Example #10
0
 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]])
Example #11
0
 def test_transform_static_output_shape(self):
   image = constant_op.constant([[1., 2.], [3., 4.]])
   result = image_ops.transform(
       image, random_ops.random_uniform([8], -1, 1),
       output_shape=constant_op.constant([3, 5]))
   self.assertAllEqual([3, 5], result.get_shape())
Example #12
0
 def test_transform_static_output_shape(self):
     image = constant_op.constant([[1., 2.], [3., 4.]])
     result = image_ops.transform(image,
                                  random_ops.random_uniform([8], -1, 1),
                                  output_shape=constant_op.constant([3, 5]))
     self.assertAllEqual([3, 5], result.get_shape())
 def test_transform_eager(self):
     image = constant_op.constant([[1., 2.], [3., 4.]])
     value = image_ops.transform(image, [1] * 8)
     with self.test_session(use_gpu=True):
         self.assertAllEqual(self.evaluate(value), np.array([[4, 4], [4,
                                                                      4]]))
Example #14
0
 def test_transform_eager(self):
   image = constant_op.constant([[1., 2.], [3., 4.]])
   value = image_ops.transform(image, [1] * 8)
   with self.test_session(use_gpu=True):
     self.assertAllEqual(self.evaluate(value), np.array([[4, 4], [4, 4]]))