Home | History | Annotate | Download | only in framework
      1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 # ==============================================================================
     15 
     16 """Utility functions for reading/writing graphs."""
     17 from __future__ import absolute_import
     18 from __future__ import division
     19 from __future__ import print_function
     20 
     21 import os
     22 import os.path
     23 
     24 from google.protobuf import text_format
     25 from tensorflow.python.framework import ops
     26 from tensorflow.python.lib.io import file_io
     27 from tensorflow.python.util.tf_export import tf_export
     28 
     29 
     30 @tf_export('io.write_graph', v1=['io.write_graph', 'train.write_graph'])
     31 def write_graph(graph_or_graph_def, logdir, name, as_text=True):
     32   """Writes a graph proto to a file.
     33 
     34   The graph is written as a text proto unless `as_text` is `False`.
     35 
     36   ```python
     37   v = tf.Variable(0, name='my_variable')
     38   sess = tf.Session()
     39   tf.train.write_graph(sess.graph_def, '/tmp/my-model', 'train.pbtxt')
     40   ```
     41 
     42   or
     43 
     44   ```python
     45   v = tf.Variable(0, name='my_variable')
     46   sess = tf.Session()
     47   tf.train.write_graph(sess.graph, '/tmp/my-model', 'train.pbtxt')
     48   ```
     49 
     50   Args:
     51     graph_or_graph_def: A `Graph` or a `GraphDef` protocol buffer.
     52     logdir: Directory where to write the graph. This can refer to remote
     53       filesystems, such as Google Cloud Storage (GCS).
     54     name: Filename for the graph.
     55     as_text: If `True`, writes the graph as an ASCII proto.
     56 
     57   Returns:
     58     The path of the output proto file.
     59   """
     60   if isinstance(graph_or_graph_def, ops.Graph):
     61     graph_def = graph_or_graph_def.as_graph_def()
     62   else:
     63     graph_def = graph_or_graph_def
     64 
     65   # gcs does not have the concept of directory at the moment.
     66   if not file_io.file_exists(logdir) and not logdir.startswith('gs:'):
     67     file_io.recursive_create_dir(logdir)
     68   path = os.path.join(logdir, name)
     69   if as_text:
     70     file_io.atomic_write_string_to_file(path,
     71                                         text_format.MessageToString(graph_def))
     72   else:
     73     file_io.atomic_write_string_to_file(path, graph_def.SerializeToString())
     74   return path
     75