ó
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 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 d l m Z d d l m Z d d l m Z d d l m Z d „  Z d „  Z d d d „ Z  d „  Z! d e" f d „  ƒ  YZ# d S(   s0   Export a TensorFlow model.

See: go/tf-exporter
i    (   t   absolute_import(   t   division(   t   print_functionN(   t   Any(   t	   constants(   t   gc(   t   manifest_pb2(   t	   graph_pb2(   t   dtypes(   t   ops(   t   gfile(   t
   tf_logging(   t   saver(   t   training_util(   t   compatc         C` s·   t  j d | ƒ t j | ƒ x“ |  j ƒ  D]… \ } } t j j t j	 | ƒ t j	 | ƒ ƒ } t  j d | | ƒ t j
 | ƒ rŸ t  j d | ƒ t j | ƒ n  t j | | ƒ q* Wd S(   sø  Callback to copy files using `gfile.Copy` to an export directory.

  This method is used as the default `assets_callback` in `Exporter.init` to
  copy assets from the `assets_collection`. It can also be invoked directly to
  copy additional supplementary files into the export directory (in which case
  it is not a callback).

  Args:
    files_to_copy: A dictionary that maps original file paths to desired
      basename in the export directory.
    export_dir_path: Directory to copy the files to.
  s'   Write assets into: %s using gfile_copy.s   Copying asset %s to path %s.s   Removing file %s.N(   t   loggingt   infoR
   t   MakeDirst   itemst   ost   patht   joinR   t   as_bytest   Existst   Removet   Copy(   t   files_to_copyt   export_dir_patht   source_filepatht   basenamet   new_path(    (    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyt   gfile_copy_callback+   s    	c         C` s4   t  j ƒ  } |  j | j j _ | j | j j _ | S(   sÆ   Creates a regression signature.

  Args:
    input_tensor: Tensor specifying the input to a graph.
    output_tensor: Tensor specifying the output of a graph.

  Returns:
    A Signature message.
  (   R   t	   Signaturet   namet   regression_signaturet   inputt   tensor_namet   output(   t   input_tensort   output_tensort	   signature(    (    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyR"   H   s    
c         C` sd   t  j ƒ  } |  j | j j _ | d k	 r? | j | j j _ n  | d k	 r` | j | j j _ n  | S(   s  Creates a classification signature.

  Args:
    input_tensor: Tensor specifying the input to a graph.
    classes_tensor: Tensor specifying the output classes of a graph.
    scores_tensor: Tensor specifying the scores of the output classes.

  Returns:
    A Signature message.
  N(	   R   R    R!   t   classification_signatureR#   R$   t   Nonet   classest   scores(   R&   t   classes_tensort   scores_tensorR(   (    (    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyR)   X   s    c         C` sF   t  j ƒ  } x3 t j |  ƒ D]" \ } } | j | j j | _ q W| S(   sš   Creates a generic signature of name to Tensor name.

  Args:
    name_tensor_map: Map from logical name to Tensor.

  Returns:
    A Signature message.
  (   R   R    t   sixt	   iteritemsR!   t   generic_signaturet   mapR$   (   t   name_tensor_mapR(   R!   t   tensor(    (    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyR1   n   s    	t   Exporterc           B` sM   e  Z d  Z d „  Z d d e d d d e d „ Z d d d „ Z d „  Z	 RS(   s[   Exporter helps package a TensorFlow model for serving.

  Args:
    saver: Saver object.
  c         C` sD   | j  ƒ  } | j d ƒ t j d | ƒ |  _ t |  _ i  |  _ d  S(   Nt   max_to_keept	   saver_def(   t   as_saver_deft
   ClearFieldt   tf_savert   Savert   _savert   Falset	   _has_initt   _assets_to_copy(   t   selfR   R7   (    (    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyt   __init__„   s
    	c         C` s}  | d k r i  } n  g  } | r xk | D]` }	 |  j |	 ƒ }
 |
 sV t d |	 ƒ ‚ n  t j j |
 ƒ } | j | |	 f ƒ | |  j |
 <q( Wn  |  j r§ t	 d ƒ ‚ n  t
 |  _ | s¼ | rIt j ƒ  } | rÞ | j | ƒ n | j t j ƒ  j ƒ  ƒ | rx | j D] } d | _ qWn  t ƒ  } | j | ƒ t j t j | ƒ n  | rŠt | t j ƒ stt d | ƒ ‚ n  t j t j | ƒ n  t j ƒ  } | r¯| j j | ƒ n  x1 t j  | ƒ D]  \ } } | j! | j | ƒ q¿Wt ƒ  } | j | ƒ t j t j" | ƒ xa | D]Y \ } } t j# ƒ  } | | _$ | j% | j& _' t ƒ  } | j | ƒ t j t j( | ƒ qW| |  _) d S(   s¡  Initialization.

    Args:
      graph_def: A GraphDef message of the graph to be used in inference.
        GraphDef of default graph is used when None.
      init_op: Op to be used in initialization.
      clear_devices: If device info of the graph should be cleared upon export.
      default_graph_signature: Default signature of the graph.
      named_graph_signatures: Map of named input/output signatures of the graph.
      assets_collection: A collection of constant asset filepath tensors. If set
        the assets will be exported into the asset directory.
      assets_callback: callback with two argument called during export with the
        list of files to copy and the asset path.
    Raises:
      RuntimeError: if init is called more than once.
      TypeError: if init_op is not an Operation or None.
      ValueError: if asset file path tensors are not non-empty constant string
        scalar tensors.
    s    invalid asset filepath tensor %ss   init should be called only oncet    s$   init_op needs to be an Operation: %sN(*   R*   t   _file_path_valuet
   ValueErrorR   R   R   t   appendR?   R>   t   RuntimeErrort   TrueR   t   GraphDeft   CopyFromR	   t   get_default_grapht   as_graph_deft   nodet   deviceR   t   Packt   add_to_collectionR   t	   GRAPH_KEYt
   isinstancet	   Operationt	   TypeErrort   INIT_OP_KEYR   t
   Signaturest   default_signatureR/   R0   t   named_signaturest   SIGNATURES_KEYt	   AssetFilet   filenameR!   t   tensor_bindingR$   t
   ASSETS_KEYt   _assets_callback(   R@   t	   graph_deft   init_opt   clear_devicest   default_graph_signaturet   named_graph_signaturest   assets_collectiont   assets_callbackt   assetst   asset_tensort   asset_filepathR   t   copyRL   t   graph_any_buft   signatures_protot   signature_nameR(   t   signatures_any_bufRZ   R4   t   assett   asset_any_buf(    (    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyt   init   sZ    							c         ` sÆ  |  j  s t d ƒ ‚ n  ˆ  j d ƒ r4 ˆ  d  ‰  n  t j | | ƒ } t j j t j	 ˆ  ƒ t j	 t
 j | ƒ ƒ } t j | ƒ r– t d | ƒ ‚ n  t j | ƒ d } t j | ƒ |  j j | t j j t j | ƒ t j t
 j ƒ ƒ d t
 j ƒ|  j rU|  j rUt j j t j	 | ƒ t j	 t
 j ƒ ƒ } t j | ƒ |  j |  j | ƒ n  t j | | ƒ | rÂ‡  f d †  }	 t j | ƒ }
 x6 |
 t j ˆ  d |	 ƒƒ D] } t j | j ƒ q¥Wn  | S(	   s“  Exports the model.

    Args:
      export_dir_base: A string path to the base export dir.
      global_step_tensor: An Tensor or tensor name providing the
        global step counter to append to the export directory path and set
        in the manifest version.
      sess: A Session to use to save the parameters.
      exports_to_keep: a gc.Path filter function used to determine the set of
        exports to keep. If set to None, all versions will be kept.

    Returns:
      The string path to the exported directory.

    Raises:
      RuntimeError: if init is not called.
      RuntimeError: if the export would overwrite an existing directory.
    s   init must be called firstt   /iÿÿÿÿsV   Overwriting exports can cause corruption and are not allowed. Duplicate export dir: %ss   -tmpt   meta_graph_suffixc         ` sF   t  j d ˆ  d |  j ƒ } | s' d  S|  j d t | j d ƒ ƒ ƒ S(   Nt   ^s	   /(\d{8})$t   export_versioni   (   t   ret   matchR   R*   t   _replacet   intt   group(   R   Ru   (   t   export_dir_base(    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyt   parser"  s    Rz   (   R>   RF   t   endswithR   t   global_stepR   R   R   R   R   R   t   VERSION_FORMAT_SPECIFIERR
   R   t   as_textR   R<   t   savet   EXPORT_BASE_NAMEt   EXPORT_SUFFIX_NAMER]   R?   t   ASSETS_DIRECTORYt   RenameR   t   negationt	   get_pathst   DeleteRecursively(   R@   Ry   t   global_step_tensort   sesst   exports_to_keepR|   t
   export_dirt   tmp_export_dirt
   assets_dirRz   t   paths_to_deletet   p(    (   Ry   sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyt   exportà   s>    			
	"c         C` s¡   t  | t j ƒ s! t d ƒ ‚ n  | j j d k rB t d ƒ ‚ n  | j t j k rc t d ƒ ‚ n  | j j	 d ƒ j
 } t | ƒ d k r™ t d ƒ ‚ n  | d S(	   s<   Returns the filepath value stored in constant `path_tensor`.s   tensor is not a Tensort   Consts#   Only constants tensor are supporteds   File paths should be stringt   valuei   s!   Only scalar tensors are supportedi    (   RQ   R	   t   TensorRS   t   opt   typet   dtypeR   t   stringt   get_attrt
   string_valt   len(   R@   t   path_tensort	   str_value(    (    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyRC   .  s    N(
   t   __name__t
   __module__t   __doc__RA   R*   R=   R   Ro   R   RC   (    (    (    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyR5   }   s   	MJ($   Rž   t
   __future__R    R   R   R   Rt   R/   t   google.protobuf.any_pb2R   t!   tensorflow.contrib.session_bundleR   R   R   t   tensorflow.core.frameworkR   t   tensorflow.python.frameworkR   R	   t   tensorflow.python.platformR
   R   R   t   tensorflow.python.trainingR   R:   R   t   tensorflow.python.utilR   R   R"   R*   R)   R1   t   objectR5   (    (    (    sl   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/session_bundle/exporter.pyt   <module>   s0   			