o
    i e.                     @   s   d Z ddlZddlZddlmZ ddlmZ zddlZW n+ e	yG   zddl
ZW n e	yD   zddlZW n e	yA   dZY nw Y nw Y nw dd Zdd Zd	d
 Z							dddZ							dddZdS )z)Utilities related to model visualization.    N)path_to_string)nestc                	   C   s>   t du rdS zt jt   W dS  tt jfy   Y dS w )z1Returns True if PyDot and Graphviz are available.NFT)pydotDotcreateOSErrorInvocationException r	   r	   V/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/keras/utils/vis_utils.pycheck_pydot(   s   r   c                 C   s2   ddl m} ddlm} t| |jot| j|jS )Nr   
functionalwrappers)tensorflow.python.keras.enginer   tensorflow.python.keras.layersr   
isinstanceWrapperlayer
Functional)r   r   r   r	   r	   r
   is_wrapped_model5   s
   r   c                 C   s&   |  ||s| t|| d S d S )N)get_edgeadd_edger   Edge)dotsrcdstr	   r	   r
   r   <   s   r   FTTB`   c           '   
      s   ddl m} ddlm}	 ddlm}
 t s&d}dtjv r"t| dS t	||r>t
jd| jd	}|d
| j |dd nt
 }|d| |dd |d| |jdd i }i }i }i }| j}| js{t
jtt| | jd}|| |S t| |	jr| js|   t|	j| j}t|D ]\}}tt|}|j}|jj}t||jr|rt|j|
j rt!|j|||||dd}|" }|d ||jj< |d ||jj< |#| nd$||jj}|jjj}d$||}|rt||
j rt!||||||dd}|" }|d ||j< |d ||j< |#| |r"d$||}n|}|r4dd }d|||j%f }|rxdd  z |j&}W n t'yN   d}Y nw t(|dr[ |j)} nt(|drod * fd!d"|j+D } nd} d#|| |f }|rt||
j st
j||d}|| q|D ]}tt|}t|j,D ]\}}|jd$ t| }!|!| j-v rzt./|j0D ]}"tt|"}#|s|1|#sJ |1|sJ t2||#| qt|"|
j s9t3|"s9t||
j s
t3|s
|1|#sJ |1|sJ t2||#| qt||
j rt2||#||j 4  qt3|r7t2||#| ||jj 4 }$t2|||$ qt|"|
j re||"j 4 }$t||
j r]||j 4 }%t2||$|% qt2||$| qt3|"rx|"jj}&t2|||& 4 | qqq|S )%a  Convert a Keras model to dot format.

  Args:
    model: A 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.

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

  Raises:
    ImportError: if graphviz or pydot are not available.
  r   r   )
sequentialr   )z}You must install pydot (`pip install pydot`) and install graphviz (see instructions at https://graphviz.gitlab.io/download/) z$for plot_model/model_to_dot to work.zIPython.core.magics.namespaceNdashed)style
graph_namelabel	labeljustlrankdirconcentrateTdpirecordshape)r#   )subgraphz{}({})z{}: {}c                 S   s   | d u rdS t | S )N?)str)dtyper	   r	   r
   format_dtype   s   z"model_to_dot.<locals>.format_dtypez%s|%sc                 S   s   t | t d dS )NNone)r/   replacer*   r	   r	   r
   format_shape   s   z"model_to_dot.<locals>.format_shaper.   input_shapeinput_shapesz, c                    s   g | ]} |qS r	   r	   ).0ishaper4   r	   r
   
<listcomp>   s    z model_to_dot.<locals>.<listcomp>z %s
|{input:|output:}|{{%s}|{%s}}z_ib-)5r   r   r   r   r   r   sysmodulesprintImportErrorr   Clusternamesetr   set_node_defaultslayers_is_graph_networkNoder/   idadd_noder   
Sequentialbuiltbuildsuper	enumerate	__class____name__r   r   r   model_to_dot	get_nodesadd_subgraphformatr0   output_shapeAttributeErrorhasattrr5   joinr6   _inbound_nodes_network_nodesr   flatteninbound_layersget_noder   r   get_name)'modelshow_shapes
show_dtypeshow_layer_namesr&   expand_nestedr(   r,   r   r   r   messager   sub_n_first_nodesub_n_last_nodesub_w_first_nodesub_w_last_noderC   nodeir   layer_id
layer_name
class_namesubmodel_wrappersub_w_nodeschild_class_namesubmodel_not_wrappersub_n_nodesr#   r1   outputlabelsinputlabelsnode_keyinbound_layerinbound_layer_idr@   output_nameinbound_layer_namer	   r9   r
   rO   A   s"  


	
	




+rO   	model.pngc              	   C   s   t | ||||||d}t|}|du rdS tj|\}	}
|
s"d}
n|
dd }
|j||
d |
dkrKzddlm} |j|d	W S  t	yJ   Y dS w dS )
aD  Converts a 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 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.

  Returns:
    A Jupyter notebook Image object if Jupyter is installed.
    This enables in-line display of the model plots in notebooks.
  )r^   r_   r`   r&   ra   r(   Npng   )rR   pdfr   )display)filename)
rO   r   ospathsplitextwriteIPythonr|   Imager>   )r]   to_filer^   r_   r`   r&   ra   r(   r   _	extensionr|   r	   r	   r
   
plot_model  s2   +r   )FFTr   Fr   F)rx   FFTr   Fr   )__doc__r~   r;   &tensorflow.python.keras.utils.io_utilsr   tensorflow.python.utilr   pydot_ngr   r>   	pydotplusr   r   r   rO   r   r	   r	   r	   r
   <module>   sP   
 U