# Copyright 2020 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.
# ==============================================================================
"""Preprocessing stage tests."""
# pylint: disable=g-classes-have-attributes
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import time
import numpy as np

from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.keras import keras_parameterized
from tensorflow.python.keras.engine import base_preprocessing_layer
from tensorflow.python.keras.layers import convolutional
from tensorflow.python.keras.layers.preprocessing import image_preprocessing
from tensorflow.python.keras.layers.preprocessing import normalization
from tensorflow.python.keras.layers.preprocessing import preprocessing_stage
from tensorflow.python.keras.layers.preprocessing import preprocessing_test_utils
from tensorflow.python.ops import array_ops
from tensorflow.python.platform import test


@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
class PreprocessingStageTest(
    keras_parameterized.TestCase,
    preprocessing_test_utils.PreprocessingLayerTest):

  def test_adapt(self):

    class PL(base_preprocessing_layer.PreprocessingLayer):

      def __init__(self, **kwargs):
        self.adapt_time = None
        self.adapt_count = 0
        super(PL, self).__init__(**kwargs)

      def adapt(self, data, reset_state=True):
        self.adapt_time = time.time()
        self.adapt_count += 1

      def call(self, inputs):
        return inputs + 1.

    # Test with NumPy array
    stage = preprocessing_stage.PreprocessingStage([
        PL(),
        PL(),
        PL(),
    ])
    stage.adapt(np.ones((3, 4)))
    self.assertEqual(stage.layers[0].adapt_count, 1)
    self.assertEqual(stage.layers[1].adapt_count, 1)
    self.assertEqual(stage.layers[2].adapt_count, 1)
    self.assertLessEqual(stage.layers[0].adapt_time, stage.layers[1].adapt_time)
    self.assertLessEqual(stage.layers[1].adapt_time, stage.layers[2].adapt_time)

    # Check call
    y = stage(array_ops.ones((3, 4)))
    self.assertAllClose(y, np.ones((3, 4)) + 3.)

    # Test with dataset
    adapt_data = dataset_ops.Dataset.from_tensor_slices(np.ones((3, 10)))
    adapt_data = adapt_data.batch(2)  # 5 batches of 2 samples

    stage.adapt(adapt_data)
    self.assertEqual(stage.layers[0].adapt_count, 2)
    self.assertEqual(stage.layers[1].adapt_count, 2)
    self.assertEqual(stage.layers[2].adapt_count, 2)
    self.assertLess(stage.layers[0].adapt_time, stage.layers[1].adapt_time)
    self.assertLess(stage.layers[1].adapt_time, stage.layers[2].adapt_time)

    # Test error with bad data
    with self.assertRaisesRegex(ValueError, 'requires a '):
      stage.adapt(None)

  def test_mixing_preprocessing_and_regular_layers(self):
    stage = preprocessing_stage.PreprocessingStage([
        image_preprocessing.CenterCrop(16, 16),
        normalization.Normalization(),
        convolutional.Conv2D(4, 3)
    ])
    data = np.ones((16, 20, 20, 3), dtype='float32')
    stage.adapt(data)
    _ = stage(data)
    stage.compile('rmsprop', 'mse')
    stage.fit(data, np.ones((16, 14, 14, 4)))
    _ = stage.evaluate(data, np.ones((16, 14, 14, 4)))
    _ = stage.predict(data)


if __name__ == '__main__':
  test.main()

tensorflow.python.keras.keras_parameterized.run_all_keras_modes tensorflow.python.keras.layers.preprocessing.normalization.Normalization numpy.ones tensorflow.python.keras.layers.preprocessing.image_preprocessing.CenterCrop tensorflow.python.platform.test.main tensorflow.python.keras.layers.convolutional.Conv2D tensorflow.python.ops.array_ops.ones
[(36, 'tensorflow.python.keras.keras_parameterized.run_all_keras_modes', 'keras_parameterized.run_all_keras_modes', ([], {'always_skip_v1': '(True)'}), False, 'from tensorflow.python.keras import keras_parameterized\n'), (105, 'tensorflow.python.platform.test.main', 'test.main', ([], {}), False, 'from tensorflow.python.platform import test\n'), (95, 'numpy.ones', 'np.ones', (['(16, 20, 20, 3)'], {'dtype': '"""float32"""'}), True, 'import numpy as np\n'), (63, 'numpy.ones', 'np.ones', (['(3, 4)'], {}), True, 'import numpy as np\n'), (71, 'tensorflow.python.ops.array_ops.ones', 'array_ops.ones', (['(3, 4)'], {}), False, 'from tensorflow.python.ops import array_ops\n'), (75, 'numpy.ones', 'np.ones', (['(3, 10)'], {}), True, 'import numpy as np\n'), (99, 'numpy.ones', 'np.ones', (['(16, 14, 14, 4)'], {}), True, 'import numpy as np\n'), (100, 'numpy.ones', 'np.ones', (['(16, 14, 14, 4)'], {}), True, 'import numpy as np\n'), (51, 'time.time', 'time.time', ([], {}), False, 'import time\n'), (72, 'numpy.ones', 'np.ones', (['(3, 4)'], {}), True, 'import numpy as np\n'), (91, 'tensorflow.python.keras.layers.preprocessing.image_preprocessing.CenterCrop', 'image_preprocessing.CenterCrop', (['(16)', '(16)'], {}), False, 'from tensorflow.python.keras.layers.preprocessing import image_preprocessing\n'), (92, 'tensorflow.python.keras.layers.preprocessing.normalization.Normalization', 'normalization.Normalization', ([], {}), False, 'from tensorflow.python.keras.layers.preprocessing import normalization\n'), (93, 'tensorflow.python.keras.layers.convolutional.Conv2D', 'convolutional.Conv2D', (['(4)', '(3)'], {}), False, 'from tensorflow.python.keras.layers import convolutional\n')]