B
    ӻd E                 @   s<  d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ yddlZW n ek
r   dZY nX G dd dejejZd*ddZdd Zdd Zdd Zd+ddZd,ddZdd Zdd Zdd Zd-d d!Z d"d# Z!d$d% Z"d&d' Z#d(d) Z$dS ).z!Utilities for unit-testing Keras.    N)parameterized)keras)tf2)context)ops)testing_utils)test)nestc                   s   e Zd Z fddZ  ZS )TestCasec                s   t j  tt|   d S )N)r   backendZclear_sessionsuperr
   tearDown)self)	__class__ ]/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/keras/keras_parameterized.pyr   (   s    
zTestCase.tearDown)__name__
__module____qualname__r   __classcell__r   r   )r   r   r
   &   s   r
   c                sF   t dkr dg dddg} fdd|D fdd}t| |S )	a  Execute the decorated test with all Keras saved model formats).

  This decorator is intended to be applied either to individual test methods in
  a `keras_parameterized.TestCase` class, or directly to a test class that
  extends it. Doing so will cause the contents of the individual test
  method (or all test methods in the class) to be executed multiple times - once
  for each Keras saved model format.

  The Keras saved model formats include:
  1. HDF5: 'h5'
  2. SavedModel: 'tf'

  Note: if stacking this decorator with absl.testing's parameterized decorators,
  those should be at the bottom of the stack.

  Various methods in `testing_utils` to get file path for saved models will
  auto-generate a string of the two saved model formats. This allows unittests
  to confirm the equivalence between the two Keras saved model formats.

  For example, consider the following unittest:

  ```python
  class MyTests(testing_utils.KerasTestCase):

    @testing_utils.run_with_all_saved_model_formats
    def test_foo(self):
      save_format = testing_utils.get_save_format()
      saved_model_dir = '/tmp/saved_model/'
      model = keras.models.Sequential()
      model.add(keras.layers.Dense(2, input_shape=(3,)))
      model.add(keras.layers.Dense(3))
      model.compile(loss='mse', optimizer='sgd', metrics=['acc'])

      keras.models.save_model(model, saved_model_dir, save_format=save_format)
      model = keras.models.load_model(saved_model_dir)

  if __name__ == "__main__":
    tf.test.main()
  ```

  This test tries to save the model into the formats of 'hdf5', 'h5', 'keras',
  'tensorflow', and 'tf'.

  We can also annotate the whole class if we want this to apply to all tests in
  the class:
  ```python
  @testing_utils.run_with_all_saved_model_formats
  class MyTests(testing_utils.KerasTestCase):

    def test_foo(self):
      save_format = testing_utils.get_save_format()
      saved_model_dir = '/tmp/saved_model/'
      model = keras.models.Sequential()
      model.add(keras.layers.Dense(2, input_shape=(3,)))
      model.add(keras.layers.Dense(3))
      model.compile(loss='mse', optimizer='sgd', metrics=['acc'])

      keras.models.save_model(model, saved_model_dir, save_format=save_format)
      model = tf.keras.models.load_model(saved_model_dir)

  if __name__ == "__main__":
    tf.test.main()
  ```

  Args:
    test_or_class: test method or class to be annotated. If None,
      this method returns a decorator that can be applied to a test method or
      test class. If it is not None this returns the decorator applied to the
      test or class.
    exclude_formats: A collection of Keras saved model formats to not run.
      (May also be a single format not wrapped in a collection).
      Defaults to None.

  Returns:
    Returns a decorator that will run the decorated test method multiple times:
    once for each desired Keras saved model format.

  Raises:
    ImportError: If abseil parameterized is not installed or not included as
      a target dependency.
  Nh5tftf_no_tracesc                s&   g | ]}|t  krd | |fqS )z_%s)r	   flatten).0saved_format)exclude_formatsr   r   
