a
    Sic,                     @   s   d Z ddlZddlZddlZddlZddlm  mZ ddl	m
Z
 ddlmZ ddlmZ dZdZdd	 Zd
d Zdd Zdd Zdd Zdd ZdS )z>Keras python-based idempotent saving functions (experimental).    N)
json_utils)generic_utils)	tf_exportzconfig.kerasFc                 C   sX   t j| t}tjj|d}| }W d   n1 s<0    Y  t	
|}t|S )zLoad a saved python model.rN)ospathjoin_CONFIG_FILEtfiogfileGFilereadr   decodedeserialize_keras_object)dirpath	file_pathfconfig_jsonconfig_dict r   `/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/saving/experimental/saving_lib.pyload#   s
    &
r   c                 C   s   t jj|st jj| tj|t}t	| }t
j|tjd}t jj|d}|| W d   n1 sr0    Y  dS )zSave a saved python model.)clswN)r
   r   r   existsmkdirr   r   r   r	   serialize_keras_objectjsondumpsr   Encoderr   write)modelr   r   Zserialized_model_dictr   r   r   r   r   save,   s    r#   c              
   C   s\  | d }| d }| d }| d }|dkr|dkrNt |tsJtd| d|S |d	krt|}|d
url|S |dd
}zt|}W n< ty } z$td| d|  d|W Y d
}~n
d
}~0 0 t	||d S td| t|}|d
ur|
|S |d
u rt|}	nt|}
t	|
|d
}	t|	dsNtd|	 d|	
|S d
S )a  Retrieve the object by deserializing the config dict.

    The config dict is a python dictionary that consists of a set of key-value
    pairs, and represents a Keras object, such as an `Optimizer`, `Layer`,
    `Metrics`, etc. The saving and loading library uses the following keys to
    record information of a Keras object:

    - `class_name`: String. For classes that have an exported Keras namespace,
      this is the full path that starts with "keras", such as
      "keras.optimizers.Adam". For classes that do not have an exported Keras
      namespace, this is the name of the class, as exactly defined in the source
      code, such as "LossesContainer".
    - `config`: Dict. Library-defined or user-defined key-value pairs that store
      the configuration of the object, as obtained by `object.get_config()`.
    - `module`: String. The path of the python module, such as
      "keras.engine.compile_utils". Built-in Keras classes
      expect to have prefix `keras`. For classes that have an exported Keras
      namespace, this is `None` since the class can be fully identified by the
      full Keras path.
    - `registered_name`: String. The key the class is registered under via
      `keras.utils.register_keras_serializable(package, name)` API. The key has
      the format of '{package}>{name}', where `package` and `name` are the
      arguments passed to `register_keras_serializable()`. If `name` is not
      provided, it defaults to the class name. If `registered_name` successfully
      resolves to a class (that was registered), `class_name` and `config`
      values in the dict will not be used. `registered_name` is only used for
      non-built-in classes.

    For example, the following dictionary represents the built-in Adam optimizer
    with the relevant config. Note that for built-in (exported symbols that have
    an exported Keras namespace) classes, the library tracks the class by the
    the import location of the built-in object in the Keras namespace, e.g.
    `"keras.optimizers.Adam"`, and this information is stored in `class_name`:

    ```
    dict_structure = {
        "class_name": "keras.optimizers.Adam",
        "config": {
            "amsgrad": false,
            "beta_1": 0.8999999761581421,
            "beta_2": 0.9990000128746033,
            "decay": 0.0,
            "epsilon": 1e-07,
            "learning_rate": 0.0010000000474974513,
            "name": "Adam"
        },
        "module": null,
        "registered_name": "Adam"
    }
    # Returns an `Adam` instance identical to the original one.
    deserialize_keras_object(dict_structure)
    ```

    If the class does not have an exported Keras namespace, the library tracks
    it by its `module` and `class_name`. For example:

    ```
    dict_structure = {
      "class_name": "LossesContainer",
      "config": {
          "losses": [...],
          "total_loss_mean": {...},
      },
      "module": "keras.engine.compile_utils",
      "registered_name": "LossesContainer"
    }

    # Returns a `LossesContainer` instance identical to the original one.
    deserialize_keras_object(dict_structure)
    ```

    And the following dictionary represents a user-customized `MeanSquaredError`
    loss:

    ```
    @keras.utils.generic_utils.register_keras_serializable(package='my_package')
    class ModifiedMeanSquaredError(keras.losses.MeanSquaredError):
      ...

    dict_structure = {
        "class_name": "ModifiedMeanSquaredError",
        "config": {
            "fn": "mean_squared_error",
            "name": "mean_squared_error",
            "reduction": "auto"
        },
        "registered_name": "my_package>ModifiedMeanSquaredError"
    }
    # Gives `ModifiedMeanSquaredError` object
    deserialize_keras_object(dict_structure)
    ```

    Args:
      config_dict: the python dict structure to deserialize the Keras object
        from.

    Returns:
      The Keras object that is deserialized from `config_dict`.

    
class_nameconfigmoduleregistered_namebuiltinsstrz7Config of string is supposed to be a string. Received: .functionNzThe function module z5 is not available. The config dictionary provided is function_namezUnrecognized type: from_configz%Unable to reconstruct an instance of )
isinstancer)   	TypeErrorr   get_custom_objects_by_nameget	importlibimport_moduleImportErrorvarsr-   r   get_symbol_from_namehasattr)r   r$   r%   r&   r'   Zcustom_functionfunction_moduleeZcustom_classr   modr   r   r   r   @   sT    g





r   c                 C   sB   d}t j| jdd}|du r,| jj}| jj}||t| t| dS )a  Retrieve the config dict by serializing the Keras object.

    `serialize_keras_object()` serializes a Keras object to a python dictionary
    that represents the object, and is a reciprocal function of
    `deserialize_keras_object()`. See `deserialize_keras_object()` for more
    information about the config format.

    Args:
      obj: the Keras object to serialize.

    Returns:
      A python dict that represents the object. The python dict can be
      deserialized via `deserialize_keras_object()`.
    Nkeras)api_name)r&   r$   r%   r'   )r   get_canonical_name_for_symbol	__class__
__module____name___get_object_config_get_object_registered_name)objr&   r$   r   r   r   r      s    r   c                 C   s&   t | tjrt| S t| jS d S )N)r.   typesFunctionTyper   get_registered_namer>   rC   r   r   r   rB     s    
rB   c                 C   sJ   t | tr| S t | tjr(| j| jdS t| dsBtd|  d|  S )zDReturn the object's config depending on string, function, or others.)r&   r,   
get_configz"Unable to recognize the config of r*   )	r.   r)   rD   rE   r?   r@   r7   r/   rH   rG   r   r   r   rA     s    

rA   )__doc__r2   r   r   rD   tensorflow.compat.v2compatv2r
   keras.saving.saved_modelr   keras.utilsr   tensorflow.python.utilr   r	   _ENABLEDr   r#   r   r   rB   rA   r   r   r   r   <module>   s"   	 '%