B
    лd.                 @   sp   d Z ddl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
 Zdd Zdd ZdS )zSavedModel utils.    N)message)text_format)saved_model_pb2)file_io)	constants)compatc          
   C   sn  t jt| ttj}t jt| ttj}t	|sXt	|sXt
d|  t }t	|rt|d}| }W dQ R X y|| |S  tjk
r } zt
d|t|f W dd}~X Y nX nt	|rTt|d}| }W dQ R X yt|d| |S  tjk
rP } zt
d|t|f W dd}~X Y nX nt
d| tjtjf dS )a  Reads the saved_model.pb or saved_model.pbtxt file containing `SavedModel`.

  Args:
    saved_model_dir: Directory containing the SavedModel file.

  Returns:
    A `SavedModel` protocol buffer.

  Raises:
    IOError: If the file does not exist, or cannot be successfully parsed.
  z%SavedModel file does not exist at: %srbNzCannot parse proto file %s: %s.zutf-8zCannot parse pbtxt file %s: %s.z-SavedModel file does not exist at: %s/{%s|%s})ospathjoinr   as_bytesr   ZSAVED_MODEL_FILENAME_PBTXTZSAVED_MODEL_FILENAME_PBr   Zfile_existsIOErrorr   Z
SavedModelFileIOreadZParseFromStringr   DecodeErrorstrr   ZMergedecodeZ
ParseError)saved_model_dirZpath_to_pbtxtZ
path_to_pbsaved_modelfZfile_contente r   [/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/tools/saved_model_utils.pyread_saved_model   s:    

((r   c             C   s2   t | }g }x |jD ]}|t|jj qW |S )zRetrieves all the tag-sets available in the SavedModel.

  Args:
    saved_model_dir: Directory containing the SavedModel.

  Returns:
    List of all tag-sets in the SavedModel, where a tag-set is represented as a
    list of strings.
  )r   meta_graphsappendlistmeta_info_deftags)r   r   Zall_tagsmeta_graph_defr   r   r   get_saved_model_tag_setsM   s
    
r    c             C   st   t | }tdd |dD }g }x6|jD ],}t|jj}||krH|S |d| q,W td| d| dS )ae  Gets MetaGraphDef from SavedModel.

  Returns the MetaGraphDef for the given tag-set and SavedModel directory.

  Args:
    saved_model_dir: Directory containing the SavedModel to inspect.
    tag_set: Group of tag(s) of the MetaGraphDef to load, in string format,
        separated by ','. The empty string tag is ignored so that passing ''
        means the empty tag set. For tag-set contains multiple tags, all tags
        must be passed in.

  Raises:
    RuntimeError: An error when the given tag-set does not exist in the
        SavedModel.

  Returns:
    A MetaGraphDef corresponding to the tag-set.
  c             S   s   g | ]}|r|qS r   r   ).0tagr   r   r   
<listcomp>s   s    z&get_meta_graph_def.<locals>.<listcomp>,z%MetaGraphDef associated with tag-set zQ could not be found in the SavedModel. Please use one of the following tag-sets: N)	r   setsplitr   r   r   r   r   RuntimeError)r   Ztag_setr   Zset_of_tags
valid_tagsr   Zmeta_graph_tagsr   r   r   get_meta_graph_def^   s    r)   )__doc__r	   Zgoogle.protobufr   r   Ztensorflow.core.protobufr   Ztensorflow.python.lib.ior   Ztensorflow.python.saved_modelr   Ztensorflow.python.utilr   r   r    r)   r   r   r   r   <module>   s   2