<commit_before># Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for transform ops."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf

from tensorflow_addons.image import transform_ops
from tensorflow_addons.utils import test_utils

_DTYPES = set([
    tf.dtypes.uint8, tf.dtypes.int32, tf.dtypes.int64, tf.dtypes.float16,
    tf.dtypes.float32, tf.dtypes.float64
])


@test_utils.run_all_in_graph_and_eager_modes
class ImageOpsTest(tf.test.TestCase):
    def test_compose(self):
        for dtype in _DTYPES:
            with test_utils.use_gpu():
                image = tf.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 = transform_ops.angles_to_projective_transforms(
                    np.pi / 2, 4, 4)
                # Translate right by 1 (the transformation matrix is always inverted,
                # hence the -1).
                translation = tf.constant([1, 0, -1, 0, 1, 0, 0, 0],
                                          dtype=tf.dtypes.float32)
                composed = transform_ops.compose_transforms(
                    [rotation, translation])
                image_transformed = transform_ops.transform(image, composed)
                self.assertAllEqual(
                    [[0, 0, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1], [0, 1, 1, 1]],
                    image_transformed)

    def test_extreme_projective_transform(self):
        for dtype in _DTYPES:
            with test_utils.use_gpu():
                image = tf.constant(
                    [[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1]],
                    dtype=dtype)
                transformation = tf.constant([1, 0, 0, 0, 1, 0, -1, 0],
                                             tf.dtypes.float32)
                image_transformed = transform_ops.transform(
                    image, transformation)
                self.assertAllEqual(
                    [[1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]],
                    image_transformed)

    def test_transform_static_output_shape(self):
        image = tf.constant([[1., 2.], [3., 4.]])
        result = transform_ops.transform(
            image, tf.random.uniform([8], -1, 1), output_shape=[3, 5])
        self.assertAllEqual([3, 5], result.shape)

    def test_transform_unknown_shape(self):
        fn = transform_ops.transform.get_concrete_function(
            tf.TensorSpec(shape=None, dtype=tf.float32),
            [1, 0, 0, 0, 1, 0, 0, 0])
        for shape in (2, 4), (2, 4, 3), (1, 2, 4, 3):
            image = tf.ones(shape=shape)
            self.assertAllEqual(self.evaluate(image), self.evaluate(fn(image)))

    def _test_grad(self, input_shape, output_shape=None):
        image_size = tf.math.cumprod(input_shape)[-1]
        image_size = tf.cast(image_size, tf.float32)
        test_image = tf.reshape(
            tf.range(0, image_size, dtype=tf.float32), input_shape)
        # Scale test image to range [0, 0.01]
        test_image = (test_image / image_size) * 0.01

        if output_shape is None:
            resize_shape = None
        elif 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]

        def transform_fn(x):
            x.set_shape(input_shape)
            transform = transform_ops.angles_to_projective_transforms(
                np.pi / 2, 4, 4)
            return transform_ops.transform(
                images=x, transforms=transform, output_shape=resize_shape)

        theoretical, numerical = tf.test.compute_gradient(
            transform_fn, [test_image])

        self.assertAllClose(theoretical[0], numerical[0])

    def test_grad(self):
        self._test_grad([16, 16])
        self._test_grad([4, 12, 12])
        self._test_grad([3, 4, 12, 12])
        self._test_grad([16, 16], [8, 8])
        self._test_grad([4, 12, 3], [8, 24, 3])
        self._test_grad([3, 4, 12, 3], [3, 8, 24, 3])

    def test_transform_data_types(self):
        for dtype in _DTYPES:
            image = tf.constant([[1, 2], [3, 4]], dtype=dtype)
            self.assertAllEqual(
                np.array([[4, 4], [4, 4]]).astype(dtype.as_numpy_dtype()),
                transform_ops.transform(image, [1] * 8))

    def test_transform_eager(self):
        image = tf.constant([[1., 2.], [3., 4.]])
        self.assertAllEqual(
            np.array([[4, 4], [4, 4]]), transform_ops.transform(
                image, [1] * 8))