<listcomp>   s   z4run_with_all_saved_model_formats.<locals>.<listcomp>c                s$   t j t  fdd}|S )z)Decorator that constructs the test cases.c                sf   |dkrt  | f|| nF|dkr8t | f|| n*|dkrTt | f|| ntd|f dS )z8A run of a single test case w/ the specified model type.r   r   r   zUnknown model type: %sN)_test_h5_saved_model_format_test_tf_saved_model_format%_test_tf_saved_model_format_no_traces
ValueError)r   r   argskwargs)fr   r   	decorated   s    zTrun_with_all_saved_model_formats.<locals>.single_method_decorator.<locals>.decorated)r   named_parameters	functoolswraps)r$   r%   )params)r$   r   single_method_decorator   s    zArun_with_all_saved_model_formats.<locals>.single_method_decorator)h5pyappend_test_or_class_decorator)test_or_classr   Zsaved_model_formatsr*   r   )r   r)   r    run_with_all_saved_model_formats-   s    U

r/   c          	   O   s*   t d | |f|| W d Q R X d S )Nr   )r   saved_model_format_scope)r$   r.   r"   r#   r   r   r   r      s    r   c          	   O   s*   t d | |f|| W d Q R X d S )Nr   )r   r0   )r$   r.   r"   r#   r   r   r   r      s    r   c          	   O   s.   t jddd | |f|| W d Q R X d S )Nr   F)Zsave_traces)r   r0   )r$   r.   r"   r#   r   r   r   r       s    r    c             C   s   |pg }| d t| |S )z=Runs all tests with the supported formats for saving weights.r   )r,   r/   )r.   r   r   r   r   run_with_all_weight_formats   s    
r1   c                s2   dddg} fdd|D fdd}t | |S )a.  Execute the decorated test with all Keras model types.

  This decorator is intended to be applied either to individual test methods in
  a `keras_parameterized.TestCase` class, or directly to a test class that
  extends it. Doing so will cause the contents of the individual test
  method (or all test methods in the class) to be executed multiple times - once
  for each Keras model type.

  The Keras model types are: ['functional', 'subclass', 'sequential']

  Note: if stacking this decorator with absl.testing's parameterized decorators,
  those should be at the bottom of the stack.

  Various methods in `testing_utils` to get models will auto-generate a model
  of the currently active Keras model type. This allows unittests to confirm
  the equivalence between different Keras models.

  For example, consider the following unittest:

  ```python
  class MyTests(testing_utils.KerasTestCase):

    @testing_utils.run_with_all_model_types(
      exclude_models = ['sequential'])
    def test_foo(self):
      model = testing_utils.get_small_mlp(1, 4, input_dim=3)
      optimizer = RMSPropOptimizer(learning_rate=0.001)
      loss = 'mse'
      metrics = ['mae']
      model.compile(optimizer, loss, metrics=metrics)

      inputs = np.zeros((10, 3))
      targets = np.zeros((10, 4))
      dataset = dataset_ops.Dataset.from_tensor_slices((inputs, targets))
      dataset = dataset.repeat(100)
      dataset = dataset.batch(10)

      model.fit(dataset, epochs=1, steps_per_epoch=2, verbose=1)

  if __name__ == "__main__":
    tf.test.main()
  ```

  This test tries building a small mlp as both a functional model and as a
  subclass model.

  We can also annotate the whole class if we want this to apply to all tests in
  the class:
  ```python
  @testing_utils.run_with_all_model_types(exclude_models = ['sequential'])
  class MyTests(testing_utils.KerasTestCase):

    def test_foo(self):
      model = testing_utils.get_small_mlp(1, 4, input_dim=3)
      optimizer = RMSPropOptimizer(learning_rate=0.001)
      loss = 'mse'
      metrics = ['mae']
      model.compile(optimizer, loss, metrics=metrics)

      inputs = np.zeros((10, 3))
      targets = np.zeros((10, 4))
      dataset = dataset_ops.Dataset.from_tensor_slices((inputs, targets))
      dataset = dataset.repeat(100)
      dataset = dataset.batch(10)

      model.fit(dataset, epochs=1, steps_per_epoch=2, verbose=1)

  if __name__ == "__main__":
    tf.test.main()
  ```


  Args:
    test_or_class: test method or class to be annotated. If None,
      this method returns a decorator that can be applied to a test method or
      test class. If it is not None this returns the decorator applied to the
      test or class.
    exclude_models: A collection of Keras model types to not run.
      (May also be a single model type not wrapped in a collection).
      Defaults to None.

  Returns:
    Returns a decorator that will run the decorated test method multiple times:
    once for each desired Keras model type.

  Raises:
    ImportError: If abseil parameterized is not installed or not included as
      a target dependency.
  
functionalsubclass
sequentialc                s&   g | ]}|t  krd | |fqS )z_%s)r	   r   )r   model)exclude_modelsr   r   r     s    z,run_with_all_model_types.<locals>.<listcomp>c                s$   t j t  fdd}|S )z)Decorator that constructs the test cases.c                sf   |dkrt  | f|| nF|dkr8t | f|| n*|dkrTt | f|| ntd|f dS )z8A run of a single test case w/ the specified model type.r2   r3   r4   zUnknown model type: %sN)_test_functional_model_type_test_subclass_model_type_test_sequential_model_typer!   )r   Z
model_typer"   r#   )r$   r   r   r%     s    zLrun_with_all_model_types.<locals>.single_method_decorator.<locals>.decorated)r   r&   r'   r(   )r$   r%   )r)   )r$   r   r*     s    z9run_with_all_model_types.<locals>.single_method_decorator)r-   )r.   r6   Zmodel_typesr*   r   )r6   r)   r   run_with_all_model_types   s    \
r:   c          	   O   s*   t d | |f|| W d Q R X d S )Nr2   )r   model_type_scope)r$   r.   r"   r#   r   r   r   r7   )  s    r7   c          	   O   s*   t d | |f|| W d Q R X d S )Nr3   )r   r;   )r$   r.   r"   r#   r   r   r   r8   .  s    r8   c          	   O   s*   t d | |f|| W d Q R X d S )Nr4   )r   r;   )r$   r.   r"   r#   r   r   r   r9   3  s    r9   Fc                sT   |rt d|dg|s&d |s<t s<d  fdd}t| |S )aG
  Execute the decorated test with all keras execution modes.

  This decorator is intended to be applied either to individual test methods in
  a `keras_parameterized.TestCase` class, or directly to a test class that
  extends it. Doing so will cause the contents of the individual test
  method (or all test methods in the class) to be executed multiple times -
  once executing in legacy graph mode, once running eagerly and with
  `should_run_eagerly` returning True, and once running eagerly with
  `should_run_eagerly` returning False.

  If Tensorflow v2 behavior is enabled, legacy graph mode will be skipped, and
  the test will only run twice.

  Note: if stacking this decorator with absl.testing's parameterized decorators,
  those should be at the bottom of the stack.

  For example, consider the following unittest:

  ```python
  class MyTests(testing_utils.KerasTestCase):

    @testing_utils.run_all_keras_modes
    def test_foo(self):
      model = testing_utils.get_small_functional_mlp(1, 4, input_dim=3)
      optimizer = RMSPropOptimizer(learning_rate=0.001)
      loss = 'mse'
      metrics = ['mae']
      model.compile(
          optimizer, loss, metrics=metrics,
          run_eagerly=testing_utils.should_run_eagerly())

      inputs = np.zeros((10, 3))
      targets = np.zeros((10, 4))
      dataset = dataset_ops.Dataset.from_tensor_slices((inputs, targets))
      dataset = dataset.repeat(100)
      dataset = dataset.batch(10)

      model.fit(dataset, epochs=1, steps_per_epoch=2, verbose=1)

  if __name__ == "__main__":
    tf.test.main()
  ```

  This test will try compiling & fitting the small functional mlp using all
  three Keras execution modes.

  Args:
    test_or_class: test method or class to be annotated. If None,
      this method returns a decorator that can be applied to a test method or
      test class. If it is not None this returns the decorator applied to the
      test or class.
    config: An optional config_pb2.ConfigProto to use to configure the
      session when executing graphs.
    always_skip_v1: If True, does not try running the legacy graph mode even
      when Tensorflow v2 behavior is not enabled.
    always_skip_eager: If True, does not execute the decorated test
      with eager execution modes.
    **kwargs: Additional kwargs for configuring tests for
     in-progress Keras behaviors/ refactorings that we haven't fully
     rolled out yet

  Returns:
    Returns a decorator that will run the decorated test method multiple times.

  Raises:
    ImportError: If abseil parameterized is not installed or not included as
      a target dependency.
  zUnrecognized keyword args: {})Z_v2_functionv2_function)Z	_v2_eagerv2_eager)Z_v1_session
