B
    )`                 @   s   d Z ddlmZ ddlmZ ddlm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 dddgZdZeddddZeddddZdS )zKeras model saving code.    )absolute_import)division)print_functionN)tf2)hdf5_format)load)save)generic_utils)path_to_string)loader_impl)keras_exportz.h5z.hdf5z.kerasTzkeras.models.save_modelc       	      C   s   ddl m} t rdnd}|p"|}t|}|dks\tdk	rHt|tjs\tj	
|d tkr| jsvt| |jsvtdt| ||| nt| ||||| dS )a#	  Saves a model as a TensorFlow SavedModel or HDF5 file.

  Usage:

  >>> model = tf.keras.Sequential([
  ...     tf.keras.layers.Dense(5, input_shape=(3,)),
  ...     tf.keras.layers.Softmax()])
  >>> model.save('/tmp/model')
  >>> loaded_model = tf.keras.models.load_model('/tmp/model')
  >>> x = tf.random.uniform((10, 3))
  >>> assert np.allclose(model.predict(x), loaded_model.predict(x))

  The saved model contains:

      - the model's configuration (topology)
      - the model's weights
      - the model's optimizer's state (if any)

  Thus the saved model can be reinstantiated in
  the exact same state, without any of the code
  used for model definition or training.

  Note that the model weights may have different scoped names after being
  loaded. Scoped names include the model/layer names, such as
  `"dense_1/kernel:0"`. It is recommended that you use the layer properties to
  access specific variables, e.g. `model.get_layer("dense_1").kernel`.

  _SavedModel serialization_

  The SavedModel serialization path uses `tf.saved_model.save` to save the model
  and all trackable objects attached to the model (e.g. layers and variables).
  `@tf.function`-decorated methods are also saved. Additional trackable objects
  and functions are added to the SavedModel to allow the model to be
  loaded back as a Keras Model object.

  Arguments:
      model: Keras model instance to be saved.
      filepath: One of the following:
        - String or `pathlib.Path` object, path where to save the model
        - `h5py.File` object where to save the model
      overwrite: Whether we should overwrite any existing model at the target
        location, or instead ask the user with a manual prompt.
      include_optimizer: If True, save optimizer's state together.
      save_format: Either 'tf' or 'h5', indicating whether to save the model
        to Tensorflow SavedModel or HDF5. Defaults to 'tf' in TF 2.X, and 'h5'
        in TF 1.X.
      signatures: Signatures to save with the SavedModel. Applicable to the 'tf'
        format only. Please see the `signatures` argument in
        `tf.saved_model.save` for details.
      options: Optional `tf.saved_model.SaveOptions` object that specifies
        options for saving to SavedModel.

  Raises:
      ImportError: If save format is hdf5, and h5py is not available.
  r   )
sequentialtfh5N   aW  Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using `save_weights`.)Ztensorflow.python.keras.enginer   r   Zenabledr
   h5py
isinstanceFileospathsplitext_HDF5_EXTENSIONSZ_is_graph_networkZ
SequentialNotImplementedErrorr   Zsave_model_to_hdf5saved_model_saver   )	modelfilepath	overwriteZinclude_optimizerZsave_formatZ
signaturesoptionsr   default_format r   R/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py
save_model/   s    ?r!   zkeras.models.load_modelc          	   C   s~   t |p
i ^ tdk	r<t| tjs.t| r<t| ||S t| } t| t	j
rht|  t| ||S W dQ R X tddS )a  Loads a model saved via `model.save()`.

  Usage:

  >>> model = tf.keras.Sequential([
  ...     tf.keras.layers.Dense(5, input_shape=(3,)),
  ...     tf.keras.layers.Softmax()])
  >>> model.save('/tmp/model')
  >>> loaded_model = tf.keras.models.load_model('/tmp/model')
  >>> x = tf.random.uniform((10, 3))
  >>> assert np.allclose(model.predict(x), loaded_model.predict(x))

  Note that the model weights may have different scoped names after being
  loaded. Scoped names include the model/layer names, such as
  `"dense_1/kernel:0"`. It is recommended that you use the layer properties to
  access specific variables, e.g. `model.get_layer("dense_1").kernel`.

  Arguments:
      filepath: One of the following:
          - String or `pathlib.Path` object, path to the saved model
          - `h5py.File` object from which to load the model
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.
      compile: Boolean, whether to compile the model
          after loading.
      options: Optional `tf.saved_model.LoadOptions` object that specifies
        options for loading from SavedModel.

  Returns:
      A Keras model instance. If the original model was compiled, and saved with
      the optimizer, then the returned model will be compiled. Otherwise, the
      model will be left uncompiled. In the case that an uncompiled model is
      returned, a warning is displayed if the `compile` argument is set to
      `True`.

  Raises:
      ImportError: if loading from an hdf5 file and h5py is not available.
      IOError: In case of an invalid savefile.
  Nz\Unable to load model. Filepath is not an hdf5 file (or h5py is not available) or SavedModel.)r	   ZCustomObjectScoper   r   r   Zis_hdf5r   Zload_model_from_hdf5r
   sixstring_typesr   Zparse_saved_modelsaved_model_loadr   IOError)r   Zcustom_objectscompiler   r   r   r    
load_model   s    *
r'   )TTNNN)NTN)__doc__
__future__r   r   r   r   r"   Ztensorflow.pythonr   Ztensorflow.python.keras.savingr   Z*tensorflow.python.keras.saving.saved_modelr   r$   r   r   Ztensorflow.python.keras.utilsr	   Z&tensorflow.python.keras.utils.io_utilsr
   Ztensorflow.python.saved_modelr   Z tensorflow.python.util.tf_exportr   r   ImportErrorr   Z%_KERAS_SAVED_MODEL_STILL_EXPERIMENTALr!   r'   r   r   r   r    <module>   s6   

    S