def testIdentityGrads(self): """Tests that Gradients for 1.0 scale should be ones for some kernels.""" in_shape = [1, 2, 3, 1] out_shape = [1, 4, 6, 1] x = np.arange(0, 6).reshape(in_shape).astype(np.float32) kernel_types = [ 'lanczos1', 'lanczos3', 'lanczos5', 'triangle', 'keyscubic' ] scale = (1.0, 1.0) translation = (0.0, 0.0) antialias = True for kernel_type in kernel_types: with self.cached_session(): input_tensor = constant_op.constant(x, shape=in_shape) with backprop.GradientTape() as tape: tape.watch(input_tensor) scale_and_translate_out = image_ops.scale_and_translate( input_tensor, out_shape[1:3], scale=constant_op.constant(scale), translation=constant_op.constant(translation), kernel_type=kernel_type, antialias=antialias) grad = tape.gradient(scale_and_translate_out, input_tensor)[0] grad_v = self.evaluate(grad) self.assertAllClose(np.ones_like(grad_v), grad_v)
def testGrads(self): in_shape = [1, 2, 3, 1] out_shape = [1, 4, 6, 1] x = np.arange(0, 6).reshape(in_shape).astype(np.float32) kernel_types = [ 'lanczos1', 'lanczos3', 'lanczos5', 'gaussian', 'box', 'triangle', 'keyscubic', 'mitchellcubic' ] scales = [(1.0, 1.0), (0.37, 0.47), (2.1, 2.1)] translations = [(0.0, 0.0), (3.14, 1.19), (2.1, 3.1), (100.0, 200.0)] for scale in scales: for translation in translations: for kernel_type in kernel_types: for antialias in [True, False]: with self.cached_session(): input_tensor = constant_op.constant(x, shape=in_shape) scale_and_translate_out = image_ops.scale_and_translate( input_tensor, out_shape[1:3], scale=constant_op.constant(scale), translation=constant_op.constant(translation), kernel_type=kernel_type, antialias=antialias) err = gradient_checker.compute_gradient_error( input_tensor, in_shape, scale_and_translate_out, out_shape, x_init_value=x) self.assertLess(err, 1e-3)
def testIdentityGrads(self): """Tests that Gradients for 1.0 scale should be ones for some kernels.""" in_shape = [1, 2, 3, 1] out_shape = [1, 4, 6, 1] x = np.arange(0, 6).reshape(in_shape).astype(np.float32) kernel_types = ['lanczos1', 'lanczos3', 'lanczos5', 'triangle', 'keyscubic'] scale = (1.0, 1.0) translation = (0.0, 0.0) antialias = True for kernel_type in kernel_types: with self.cached_session(): input_tensor = constant_op.constant(x, shape=in_shape) with backprop.GradientTape() as tape: tape.watch(input_tensor) scale_and_translate_out = image_ops.scale_and_translate( input_tensor, out_shape[1:3], scale=constant_op.constant(scale), translation=constant_op.constant(translation), kernel_type=kernel_type, antialias=antialias) grad = tape.gradient(scale_and_translate_out, input_tensor)[0] grad_v = self.evaluate(grad) self.assertAllClose(np.ones_like(grad_v), grad_v)
def testGrads(self): in_shape = [1, 2, 3, 1] out_shape = [1, 4, 6, 1] x = np.arange(0, 6).reshape(in_shape).astype(np.float32) kernel_types = [ 'lanczos1', 'lanczos3', 'lanczos5', 'gaussian', 'box', 'triangle', 'keyscubic', 'mitchellcubic' ] scales = [(1.0, 1.0), (0.37, 0.47), (2.1, 2.1)] translations = [(0.0, 0.0), (3.14, 1.19), (2.1, 3.1), (100.0, 200.0)] for scale in scales: for translation in translations: for kernel_type in kernel_types: for antialias in [True, False]: with self.cached_session(): input_tensor = constant_op.constant(x, shape=in_shape) scale_and_translate_out = image_ops.scale_and_translate( input_tensor, out_shape[1:3], scale=constant_op.constant(scale), translation=constant_op.constant(translation), kernel_type=kernel_type, antialias=antialias) err = gradient_checker.compute_gradient_error( input_tensor, in_shape, scale_and_translate_out, out_shape, x_init_value=x) self.assertLess(err, 1e-3)
def scale_trans(input_tensor, scale=scale, translation=translation, kernel_type=kernel_type, antialias=antialias): # pylint: disable=cell-var-from-loop return image_ops.scale_and_translate( input_tensor, out_shape[1:3], scale=constant_op.constant(scale), translation=constant_op.constant(translation), kernel_type=kernel_type, antialias=antialias)