
    2Vh*                        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Z edd	g      dd
       Z eddg      dd       Z ed      	 dd       Z ed      dd       Zy# e$ r dZY Kw xY w)    N)logging)keras_export)legacy_h5_format)
saving_lib)
file_utils)io_utilszkeras.saving.save_modelzkeras.models.save_modelc                    |j                  dd      }|j                  dd      }|r[t        |      j                  d      st        |      j                  d      rt        j                  d|        nt        d|       |r%t        d	t        |j                                      t        |      j                  d      rt        j                  d
       t        |      j                  d      }|| }	 | xr t        j                  j                  |      }|r|st        j                  |      }	|	sy|r0t        |      j                  d      rt        j                   | |      S |st        j                   | |d      S t        |      j                  d      rt#        j$                  | |||      S t        d| d      # t        $ r d}Y w xY w)a  Saves a model as a `.keras` file.

    Args:
        model: Keras model instance to be saved.
        filepath: `str` or `pathlib.Path` object. Path where to save the model.
        overwrite: Whether we should overwrite any existing model at the target
            location, or instead ask the user via an interactive prompt.
        zipped: Whether to save the model as a zipped `.keras`
            archive (default when saving locally), or as an unzipped directory
            (default when saving on the Hugging Face Hub).

    Example:

    ```python
    model = keras.Sequential(
        [
            keras.layers.Dense(5, input_shape=(3,)),
            keras.layers.Softmax(),
        ],
    )
    model.save("model.keras")
    loaded_model = keras.saving.load_model("model.keras")
    x = keras.random.uniform((10, 3))
    assert np.allclose(model.predict(x), loaded_model.predict(x))
    ```

    Note that `model.save()` is an alias for `keras.saving.save_model()`.

    The saved `.keras` file is a `zip` archive that contains:

    - The model's configuration (architecture)
    - The model's weights
    - The model's optimizer's state (if any)

    Thus models can be reinstantiated in the exact same state.
    include_optimizerTsave_formatF.h5.hdf5.keraszThe `save_format` argument is deprecated in Keras 3. We recommend removing this argument as it can be inferred from the file path. Received: save_format=zThe `save_format` argument is deprecated in Keras 3. Please remove this argument and pass a file path with either `.keras` or `.h5` extension.Received: save_format=z-The following argument(s) are not supported: a  You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. hf://N)zippeda  Invalid filepath extension for saving. Please add either a `.keras` extension for the native Keras format (recommended) or a `.h5` extension. Use `model.export(filepath)` if you want to export a SavedModel for use with TFLite/TFServing/etc. Received: filepath=.)popstrendswithr   warning
ValueErrorlistkeys
startswithospathexists	TypeErrorr   ask_to_proceed_with_overwriter   
save_modelr   save_model_to_hdf5)
modelfilepath	overwriter   kwargsr
   r   is_hfr   proceeds
             K/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/saving/saving_api.pyr    r       s   L 

#6=**]E2Kx=!!"23s8}7M7M8
 OO) *57 ) *57  ;FKKM"#%
 	
 8}./C	
 M$$W-E~)9!9 i88B#h-((2$$UH55$$UHUCC
8}./228Y(9
 	
 	
 'Zq	*   s   8$G GGzkeras.saving.load_modelzkeras.models.load_modelc                 t   t        |       j                  d      xr t        j                  |       }t	        j
                  |       xr) t	        j                  t	        j                  | d            }t        |       j                  d      }t	        j                  |       rt	        j
                  |       sx|sv|stt	        j                  t        j                         t        j                  j                  |             }t	        j                  | |       t        j                  |      r|} d}|s|s|rt        j                   | |||      S t        |       j                  d      rt#        j$                  | ||      S t        |       j                  d      rt'        d|  d	      t'        d