@test_utils.run_all_in_graph_and_eager_modes
class RotateOpTest(tf.test.TestCase):
    def test_zeros(self):
        for dtype in _DTYPES:
            for shape in [(5, 5), (24, 24), (2, 24, 24, 3)]:
                for angle in [0, 1, np.pi / 2.0]:
                    image = tf.zeros(shape, dtype)
                    self.assertAllEqual(
                        transform_ops.rotate(image, angle),
                        np.zeros(shape, dtype.as_numpy_dtype()))

    def test_rotate_even(self):
        for dtype in _DTYPES:
            image = tf.reshape(tf.cast(tf.range(36), dtype), (6, 6))
            image_rep = tf.tile(image[None, :, :, None], [3, 1, 1, 1])
            angles = tf.constant([0.0, np.pi / 4.0, np.pi / 2.0], tf.float32)
            image_rotated = transform_ops.rotate(image_rep, angles)
            # yapf: disable
            self.assertAllEqual(
                image_rotated[:, :, :, 0],
                [[[0, 1, 2, 3, 4, 5],
                  [6, 7, 8, 9, 10, 11],
                  [12, 13, 14, 15, 16, 17],<commit_msg>as_numpy_dtype is a property, not a method (#776)<commit_after># Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for transform ops."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf

from tensorflow_addons.image import transform_ops
from tensorflow_addons.utils import test_utils

_DTYPES = set([
    tf.dtypes.uint8, tf.dtypes.int32, tf.dtypes.int64, tf.dtypes.float16,
    tf.dtypes.float32, tf.dtypes.float64
])


@test_utils.run_all_in_graph_and_eager_modes
class ImageOpsTest(tf.test.TestCase):
    def test_compose(self):
        for dtype in _DTYPES:
            with test_utils.use_gpu():
                image = tf.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 = transform_ops.angles_to_projective_transforms(
                    np.pi / 2, 4, 4)
                # Translate right by 1 (the transformation matrix is always inverted,
                # hence the -1).
                translation = tf.constant([1, 0, -1, 0, 1, 0, 0, 0],
                                          dtype=tf.dtypes.float32)
                composed = transform_ops.compose_transforms(
                    [rotation, translation])
                image_transformed = transform_ops.transform(image, composed)
                self.assertAllEqual(
                    [[0, 0, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1], [0, 1, 1, 1]],
                    image_transformed)

    def test_extreme_projective_transform(self):
        for dtype in _DTYPES:
            with test_utils.use_gpu():
                image = tf.constant(
                    [[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1]],
                    dtype=dtype)
                transformation = tf.constant([1, 0, 0, 0, 1, 0, -1, 0],
                                             tf.dtypes.float32)
                image_transformed = transform_ops.transform(
                    image, transformation)
                self.assertAllEqual(
                    [[1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]],
                    image_transformed)

    def test_transform_static_output_shape(self):
        image = tf.constant([[1., 2.], [3., 4.]])
        result = transform_ops.transform(
            image, tf.random.uniform([8], -1, 1), output_shape=[3, 5])
        self.assertAllEqual([3, 5], result.shape)

    def test_transform_unknown_shape(self):
        fn = transform_ops.transform.get_concrete_function(
            tf.TensorSpec(shape=None, dtype=tf.float32),
            [1, 0, 0, 0, 1, 0, 0, 0])
        for shape in (2, 4), (2, 4, 3), (1, 2, 4, 3):
            image = tf.ones(shape=shape)
            self.assertAllEqual(self.evaluate(image), self.evaluate(fn(image)))

    def _test_grad(self, input_shape, output_shape=None):
        image_size = tf.math.cumprod(input_shape)[-1]
        image_size = tf.cast(image_size, tf.float32)
        test_image = tf.reshape(
            tf.range(0, image_size, dtype=tf.float32), input_shape)
        # Scale test image to range [0, 0.01]
        test_image = (test_image / image_size) * 0.01

        if output_shape is None:
            resize_shape = None
        elif 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]

        def transform_fn(x):
            x.set_shape(input_shape)
            transform = transform_ops.angles_to_projective_transforms(
                np.pi / 2, 4, 4)
            return transform_ops.transform(
                images=x, transforms=transform, output_shape=resize_shape)

        theoretical, numerical = tf.test.compute_gradient(
            transform_fn, [test_image])

        self.assertAllClose(theoretical[0], numerical[0])

    def test_grad(self):
        self._test_grad([16, 16])
        self._test_grad([4, 12, 12])
        self._test_grad([3, 4, 12, 12])
        self._test_grad([16, 16], [8, 8])
        self._test_grad([4, 12, 3], [8, 24, 3])
        self._test_grad([3, 4, 12, 3], [3, 8, 24, 3])

    def test_transform_data_types(self):
        for dtype in _DTYPES:
            image = tf.constant([[1, 2], [3, 4]], dtype=dtype)
            self.assertAllEqual(
                np.array([[4, 4], [4, 4]]).astype(dtype.as_numpy_dtype),
                transform_ops.transform(image, [1] * 8))

    def test_transform_eager(self):
        image = tf.constant([[1., 2.], [3., 4.]])
        self.assertAllEqual(
            np.array([[4, 4], [4, 4]]), transform_ops.transform(
                image, [1] * 8))


@test_utils.run_all_in_graph_and_eager_modes
class RotateOpTest(tf.test.TestCase):
    def test_zeros(self):
        for dtype in _DTYPES:
            for shape in [(5, 5), (24, 24), (2, 24, 24, 3)]:
                for angle in [0, 1, np.pi / 2.0]:
                    image = tf.zeros(shape, dtype)
                    self.assertAllEqual(
                        transform_ops.rotate(image, angle),
                        np.zeros(shape, dtype.as_numpy_dtype))

    def test_rotate_even(self):
        for dtype in _DTYPES:
            image = tf.reshape(tf.cast(tf.range(36), dtype), (6, 6))
            image_rep = tf.tile(image[None, :, :, None], [3, 1, 1, 1])
            angles = tf.constant([0.0, np.pi / 4.0, np.pi / 2.0], tf.float32)
            image_rotated = transform_ops.rotate(image_rep, angles)
            # yapf: disable
            self.assertAllEqual(
                image_rotated[:, :, :, 0],
                [[[0, 1, 2, 3, 4, 5],
                  [6, 7, 8, 9, 10, 11],
                  [12, 13, 14, 15, 16, 17],
{'fraction_non_alphanumeric': 0.13558017851646662, 'fraction_numerical': 0.04362880886426593, 'mean_word_length': 2.031016791044776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3391918', 'n_tokens_mistral': 4503, 'n_tokens_neox': 3989, 'n_words': 1090}