o
    i ej'                     @   sh  d 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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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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/Z0 dd$l1m2Z2 dd%l3m4Z4 dd&l3m5Z6 dd'l7m8Z8 eeeeeeeeeeeeee eeeeeeee!e#e$e%fZ9eee'e(e)fZ:e; a<d(d) Z=e8d*d5d,d-Z>e8d.d6d/d0Z?d1d2 Z@d7d3d4ZAdS )8z.Layer serialization/deserialization functions.    N)
base_layer)input_layer)
input_spec)
activation)	attention)convolutional)core)locally_connected)merging)pooling)regularization)	reshaping)rnn)batch_normalization)batch_normalization_v1)group_normalization)layer_normalization)unit_normalization)category_encoding)discretization)hashed_crossing)hashing)image_preprocessing)integer_lookup)normalization)string_lookup)text_vectorization)cell_wrappers)gru)lstm)base_metric)serialization_lib)serialization)
json_utils)generic_utils)
tf_inspect)keras_exportc                     s  t tdsi t_dt_tjrtjtjj krdS i t_tjj t_tj	 t
jtjt fddd tjj rFt
jtjt fddd tjtjd< tjtjd< d	d
lm}  d	dlm} d	dlm} d	dlm} tjtjd< tjtjd< | jtjd< | jtjd< |tjd< | jtjd< |tjd< |tjd< tjj rd	dl m!} |tjd< nd	dl"m!} |tjd< t#j$tjd< t#j%tjd< t#j&tjd< t#j'tjd< t#j(tjd< t#j)tjd< t#j*tjd< t#j+tjd< dS ) z5Populates dict ALL_OBJECTS with every built-in layer.ALL_OBJECTSNc                       t | o	t|  S Ninspectisclass
issubclassxbase_cls M/var/www/myenv/lib/python3.10/site-packages/keras/src/layers/serialization.py<lambda>|       z1populate_deserializable_objects.<locals>.<lambda>)
obj_filterc                    r(   r)   r*   r.   r0   r2   r3   r4      r5   BatchNormalizationV1BatchNormalizationV2r   )models)SequenceFeatures)LinearModel)WideDeepModelInput	InputSpec
FunctionalModelr:   
Sequentialr;   r<   )DenseFeaturesrB   addsubtractmultiplyaveragemaximumminimumconcatenatedot),hasattrLOCALr'   GENERATED_WITH_V2tf__internal__tf2enabledr   Layerr$   !populate_dict_with_module_objectsALL_MODULESALL_V2_MODULESr   BatchNormalizationr   	keras.srcr9   0keras.src.feature_column.sequence_feature_columnr:   keras.src.premade_models.linearr;   "keras.src.premade_models.wide_deepr<   r   r=   r   r>   r?   r@   rA   *keras.src.feature_column.dense_features_v2rB   'keras.src.feature_column.dense_featuresr
   rC   rD   rE   rF   rG   rH   rI   rJ   )r9   r:   r;   r<   rB   r2   r0   r3   populate_deserializable_objectsf   sl   






r]   zkeras.layers.serializeFc                 C   s4   t | tjrtd|  d|rt| S t| S )a   Serializes a `Layer` object into a JSON-compatible representation.

    Args:
      layer: The `Layer` object to serialize.

    Returns:
      A JSON-serializable dict representing the object's config.

    Example:

    ```python
    from pprint import pprint
    model = tf.keras.models.Sequential()
    model.add(tf.keras.Input(shape=(16,)))
    model.add(tf.keras.layers.Dense(32, activation='relu'))

    pprint(tf.keras.layers.serialize(model))
    # prints the configuration of the model, as a dict.
    zCannot serialize z since it is a metric. Please use the `keras.metrics.serialize()` and `keras.metrics.deserialize()` APIs to serialize and deserialize metrics.)
isinstancer    Metric
ValueErrorlegacy_serializationserialize_keras_objectr!   )layeruse_legacy_formatr2   r2   r3   	serialize   s   


re   zkeras.layers.deserializec                 C   sD   t   | std|  |rtj| tj|ddS tj| tj|ddS )a8  Instantiates a layer from a config dictionary.

    Args:
        config: dict of the form {'class_name': str, 'config': dict}
        custom_objects: dict mapping class names (or function names) of custom
          (non-Keras) objects to class/functions

    Returns:
        Layer instance (may be Model, Sequential, Network, Layer...)

    Example:

    ```python
    # Configuration of Dense(32, activation='relu')
    config = {
      'class_name': 'Dense',
      'config': {
        'activation': 'relu',
        'activity_regularizer': None,
        'bias_constraint': None,
        'bias_initializer': {'class_name': 'Zeros', 'config': {}},
        'bias_regularizer': None,
        'dtype': 'float32',
        'kernel_constraint': None,
        'kernel_initializer': {'class_name': 'GlorotUniform',
                               'config': {'seed': None}},
        'kernel_regularizer': None,
        'name': 'dense',
        'trainable': True,
        'units': 32,
        'use_bias': True
      }
    }
    dense_layer = tf.keras.layers.deserialize(config)
    ```
    z2Cannot deserialize empty config. Received: config=rc   )module_objectscustom_objectsprintable_module_name)r]   r`   ra   deserialize_keras_objectrL   r'   r!   )configrg   rd   r2   r2   r3   deserialize   s$   &rk   c                 C   s   t tdst  tj| S )z?Returns class if `class_name` is registered, else returns None.r'   )rK   rL   r]   r'   get)
class_namer2   r2   r3   get_builtin_layer  s   
rn   c                 C   s"   t   tj| tj|d}t||S )z(Instantiates a layer from a JSON string.)rf   rg   )r]   r#   decode_and_deserializerL   r'   rk   )json_stringrg   rj   r2   r2   r3   deserialize_from_json#  s   
rq   )F)NFr)   )B__doc__	threadingtensorflow.compat.v2compatv2rN   keras.src.enginer   r   r   keras.src.layersr   r   r   r   r	   r
   r   r   r   r   keras.src.layers.normalizationr   r   r   r   r   keras.src.layers.preprocessingr   r   r   r   r   r   r   preprocessing_normalizationr   r   keras.src.layers.rnnr   r   r   keras.src.metricsr    keras.src.savingr!   keras.src.saving.legacyr"   ra   #keras.src.saving.legacy.saved_modelr#   keras.src.utilsr$   r%   r+    tensorflow.python.util.tf_exportr&   rT   rU   localrL   r]   re   rk   rn   rq   r2   r2   r2   r3   <module>   s   	Y!: