def test_bilinear(self): with self.cached_session(): image = constant_op.constant( [[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtypes.float32) # The following result matches: # >>> scipy.ndimage.rotate(image, 45, order=1, reshape=False) # which uses spline interpolation of order 1, equivalent to bilinear # interpolation. self.assertAllClose( image_ops.rotate(image, np.pi / 4.0, interpolation="BILINEAR").eval(), [[0.000, 0.000, 0.343, 0.000, 0.000], [0.000, 0.586, 0.914, 0.586, 0.000], [0.343, 0.914, 0.000, 0.914, 0.343], [0.000, 0.586, 0.914, 0.586, 0.000], [0.000, 0.000, 0.343, 0.000, 0.000]], atol=0.001) self.assertAllClose( image_ops.rotate(image, np.pi / 4.0, interpolation="NEAREST").eval(), [[0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [1, 1, 0, 1, 1], [0, 1, 1, 1, 0], [0, 0, 1, 0, 0]])
def test_bilinear(self): with self.test_session(): image = constant_op.constant( [[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtypes.float32) # The following result matches: # >>> scipy.ndimage.rotate(image, 45, order=1, reshape=False) # which uses spline interpolation of order 1, equivalent to bilinear # interpolation. self.assertAllClose( image_ops.rotate(image, np.pi / 4.0, interpolation="BILINEAR").eval(), [[0.000, 0.000, 0.343, 0.000, 0.000], [0.000, 0.586, 0.914, 0.586, 0.000], [0.343, 0.914, 0.000, 0.914, 0.343], [0.000, 0.586, 0.914, 0.586, 0.000], [0.000, 0.000, 0.343, 0.000, 0.000]], atol=0.001) self.assertAllClose( image_ops.rotate(image, np.pi / 4.0, interpolation="NEAREST").eval(), [[0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [1, 1, 0, 1, 1], [0, 1, 1, 1, 0], [0, 0, 1, 0, 0]])
def random_rotate(image, segment, pad_value, ignore_label, min_degree=-10, max_degree=10): """ :param image: [H, W, 3] :param segment: [H, W, 1] or [H, W] :param pad_value: :param ignore_label: :param min_degree: :param max_degree: :return: """ if segment is None: return _random_rotate_img(image, pad_value, min_degree=-min_degree, max_degree=max_degree) angle = tf.random_uniform(shape=[], minval=min_degree, maxval=max_degree) angle = angle / 180 * 3.1415926 if tensor_rank(segment) == 2: # to support mask's broadcast segment = tf.expand_dims(segment, axis=-1) image_dtype, segment_dtype = image.dtype, segment.dtype image = tf.cast(image, tf.float32) segment = tf.cast(segment, tf.int32) ones_mask = tf.ones_like(segment, dtype=tf.int32) rot_img = rotate(image, angle, interpolation='BILINEAR') rot_segment = rotate(segment, angle, interpolation='NEAREST') rot_ones_mask = rotate(ones_mask, angle, interpolation='NEAREST') float_mask = tf.cast(rot_ones_mask, tf.float32) # float_mask's last dimeension is one, so will do broadcast! rot_img = float_mask * rot_img + (1 - float_mask) * pad_value int_mask = tf.cast(rot_ones_mask, tf.int32) rot_segment = int_mask * rot_segment + (1 - int_mask) * ignore_label rot_img = tf.cast(rot_img, image_dtype) rot_segment = tf.cast(rot_segment, segment_dtype) return rot_img, rot_segment
def test_zeros(self): for dtype in _DTYPES: with self.cached_session(): for shape in [(5, 5), (24, 24), (2, 24, 24, 3)]: for angle in [0, 1, np.pi / 2.0]: image = array_ops.zeros(shape, dtype) self.assertAllEqual( image_ops.rotate(image, angle).eval(), np.zeros(shape, dtype.as_numpy_dtype()))
def test_zeros(self): for dtype in _DTYPES: with self.test_session(): for shape in [(5, 5), (24, 24), (2, 24, 24, 3)]: for angle in [0, 1, np.pi / 2.0]: image = array_ops.zeros(shape, dtype) self.assertAllEqual( image_ops.rotate(image, angle).eval(), np.zeros(shape, dtype.as_numpy_dtype()))
def _random_rotate(image, angle): """ random rotate image. [-angle, angle] uniform distribution. :param image: image :param angle: range of rotate angle. degree. :return: rotated image tensor """ angle = angle * math.pi / float(180) # rotate_factor = random_ops.random_uniform([1], -1, 1)[0] rotate_factor = random_ops.truncated_normal([1], mean=0.0, stddev=0.5)[0] random_angle = tf.multiply(rotate_factor, angle) return image_ops.rotate(image, random_angle, interpolation="BILINEAR")
def test_bilinear_uint8(self): with self.test_session(): image = constant_op.constant( np.asarray( [[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 255, 255, 255, 0.0], [0.0, 255, 0.0, 255, 0.0], [0.0, 255, 255, 255, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]], np.uint8), dtypes.uint8) # == np.rint((expected image above) * 255) self.assertAllEqual( image_ops.rotate(image, np.pi / 4.0, interpolation="BILINEAR").eval(), [[0.0, 0.0, 87., 0.0, 0.0], [0.0, 149, 233, 149, 0.0], [87., 233, 0.0, 233, 87.], [0.0, 149, 233, 149, 0.0], [0.0, 0.0, 87., 0.0, 0.0]])
def _random_rotate_img(image, pad_value, min_degree=-10, max_degree=10): ''' :param image: [H, W, 3] :param pad_value: :param min_degree: :param max_degree: :return: ''' angle = tf.random_uniform(shape=[], minval=min_degree, maxval=max_degree) angle = angle / 180 * 3.1415926 image_dtype = image.dtype image = tf.cast(image, tf.float32) ones_mask = tf.ones_like(image, dtype=tf.int32)[..., 0] rot_img = rotate(image, angle, interpolation='BILINEAR') rot_ones_mask = rotate(ones_mask, angle, interpolation='NEAREST') float_mask = tf.cast(rot_ones_mask, tf.float32) # float_mask's last dimeension is one, so will do broadcast! rot_img = float_mask * rot_img + (1 - float_mask) * pad_value rot_img = tf.cast(rot_img, image_dtype) return rot_img
def test_rotate_odd(self): for dtype in _DTYPES: with self.cached_session(): image = array_ops.reshape( math_ops.cast(math_ops.range(25), dtype), (5, 5)) image_rep = array_ops.tile(image[None, :, :, None], [3, 1, 1, 1]) angles = constant_op.constant([np.pi / 4.0, 1.0, -np.pi / 2.0], dtypes.float32) image_rotated = image_ops.rotate(image_rep, angles) self.assertAllEqual(image_rotated[:, :, :, 0].eval(), [[[0, 3, 8, 9, 0], [1, 7, 8, 13, 19], [6, 6, 12, 18, 18], [5, 11, 16, 17, 23], [0, 15, 16, 21, 0]], [[0, 3, 9, 14, 0], [2, 7, 8, 13, 19], [1, 6, 12, 18, 23], [5, 11, 16, 17, 22], [0, 10, 15, 21, 0]], [[20, 15, 10, 5, 0], [21, 16, 11, 6, 1], [22, 17, 12, 7, 2], [23, 18, 13, 8, 3], [24, 19, 14, 9, 4]]])
def test_bilinear_uint8(self): with self.cached_session(): image = constant_op.constant( np.asarray( [[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 255, 255, 255, 0.0], [0.0, 255, 0.0, 255, 0.0], [0.0, 255, 255, 255, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]], np.uint8), dtypes.uint8) # == np.rint((expected image above) * 255) self.assertAllEqual( image_ops.rotate(image, np.pi / 4.0, interpolation="BILINEAR").eval(), [[0.0, 0.0, 87., 0.0, 0.0], [0.0, 149, 233, 149, 0.0], [87., 233, 0.0, 233, 87.], [0.0, 149, 233, 149, 0.0], [0.0, 0.0, 87., 0.0, 0.0]])
def test_rotate_odd(self): for dtype in _DTYPES: with self.test_session(): image = array_ops.reshape( math_ops.cast(math_ops.range(25), dtype), (5, 5)) image_rep = array_ops.tile(image[None, :, :, None], [3, 1, 1, 1]) angles = constant_op.constant([np.pi / 4.0, 1.0, -np.pi / 2.0], dtypes.float32) image_rotated = image_ops.rotate(image_rep, angles) self.assertAllEqual( image_rotated[:, :, :, 0].eval(), [[[0, 3, 8, 9, 0], [1, 7, 8, 13, 19], [6, 6, 12, 18, 18], [5, 11, 16, 17, 23], [0, 15, 16, 21, 0]], [[0, 3, 9, 14, 0], [2, 7, 8, 13, 19], [1, 6, 12, 18, 23], [5, 11, 16, 17, 22], [0, 10, 15, 21, 0]], [[20, 15, 10, 5, 0], [21, 16, 11, 6, 1], [ 22, 17, 12, 7, 2 ], [23, 18, 13, 8, 3], [24, 19, 14, 9, 4]]])
def test_rotate_even(self): for dtype in _DTYPES: with self.cached_session(): image = array_ops.reshape( math_ops.cast(math_ops.range(36), dtype), (6, 6)) image_rep = array_ops.tile(image[None, :, :, None], [3, 1, 1, 1]) angles = constant_op.constant([0.0, np.pi / 4.0, np.pi / 2.0], dtypes.float32) image_rotated = image_ops.rotate(image_rep, angles) self.assertAllEqual(image_rotated[:, :, :, 0].eval(), [[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35]], [[0, 3, 4, 11, 17, 0], [2, 3, 9, 16, 23, 23], [1, 8, 15, 21, 22, 29], [6, 13, 20, 21, 27, 34], [12, 18, 19, 26, 33, 33], [0, 18, 24, 31, 32, 0]], [[5, 11, 17, 23, 29, 35], [4, 10, 16, 22, 28, 34], [3, 9, 15, 21, 27, 33], [2, 8, 14, 20, 26, 32], [1, 7, 13, 19, 25, 31], [0, 6, 12, 18, 24, 30]]])
def test_rotate_even(self): for dtype in _DTYPES: with self.test_session(): image = array_ops.reshape( math_ops.cast(math_ops.range(36), dtype), (6, 6)) image_rep = array_ops.tile(image[None, :, :, None], [3, 1, 1, 1]) angles = constant_op.constant([0.0, np.pi / 4.0, np.pi / 2.0], dtypes.float32) image_rotated = image_ops.rotate(image_rep, angles) self.assertAllEqual( image_rotated[:, :, :, 0].eval(), [[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35]], [[0, 3, 4, 11, 17, 0], [2, 3, 9, 16, 23, 23], [1, 8, 15, 21, 22, 29], [6, 13, 20, 21, 27, 34], [12, 18, 19, 26, 33, 33], [0, 18, 24, 31, 32, 0]], [[5, 11, 17, 23, 29, 35], [4, 10, 16, 22, 28, 34], [3, 9, 15, 21, 27, 33], [2, 8, 14, 20, 26, 32], [1, 7, 13, 19, 25, 31], [0, 6, 12, 18, 24, 30]]])
def test_rotate_static_shape(self): image = array_ops.diag([1., 2., 3.]) result = image_ops.rotate( image, random_ops.random_uniform((), -1, 1), interpolation="BILINEAR") self.assertEqual(image.get_shape(), result.get_shape())
def test_rotate_static_shape(self): image = array_ops.diag([1., 2., 3.]) result = image_ops.rotate(image, random_ops.random_uniform((), -1, 1), interpolation="BILINEAR") self.assertEqual(image.get_shape(), result.get_shape())