
    0h:J                     2   d Z ddl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Zd Zd Zd Zd	 Z ed
      	 	 	 	 	 	 	 	 	 	 dd       Z ed      	 	 	 	 	 	 	 	 	 	 dd       Zy# e$ r' 	 ddlZn# e$ r 	 ddlZn# e$ r dZY nw xY wY nw xY wY jw xY w)z)Utilities related to model visualization.    N)io_utils)layer_utils)keras_exportc                      t         duS )z#Returns True if PyDot is available.N)pydot     V/var/www/html/engine/venv/lib/python3.12/site-packages/tf_keras/src/utils/vis_utils.pycheck_pydotr   -   s    r	   c                      t               sy	 t        j                  j                  t        j                                y# t        t
        f$ r Y yt        j                  $ r Y yw xY w)z6Returns True if both PyDot and Graphviz are available.FT)r   r   DotcreateOSErrorFileNotFoundErrorInvocationExceptionr   r	   r
   check_graphvizr   2   sU    =	 			%&' $$ s   1? A%A%$A%c                 v    ddl m} ddlm} t	        | |      xr  t	        | j
                  |j                        S )Nr   
functionalWrapper)tf_keras.src.enginer   tf_keras.src.layersr   
isinstancelayer
Functional)r   r   r   s      r
   is_wrapped_modelr   B   s2    .+eW% *Z**+ r	   c                 t    | j                  ||      s&| j                  t        j                  ||             y y )N)get_edgeadd_edger   Edge)dotsrcdsts      r
   r    r    K   s,    <<S!UZZS)* "r	   zkeras.utils.model_to_dotc                    | j                   st        d      ddlm} ddlm} ddlm} t               st        d      |rPt        j                  d| j                        }|j                  d	| j                         |j                  d
d       n\t        j                         }|j                  d|       |j                  dd       |j                  d|       |j                  d       |t        |      dk7  rt        d| dt        |             t!        |d   t"              rt!        |d   t"              st        d|       t%        j&                  | |      }|d   dk  s|d   t        | j(                        kD  r$t        dt        | j(                         d|       i }i }i }i }| j(                  }| j*                  sFt        j,                  t#        t/        |             | j                        }|j1                  |       |S t!        | |j2                        r0| j                   s| j5                          t6        |j2                  | R  }t9        |      D ]=  \  }}|r||d   k  s||d   k\  rt#        t/        |            }|j                  }|j:                  j<                  }t!        ||      r|rt!        |j>                  |j@                        rxtC        |j>                  |||||d|	|
	      }|jE                         }|d   ||j>                  j                  <   |d   ||j>                  j                  <   |jG                  |       nD| d|j>                  j                   d}|j>                  j:                  j<                  }| d| d}|rot!        ||j@                        rYtC        ||||||d|	|
	      }|jE                         }|d   ||j                  <   |d   ||j                  <   |jG                  |       |}|	rtI        |d      r|jJ                  xtI        |jJ                  d      r|jJ                  j                  } nBtI        |jJ                  d       r|jJ                  j<                  } nt#        |jJ                        } d!|d"| d#}|r| d"| }|rd$ }!| d" |!|jL                         }|rd% }"	  |"|jN                        }#tI        |d'      r |"|jR                        }$n>tI        |d(      r0d)jU                  |jV                  D %cg c]
  }% |"|%       c}%      }$nd&}$d!|d*|$d+|#d,}|
