
    0h<\                     l   d Z ddlZddlmc 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 dd
lmZ ddlmZ ddlmZ 	 ddlZej4                  	 	 	 	 	 	 dd       Zej4                  dd       Z	 ddZ	 ddZd Zd Z ejB                  jE                  e       y# e$ r dZY aw xY w)zKeras model saving code.    N)backend)object_registration)hdf5_format)saving_utils)serialization)load)save)keras_option_scope)io_utils)traceback_utilsc                    ddl m} t        j                  j                  j                         rdnd}	|xs |	}t        j                  |      }t        j                  |        |dk(  s5t        t        |t        j                        st        j                  |      rF| j                  s!t        | |j                        st!        d      t#        j$                  | |||       yt'        j(                         5  t+        |d      5  t-        j.                  | ||||||       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)	a  Saves a model as a TensorFlow SavedModel or HDF5 file.

    See the [Serialization and Saving
    guide](https://keras.io/guides/serialization_and_saving/) for details.

    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 `model.save()` is an alias for `tf.keras.models.save_model()`.

    The SavedModel and HDF5 file contains:

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

    Thus models 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 format__

    TF-Keras SavedModel uses `tf.saved_model.save` to save the model and all
    trackable objects attached to the model (e.g. layers and variables). The
    model config, weights, and optimizer are saved in the SavedModel.
    Additionally, for every TF-Keras layer attached to the model, the SavedModel
    stores:

      * the config and metadata -- e.g. name, dtype, trainable status
      * traced call and loss functions, which are stored as TensorFlow
        subgraphs.

    The traced functions allow the SavedModel format to save and load custom
    layers without the original class definition.

    You can choose to not save the traced functions by disabling the
    `save_traces` option. This will decrease the time it takes to save the model
    and the amount of disk space occupied by the output SavedModel. If you
    enable this option, then you _must_ provide all custom class definitions
    when loading the model. See the `custom_objects` argument in
    `tf.keras.models.load_model`.

    Args:
        model: TF-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: (only applies to SavedModel format)
          `tf.saved_model.SaveOptions` object that specifies options for saving
          to SavedModel.
        save_traces: (only applies to SavedModel format) When enabled, the
          SavedModel will store the function traces for each layer. This
          can be disabled, so that only the configs of each layer are stored.
          Defaults to `True`. Disabling this will decrease serialization time
          and reduce file size, but it requires that all custom layers/models
          implement a `get_config()` method.

    Raises:
        ImportError: If save format is hdf5, and h5py is not available.
    r   )
sequentialtfh5NaW  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`.Tsave_tracesin_tf_saved_model_scope)tf_keras.src.enginer   r   __internal__tf2enabledr   path_to_stringr   try_build_compiled_argumentsh5py
isinstanceFileis_hdf5_filepath_is_graph_network
SequentialNotImplementedErrorr   save_model_to_hdf5r   SharedObjectSavingScoper
   saved_model_saver	   )
modelfilepath	overwriteinclude_optimizersave_format
signaturesoptionsr   r   default_formats
             Y/var/www/html/engine/venv/lib/python3.12/site-packages/tf_keras/src/saving/legacy/save.py
save_modelr-   &   s4   x /__0088:TN/K&&x0H
 --e4 	tHdii!@((2 &&z:((0
 &E  	&&8Y(9	
 224 	#'  !%%%	 	 	 	s$   7D>D2!D>2D;	7D>>Ec           
         t        j                         5  |xs i }t        j                  j                  }t        j
                  }i |||}t        j                  |      5  t        dd      5  t        j                  j                  |      5  t        j                  |       }t        |t              rt        j                  j                   j#                  |      st%        d|       t        j                  j                   j'                  |      r;t)        j*                  |||      cddd       cddd       cddd       cddd       S t,        t/        d|       t1        j2                  t        j                  j                   j5                  |d      ||      cddd       cddd       cddd       cddd       S t,        Ut        | t,        j6                        r;t1        j2                  | ||      cddd       cddd       cddd       cddd       S ddd       ddd       ddd       ddd       t%        d	|        # 1 sw Y   /xY w# 1 sw Y   3xY w# 1 sw Y   7xY w# 1 sw Y   ;xY w)
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`.

    Args:
        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 TF-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.
    FTr   zNo file or directory found at NzCFilepath looks like a hdf5 file but h5pyis not available. filepath=rb)modezpUnable to load model. Filepath is not an hdf5 file (or h5py is not available) or SavedModel. Received: filepath=)r   SharedObjectLoadingScoper   _THREAD_LOCAL_CUSTOM_OBJECTS__dict___GLOBAL_CUSTOM_OBJECTSCustomObjectScoper
   r   r   load_contextr   r   r   striogfileexistsIOErrorisdirsaved_model_loadr   r   ImportErrorr   load_model_from_hdf5GFiler   )r%   custom_objectscompiler*   tlcogcofilepath_strs          r,   
load_modelrF      sZ   T 
	/	/	1 $'-2"??HH!88:N:d:c: 22>B 	#!4  __11': #+#:#:8#DL!,4!uu{{11,?")"@ O#  55;;,,\:#3#8#8 ,gw$  	 	$ $,  $|&1%11=%@'" !"
 $/#C#C " 1 1,T 1 J . '$'  	 	$ $B )j499.M*??$ng 3  	 	$ $	$L 	88@z	C ;  	 	$ $s   AI8#I,1 I BI	.	I 7	I, 	I8AI	&	I /	I,8	I86I		I 
	I,	I8&I .I,6I8II  I)%I,,I5	1I88Jc                    | j                          t        j                  |      }t        j                  |      }||rd}n>d}n;|j                         j                         }|dv rd}n|dv rd}nt        d| d      |dk(  r|rt        d| d	      |dk(  rt        t        d
      |dk(  r|dz   }n|}|s7t        j                  j                  |      rt        j                  |      }|sy|dk(  r6t        j                  |d      5 }	t        j                   |	|        ddd       yt#        j$                         st'        j(                          | j*                  j-                  ||       t"        j.                  j0                  j3                  t        j                  j5                  |      |d|g       y# 1 sw Y   yxY w)ae  Saves all layer weights.

    Either saves in HDF5 or in TensorFlow format based on the `save_format`
    argument.

    When saving in HDF5 format, the weight file has:
        - `layer_names` (attribute), a list of strings
            (ordered names of model layers).
        - For every layer, a `group` named `layer.name`
            - For every such layer group, a group attribute `weight_names`,
                a list of strings
                (ordered names of weights tensor of the layer).
            - For every weight in the layer, a dataset
                storing the weight value, named after the weight tensor.

    When saving in TensorFlow format, all objects referenced by the network
    are saved in the same format as `tf.train.Checkpoint`, including any
    `Layer` instances or `Optimizer` instances assigned to object
    attributes. For networks constructed from inputs and outputs using
    `tf.keras.Model(inputs, outputs)`, `Layer` instances used by the network
    are tracked/saved automatically. For user-defined classes which inherit
    from `tf.keras.Model`, `Layer` instances must be assigned to object
    attributes, typically in the constructor. See the documentation of
    `tf.train.Checkpoint` and `tf.keras.Model` for details.

    While the formats are the same, do not mix `save_weights` and
    `tf.train.Checkpoint`. Checkpoints saved by `Model.save_weights` should
    be loaded using `Model.load_weights`. Checkpoints saved using
    `tf.train.Checkpoint.save` should be restored using the corresponding
    `tf.train.Checkpoint.restore`. Prefer `tf.train.Checkpoint` over
    `save_weights` for training checkpoints.

    The TensorFlow format matches objects and variables by starting at a
    root object, `self` for `save_weights`, and greedily matching attribute
    names. For `Model.save` this is the `Model`, and for `Checkpoint.save`
    this is the `Checkpoint` even if the `Checkpoint` has a model attached.
    This means saving a `tf.keras.Model` using `save_weights` and loading
    into a `tf.train.Checkpoint` with a `Model` attached (or vice versa)
    will not match the `Model`'s variables. See the
    [guide to training checkpoints](
    https://www.tensorflow.org/guide/checkpoint) for details on
    the TensorFlow format.

    Args:
        filepath: String or PathLike, path to the file to save the weights
            to. When saving in TensorFlow format, this is the prefix used
            for checkpoint files (multiple files are generated). Note that
            the '.h5' suffix causes weights to be saved in HDF5 format.
        overwrite: Whether to silently overwrite any existing file at the
            target location, or provide the user with a manual prompt.
        save_format: Either 'tf' or 'h5'. A `filepath` ending in '.h5' or
            '.keras' will default to HDF5 if `save_format` is `None`.
            Otherwise `None` defaults to 'tf'.
        options: Optional `tf.train.CheckpointOptions` object that specifies
            options for saving weights.

    Raises:
        ImportError: If `h5py` is not available when attempting to save in
            HDF5 format.
    Nr   r   )
