o
    NeC:                     @   sp  d Z ddlZddl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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 zddlZW n eyb   dZY nw d
ZdZdZdZdZe Zde_ e!h dZ"dd Z#d,ddZ$dd Z%dd Z&dd Z'dd Z(dd Z)G dd  d Z*G d!d" d"Z+d#d$ Z,d-d&d'Z-d(d) Z.d*d+ Z/dS ).z3Python-based idempotent model-saving functionality.    N)losses)
base_layer)	optimizer)deserialize_keras_object)serialize_keras_object)generic_utils)io_utilsselfzconfig.jsonzmetadata.jsonzvariables.h5ZassetsF>   Znon_trainable_variablesZ_self_tracked_trackablesweightsZ$_self_unconditional_dependency_namesZtrainable_weightsZ
submodules__dict__Zstate_updatesZ_output_layersZnon_trainable_weightsZ_trainable_weights	variablesZ_layer_call_argspecsZ_input_layersZ_non_trainable_weightsZupdatesZtrainable_variablesc              
   C   s  | dstd| tdu rtd| jstjddd ttdd	}d
t_	t
| }t|}ttjtj dd}t }zzttjj|td}|| W d   n1 s_w   Y  ttjj|td}|| W d   n1 sw   Y  ttjj|td}tjj|t}	t| t|t |	dt! d t"|dd |#  t$%|d}
t&|
|d W d   n1 sw   Y  t'|
d W n t(y } z|d}~ww W |t_	tjj)| dS |t_	tjj)| w )a  Save a zip-archive representing a Keras model to the given filepath.

    The zip-based archive contains the following structure:

    - JSON-based configuration file (config.json): Records of model, layer, and
        other trackables' configuration.
    - NPZ-based trackable state files, found in respective directories, such as
        model/states.npz, model/dense_layer/states.npz, etc.
    - Metadata file (this is a TODO).

    The states of Keras trackables (layers, optimizers, loss, and metrics) are
    automatically saved as long as they can be discovered through the attributes
    returned by `dir(Model)`. Typically, the state includes the variables
    associated with the trackable, but some specially purposed layers may
    contain more such as the vocabularies stored in the hashmaps. The trackables
    define how their states are saved by exposing `save_state()` and
    `load_state()` APIs.

    For the case of layer states, the variables will be visited as long as
    they are either 1) referenced via layer attributes, or 2) referenced via a
    container (list, tuple, or dict), and the container is referenced via a
    layer attribute.
    .kerasDInvalid filename: expected a `.keras` extension. Received: filepath=Nz0h5py must be installed in order to save a model.zYou are saving a model that has not yet been built. It might not contain any weights yet. Consider building the model first by calling it on some data.   )
stacklevelvalueFTz%Y-%m-%d@%H:%M:%S)Zkeras_versionZ
date_savedw weights_handlerassets_handler
inner_pathvisited_trackablesZsavingaction)*endswith
ValueErrorh5pyImportErrorZbuiltwarningswarngetattr_SAVING_V3_ENABLEDr   r   jsondumpskeras__version__datetimenowstrftime_get_temp_diropentfiogfilejoin_METADATA_FILENAMEwrite_CONFIG_FILENAMEFile_VARS_FNAME_ASSETS_DIRNAME_save_stateH5IOHandlerDiskIOHandlerset_print_h5_fileclosezipfileZipFile_write_recursively_print_zip_file	Exceptionrmtree)modelfilepathsaving_v3_enabled_valueZserialized_model_dictconfig_jsonZmetadata_json	temp_pathfh5_file
assets_dirzipfile_to_savee rL   TD:\Projects\ConvertPro\env\Lib\site-packages\keras/saving/experimental/saving_lib.py
save_modelK   sl   

rN   c              
   C   s  |  dstd|  tdu rtdttdd}dt_t }zzwt	| d}t
|d	 || W d   n1 s=w   Y  ttjj|td}| }W d   n1 s\w   Y  t|}t||}ttjj|td}	t|	d	d
 tjj|t}
t|t|	t|
dt d |	  W n ty } z|d}~ww |W |t_tjj |rtjj!| S S |t_tjj |rtjj!| w w )z.Load a zip archive representing a Keras model.r   r   Nz0h5py must be installed in order to load a model.r   FTrZloadingr   r   r   )"r   r   r   r   r!   r"   r   r*   r<   r=   r?   
extractallr+   r,   r-   r.   r/   r2   readr#   loadsr   r3   r4   r:   r5   _load_stater7   r8   r9   r;   r@   existsrA   )rC   Zcustom_objectsrD   rF   Zzipfile_to_loadrG   rE   Zconfig_dictrB   rH   rI   rK   rL   rL   rM   
load_model   sZ   