v1_sessionc                s&   t j t  fdd}|S )z)Decorator that constructs the test cases.c                sf   |dkrt |  f|| nD|dkr:t| f|| n(|dkrVt| f|| ntd| S dS )z2A run of a single test case w/ specified run mode.r>   r=   r<   zUnknown run mode %sN)_v1_session_test_v2_eager_test_v2_function_testr!   )r   Zrun_moder"   r#   )configr$   r   r   r%     s    zGrun_all_keras_modes.<locals>.single_method_decorator.<locals>.decorated)r   r&   r'   r(   )r$   r%   )rB   r)   )r$   r   r*     s    z4run_all_keras_modes.<locals>.single_method_decorator)r!   formatr,   r   enabledr-   )r.   rB   Zalways_skip_v1Zalways_skip_eagerr#   r*   r   )rB   r)   r   run_all_keras_modes8  s    I

rE   c             O   sZ   t   D td. |j|d | |f|| W d Q R X W d Q R X W d Q R X d S )NF)rB   )r   Zget_default_graphZ
as_defaultr   run_eagerly_scopeZtest_session)r$   r.   rB   r"   r#   r   r   r   r?     s    r?   c          
   O   s>   t  , td | |f|| W d Q R X W d Q R X d S )NT)r   
eager_moder   rF   )r$   r.   r"   r#   r   r   r   r@     s    
r@   c          
   O   s>   t  , td | |f|| W d Q R X W d Q R X d S )NF)r   rG   r   rF   )r$   r.   r"   r#   r   r   r   rA     s    
rA   c                s     fdd}| dk	r|| S |S )a  Decorate a test or class with a decorator intended for one method.

  If the test_or_class is a class:
    This will apply the decorator to all test methods in the class.

  If the test_or_class is an iterable of already-parameterized test cases:
    This will apply the decorator to all the cases, and then flatten the
    resulting cross-product of test cases. This allows stacking the Keras
    parameterized decorators w/ each other, and to apply them to test methods
    that have already been marked with an absl parameterized decorator.

  Otherwise, treat the obj as a single method and apply the decorator directly.

  Args:
    test_or_class: A test method (that may have already been decorated with a
      parameterized decorator, or a test class that extends
      keras_parameterized.TestCase
    single_method_decorator:
      A parameterized decorator intended for a single test method.
  Returns:
    The decorated result.
  c                s   t | tjjr(tj fdd| D S t | tr| }x@|j	 
 D ].\}}t|rF|tjjrFt|| | qFW t|t||j|j|j	 }|S  | S )Nc             3   s   | ]} |V  qd S )Nr   )r   method)r*   r   r   	<genexpr>  s    zL_test_or_class_decorator.<locals>._decorate_test_or_class.<locals>.<genexpr>)
isinstancecollectionsabcIterable	itertoolschainfrom_iterabletype__dict__copyitemscallable
startswithunittestZ
TestLoaderZtestMethodPrefixsetattr__new__r   	__bases__)objclsnamevalue)r*   r   r   _decorate_test_or_class  s    

z9_test_or_class_decorator.<locals>._decorate_test_or_classNr   )r.   r*   r_   r   )r*   r   r-     s    r-   )NN)NN)NN)NNFF)%__doc__rK   r'   rN   rW   Zabsl.testingr   Ztensorflow.pythonr   r   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   Ztensorflow.python.kerasr   Ztensorflow.python.platformr   Ztensorflow.python.utilr	   r+   ImportErrorr
   r/   r   r   r    r1   r:   r7   r8   r9   rE   r?   r@   rA   r-   r   r   r   r   <module>   sH   
 
n

 
r   
e