ó
V¾÷Xc           @` sá   d  Z  d d l m Z d d l m Z d d l m 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 l m Z d d l m Z d „  Z d d d d „ Z d S(   s‘   Importer for an exported TensorFlow model.

This module provides a function to create a SessionBundle containing both the
Session and MetaGraph.
i    (   t   absolute_import(   t   division(   t   print_functionN(   t	   constants(   t   manifest_pb2(   t	   graph_pb2(   t   meta_graph_pb2(   t   session(   t   ops(   t   file_io(   t   saverc         C` s%   t  j j |  t j ƒ } t j | ƒ S(   sî   Checks if the model path contains session bundle model.

  Args:
    export_dir: string path to model checkpoint, for example 'model/00000123'

  Returns:
    true if path contains session bundle model files, ie META_GRAPH_DEF_FILENAME
  (   t   ost   patht   joinR   t   META_GRAPH_DEF_FILENAMER	   t   file_exists(   t
   export_dirt   meta_graph_filename(    (    sr   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/session_bundle.pyt   maybe_session_bundle_dir$   s    
t    c         C` sb  | se t  j j |  t j ƒ } t j | ƒ s@ t d | ƒ ‚ n  t j	 ƒ  } | j
 t j | ƒ ƒ n  d } g  } t } t  j j |  t j ƒ } t j | ƒ }	 |	 rÔ t  j j |  t j ƒ }
 t j |
 ƒ } t } n` t  j j |  t j ƒ } t j | ƒ r| g } n- t  j j |  t j ƒ } t j | ƒ } t } | sCd } n" |	 sP| r\t j } n	 t j } t  j j |  t j ƒ } | j } t j ƒ  } t j | k rý| t j j j } t | ƒ d k rÙt d | ƒ ‚ qý| d j | ƒ | j j | ƒ n  t j ƒ  t  j! | d d d | ƒ} t# j$ | ƒ } | rY| j% | t  j j |  | ƒ ƒ n  d } t j& | k r¿| t j& j' j } t | ƒ d k r¦t d | ƒ ‚ n  t j( t j& ƒ d } n  i  } t j) | k r6| t j) j j } xL | D]A } t* j+ ƒ  } | j | ƒ t  j j | | j, ƒ | | j- j. <qîWn  | rX| j/ d	 | g d
 | ƒ n  | | f S(   so  Load session bundle from the given path.

  The function reads input from the export_dir, constructs the graph data to the
  default graph and restores the parameters for the session created.

  Args:
    export_dir: the directory that contains files exported by exporter.
    target: The execution engine to connect to. See target in tf.Session()
    config: A ConfigProto proto with configuration options. See config in
    tf.Session()
    meta_graph_def: optional object of type MetaGraphDef. If this object is
    present, then it is used instead of parsing MetaGraphDef from export_dir.

  Returns:
    session: a tensorflow session created from the variable files.
    meta_graph: a meta graph proto saved in the exporter directory.

  Raises:
    RuntimeError: if the required files are missing or contain unrecognizable
    fields, i.e. the exported model is invalid.
  s#   Expected meta graph file missing %sR   i   s-   Expected exactly one serving GraphDef in : %si    t   grapht   configs,   Expected exactly one serving init op in : %st   fetchest	   feed_dictN(0   R   R   R   R   R   R	   R   t   RuntimeErrorR   t   MetaGraphDeft   ParseFromStringt   read_file_to_stringt   Falset   VARIABLES_INDEX_FILENAME_V2t   VARIABLES_FILENAME_PATTERN_V2t   get_matching_filest   Truet   VARIABLES_FILENAMEt   VARIABLES_FILENAME_PATTERNt   ASSETS_DIRECTORYt   collection_defR   t   GraphDeft	   GRAPH_KEYt   any_listt   valuet   lent   Unpackt	   graph_deft   CopyFromR   t   reset_default_graphR   t   Sessiont   Nonet	   saver_libt   import_meta_grapht   restoret   INIT_OP_KEYt	   node_listt   get_collectiont
   ASSETS_KEYR   t	   AssetFilet   filenamet   tensor_bindingt   tensor_namet   run(   R   t   targetR   t   meta_graph_defR   t   variables_filenamet   variables_filename_listt   checkpoint_shardedt   variables_index_filenamet   checkpoint_v2t   variables_filename_patternt   restore_filest
   assets_dirR$   R+   t   graph_def_anyt   sessR
   t   init_op_tensort   init_opst   asset_tensor_dictt
   assets_anyt   assett   asset_pb(    (    sr   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/session_bundle.pyt   load_session_bundle_from_path3   s„    						
"	 (   t   __doc__t
   __future__R    R   R   R   t!   tensorflow.contrib.session_bundleR   R   t   tensorflow.core.frameworkR   t   tensorflow.core.protobufR   t   tensorflow.python.clientR   t   tensorflow.python.frameworkR   t   tensorflow.python.lib.ioR	   t   tensorflow.python.trainingR
   R0   R   R/   RN   (    (    (    sr   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/session_bundle.pyt   <module>   s    	