Functions | Variables
freeze_graph Namespace Reference

Functions

def freeze_graph (input_graph, input_saver, input_binary, input_checkpoint, output_node_names, restore_op_name, filename_tensor_name, output_graph, clear_devices, initializer_nodes, variable_names_blacklist="")
 
def main (unused_args)
 

Variables

 FLAGS = None
 
 parser = argparse.ArgumentParser()
 
 type
 
 default
 
 help
 
 nargs
 
 const
 
 unparsed
 
 main
 
 argv
 

Function Documentation

def freeze_graph.freeze_graph (   input_graph,
  input_saver,
  input_binary,
  input_checkpoint,
  output_node_names,
  restore_op_name,
  filename_tensor_name,
  output_graph,
  clear_devices,
  initializer_nodes,
  variable_names_blacklist = "" 
)
Converts all variables in a graph and checkpoint into constants.

Definition at line 68 of file freeze_graph.py.

68  variable_names_blacklist=""):
69  """Converts all variables in a graph and checkpoint into constants."""
70 
71  del restore_op_name, filename_tensor_name # Unused by updated loading code.
72 
73  if not gfile.Exists(input_graph):
74  print("Input graph file '" + input_graph + "' does not exist!")
75  return -1
76 
77  if input_saver and not gfile.Exists(input_saver):
78  print("Input saver file '" + input_saver + "' does not exist!")
79  return -1
80 
81  # 'input_checkpoint' may be a prefix if we're using Saver V2 format
82  if not saver_lib.checkpoint_exists(input_checkpoint):
83  print("Input checkpoint '" + input_checkpoint + "' doesn't exist!")
84  return -1
85 
86  if not output_node_names:
87  print("You need to supply the name of a node to --output_node_names.")
88  return -1
89 
90  input_graph_def = graph_pb2.GraphDef()
91  mode = "rb" if input_binary else "r"
92  with gfile.FastGFile(input_graph, mode) as f:
93  if input_binary:
94  input_graph_def.ParseFromString(f.read())
95  else:
96  text_format.Merge(f.read(), input_graph_def)
97  # Remove all the explicit device specifications for this node. This helps to
98  # make the graph more portable.
99  if clear_devices:
100  for node in input_graph_def.node:
101  node.device = ""
102 
103  _ = importer.import_graph_def(input_graph_def, name="")
104 
105  with session.Session() as sess:
106  if input_saver:
107  with gfile.FastGFile(input_saver, mode) as f:
108  saver_def = saver_pb2.SaverDef()
109  if input_binary:
110  saver_def.ParseFromString(f.read())
111  else:
112  text_format.Merge(f.read(), saver_def)
113  saver = saver_lib.Saver(saver_def=saver_def)
114  saver.restore(sess, input_checkpoint)
115  else:
116  var_list = {}
117  reader = pywrap_tensorflow.NewCheckpointReader(input_checkpoint)
118  var_to_shape_map = reader.get_variable_to_shape_map()
119  for key in var_to_shape_map:
120  try:
121  tensor = sess.graph.get_tensor_by_name(key + ":0")
122  except KeyError:
123  # This tensor doesn't exist in the graph (for example it's
124  # 'global_step' or a similar housekeeping element) so skip it.
125  continue
126  var_list[key] = tensor
127  saver = saver_lib.Saver(var_list=var_list)
128  saver.restore(sess, input_checkpoint)
129  if initializer_nodes:
130  sess.run(initializer_nodes)
131 
132  variable_names_blacklist = (variable_names_blacklist.split(",") if
133  variable_names_blacklist else None)
134  output_graph_def = graph_util.convert_variables_to_constants(
135  sess,
136  input_graph_def,
137  output_node_names.split(","),
138  variable_names_blacklist=variable_names_blacklist)
139 
140  with gfile.GFile(output_graph, "wb") as f:
141  f.write(output_graph_def.SerializeToString())
142  print("%d ops in the final graph." % len(output_graph_def.node))
143 
144 
def freeze_graph.main (   unused_args)

Definition at line 145 of file freeze_graph.py.

145 def main(unused_args):
146  freeze_graph(FLAGS.input_graph, FLAGS.input_saver, FLAGS.input_binary,
147  FLAGS.input_checkpoint, FLAGS.output_node_names,
148  FLAGS.restore_op_name, FLAGS.filename_tensor_name,
149  FLAGS.output_graph, FLAGS.clear_devices, FLAGS.initializer_nodes,
150  FLAGS.variable_names_blacklist)
151 
152 

Variable Documentation

freeze_graph.argv

Definition at line 218 of file freeze_graph.py.

freeze_graph.const

Definition at line 179 of file freeze_graph.py.

freeze_graph.default

Definition at line 159 of file freeze_graph.py.

freeze_graph.FLAGS = None

Definition at line 55 of file freeze_graph.py.

freeze_graph.help

Definition at line 160 of file freeze_graph.py.

freeze_graph.main

Definition at line 218 of file freeze_graph.py.

freeze_graph.nargs

Definition at line 178 of file freeze_graph.py.

freeze_graph.parser = argparse.ArgumentParser()

Definition at line 154 of file freeze_graph.py.

freeze_graph.type

Definition at line 158 of file freeze_graph.py.

freeze_graph.unparsed

Definition at line 217 of file freeze_graph.py.