o
    ?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OSErrorZInvocationException r   r   h/home/www/facesmatcher.com/pyenv/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)Z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.namespaceNZdashed)styleZ
graph_namelabelZ	labeljustlrankdirZ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   ).0Zishaper.   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   ZClusternamesetr   Zset_node_defaultslayersZ_is_graph_networkNoder)   idadd_noder   Z
SequentialZbuiltbuildsuper	enumerate	__class____name__r   r   r   model_to_dotZ	get_nodesZadd_subgraphformatr*   Zoutput_shapeAttributeErrorhasattrr/   joinr0   Z_inbound_nodesZ_network_nodesr   flattenZinbound_layersZget_noder   r   get_name)'modelshow_shapes
show_dtypeshow_layer_namesr!   expand_nestedr"   r&   r   r   r   messager   Zsub_n_first_nodeZsub_n_last_nodeZsub_w_first_nodeZsub_w_last_noder:   nodeir   Zlayer_idZ
layer_name
class_nameZsubmodel_wrapperZsub_w_nodesZchild_class_nameZsubmodel_not_wrapperZsub_n_nodesr   r+   ZoutputlabelsZinputlabelsZnode_keyZinbound_layerZinbound_layer_idr8   Zoutput_nameZinbound_layer_namer   r2   r	   rC   A   s"  


	
	




+rC   	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.
  )rK   rL   rM   r!   rN   r"   NZpng   )rD   Zpdfr   )display)filename)
rC   r   ospathsplitextwriteZIPythonrU   ZImager7   )rJ   Zto_filerK   rL   rM   r!   rN   r"   r   _	extensionrU   r   r   r	   
plot_model  s2   +r]   )FFTr   Fr   F)rS   FFTr   Fr   )__doc__rW   r4   Z&tensorflow.python.keras.utils.io_utilsr   Ztensorflow.python.utilr   Zpydot_ngr   r7   Z	pydotplusr
   r   r   rC   r]   r   r   r   r	   <module>   sP   
 U