
    ijԁ                        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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 eeeefZ eh d      Z eh d      Z G d d      Z G d d      Z  ed      d        Z!d Z" G d d      Z#d Z$d Z%d Z& eddg      d        Z'd Z(d&dZ)d&dZ*d Z+d  Z, ed!d"g      	 d'd#       Z-d$ Z.	 d&d%Z/y)(z6Object config serialization and deserialization logic.    N)
api_export)backend)keras_export)global_state)object_registration)KerasSaveable)python_utils)
tensorflow>   lossesmetrics
optimizersactivationsconstraintsinitializersregularizers>   keras.utils.get_filekeras.utils.load_imgkeras.utils.save_imgkeras.models.load_modelkeras.models.save_modelkeras.saving.load_modelkeras.saving.save_modelkeras.saving.load_weightskeras.saving.save_weightskeras.utils.clear_sessionkeras.backend.clear_sessionkeras.models.model_from_json"keras.preprocessing.image.load_img"keras.preprocessing.image.save_img'keras.utils.register_keras_serializable'keras.utils.text_dataset_from_directory(keras.saving.register_keras_serializable(keras.utils.audio_dataset_from_directory(keras.utils.image_dataset_from_directory/keras.preprocessing.text_dataset_from_directory0keras.preprocessing.image_dataset_from_directory"keras.utils.serialize_keras_object#keras.saving.serialize_keras_object$keras.utils.deserialize_keras_object%keras.saving.deserialize_keras_object*keras.config.enable_unsafe_deserializationc                       e Zd Zd Zd Zy)SerializableDictc                     || _         y N)config)selfr0   s     w/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/saving/serialization_lib.py__init__zSerializableDict.__init__C   s	        c                 ,    t        | j                        S r/   )serialize_keras_objectr0   r1   s    r2   	serializezSerializableDict.serializeF   s    %dkk22r4   N)__name__
__module____qualname__r3   r8    r4   r2   r-   r-   B   s    3r4   r-   c                   $    e Zd ZdZddZd Zd Zy)SafeModeScopezBScope to propagate safe mode flag to nested deserialization calls.c                     || _         y r/   )	safe_mode)r1   r@   s     r2   r3   zSafeModeScope.__init__M   s	    "r4   c                 b    t               | _        t        j                  d| j                         y Nsafe_mode_saving)in_safe_modeoriginal_valuer   set_global_attributer@   r7   s    r2   	__enter__zSafeModeScope.__enter__P   s!    *n))*<dnnMr4   c                 D    t        j                  d| j                         y rB   )r   rF   rE   r1   argskwargss      r2   __exit__zSafeModeScope.__exit__T   s    )) 3 3	
r4   N)T)r9   r:   r;   __doc__r3   rG   rL   r<   r4   r2   r>   r>   J   s    L#N
r4   r>   r+   c                  0    t        j                  dd       y)zADisables safe mode globally, allowing deserialization of lambdas.rC   FNr   rF   r<   r4   r2   enable_unsafe_deserializationrP   Z   s     %%&8%@r4   c                  ,    t        j                  d      S rB   r   get_global_attributer<   r4   r2   rD   rD   `   s    ,,-?@@r4   c                       e Zd ZdZd Zd Zy)ObjectSharingScopez?Scope to enable detection and reuse of previously seen objects.c                 \    t        j                  di        t        j                  di        y Nshared_objects/id_to_obj_mapshared_objects/id_to_config_maprO   r7   s    r2   rG   zObjectSharingScope.__enter__g   s$    ))*H"M))*KRPr4   c                 \    t        j                  dd        t        j                  dd        y rW   rO   rI   s      r2   rL   zObjectSharingScope.__exit__k   s&    ))*H$O))-t	
r4   N)r9   r:   r;   rM   rG   rL   r<   r4   r2   rU   rU   d   s    IQ
r4   rU   c                 V    t        j                  d      }||j                  | d      S y)z:Retrieve an object previously seen during deserialization.rX   N)r   rS   get)obj_idid_to_obj_maps     r2   get_shared_objectr_   r   s5     55&M    .. !r4   c                     |d   dk(  rd|d<   t        j                  d      }|yt        t        |             }||vr|||<   y||d<   ||   }||d<   y)z>Call after serializing an object, to keep track of its config.module__main__NrY   shared_object_id)r   rS   intid)objr0   id_to_config_mapr]   prev_configs        r2   !record_object_after_serializationri   {   sy    h:%x#88) C\F%%#) %+!"&v.*0&'r4   c                 >    t        j                  d      }|y| ||<   y)zFCall after deserializing an object, to keep track of it in the future.rX   NrR   )rf   r]   r^   s      r2   #record_object_after_deserializationrk      s*     55&M M&r4   r(   r'   c                 	   | | S t        | t              r| S t        | t        t        f      r5| D cg c]  }t	        |       }}t        | t              rt        |      S |S t        | t
              rt        |       S t        | t              rdd| j                  d      idS t        | t              rBdt	        | j                        t	        | j                        t	        | j                        ddS t        | t        t                    rdi dS t        | t        j                         rIt#        | d	d      }|rt        |      }|d
   j$                  |d
<   d| j&                  | j(                  |ddS t*        j,                  r8t        | t*        j.                        r| j0                  | j3                         S dS t        j4                  |       rFdt        j6                  |       j9                         t        j:                  | j(                        ddS t        |       j<                  t>        j@                  k(  rlt        | t>        jB                        rB| jD                  d
kD  r3d| j9                         t        j:                  | j(                        ddS | jG                         S t*        j,                  r&t        | t*        jH                        r| j$                  S t        | tJ        jL                        rV| j@                  dk(  rGtO        jP                  dtS        jT                  |        d       ddtW        jX                  |       idS t*        j,                  rpt        | t*        jZ                        rV| j]                         }t        t_        d |            }d| j`                  j@                  | j`                  j<                  |ddS tc        |       }te        | j`                  |      }|tg        | |       ti        | |       |S t        | tJ        jL                        r| j<                  }n| j`                  j<                  }| j`                  j@                  }|dk(  rd}	nOt        | tJ        jL                        rtk        jl                  |       }	ntk        jl                  | j`                        }	||||	d}
tg        | |
       ti        | |
       |
S c c}w )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	__bytes__valueutf-8)
class_namer0   	__slice__)startstopstep__ellipsis___keras_historyr   __keras_tensor__)shapedtypekeras_history
__tensor__)rn   ry   	__numpy__z<lambda>a  The object being serialized includes a `lambda`. This is unsafe. In order to reload the object, you will have to pass `safe_mode=False` to the loading function. Please avoid using `lambda` in the future, and use named Python functions instead. This is the `lambda` being serialized:    )
stacklevel
__lambda__c                     t        | t        j                        r| j                         S t        | t        j                        r| j
                  S | S r/   )
isinstancetfTensorShapeas_listDTypenamexs    r2   <lambda>z(serialize_keras_object.<locals>.<lambda>  sG    !!R^^4 IIK  %/q"(($;!&&  BC r4   __typespec__)rp   	spec_namera   r0   registered_namebuiltinsra   rp   r0   r   )7r   PLAIN_TYPESlisttupler6   dictserialize_dictbytesdecodeslicerr   rs   rt   typeEllipsisr   KerasTensorgetattrr   rx   ry   r   	availabler   _dimsr   	is_tensorconvert_to_numpytoliststandardize_dtyper:   npr9   ndarrayndimitemr   typesFunctionTypewarningswarninspect	getsourcer	   	func_dumpTypeSpec
_serializemap	__class___get_class_or_fn_configserialize_with_public_classget_build_and_compile_configri   r   get_registered_name)rf   r   
config_arrhistory	ts_configinner_configconfig_with_public_classra   rp   r   r0   s              r2   r6   r6      s+   * {
#{#
#e}%9<=A,Q/=
=$.sE$:uZ J
J#tc"" #u%

7 34
 	
 #u%/		:.sxx8.sxx8
 	
 #tH~&,;;#w**+#/67mG GAJ,!(
 	
 
||
37 #		 5s{{}?4?& 11#6==? 22399=
 	
 Cyr{{*c2::&388a<) ZZ\$66syyA  88:	||
31xx#u))*s||z/I6
 7>6G6G6L5MO 	
 '//4
 	
 
||
34NN$	
 	
	 )//mm..#
 	
 +3/L:|   +$S*BC)#/GH'' #u))*))''Jc5--.1EEcJO1EEO
  *	F !f-%c62MM >s   Sc                     t        | d      r | j                         }|t        |      |d<   t        | d      r | j                         }|t        |      |d<   y )Nget_build_configbuild_configget_compile_configcompile_config)hasattrr   r   r   )rf   r0   r   r   s       r2   r   r   <  sb    s&'++-#%3L%AF>"s()//1%'5n'EF#$
r4   c                     t        j                  |       }|3t        j                  |       }|y| j                  | j
                  ||dS |j                  d      }dj                  |dd       |d   |ddS )zSerializes classes from public Keras API or object registration.

    Called to check and retrieve the config of any class that has a public
    Keras API or has been registered as serializable via
    `keras.saving.register_keras_serializable()`.
    Nr   .)r   get_name_from_symbolr   r   r:   r9   splitjoin)clsr   keras_api_namer   partss        r2   r   r   H  s      44S9N -AA#F" nn,,".	
 	
   %E((5":&Bi	 r4   c                    |r|d||dS t        j                  |       }|r*|j                  d      }dj                  |dd       d||dS t	        j
                  |       }|s| j                  dk(  sy| j                  d||dS )aO  Serializes functions from public Keras API or object registration.

    Called to check and retrieve the config of any function that has a public
    Keras API or has been registered as serializable via
    `keras.saving.register_keras_serializable()`. If function's module name
    is already known, returns corresponding config.
    functionr   r   Nr   r   )r   r   r   r   r   r   r:   )fnr0   fn_module_namer   r   r   s         r2   serialize_with_public_fnr   k  s     $$%	
 	
  44R8N$$S)hhuSbz*$%	
 	
 .AA"Er}}
'Bmm$.	
 	
r4   c                 h   t        | t        j                        rt        j                  |       S t        | d      r<| j                         }t        |t              st        d|  d|       t        |      S t        | d      rt        j                  |       S t        d|  dt        |        d      )z1Return the object's config depending on its type.
get_configzThe `get_config()` method of z$ should return a dict. It returned: r9   zCannot serialize object z	 of type zG. To be serializable, a class must implement the `get_config()` method.)r   r   r   r   r   r   r   r   	TypeErrorr   r   )rf   r0   s     r2   r   r     s     #u))*"66s;;sL!!&$'/u 5((.x1  f%%	j	!"66s;;&se9T#YK @@ @
 	
r4   c                 h    | j                         D ci c]  \  }}|t        |       c}}S c c}}w r/   )itemsr6   )rf   keyrn   s      r2   r   r     s+    ADM:3C'..MMMs   .r*   r)   c           
         t               }||n|}|j                  dd      }|xs i }t        j                  di       }t        j
                  }i |||}| yt        | t              r|r|j                  |       ||    S t        | t        t        f      r| D cg c]  }t        |||       c}S |d\  }	}
}t        | t              rd| v r| d   }	d| vrt        d|        |r1| d   |v s'| j                  d	      |v st        |	t              r|	|v rd
}nM| d   dk(  r| d   }
|
dk(  r| d   } n5| d	   } n/| j                  dd      t        d| d    d| d    d      | d   } |sY| |vr| S t        ||    t        j                         rt        t#        ||    | |
      |      S t        t%        ||    |	      |      S t        | t&              r| S t        | t              st        d|        d| vsd| vr0| j)                         D ci c]  \  }}|t        |||       c}}S | d   }| d   | d   ni }	|xs i }|dk(  r)t+        j,                  |	d   |	d         }|	d   |_        |S |dk(  rt+        j0                  |	d   |	d         S |dk(  rt3        j4                  |	d   |	d         S | d   dk(  r|	d   j7                  d      S | d   dk(  rt8        S | d   d k(  r:t;        t        |	d!   ||      t        |	d"   ||      t        |	d#   ||            S | d   d$k(  r%|rt        d%      t=        j>                  |	d         S t@        y| d   d&k(  rqt        |	t        t        f      st        d'tC        |	       d(|        tE        | d)   | d	   | d   d*| |+      }tG        d, |	      }	|jI                  t        |	            S | j                  dd      }| j                  d	|      }|dk(  r=|	}t        |t              st        d-tC        |       d(|        tE        |||d| |+      S t        |	t              st        d.| d/tC        |	       d(|        d0| v rtK        | d0         }||S tE        |||d*| |+      }t        |t        j                         r|S tM        |d1      st        d2| d3|        t	        jN                  |      }tQ        |      }|5  |5  	 |jS                  |	      }| j                  d6d      }|r$|jT                  s|jW                  |       d
|_*        | j                  d7d      }|r|jY                  |       d
|_-        ddd       ddd       d0| v rt]        | d0          S c c}w c c}}w # t        $ r}t        | d4|  d5|       d}~ww xY w# 1 sw Y   UxY w# 1 sw Y   YxY w)8a  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. 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. Built-in Keras classes
      expect to have prefix `keras`.
    - `registered_name`: String. The key the class is registered under via
      `keras.saving.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 uses the class name. If `registered_name` successfully
      resolves to a class (that was registered), the `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:

    ```python
    dict_structure = {
        "class_name": "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": "keras.optimizers",
        "registered_name": None
    }
    # 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:

    ```python
    dict_structure = {
      "class_name": "MetricsList",
      "config": {
          ...
      },
      "module": "keras.trainers.compile_utils",
      "registered_name": "MetricsList"
    }

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

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

    ```python
    @keras.saving.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"
    }
    # Returns the `ModifiedMeanSquaredError` object
    deserialize_keras_object(dict_structure)
    ```

    Args:
        config: Python dict describing the object.
        custom_objects: Python dict containing a mapping between custom
            object names the corresponding classes or functions.
        safe_mode: Boolean, defaults to False. If True, disables unsafe
            lambda deserialization.

            Note that safe_mode is designed to protect against code
            serialized within the Keras model file being loaded. It does
            not provide isolation from the local Python environment and
            does not guard against modifications made outside of the
            serialized file.

    Returns:
        The object described by the `config` dictionary.
    Nmodule_objectscustom_objects_scope_dict)custom_objectsr@   )NNFr0   rp   z%Unknown `config` as a `dict`, config=r   Tr   ra   r   _z#Cannot deserialize object of type `z`. If `zj` is a custom class, please register it using the `@keras.saving.register_keras_serializable()` decorator.r   )r   zCould not parse config: rw   rx   ry   )ry   rz   r{   rn   r|   rm   ro   ru   rq   rr   rs   rt   r   aF  Requested the deserialization of a Python lambda. This carries a potential risk of arbitrary code execution and thus it is disallowed by default. If you trust the source of the artifact, you can override this error by passing `safe_mode=False` to the loading function, or calling `keras.config.enable_unsafe_deserialization().r   zTExpected 'config' to be a list or a tuplefor a __typespec__ class name,
instead got z
Full config: r   class)obj_typefull_configr   c                     t        | t              rt        j                  |       S t	        t        j
                  t        |             rt        t        |       S | S r/   )r   r   r   r   r   dtypesstrr   r   s    r2   r   z*deserialize_keras_object.<locals>.<lambda>  sL    a& q!  )0		3q6(Bgb!n  IJ r4   zLExpected 'config' to be a non-null str fora function classname,
instead got z#Expected 'config' to be a dict for z,
instead got rc   from_configz&Unable to reconstruct an instance of 'zM' because the class is missing a `from_config()` method. Full object config: z could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by `get_config()` are explicitly deserialized in the model's `from_config()` method.

config=z.

Exception encountered: r   r   )/rD   popr   rS   r   GLOBAL_CUSTOM_OBJECTSr   r   r\   r   r   deserialize_keras_objectr   
ValueErrorr   r   r   r   r   r   r   r   r    _pre_serialization_keras_historyconvert_to_tensorr   arrayencoder   r   r	   	func_loadr   r   _retrieve_class_or_fnr   _deserializer_   r   CustomObjectScoper>   r   builtbuild_from_configcompile_from_configcompiledrk   )r0   r   r@   rK   safe_scope_argr   tlcogcor   r   r   has_custom_objectr   rn   rp   rf   ra   r   fn_namer   custom_obj_scopesafe_mode_scopeinstanceer   r   s                             r2   r   r     sz   X "^N"0"<)IZZ 0$7N#)rN,,-H"MD