rU   c                 C   sb   t jj|s| || d S t jj|D ]}t jj||}t jj||}t| || qd S N)r,   r-   r.   isdirr1   listdirr/   r>   )rJ   Zsystem_pathzip_path	file_nameZsystem_file_pathZzip_file_pathrL   rL   rM   r>      s   r>   c              	   C      t | |v rd S t| dr| || t| dr"| || |t |  t| D ]C}|tv r4q-zt| |}W n	 t	yD   Y q-w t
|rYt|||tjj|||d q-t|tttfrpt|||tjj|||d q-d S )N_save_own_variables_save_assetsr   r   )idhasattrr\   maker]   adddirATTR_SKIPLISTr!   r@   _is_keras_trackabler6   r,   r-   r.   r/   
isinstancelistdicttuple_save_container_state	trackabler   r   r   r   Z
child_attrZ	child_objrL   rL   rM   r6      sB   

r6   c              	   C   r[   )N_load_own_variables_load_assetsr^   )r_   r`   rm   getrn   rb   rc   rd   r!   r@   re   rS   r,   r-   r.   r/   rf   rg   rh   ri   _load_container_staterk   rL   rL   rM   rS     sB   

rS   c              	   C   z   i }| D ]6}t |r:t|jj}||v r'||  d7  < | d||  }nd||< t|||tjj	|||d qd S N   _r   r^   )
re   r   to_snake_case	__class____name__r6   r,   r-   r.   r/   	containerr   r   r   r   Z
used_namesrl   namerL   rL   rM   rj   )  s"   rj   c              	   C   rq   rr   )
re   r   ru   rv   rw   rS   r,   r-   r.   r/   rx   rL   rL   rM   rp   A  s"   rp   c                   @   $   e Zd Zdd Zdd Zdd ZdS )r8   c                 C   
   || _ d S rV   )base_directory)r	   r}   rL   rL   rM   __init__W     
zDiskIOHandler.__init__c                 C   s<   |s| j S tjj| j |}tjj|stjj| |S rV   )r}   r,   r-   r.   r/   rT   makedirsr	   pathrL   rL   rM   ra   Z  s   zDiskIOHandler.makec                 C   s2   |s| j S tjj| j |}tjj|r|S d S rV   )r}   r,   r-   r.   r/   rT   r   rL   rL   rM   ro   b  s   zDiskIOHandler.getNrw   
__module____qualname__r~   ra   ro   rL   rL   rL   rM   r8   V  s    r8   c                   @   r{   )r7   c                 C   r|   rV   )rH   )r	   rH   rL   rL   rM   r~   l  r   zH5IOHandler.__init__c                 C   s"   |s| j dS | j |dS )Nvars)rH   Zcreate_groupr   rL   rL   rM   ra   o  s   zH5IOHandler.makec                 C   s8   |s| j d S || j v r| j | d S td|  i S )Nr   z"Warning: asset missing from file: )rH   printr   rL   rL   rM   ro   t  s   

zH5IOHandler.getNr   rL   rL   rL   rM   r7   k  s    r7   c                  C   s    t  } t j| d}|  | S )N)rc   )tempfilemkdtempTemporaryFiler;   )temp_dirZtestfilerL   rL   rM   r*   }  s   r*   r   c                 C   s`   |st d|  d| d t| dsd S |  D ]}t d| |  t| | |d d qd S )NzKeras weights file (z) :keysz...)prefix)r   r`   r   r:   )rH   r   r   keyrL   rL   rM   r:     s   
r:   c                 C   sZ   t d| d t dd  | jD ]}d|jd d  }t d|j||jf  qd S )NzKeras model archive r   z%-46s %19s %12s)z	File NamezModified    Sizez%d-%02d-%02d %02d:%02d:%02d   z%-46s %s %12d)r   Z	print_msgfilelist	date_timefilename	file_size)r<   r   zinfodaterL   rL   rM   r?     s   
r?   c                 C   s&   ddl m} t| tjtj|jtj	fS )Nr   )base_metric)
Zkeras.metricsr   rf   r   ZLayerr   Z	OptimizerZMetricr   ZLoss)objr   rL   rL   rM   re     s   re   rV   )r   N)0__doc__r'   r#   r   	threadingr   r<   Ztensorflow.compat.v2compatv2r,   r%   r   Zkeras.enginer   Z'keras.optimizers.optimizer_experimentalr   Z+keras.saving.experimental.serialization_libr   r   Zkeras.utilsr   r   r   r   Z_SELF_DIRNAMEr2   r0   r4   r5   localr"   r   	frozensetrd   rN   rU   r>   r6   rS   rj   rp   r8   r7   r*   r:   r?   re   rL   rL   rL   rM   <module>   sX   
U-
*(

