σ
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   Z d   Z d   Z d   Z d   Z d d d d  Z d S(   s   Shim for systems that need to load both SessionBundle and SavedModel.

This is intended to be used during migration to SavedModel.
i    (   t   absolute_import(   t   division(   t   print_functionN(   t	   constants(   t   manifest_pb2(   t   session_bundle(   t   meta_graph_pb2(   t   session(   t
   meta_graph(   t   loader(   t   signature_constantsc         C` s*   t  j d |   } | j | j |  d S(   sZ  Add input tensor to signature_def.

  Args:
    tensor_name: string name of tensor to add to signature_def inputs
    map_key: string key to key into signature_def inputs map
    signature_def: object of type  meta_graph_pb2.SignatureDef()

  Sideffect:
    adds a TensorInfo with tensor_name to signature_def inputs map keyed with
    map_key
  t   nameN(   R   t
   TensorInfot   inputst   CopyFrom(   t   tensor_namet   map_keyt   signature_deft   tensor_info(    (    so   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/bundle_shim.pyt   _add_input_to_signature_def#   s    c         C` s*   t  j d |   } | j | j |  d S(   s^  Add output tensor to signature_def.

  Args:
    tensor_name: string name of tensor to add to signature_def outputs
    map_key: string key to key into signature_def outputs map
    signature_def: object of type  meta_graph_pb2.SignatureDef()

  Sideffect:
    adds a TensorInfo with tensor_name to signature_def outputs map keyed with
    map_key
  R   N(   R   R   t   outputsR   (   R   R   R   R   (    (    so   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/bundle_shim.pyt   _add_output_to_signature_def3   s    c         C` s	  |  j  } t j   } | j d  d k rt | j } t j | _ t | j	 j
 t j |  t | j j
 t j |  n | j d  d k rμ | j } t j | _ t | j	 j
 t j |  t | j j
 t j |  t | j j
 t j |  n t d | j d    | S(   s_  Convert default signature to object of type SignatureDef.

  Args:
    signatures: object of type manifest_pb2.Signatures()

  Returns:
    object of type SignatureDef which contains a converted version of default
    signature from input signatures object

  Raises:
    RuntimeError: if default signature type is not classification or regression.
  t   typet   regression_signaturet   classification_signaturesj   Only classification and regression default signatures are supported for up-conversion. %s is not supported(   t   default_signatureR   t   SignatureDeft
   WhichOneofR   R
   t   REGRESS_METHOD_NAMEt   method_nameR   t   inputR   t   REGRESS_INPUTSR   t   outputt   REGRESS_OUTPUTSR   t   CLASSIFY_METHOD_NAMEt   CLASSIFY_INPUTSt   classest   CLASSIFY_OUTPUT_CLASSESt   scorest   CLASSIFY_OUTPUT_SCORESt   RuntimeError(   t
   signaturesR   R   R   R   (    (    so   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/bundle_shim.pyt+   _convert_default_signature_to_signature_defD   s4    		
	
c         C` sύ   t  j   } |  j t j } |  j t j } | j d  d k sV | j d  d k r t d | j d  | j d  f   n  t j | _	 x3 | j
 j j   D] \ } } t | j | |  q  Wx3 | j
 j j   D] \ } } t | j | |  qΦ W| S(   sh  Convert named signatures to object of type SignatureDef.

  Args:
    signatures: object of type manifest_pb2.Signatures()

  Returns:
    object of type SignatureDef which contains a converted version of named
    signatures from input signatures object

  Raises:
    RuntimeError: if input and output named signatures are not of type
    GenericSignature
  R   t   generic_signatures   Named input and output signatures can only be up-converted if they are generic signature. Input signature type is %s, output signature type is %s(   R   R   t   named_signaturesR
   t   PREDICT_INPUTSt   PREDICT_OUTPUTSR   R(   t   PREDICT_METHOD_NAMER   R+   t   mapt   itemsR   R   R   (   R)   R   t   input_signaturet   output_signaturet   keyt   val(    (    so   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/bundle_shim.pyt*   _convert_named_signatures_to_signature_defo   s     

c         C` s   |  j  } t j   } | t j j j d } | j |  d } d } | j	 d  rc t
 |  } n  t | j  d k r t |  } n  | | f S(   sμ  Produce default and named upconverted SignatureDef objects from Signatures.

  Args:
    metagraph_def: object of type meta_graph_pb2.MetaGraphDef containing legacy
    format Session Bundle signatures

  Returns:
    default_signature_def: object of type SignatureDef which contains an
        upconverted version of default signatures in metagraph_def
    named_signature_def: object of type SignatureDef which contains an
        upconverted version of named signatures in metagraph_def
  i    R   i   N(   t   collection_defR   t
   Signaturest   legacy_constantst   SIGNATURES_KEYt   any_listt   valuet   Unpackt   Nonet   HasFieldR*   t   lenR,   R6   (   t   metagraph_defR7   t   signatures_protoR)   t   default_signature_deft   named_signature_def(    (    so   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/bundle_shim.pyt%   _convert_signatures_to_signature_defs   s    	c   	      C` sΥ   t  j j |  t j  } t j |  } | j r? t d   n  t	 |  \ } } | rq | j t
 j j |  n  | rͺ t
 j } | r | d 7} n  | j | j |  n  t j |  | | d | \ } } | | f S(   sT  Load legacy TF Exporter/SessionBundle checkpoint.

  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()

  Returns:
    session: a tensorflow session created from the variable files.
    metagraph_def: The `MetaGraphDef` protocol buffer loaded in the provided
    session. This can be used to further extract signature-defs,
    collection-defs, etc.
    This model is up-converted to SavedModel format. Specifically, metagraph_def
    SignatureDef field is populated with Signatures converted from legacy
    signatures contained within CollectionDef

  Raises:
    RuntimeError: If metagraph already contains signature_def and cannot be
    up-converted.
  s:   Legacy graph contains signature def, unable to up-convert.t   _from_namedt   meta_graph_def(   t   ost   patht   joinR9   t   META_GRAPH_DEF_FILENAMER   t   read_meta_graph_fileR   R(   RE   R
   t!   DEFAULT_SERVING_SIGNATURE_DEF_KEYR   R   t   load_session_bundle_from_path(	   t
   export_dirt   targett   configt   meta_graph_filenameRA   RC   RD   t   signature_def_keyt   sess(    (    so   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/bundle_shim.pyt*   _load_saved_model_from_session_bundle_path²   s$    	

	t    c         C` s   d } d } t j |   rN t j | d d d | } t j | | |   } n: t j |   rx t |  | |  \ } } n t	 d |    | | f S(   s½  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.
    tags: Set of string tags to identify the required MetaGraphDef when model is
          saved as SavedModel. These should correspond to the tags used when
          saving the variables using the SavedModel `save()` API.
    target: The execution engine to connect to. See target in tf.Session()
    config: A ConfigProto proto with configuration options. See config in
            tf.Session()

  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.
  t   graphRQ   sL   SessionBundle or SavedModelBundle not found at specified export location: %sN(
   R>   R	   t   maybe_saved_model_directoryR   t   Sessiont   loadR   t   maybe_session_bundle_dirRU   R(   (   RO   t   tagsRP   RQ   RA   RT   (    (    so   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/bundle_shim.pyt3   load_session_bundle_or_saved_model_bundle_from_pathθ   s    
(   t   __doc__t
   __future__R    R   R   RH   t!   tensorflow.contrib.session_bundleR   R9   R   R   t   tensorflow.core.protobufR   t   tensorflow.python.clientR   t   tensorflow.python.frameworkR   t   tensorflow.python.saved_modelR	   R
   R   R   R*   R6   RE   RU   R>   R]   (    (    (    so   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/bundle_shim.pyt   <module>   s*   			+	%		7