r|jX                  rd-nd. d"| }|rt!        ||j@                        rt        j,                  ||      }|j1                  |       @ t9        |      D ]  \  }}|r||d   k  s||d   k\  rt#        t/        |            }t9        |jZ                        D ]u  \  }}|j                  d/z   t#        |      z   }&|&| j\                  v s1t^        j`                  jc                  |jd                        D ]  }'t#        t/        |'            }(|s4|jg                  |(      sJ |jg                  |      sJ ti        ||(|       Nt!        |'|j@                        stk        |'      st!        ||j@                        s?tk        |      s4|jg                  |(      sJ |jg                  |      sJ ti        ||(|       t!        ||j@                        r*ti        ||(||j                     jm                                tk        |      sti        ||(|       ||j>                  j                     jm                         })ti        |||)       Tt!        |'|j@                        rn||'j                     jm                         })t!        ||j@                        r,||j                     jm                         }*ti        ||)|*       ti        ||)|       tk        |'      s|'j>                  j                  }+ti        |||+   jm                         |        x  |S # tP        $ r d&}#Y w xY wc c}%w )0a  Convert a TF-Keras model to dot format.

    Args:
      model: A TF-Keras model instance.
      show_shapes: whether to display shape information.
      show_dtype: whether to display layer dtypes.
      show_layer_names: whether to display layer names.
      rankdir: `rankdir` argument passed to PyDot,
          a string specifying the format of the plot:
          'TB' creates a vertical plot;
          'LR' creates a horizontal plot.
      expand_nested: whether to expand nested models into clusters.
      dpi: Dots per inch.
      subgraph: whether to return a `pydot.Cluster` instance.
      layer_range: input of `list` containing two `str` items, which is the
          starting layer name and ending layer name (both inclusive) indicating
          the range of layers for which the `pydot.Dot` will be generated. It
          also accepts regex patterns instead of exact name. In such case, start
          predicate will be the first element it matches to `layer_range[0]`
          and the end predicate will be the last element it matches to
          `layer_range[1]`. By default `None` which considers all layers of
          model. Note that you must pass range such that the resultant subgraph
          must be complete.
      show_layer_activations: Display layer activations (only for layers that
          have an `activation` property).
      show_trainable: whether to display if a layer is trainable. Displays 'T'
          when the layer is trainable and 'NT' when it is not trainable.

    Returns:
      A `pydot.Dot` instance representing the TF-Keras model or
      a `pydot.Cluster` instance representing nested model if
      `subgraph=True`.

    Raises:
      ValueError: if `model_to_dot` is called before the model is built.
      ImportError: if pydot is not available.
    yThis model has not yet been built. Build the model first by calling `build()` or by calling the model on a batch of data.r   r   )
sequentialr   zFYou must install pydot (`pip install pydot`) for model_to_dot to work.dashed)style
graph_namelabel	labeljustlrankdirconcentrateTdpirecordshape   z;layer_range must be of shape (2,). Received: layer_range = z of length    z7layer_range should contain string type only. Received: z2Both values in layer_range should be in range (0, z. Received: )r+   )subgraphshow_layer_activationsshow_trainable()
activationname__name__{|}c                     | yt        |       S )N?)str)dtypes    r
   format_dtypez"model_to_dot.<locals>.format_dtype  s    =u:%r	   c                     t        |       j                  t        d       d      j                  dd      j                  dd      S )NNoner?   z\{rA   z\})rD   replacer2   s    r
   format_shapez"model_to_dot.<locals>.format_shape  s6    JWSY/WS%(WS%(	r	   rC   input_shapeinput_shapesz, z}|{input:|output:}|{{z}|{z}}TNTz_ib-)7built
ValueErrorr   r   r'   r   r   r   ImportErrorr   Clusterr=   setr   set_node_defaultslenr   rD   r   #get_layer_index_bound_by_layer_namelayers_is_graph_networkNodeidadd_node
Sequentialbuildsuper	enumerate	__class__r>   r   r   model_to_dot	get_nodesadd_subgraphhasattrr<   rE   output_shapeAttributeErrorrK   joinrL   	trainable_inbound_nodes_network_nodestfnestflatteninbound_layersget_noder    r   get_name),modelshow_shapes
show_dtypeshow_layer_namesr.   expand_nestedr0   r6   layer_ranger7   r8   r   r'   r   r"   sub_n_first_nodesub_n_last_nodesub_w_first_nodesub_w_last_noderW   nodeir   layer_id
layer_name
class_namesubmodel_wrappersub_w_nodeschild_class_namesubmodel_not_wrappersub_n_nodesr+   activation_namerF   rJ   outputlabelsinputlabelsishapenode_keyinbound_layerinbound_layer_idr=   output_nameinbound_layer_names,                                               r
   ra   ra   P   s   h ;;,
 	
 /.+=$
 	

 mm(uzzB$S!iik	7#t$sH-{q !!,[[9I8JL  +a.#.jNC7
 (M+  "EE;
 q>AQ#ell2C!CDu||$%\+@ 
 OO\\F""zz#bi.

;T
	E:00	1{{KKMz,,e; f% v5a+a.0AQ4Gr%y> ZZ
__--
eW%EKK9N9N!O#/KK$!!+A#1
$  /88:5@^ !1!124?O 0 01  !12 *|1U[[-=-=,>a@
#(;;#8#8#A#A  *|1-=,>a@
Zz/D/DE#/ '=-
$  /88:K+6q>UZZ(*5b/OEJJ'12  #|,  ,u''0"'"2"2"7"7)):6"'"2"2";";"%e&6&6"7!&8E !l!E7+E & gQ|EKK89:E #+E,>,>? um,*5+<+<=/"ii8=8J8JKf\&)K "E #oos48%AEJuj6K6K$L::he4DLLmvr f% :5a;q>1Q+a.5Hr%y> !5!56 6	GAtzzF*SV3H5///%'WW__T5H5H%I 3M'*2m+<'=$("||,<==="||H555 &6A  *):+@+@ "2="A#- %z'<'<$&6u&='*||4D'E E'E'*||H'= ='= (.> I!+E:3H3H!I ($'$4$4UZZ$@$I$I$K!" "2%!8 (.> I'7$)KK$4$4(""*(* !% !)h ='z7L7LM#2 - 2 2$&hj !  *%1F1FG.>$)JJ/""*(* !, !)dK @ (dH =-m<1>1D1D1I1I.$ # /0B C L L N (_36		:v Ji " #"# Ls   	a,a>
,a;:a;zkeras.utils.plot_modelc                    | j                   st        d      t               s5d}dt        j                  v rt        j                  |       yt        |      t        | ||||||||	|

      }t        j                  |      }|yt        j                  j                  |      \  }}|sd}n|dd }|j                  ||       |d	k7  r	 d