|  d|  d      )a  Loads a model saved via `model.save()`.

    Args:
        filepath: `str` or `pathlib.Path` object, path to the saved model file.
        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.
        safe_mode: Boolean, whether to disallow unsafe `lambda` deserialization.
            When `safe_mode=False`, loading an object has the potential to
            trigger arbitrary code execution. This argument is only
            applicable to the Keras v3 model format. Defaults to `True`.

    Returns:
        A Keras model instance. If the original model was compiled,
        and the argument `compile=True` is set, then the returned model
        will be compiled. Otherwise, the model will be left uncompiled.

    Example:

    ```python
    model = keras.Sequential([
        keras.layers.Dense(5, input_shape=(3,)),
        keras.layers.Softmax()])
    model.save("model.keras")
    loaded_model = keras.saving.load_model("model.keras")
    x = np.random.random((10, 3))
    assert np.allclose(model.predict(x), loaded_model.predict(x))
    ```

    Note that the model variables may have different name values
    (`var.name` property, e.g. `"dense_1/kernel:0"`) after being reloaded.
    It is recommended that you use layer attributes to
    access specific variables, e.g. `model.get_layer("dense_1").kernel`.
    r   zconfig.jsonr   T)custom_objectscompile	safe_moder   )r*   r+   zFile not found: filepath=z<. Please ensure the file is an accessible `.keras` zip file.$File format not supported: filepath=a  . Keras 3 only supports V3 `.keras` files and legacy H5 format files (`.h5` extension). Note that the legacy SavedModel format is not supported by `load_model()` in Keras 3. In order to reload a TensorFlow SavedModel as an inference-only layer in Keras 3, use `keras.layers.TFSMLayer(za, call_endpoint='serving_default')` (note that your `call_endpoint` might have a different name).)r   r   zipfile
is_zipfiler   isdirr   joinr   is_remote_pathr   get_temp_dirr   r   basenamecopy
load_modelr   load_model_from_hdf5r   )r#   r*   r+   r,   is_keras_zipis_keras_dirr&   
local_paths           r(   r6   r6   |   s   J x=))(3 8J8J9L ##H- *2C2C-03L M$$W-E 	!!(+  *__##%rww'7'7'A


 	*- j)!HL|u$$)	
 	
 8}./44^W
 	
 
X			)'z 2 
 	
 28* =' j ,
,
 	
    zkeras.saving.save_weightsc                 h   t        |      }||j                  d      st        d|       ||j                  d      st        d|       	 t        j                  j                  |      }|r|st        j                  |      }|sy t        j                  | ||fi | y # t        $ r d}Y Bw xY w)N.weights.h5z;The filename must end in `.weights.h5`. Received: filepath=)z
weights.h5zweights.jsonz`The filename must end in `.weights.json` when `max_shard_size` is specified. Received: filepath=F)r   r   r   r   r   r   r   r   r   r   save_weights_only)r"   r#   r$   max_shard_sizer%   filepath_strr   r'   s           r(   save_weightsrA      s     x=Ll&;&;M&J"".1
 	
 
	#L,A,A&- --9N<
 	
) i88F  .KFK  s   B# #B10B1zkeras.saving.load_weightsc                    t        |      }|j                  d      r)|rt        d|       t        j                  | ||       y |j                  d      s|j                  d      r<|j                  dd       }|rt        d|       t        j                  | |||       y |j                  d      s|j                  d	      r|j                  d
d      }|rt        d|       t        st        d      t        j                  |d      5 }d|j                  vr	d|v r|d   }|rt        j                  || |       nt        j                  ||        d d d        y t        d| d      # 1 sw Y   y xY w)Nr   zInvalid keyword arguments: )skip_mismatchr=   z.weights.jsonobjects_to_skip)rC   rD   r   r   by_nameFz2Loading a H5 file requires `h5py` to be installed.rlayer_namesmodel_weightsr-   zY. Keras 3 only supports V3 `.keras` and `.weights.h5` files, or legacy V1/V2 `.h5` files.)r   r   r   r   load_weights_onlyr   h5pyImportErrorFileattrsr   $load_weights_from_hdf5_group_by_nameload_weights_from_hdf5_group)r"   r#   rC   r%   r@   rD   rE   fs           r(   load_weightsrQ      s   x=LX&:6(CDD$$8=	
 
		}	-1F1F2 !**%6=:6(CDD$$'+		
 
		u	%)>)>w)G**Y.:6(CDDD  YYx% 	HAGG+10Do& EEum !==aG	H 	H 28* =2 2
 	
	H 	Hs   AE//E8)TN)NTT)F)r   r.   abslr   keras.src.api_exportr   keras.src.legacy.savingr   keras.src.savingr   keras.src.utilsr   r   rJ   rK   r    r6   rA   rQ    r;   r(   <module>rX      s    	   - 4 ' & $
 (*CDEf FfR (*CDE]
 F]
@ )*48L +L6 )**
 +*
Y  Ds   A4 4A>=A>