tensorflowr   )hdf5r   kerasz(Unknown format. Received: `save_format`=z$. Was expecting one of {"tf", "h5"}.zBsave_weights got save_format="tf"/"tensorflow", but the filepath (zT) looks like an HDF5 file. Omit the ".h5"/".keras" when saving in TensorFlow format.zi`save_weights` requires h5py when saving in hdf5, but h5py is not available. Try installing h5py package.z.indexw)r*   T)save_dirmodel_checkpoint_pathsave_relative_pathsall_model_checkpoint_paths)_assert_weights_createdr   r   r   r   lowerstrip
ValueErrorr   r>   ospathisfileask_to_proceed_with_overwriter   r   save_weights_to_hdf5_groupr   executing_eagerlyr   get_session_checkpointwriter   trainupdate_checkpoint_statedirname)
r$   r%   r&   r(   r*   filepath_is_h5user_formatcheck_filepathproceedfs
             r,   save_weightsre     s   ~ 
!!#&&x0H!228<NKK!'')//1..K33K:;- H5 5  d~!
 #HH
 	
 dt|:
 	
 d!H,!788HdYYx% 	=221e<	= 	= ##%!': 	55WW__X."* $(0z	 	6 	
	= 	=s   F>>Gc                     t        j                  | j                        rf| j                  j                  j                  dkD  rCt        j                  |      s.| j                  j                  j                  }t        d|       |r|st        d      t        |      \  }}|dk(  r| j                  j                  ||      }|rt        d      t        j                         s?t        j                         }t        j                  j                   j#                  ||       |j%                          nd}t&        t)        d      | j*                  s| j,                  st        d	      | j/                          t'        j0                  |d
      5 }	d|	j2                  vr	d|	v r|	d   }	|rt5        j6                  |	| |       nt5        j8                  |	|        ddd       | j:                  D ]  }
|
j=                           |S # 1 sw Y   ,xY w)a	  Loads all layer weights, either from a SavedModel or H5 weights file.

    If `by_name` is False weights are loaded based on the network's
    topology. This means the architecture should be the same as when the
    weights were saved.  Note that layers that don't have weights are not
    taken into account in the topological ordering, so adding or removing
    layers is fine as long as they don't have weights.

    If `by_name` is True, weights are loaded into layers only if they share
    the same name. This is useful for fine-tuning or transfer-learning
    models where some of the layers have changed.

    Only topological loading (`by_name=False`) is supported when loading
    weights from the TensorFlow format. Note that topological loading
    differs slightly between TensorFlow and HDF5 formats for user-defined
    classes inheriting from `tf.keras.Model`: HDF5 loads based on a
    flattened list of weights, while the TensorFlow format loads based on
    the object-local names of attributes to which layers are assigned in the
    `Model`'s constructor.

    Args:
        filepath: String, path to the weights file to load. For weight files
            in TensorFlow format, this is the file prefix (the same as was
            passed to `save_weights`). This can also be a path to a
            SavedModel saved from `model.save`.
        by_name: Boolean, whether to load weights by name or by topological
            order. Only topological loading is supported for weight files in
            TensorFlow format.
        skip_mismatch: Boolean, whether to skip loading of layers where
            there is a mismatch in the number of weights, or a mismatch in
            the shape of the weight (only valid when `by_name=True`).
        options: Optional `tf.train.CheckpointOptions` object that specifies
            options for loading weights.

    Returns:
        When loading a weight file in TensorFlow format, returns the same
        status object as `tf.train.Checkpoint.restore`. When graph building,
        restore ops are run automatically as soon as the network is built
        (on first call for user-defined classes inheriting from `Model`,
        immediately if it is already built).

        When loading weights in HDF5 format, returns `None`.

    Raises:
        ImportError: If `h5py` is not available and the weight file is in
            HDF5 format.
        ValueError: If `skip_mismatch` is set to `True` when `by_name` is
            `False`.
       zmLoad weights is not implemented with TPUStrategy with `steps_per_run` greater than 1. The `steps_per_run` is z\When calling model.load_weights, skip_mismatch can only be set to True when by_name is True.r   zWeights may only be loaded based on topology into Models when loading TensorFlow-formatted weights (got by_name=True to load_weights).)statussessionNzY`load_weights` requires h5py package when loading weights from HDF5. Try installing h5py.zUnable to load weights saved in HDF5 format into a subclassed Model which has not created its variables yet. Call the Model first, then load the weights.rlayer_namesmodel_weights)r   is_tpu_strategy_distribution_strategyextendedsteps_per_runr   r   rS   _detect_save_formatr[   readr    r   rY   rZ   r   trackingstreaming_restoreassert_nontrivial_matchr   r>   r   builtrP   r   attrsr   $load_weights_from_hdf5_group_by_nameload_weights_from_hdf5_grouplayersfinalize_state)r$   r%   by_nameskip_mismatchr*   sprr(   rh   ri   rd   layers              r,   load_weightsr     s   h u;;<''00>>B--h7..77EEC&&)U, 
 W0
 	

 09Hkd""''':%6 
 ##%))+G OO$$66w 7  	&&(<2  &&u{{? 
 	%%'YYx% 	CAGG+10Do&@@um 88EB	C  M	C 	Cs   AHHc                    t        j                  |       } t        j                  |       r| dfS t	        |       rd}| |fS t
        j                  j                  |       r~t        j                  j                  | t
        j                  j                  t
        j                  j                        }t	        |      r|} d}| |fS t        dj                  |             d}| |fS )z-Returns path to weights file and save format.r   r   zUnable to load weights. filepath {} appears to be a SavedModel directory, but checkpoint either doesn't exist, or is incorrectly formatted.)r   r   r   r   _is_readable_tf_checkpointr   saved_modelcontains_saved_modelrT   rU   joinVARIABLES_DIRECTORYVARIABLES_FILENAMErS   format)r%   r(   	ckpt_paths      r,   rq   rq     s     &&x0H$$X.~
 "(+( [  ' 
	,	,X	6GGLLNN..NN--
	
 &i0 HK [   66<fX6F  [      c                     	 t         j                  j                  j                  j	                  |        y# t         j
                  j                  $ r Y yw xY w)NTF)r   compatv1r]   NewCheckpointReadererrorsDataLossError)r%   s    r,   r   r     sB    
		..x899"" s   36 AA)TTNNNT)NTN)TNN)FFN)#__doc__rT   tensorflow.compat.v2r   v2r   tf_keras.srcr   tf_keras.src.savingr   tf_keras.src.saving.legacyr   r   r   &tf_keras.src.saving.legacy.saved_modelr   r=   r	   r#   ,tf_keras.src.saving.legacy.saved_model.utilsr
   tf_keras.src.utilsr   r   r   r>   filter_tracebackr-   rF   re   r   rq   r   r   register_load_model_function r   r,   <module>r      s     	 ! !   3 2 3 4 K K K ' .
 !! H "HV !!R "Rl @Dw
v BFpf!D  , ,Z 8A  Ds   B) )B32B3