3
3C66$6#6N~ 	63v&2 f%%&4-(
 	
  %.I
 	
 !:K7n&7fd#6!%h/6) ;F8D 
 |$6::/0NB|S1$6 %)! %3!'!1!Z/#H-F#$56F ::h,4#"<01 2"<01 2%%   - ^+.0%2D2DE/,&v. $2	  ,+"6*  .	  &+&fd#26(;<<6!XV%;
 %lln	
 U )n	 
 	
 %J'-h'7'C6(#L#)rN ''!!!g)>
 0<O/L,
\!((!g)>
 	
 [ xxW-\'5JKKl{*G$++G44l~-l{*$W%-#
 %V$-#
 %V$-#
 	
" l|+A  %%l7&;<<	~&..@,6#L12 3  &x)  $;$%8)
 
 
 l 344 ZZ$'Fjj!2J?OZ'3'#G}o .  &x)  %)
 	
 lD)1* >-. /"8%
 	
 V#'9 :;?J
%C #u))*
3&4ZL A##)(,
 	
 +<<^L#I.O	 %? %
	|4H zz.$7&&|4!HN$4d;((8 $H'% %* V#+f/0	
 Oc
V
`  	% 
 %X%A!F 	% % % %sU   V V!V;$V/&V7A$V/V;	V,V''V,,V//V8	4V;;Wc                 ~    | j                  d      }|sy ||| j                  d      fv ry t        d|d| d|        )Nr   rp   z`registered_name=z#` was provided in the config, but 'z' resolved to a Keras built-in and does not use registered names. Remove the `registered_name` field from the config. Full object config: )r\   r   )r   r   resolved_nameexplicits       r2   &_assert_no_registered_name_for_builtinr   !  s]    01HD+//,788

H< (? ''2m	5 r4   c                    |dk(  rt        j                  | |      }nt        j                  ||      }||S |rX|dk(  s|j                  d      rD| d|  }|t        v rt	        d| d      t        j                  |      }|t        || |       |S |dk(  rz|dk(  rut        D ]6  }	t        j                  d|	 d|        }|!t        || d|	 d|         |c S  d	| vr2d	|  }
|j                         D ]  \  }}|j                  |
      s|c S  |j                  dd
      d   }|dv re	 t        j                  |      }	t        |	      j                  | d       }t!        |t"              rt%        |t&              r|S t	        d| d|  d      t+        d| d|  d|       # t(        $ r t+        d| d|  d| d|       w xY w)Nr   r   keraszkeras.r   zCannot deserialize `z` because it is not a modeling function. Non-modeling APIs are disallowed during deserialization for security. Model configuration should only contains operations, layers, metrics, losses, or registered custom objects and functions.r   >   )maxsplitr   >   r   keras_cv	keras_hub	keras_nlpzCould not deserialize 'z,' because it is not a KerasSaveable subclasszCould not deserialize z 'z' because its parent module z) cannot be imported. Full object config: zCould not locate z'. Make sure custom classes and functions are decorated with `@keras.saving.register_keras_serializable()`. If they are already decorated, make sure they are all imported so that the decorator is run before trying to load them. Full object config: )r   get_registered_object
startswithNON_MODELING_APISr   r   get_symbol_from_namer   BUILTIN_MODULESr   endswithr   	importlibimport_modulevarsr\   r   r   
issubclassr   ModuleNotFoundErrorr   )r   r   ra   r   r   r   
custom_objapi_namerf   modseparated_namecustom_namecustom_objectpackages                 r2   r   r   /  sl   
 :(>>

 )>>N

 
 W 1 1( ; 4&)H,, *8* 5B B  11(;C6x 
 z!f
&:&  55se1TF6KL?:#TVC5$+@ J $#$TF2@2F2F2H -.K"++N;,,- ,,sQ,/2EE--f53immD$/c4(Z]-KJ$1&4& A= =  
H:Rv .? @Km		M  ' ,XJb ?))/ 1++6-9 s   0AF( F( (!G	r/   )NT)0rM   r  r   r   r   numpyr   	keras.srcr   r   keras.src.api_exportr   keras.src.backend.commonr   keras.src.savingr   keras.src.saving.keras_saveabler   keras.src.utilsr	   keras.src.utils.module_utilsr
   r   r   rd   floatboolr   	frozensetr	  r  r-   r>   rP   rD   rU   r_   ri   rk   r6   r   r   r   r   r   r   r   r   r<   r4   r2   <module>r"     s1   <         - 1 0 9 ( 9C% 
  @3 3
 
  :;A <A
A
 
/1$  -,\\~	 F!
H
0N /. ,0mm` JNZr4   