B
    ӻd/                 @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ yddl	Z
W n\ ek
r   yddlZ
W n: ek
r   yddl
Z
W n ek
r   dZ
Y nX Y nX Y nX dd Zdd	 Zd
d ZeddddZeddddZdS )z)Utilities related to model visualization.    N)path_to_string)nest)keras_exportc            	   C   s@   t dkrdS yt jt   dS  tt jfk
r:   dS X dS )z1Returns True if PyDot and Graphviz are available.NFT)pydotDotcreateOSErrorZInvocationException r	   r	   Y/var/www/html/venv/lib/python3.7/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o0t| 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_model6   s    r   c             C   s"   |  ||s| t|| d S )N)Zget_edgeadd_edger   Edge)dotsrcdstr	   r	   r
   r   =   s    r   zkeras.utils.model_to_dotFTTB`   c       '   
      s$  ddl m} ddlm}	 ddlm}
 t sLd}dtjkrDt| dS t	||r|t
jd| jd	}|d
| j |dd n8t
 }|d| |dd |d| |jdd i }i }i }i }| j}| jst
jtt| | jd}|| |S t| |	jr"| js|   t|	j| j}x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< |#| |rTd$||}n|}|rxdd }d|||j%f }|rdd  y |j&}W n t'k
r   d}Y nX t(|drȈ |j)} n,t(|drd * fd!d"|j+D } nd} d#|| |f }|rt||
j s.t
j||d}|| q.W x|D ]}tt|}xt|j,D ]\}}|jd$ t| }!|!| j-kr\xt./|j0D ]z}"tt|"}#|s|1|#st2|1|st2t3||#| qt|"|
j st4|"st||
j s:t4|s:|1|#st2|1|s,t2t3||#| nXt||
j r`t3||#||j 5  n2t4|rt3||#| ||jj 5 }$t3|||$ nzt|"|
j r||"j 5 }$t||
j r||j 5 }%t3||$|% nt3||$| n&t4|"r|"jj}&t3|||& 5 | qW q\W q<W |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   )
sequential)r   )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 krdS t | S d 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'   replace)r#   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-)6r   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-   joinr.   Z_inbound_nodesZ_network_nodesr   flattenZinbound_layersZget_nodeAssertionErrorr   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_noder7   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_idr5   Zoutput_nameZinbound_layer_namer	   )r,   r
   r@   B   s    











r@   zkeras.utils.plot_model	model.pngc          	   C   s   t | ||||||d}t|}|dkr*dS tj|\}	}
|
sDd}
n|
dd }
|j||
d |
dkryddlm} |j|d	S  t	k
r   Y nX 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.
  )rI   rJ   rK   r    rL   r!   NZpng   )rA   Zpdfr   )display)filename)
r@   r   ospathsplitextwriteZIPythonrS   ZImager4   )rH   Zto_filerI   rJ   rK   r    rL   r!   r   _	extensionrS   r	   r	   r
   
plot_model  s,    ,r[   )FFTr   Fr   F)rQ   FFTr   Fr   )__doc__rU   r1   Z&tensorflow.python.keras.utils.io_utilsr   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr   Zpydot_ngr   r4   Z	pydotplusr   r   r   r@   r[   r	   r	   r	   r
   <module>   sF          M      