dlm} |j!                  |      S y# t        $ r Y yw xY w)a	  Converts a TF-Keras model to dot format and save to a file.

    Example:

    ```python
    input = tf.keras.Input(shape=(100,), dtype='int32', name='input')
    x = tf.keras.layers.Embedding(
        output_dim=512, input_dim=10000, input_length=100)(input)
    x = tf.keras.layers.LSTM(32)(x)
    x = tf.keras.layers.Dense(64, activation='relu')(x)
    x = tf.keras.layers.Dense(64, activation='relu')(x)
    x = tf.keras.layers.Dense(64, activation='relu')(x)
    output = tf.keras.layers.Dense(1, activation='sigmoid', name='output')(x)
    model = tf.keras.Model(inputs=[input], outputs=[output])
    dot_img_file = '/tmp/model_1.png'
    tf.keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)
    ```

    Args:
      model: A TF-Keras model instance
      to_file: File name of the plot image.
      show_shapes: whether to display shape information.
      show_dtype: whether to display layer dtypes.
      show_layer_names: whether to display layer names.
      rankdir: `rankdir` argument passed to PyDot,
          a string specifying the format of the plot: 'TB' creates a vertical
            plot; 'LR' creates a horizontal plot.
      expand_nested: Whether to expand nested models into clusters.
      dpi: Dots per inch.
      layer_range: input of `list` containing two `str` items, which is the
        starting layer name and ending layer name (both inclusive) indicating
        the range of layers for which the plot will be generated. It also
        accepts regex patterns instead of exact name. In such case, start
        predicate will be the first element it matches to `layer_range[0]` and
        the end predicate will be the last element it matches to
        `layer_range[1]`. By default `None` which considers all layers of model.
        Note that you must pass range such that the resultant subgraph must be
        complete.
      show_layer_activations: Display layer activations (only for layers that
        have an `activation` property).
      show_trainable: whether to display if a layer is trainable. Displays 'T'
        when the layer is trainable and 'NT' when it is not trainable.

    Raises:
      ImportError: if graphviz or pydot are not available.
      ValueError: if `plot_model` is called before the model is built.

    Returns:
      A Jupyter notebook Image object if Jupyter is installed.
      This enables in-line display of the model plots in notebooks.
    r&   zYou must install pydot (`pip install pydot`) and install graphviz (see instructions at https://graphviz.gitlab.io/download/) for plot_model to work.zIPython.core.magics.namespaceN)	rr   rs   rt   r.   ru   r0   rv   r7   r8   pngr5   )formatpdfr   )display)filename)rO   rP   r   sysmodulesr   	print_msgrQ   ra   path_to_stringospathsplitextwriteIPythonr   Image)rq   to_filerr   rs   rt   r.   ru   r0   rv   r7   r8   messager"   _	extensionr   s                   r
   
plot_modelr   }  s   D ;;,
 	
 & 	 +ckk9 w'g&&
)#5%C %%g.G
{77##G,LAy	abM	IIgiI( E	'=='=22	 
  		s   	C" "	C.-C.)
FFTTBF`   FNFF)
z	model.pngFFTr   Fr   NFF)__doc__r   r   tensorflow.compat.v2compatv2rk   tf_keras.src.utilsr   r    tensorflow.python.util.tf_exportr   pydot_ngr   rQ   	pydotplusr   r   r   r    ra   r   r   r	   r
   <module>r      s   " 0 	 
 ! ! ' * :
 +
 () 
 i *iX	 &' 
 v (v{
  	! 	 	E				sY   A* *B0A54B5B;B ?B B
B	B

BBBBB