def testCompareGpuVsCpu(self):
        in_shape = [1, 4, 6, 3]
        out_shape = [1, 8, 16, 3]

        for nptype in self.TYPES:
            x = np.arange(0,
                          np.prod(in_shape)).reshape(in_shape).astype(nptype)
            for align_corners in [True, False]:
                with self.test_session(use_gpu=False):
                    input_tensor = constant_op.constant(x, shape=in_shape)
                    resize_out = image_ops.resize_nearest_neighbor(
                        input_tensor,
                        out_shape[1:3],
                        align_corners=align_corners)
                    grad_cpu = gradient_checker.compute_gradient(
                        input_tensor,
                        in_shape,
                        resize_out,
                        out_shape,
                        x_init_value=x)

                with self.test_session(use_gpu=True):
                    input_tensor = constant_op.constant(x, shape=in_shape)
                    resize_out = image_ops.resize_nearest_neighbor(
                        input_tensor,
                        out_shape[1:3],
                        align_corners=align_corners)
                    grad_gpu = gradient_checker.compute_gradient(
                        input_tensor,
                        in_shape,
                        resize_out,
                        out_shape,
                        x_init_value=x)
                self.assertAllClose(grad_cpu, grad_gpu, rtol=1e-5, atol=1e-5)
Example #2
0
def resize_image(image,
                 height_factor,
                 width_factor,
                 data_format='NHWC',
                 interpolation='nearest'):
    if data_format[-1] == 'C':
        row, col = 1, 2
    else:
        row, col = 2, 3
    old_shape = int_shape(image)
    spatial = array_ops.shape(image)[row:col + 1]
    spatial *= array_ops.constant(np.array([height_factor, width_factor]),
                                  'int32')
    if data_format[-1] != 'C':
        image = transpose_to_channels_last(image)
    if interpolation == 'nearest':
        image = image_ops.resize_nearest_neighbor(image, size=spatial)
    elif interpolation == 'bilinear':
        image = image_ops.resize_bilinear(image, size=spatial)
    else:
        raise ValueError("Unknown interpolation named " + interpolation)
    height = old_shape[row] or old_shape[row] * height_factor
    width = old_shape[col] or old_shape[col] * width_factor
    if data_format[-1] != 'C':
        image = transpose_to_channels_first(image)
        image.set_shape(old_shape[:2] + (height, width))
    else:
        image.set_shape((old_shape[0], ) + (height, width) + (old_shape[-1], ))
    return image
  def testCompareGpuVsCpu(self):
    in_shape = [1, 4, 6, 3]
    out_shape = [1, 8, 16, 3]

    for nptype in self.TYPES:
      x = np.arange(0, np.prod(in_shape)).reshape(in_shape).astype(nptype)
      for align_corners in [True, False]:
        with self.test_session(use_gpu=False):
          input_tensor = constant_op.constant(x, shape=in_shape)
          resize_out = image_ops.resize_nearest_neighbor(
              input_tensor, out_shape[1:3], align_corners=align_corners)
          grad_cpu = gradient_checker.compute_gradient(
              input_tensor, in_shape, resize_out, out_shape, x_init_value=x)

        with self.test_session(use_gpu=True):
          input_tensor = constant_op.constant(x, shape=in_shape)
          resize_out = image_ops.resize_nearest_neighbor(
              input_tensor, out_shape[1:3], align_corners=align_corners)
          grad_gpu = gradient_checker.compute_gradient(
              input_tensor, in_shape, resize_out, out_shape, x_init_value=x)
        self.assertAllClose(grad_cpu, grad_gpu, rtol=1e-5, atol=1e-5)
    def testShapeIsCorrectAfterOp(self):
        in_shape = [1, 2, 2, 1]
        out_shape = [1, 4, 6, 1]

        for nptype in self.TYPES:
            x = np.arange(0, 4).reshape(in_shape).astype(nptype)

            input_tensor = constant_op.constant(x, shape=in_shape)
            resize_out = image_ops.resize_nearest_neighbor(
                input_tensor, out_shape[1:3])
            with self.cached_session(use_gpu=True):
                self.assertEqual(out_shape, list(resize_out.get_shape()))
                resize_out = self.evaluate(resize_out)
            self.assertEqual(out_shape, list(resize_out.shape))
  def testGradFromResizeToSmallerInBothDims(self):
    in_shape = [1, 4, 6, 1]
    out_shape = [1, 2, 3, 1]

    for nptype in self.TYPES:
      x = np.arange(0, 24).reshape(in_shape).astype(nptype)

      with self.test_session(use_gpu=True):
        input_tensor = constant_op.constant(x, shape=in_shape)
        resize_out = image_ops.resize_nearest_neighbor(input_tensor,
                                                       out_shape[1:3])
        err = gradient_checker.compute_gradient_error(
            input_tensor, in_shape, resize_out, out_shape, x_init_value=x)
      self.assertLess(err, 1e-3)
Example #6
0
  def testGradFromResizeToSmallerInBothDims(self):
    in_shape = [1, 4, 6, 1]
    out_shape = [1, 2, 3, 1]

    for nptype in self.TYPES:
      x = np.arange(0, 24).reshape(in_shape).astype(nptype)

      with self.cached_session(use_gpu=True):
        input_tensor = constant_op.constant(x, shape=in_shape)
        resize_out = image_ops.resize_nearest_neighbor(input_tensor,
                                                       out_shape[1:3])
        err = gradient_checker.compute_gradient_error(
            input_tensor, in_shape, resize_out, out_shape, x_init_value=x)
      self.assertLess(err, 1e-3)
  def testShapeIsCorrectAfterOp(self):
    in_shape = [1, 2, 2, 1]
    out_shape = [1, 4, 6, 1]

    for nptype in self.TYPES:
      x = np.arange(0, 4).reshape(in_shape).astype(nptype)

      with self.test_session(use_gpu=True) as sess:
        input_tensor = constant_op.constant(x, shape=in_shape)
        resize_out = image_ops.resize_nearest_neighbor(input_tensor,
                                                       out_shape[1:3])
        self.assertEqual(out_shape, list(resize_out.get_shape()))

        resize_out = sess.run(resize_out)
      self.assertEqual(out_shape, list(resize_out.shape))
Example #8
0
 def testExceptionThrowing(self, align_corners, half_pixel_centers,
                           data_type):
     with self.session(), test_util.force_gpu():
         input_image = array_ops.zeros((1, 2, 2, 1), dtype=data_type)
         with backprop.GradientTape() as tape:
             tape.watch(input_image)
             output_image = image_ops.resize_nearest_neighbor(
                 input_image, (3, 3),
                 align_corners=align_corners,
                 half_pixel_centers=half_pixel_centers)
         with self.assertRaisesRegex(
                 errors.UnimplementedError,
                 'A deterministic GPU implementation of ResizeNearestNeighborGrad'
                 + ' is not currently available.'):
             gradient = tape.gradient(output_image, input_image)
             self.evaluate(gradient)
 def resize_nn(t, shape=out_shape, align_corners=align_corners):
     return image_ops.resize_nearest_neighbor(
         t, shape[1:3], align_corners=align_corners)
 def resize_nn(t, shape=out_shape):
     return image_ops.resize_nearest_neighbor(t, shape[1:3])
Example #11
0
 def call(self, inputs, training=None):
     # return tf.image.resize(inputs, self.size)
     # return resize_images(inputs, self.size)
     return image_ops.resize_nearest_neighbor(inputs, self.size)