Python tensorflow.python.framework.graph_io.write_graph() Examples
The following are 28
code examples of tensorflow.python.framework.graph_io.write_graph().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.python.framework.graph_io
, or try the search function
.
Example #1
Source File: image_classifier_tf.py From aiexamples with Apache License 2.0 | 6 votes |
def keras_to_tensorflow(keras_model, output_dir, model_name, out_prefix="output_", log_tensorboard=True): if os.path.exists(output_dir) == False: os.mkdir(output_dir) out_nodes = [] for i in range(len(keras_model.outputs)): out_nodes.append(out_prefix + str(i + 1)) tf.identity(keras_model.output[i], out_prefix + str(i + 1)) sess = K.get_session() init_graph = sess.graph.as_graph_def() main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes) graph_io.write_graph(main_graph, output_dir, name=model_name, as_text=False) if log_tensorboard: import_pb_to_tensorboard.import_to_tensorboard(os.path.join(output_dir, model_name), output_dir)
Example #2
Source File: convert_test.py From tf-encrypted with Apache License 2.0 | 6 votes |
def export(x: tf.Tensor, filename: str, sess=None): should_close = False if sess is None: should_close = True sess = tf.Session() pred_node_names = ["output"] tf.identity(x, name=pred_node_names[0]) graph = graph_util.convert_variables_to_constants( sess, sess.graph.as_graph_def(), pred_node_names ) graph = graph_util.remove_training_nodes(graph) path = graph_io.write_graph(graph, ".", filename, as_text=False) if should_close: sess.close() return path
Example #3
Source File: convert.py From tf-encrypted with Apache License 2.0 | 6 votes |
def export_cnn() -> None: input = tf.placeholder(tf.float32, shape=(1, 1, 3, 3)) filter = tf.constant(np.ones((3, 3, 1, 1)), dtype=tf.float32) x = tf.nn.conv2d(input, filter, (1, 1, 1, 1), "SAME", data_format="NCHW") x = tf.nn.sigmoid(x) x = tf.nn.relu(x) pred_node_names = ["output"] tf.identity(x, name=pred_node_names[0]) with tf.Session() as sess: constant_graph = graph_util.convert_variables_to_constants( sess, sess.graph.as_graph_def(), pred_node_names ) frozen = graph_util.remove_training_nodes(constant_graph) output = "cnn.pb" graph_io.write_graph(frozen, ".", output, as_text=False)
Example #4
Source File: util.py From LiTS---Liver-Tumor-Segmentation-Challenge with MIT License | 5 votes |
def convertMetaModelToPbModel(meta_model, pb_model): # Step 1 # import the model metagraph saver = tf.train.import_meta_graph(meta_model + '.meta', clear_devices=True) # make that as the default graph graph = tf.get_default_graph() sess = tf.Session() # now restore the variables saver.restore(sess, meta_model) # Step 2 # Find the output name for op in graph.get_operations(): print(op.name) # Step 3 output_graph_def = graph_util.convert_variables_to_constants( sess, # The session sess.graph_def, # input_graph_def is useful for retrieving the nodes ["Placeholder", "output/Sigmoid"]) # Step 4 # output folder output_fld = './' # output pb file name output_model_file = 'model.pb' # write the graph graph_io.write_graph(output_graph_def, pb_model + output_fld, output_model_file, as_text=False)
Example #5
Source File: optimize_for_inference.py From keras-lambda with MIT License | 5 votes |
def main(unused_args): if not gfile.Exists(FLAGS.input): print("Input graph file '" + FLAGS.input + "' does not exist!") return -1 input_graph_def = graph_pb2.GraphDef() with gfile.Open(FLAGS.input, "r") as f: data = f.read() if FLAGS.frozen_graph: input_graph_def.ParseFromString(data) else: text_format.Merge(data.decode("utf-8"), input_graph_def) output_graph_def = optimize_for_inference_lib.optimize_for_inference( input_graph_def, FLAGS.input_names.split(","), FLAGS.output_names.split(","), FLAGS.placeholder_type_enum) if FLAGS.frozen_graph: f = gfile.FastGFile(FLAGS.output, "w") f.write(output_graph_def.SerializeToString()) else: graph_io.write_graph(output_graph_def, os.path.dirname(FLAGS.output), os.path.basename(FLAGS.output)) return 0
Example #6
Source File: optimize_for_inference.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def main(unused_args): if not gfile.Exists(FLAGS.input): print("Input graph file '" + FLAGS.input + "' does not exist!") return -1 input_graph_def = graph_pb2.GraphDef() with gfile.Open(FLAGS.input, "rb") as f: data = f.read() if FLAGS.frozen_graph: input_graph_def.ParseFromString(data) else: text_format.Merge(data.decode("utf-8"), input_graph_def) output_graph_def = optimize_for_inference_lib.optimize_for_inference( input_graph_def, FLAGS.input_names.split(","), FLAGS.output_names.split(","), FLAGS.placeholder_type_enum) if FLAGS.frozen_graph: f = gfile.FastGFile(FLAGS.output, "w") f.write(output_graph_def.SerializeToString()) else: graph_io.write_graph(output_graph_def, os.path.dirname(FLAGS.output), os.path.basename(FLAGS.output)) return 0
Example #7
Source File: optimize_for_inference.py From dcscn-super-resolution with MIT License | 5 votes |
def main(unused_args): if not gfile.Exists(FLAGS.input): print("Input graph file '" + FLAGS.input + "' does not exist!") return -1 input_graph_def = graph_pb2.GraphDef() with gfile.Open(FLAGS.input, "rb") as f: data = f.read() if FLAGS.frozen_graph: input_graph_def.ParseFromString(data) else: text_format.Merge(data.decode("utf-8"), input_graph_def) output_graph_def = optimize_for_inference_lib.optimize_for_inference( input_graph_def, FLAGS.input_names.split(","), FLAGS.output_names.split(","), FLAGS.placeholder_type_enum, FLAGS.toco_compatible) if FLAGS.frozen_graph: f = gfile.FastGFile(FLAGS.output, "w") f.write(output_graph_def.SerializeToString()) else: graph_io.write_graph(output_graph_def, os.path.dirname(FLAGS.output), os.path.basename(FLAGS.output)) return 0
Example #8
Source File: util.py From VNet with MIT License | 5 votes |
def convertMetaModelToPbModel(meta_model, pb_model): # Step 1 # import the model metagraph saver = tf.train.import_meta_graph(meta_model + '.meta', clear_devices=True) # make that as the default graph graph = tf.get_default_graph() sess = tf.Session() # now restore the variables saver.restore(sess, meta_model) # Step 2 # Find the output name for op in graph.get_operations(): print(op.name) # Step 3 output_graph_def = graph_util.convert_variables_to_constants( sess, # The session sess.graph_def, # input_graph_def is useful for retrieving the nodes ["Input", "output/Sigmoid"]) # Step 4 # output folder output_fld = './' # output pb file name output_model_file = 'model.pb' # write the graph graph_io.write_graph(output_graph_def, pb_model + output_fld, output_model_file, as_text=False)
Example #9
Source File: util.py From VNet3D with MIT License | 5 votes |
def convertMetaModelToPbModel(meta_model, pb_model): # Step 1 # import the model metagraph saver = tf.train.import_meta_graph(meta_model + '.meta', clear_devices=True) # make that as the default graph graph = tf.get_default_graph() sess = tf.Session() # now restore the variables saver.restore(sess, meta_model) # Step 2 # Find the output name for op in graph.get_operations(): print(op.name) # Step 3 output_graph_def = graph_util.convert_variables_to_constants( sess, # The session sess.graph_def, # input_graph_def is useful for retrieving the nodes ["Placeholder", "output/Sigmoid"]) # Step 4 # output folder output_fld = './' # output pb file name output_model_file = 'model.pb' # write the graph graph_io.write_graph(output_graph_def, pb_model + output_fld, output_model_file, as_text=False)
Example #10
Source File: mnist_deep_cnn.py From tf-encrypted with Apache License 2.0 | 5 votes |
def export_to_pb(sess, x, filename): pred_names = ["output"] tf.identity(x, name=pred_names[0]) graph = graph_util.convert_variables_to_constants( sess, sess.graph.as_graph_def(), pred_names ) graph = graph_util.remove_training_nodes(graph) path = graph_io.write_graph(graph, ".", filename, as_text=False) print("saved the frozen graph (ready for inference) at: ", filename) return path
Example #11
Source File: main.py From tf-encrypted with Apache License 2.0 | 5 votes |
def export_to_pb(sess, x, filename): pred_names = ["output"] tf.identity(x, name=pred_names[0]) graph = graph_util.convert_variables_to_constants( sess, sess.graph.as_graph_def(), pred_names ) graph = graph_util.remove_training_nodes(graph) path = graph_io.write_graph(graph, ".", filename, as_text=False) print("saved the frozen graph (ready for inference) at: ", path)
Example #12
Source File: low_level_runner.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def build_model(self, model_fn, params): """Build the TPU model and infeed enqueue ops.""" tf.logging.info("TrainLowLevelRunner: build_model method") def tpu_train_step(loss): """Generate the TPU graph.""" del loss values = self.infeed_queue[0].generate_dequeue_op(tpu_device=0) unflattened_inputs = data_nest.pack_sequence_as(self.feature_structure, values) features = unflattened_inputs["features"] labels = unflattened_inputs["labels"] estimator_spec = model_fn(features, labels, tf.estimator.ModeKeys.TRAIN, params) loss, train_op = estimator_spec.loss, estimator_spec.train_op with tf.control_dependencies([train_op]): return tf.identity(loss) def train_loop(): return tpu.repeat(self.iterations, tpu_train_step, [_INITIAL_LOSS]) with self.graph.as_default(): (self.loss,) = tpu.shard( train_loop, inputs=[], num_shards=self.hparams.num_shards, outputs_from_all_shards=False, ) global_initializer = tf.global_variables_initializer() local_initializer = tf.local_variables_initializer() graph_io.write_graph( self.graph.as_graph_def(add_shapes=True), self.hparams.out_dir, "graph.pbtxt") self.saver = tf.train.Saver() self.sess.run(global_initializer) self.sess.run(local_initializer)
Example #13
Source File: low_level_runner.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def build_model(self, model_fn, params): """Build the TPU model and infeed enqueue ops.""" tf.logging.info("TrainLowLevelRunner: build_model method") def tpu_train_step(loss): """Generate the TPU graph.""" del loss values = self.infeed_queue[0].generate_dequeue_op(tpu_device=0) unflattened_inputs = data_nest.pack_sequence_as(self.feature_structure, values) features = unflattened_inputs["features"] labels = unflattened_inputs["labels"] estimator_spec = model_fn(features, labels, tf.estimator.ModeKeys.TRAIN, params) loss, train_op = estimator_spec.loss, estimator_spec.train_op with tf.control_dependencies([train_op]): return tf.identity(loss) def train_loop(): return tpu.repeat(self.iterations, tpu_train_step, [_INITIAL_LOSS]) with self.graph.as_default(): (self.loss,) = tpu.shard( train_loop, inputs=[], num_shards=self.hparams.num_shards, outputs_from_all_shards=False, ) global_initializer = tf.global_variables_initializer() local_initializer = tf.local_variables_initializer() graph_io.write_graph( self.graph.as_graph_def(add_shapes=True), self.hparams.out_dir, "graph.pbtxt") self.saver = tf.train.Saver() self.sess.run(global_initializer) self.sess.run(local_initializer)
Example #14
Source File: __init__.py From ImageAI with MIT License | 5 votes |
def save_model_to_tensorflow(self, new_model_folder, new_model_name=""): """ 'save_model_to_tensorflow' function allows you to save your loaded Keras (.h5) model and save it to the Tensorflow (.pb) model format. - new_model_folder (required), the path to the folder you want the converted Tensorflow model to be saved - new_model_name (required), the desired filename for your converted Tensorflow model e.g 'my_new_model.pb' :param new_model_folder: :param new_model_name: :return: """ if(self.__modelLoaded == True): out_prefix = "output_" output_dir = new_model_folder if os.path.exists(output_dir) == False: os.mkdir(output_dir) model_name = os.path.join(output_dir, new_model_name) keras_model = self.__model_collection[0] out_nodes = [] for i in range(len(keras_model.outputs)): out_nodes.append(out_prefix + str(i + 1)) tf.identity(keras_model.output[i], out_prefix + str(i + 1)) sess = K.get_session() from tensorflow.python.framework import graph_util, graph_io init_graph = sess.graph.as_graph_def() main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes) graph_io.write_graph(main_graph, output_dir, name=model_name, as_text=False) print("Tensorflow Model Saved")
Example #15
Source File: low_level_runner.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def build_model(self, model_fn, params): """Build the TPU model and infeed enqueue ops.""" tf.logging.info("TrainLowLevelRunner: build_model method") def tpu_train_step(loss): """Generate the TPU graph.""" del loss values = self.infeed_queue[0].generate_dequeue_op(tpu_device=0) unflattened_inputs = data_nest.pack_sequence_as(self.feature_structure, values) features = unflattened_inputs["features"] labels = unflattened_inputs["labels"] estimator_spec = model_fn(features, labels, tf.estimator.ModeKeys.TRAIN, params) loss, train_op = estimator_spec.loss, estimator_spec.train_op with tf.control_dependencies([train_op]): return tf.identity(loss) def train_loop(): return tpu.repeat(self.iterations, tpu_train_step, [_INITIAL_LOSS]) with self.graph.as_default(): (self.loss,) = tpu.shard( train_loop, inputs=[], num_shards=self.hparams.num_shards, outputs_from_all_shards=False, ) global_initializer = tf.global_variables_initializer() local_initializer = tf.local_variables_initializer() graph_io.write_graph( self.graph.as_graph_def(add_shapes=True), self.hparams.out_dir, "graph.pbtxt") self.saver = tf.train.Saver() self.sess.run(global_initializer) self.sess.run(local_initializer)
Example #16
Source File: optimize_for_inference.py From lambda-packs with MIT License | 5 votes |
def main(unused_args): if not gfile.Exists(FLAGS.input): print("Input graph file '" + FLAGS.input + "' does not exist!") return -1 input_graph_def = graph_pb2.GraphDef() with gfile.Open(FLAGS.input, "rb") as f: data = f.read() if FLAGS.frozen_graph: input_graph_def.ParseFromString(data) else: text_format.Merge(data.decode("utf-8"), input_graph_def) output_graph_def = optimize_for_inference_lib.optimize_for_inference( input_graph_def, FLAGS.input_names.split(","), FLAGS.output_names.split(","), FLAGS.placeholder_type_enum) if FLAGS.frozen_graph: f = gfile.FastGFile(FLAGS.output, "w") f.write(output_graph_def.SerializeToString()) else: graph_io.write_graph(output_graph_def, os.path.dirname(FLAGS.output), os.path.basename(FLAGS.output)) return 0
Example #17
Source File: optimize_for_inference.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def main(unused_args): if not gfile.Exists(FLAGS.input): print("Input graph file '" + FLAGS.input + "' does not exist!") return -1 input_graph_def = graph_pb2.GraphDef() with gfile.Open(FLAGS.input, "r") as f: data = f.read() if FLAGS.frozen_graph: input_graph_def.ParseFromString(data) else: text_format.Merge(data.decode("utf-8"), input_graph_def) output_graph_def = optimize_for_inference_lib.optimize_for_inference( input_graph_def, FLAGS.input_names.split(","), FLAGS.output_names.split(","), FLAGS.placeholder_type_enum) if FLAGS.frozen_graph: f = gfile.FastGFile(FLAGS.output, "w") f.write(output_graph_def.SerializeToString()) else: graph_io.write_graph(output_graph_def, os.path.dirname(FLAGS.output), os.path.basename(FLAGS.output)) return 0
Example #18
Source File: strip_unused_test.py From auto-alt-text-lambda-api with MIT License | 4 votes |
def testStripUnusedMultipleInputs(self): input_graph_name = "input_graph.pb" output_graph_name = "output_graph.pb" # We'll create an input graph that multiplies two input nodes. with ops.Graph().as_default(): constant_node1 = constant_op.constant(1.0, name="constant_node1") constant_node2 = constant_op.constant(2.0, name="constant_node2") input_node1 = math_ops.subtract(constant_node1, 3.0, name="input_node1") input_node2 = math_ops.subtract(constant_node2, 5.0, name="input_node2") output_node = math_ops.multiply( input_node1, input_node2, name="output_node") math_ops.add(output_node, 2.0, name="later_node") sess = session.Session() output = sess.run(output_node) self.assertNear(6.0, output, 0.00001) graph_io.write_graph(sess.graph, self.get_temp_dir(), input_graph_name) # We save out the graph to disk, and then call the const conversion # routine. input_graph_path = os.path.join(self.get_temp_dir(), input_graph_name) input_binary = False input_node_names = "input_node1,input_node2" input_node_types = [ dtypes.float32.as_datatype_enum, dtypes.float32.as_datatype_enum ] output_binary = True output_node_names = "output_node" output_graph_path = os.path.join(self.get_temp_dir(), output_graph_name) strip_unused_lib.strip_unused_from_files(input_graph_path, input_binary, output_graph_path, output_binary, input_node_names, output_node_names, input_node_types) # Now we make sure the variable is now a constant, and that the graph still # produces the expected result. with ops.Graph().as_default(): output_graph_def = graph_pb2.GraphDef() with open(output_graph_path, "rb") as f: output_graph_def.ParseFromString(f.read()) _ = importer.import_graph_def(output_graph_def, name="") self.assertEqual(3, len(output_graph_def.node)) for node in output_graph_def.node: self.assertNotEqual("Add", node.op) self.assertNotEqual("Sub", node.op) if node.name == input_node_names: self.assertTrue("shape" in node.attr) with session.Session() as sess: input_node1 = sess.graph.get_tensor_by_name("input_node1:0") input_node2 = sess.graph.get_tensor_by_name("input_node2:0") output_node = sess.graph.get_tensor_by_name("output_node:0") output = sess.run(output_node, feed_dict={input_node1: [10.0], input_node2: [-5.0]}) self.assertNear(-50.0, output, 0.00001)
Example #19
Source File: freeze_graph_test.py From keras-lambda with MIT License | 4 votes |
def _testFreezeGraph(self, saver_write_version): checkpoint_prefix = os.path.join(self.get_temp_dir(), "saved_checkpoint") checkpoint_state_name = "checkpoint_state" input_graph_name = "input_graph.pb" output_graph_name = "output_graph.pb" # We'll create an input graph that has a single variable containing 1.0, # and that then multiplies it by 2. with ops.Graph().as_default(): variable_node = variables.Variable(1.0, name="variable_node") output_node = math_ops.multiply(variable_node, 2.0, name="output_node") sess = session.Session() init = variables.global_variables_initializer() sess.run(init) output = sess.run(output_node) self.assertNear(2.0, output, 0.00001) saver = saver_lib.Saver(write_version=saver_write_version) checkpoint_path = saver.save( sess, checkpoint_prefix, global_step=0, latest_filename=checkpoint_state_name) graph_io.write_graph(sess.graph, self.get_temp_dir(), input_graph_name) # We save out the graph to disk, and then call the const conversion # routine. input_graph_path = os.path.join(self.get_temp_dir(), input_graph_name) input_saver_def_path = "" input_binary = False output_node_names = "output_node" restore_op_name = "save/restore_all" filename_tensor_name = "save/Const:0" output_graph_path = os.path.join(self.get_temp_dir(), output_graph_name) clear_devices = False freeze_graph.freeze_graph(input_graph_path, input_saver_def_path, input_binary, checkpoint_path, output_node_names, restore_op_name, filename_tensor_name, output_graph_path, clear_devices, "") # Now we make sure the variable is now a constant, and that the graph still # produces the expected result. with ops.Graph().as_default(): output_graph_def = graph_pb2.GraphDef() with open(output_graph_path, "rb") as f: output_graph_def.ParseFromString(f.read()) _ = importer.import_graph_def(output_graph_def, name="") self.assertEqual(4, len(output_graph_def.node)) for node in output_graph_def.node: self.assertNotEqual("VariableV2", node.op) self.assertNotEqual("Variable", node.op) with session.Session() as sess: output_node = sess.graph.get_tensor_by_name("output_node:0") output = sess.run(output_node) self.assertNear(2.0, output, 0.00001)
Example #20
Source File: strip_unused_test.py From keras-lambda with MIT License | 4 votes |
def testStripUnusedMultipleInputs(self): input_graph_name = "input_graph.pb" output_graph_name = "output_graph.pb" # We'll create an input graph that multiplies two input nodes. with ops.Graph().as_default(): constant_node1 = constant_op.constant(1.0, name="constant_node1") constant_node2 = constant_op.constant(2.0, name="constant_node2") input_node1 = math_ops.subtract(constant_node1, 3.0, name="input_node1") input_node2 = math_ops.subtract(constant_node2, 5.0, name="input_node2") output_node = math_ops.multiply( input_node1, input_node2, name="output_node") math_ops.add(output_node, 2.0, name="later_node") sess = session.Session() output = sess.run(output_node) self.assertNear(6.0, output, 0.00001) graph_io.write_graph(sess.graph, self.get_temp_dir(), input_graph_name) # We save out the graph to disk, and then call the const conversion # routine. input_graph_path = os.path.join(self.get_temp_dir(), input_graph_name) input_binary = False input_node_names = "input_node1,input_node2" input_node_types = [ dtypes.float32.as_datatype_enum, dtypes.float32.as_datatype_enum ] output_binary = True output_node_names = "output_node" output_graph_path = os.path.join(self.get_temp_dir(), output_graph_name) strip_unused_lib.strip_unused_from_files(input_graph_path, input_binary, output_graph_path, output_binary, input_node_names, output_node_names, input_node_types) # Now we make sure the variable is now a constant, and that the graph still # produces the expected result. with ops.Graph().as_default(): output_graph_def = graph_pb2.GraphDef() with open(output_graph_path, "rb") as f: output_graph_def.ParseFromString(f.read()) _ = importer.import_graph_def(output_graph_def, name="") self.assertEqual(3, len(output_graph_def.node)) for node in output_graph_def.node: self.assertNotEqual("Add", node.op) self.assertNotEqual("Sub", node.op) if node.name == input_node_names: self.assertTrue("shape" in node.attr) with session.Session() as sess: input_node1 = sess.graph.get_tensor_by_name("input_node1:0") input_node2 = sess.graph.get_tensor_by_name("input_node2:0") output_node = sess.graph.get_tensor_by_name("output_node:0") output = sess.run(output_node, feed_dict={input_node1: [10.0], input_node2: [-5.0]}) self.assertNear(-50.0, output, 0.00001)
Example #21
Source File: strip_unused_test.py From keras-lambda with MIT License | 4 votes |
def testStripUnused(self): input_graph_name = "input_graph.pb" output_graph_name = "output_graph.pb" # We'll create an input graph that has a single constant containing 1.0, # and that then multiplies it by 2. with ops.Graph().as_default(): constant_node = constant_op.constant(1.0, name="constant_node") wanted_input_node = math_ops.subtract(constant_node, 3.0, name="wanted_input_node") output_node = math_ops.multiply( wanted_input_node, 2.0, name="output_node") math_ops.add(output_node, 2.0, name="later_node") sess = session.Session() output = sess.run(output_node) self.assertNear(-4.0, output, 0.00001) graph_io.write_graph(sess.graph, self.get_temp_dir(), input_graph_name) # We save out the graph to disk, and then call the const conversion # routine. input_graph_path = os.path.join(self.get_temp_dir(), input_graph_name) input_binary = False input_node_names = "wanted_input_node" output_binary = True output_node_names = "output_node" output_graph_path = os.path.join(self.get_temp_dir(), output_graph_name) strip_unused_lib.strip_unused_from_files(input_graph_path, input_binary, output_graph_path, output_binary, input_node_names, output_node_names, dtypes.float32.as_datatype_enum) # Now we make sure the variable is now a constant, and that the graph still # produces the expected result. with ops.Graph().as_default(): output_graph_def = graph_pb2.GraphDef() with open(output_graph_path, "rb") as f: output_graph_def.ParseFromString(f.read()) _ = importer.import_graph_def(output_graph_def, name="") self.assertEqual(3, len(output_graph_def.node)) for node in output_graph_def.node: self.assertNotEqual("Add", node.op) self.assertNotEqual("Sub", node.op) if node.name == input_node_names: self.assertTrue("shape" in node.attr) with session.Session() as sess: input_node = sess.graph.get_tensor_by_name("wanted_input_node:0") output_node = sess.graph.get_tensor_by_name("output_node:0") output = sess.run(output_node, feed_dict={input_node: [10.0]}) self.assertNear(20.0, output, 0.00001)
Example #22
Source File: convert.py From subsync with Apache License 2.0 | 4 votes |
def convertGraph(modelPath, output, outPath): ''' Converts an HD5F file to a .pb file for use with Tensorflow. Args: modelPath (str): path to the .h5 file output (str): name of the referenced output outPath (str): path to the output .pb file Returns: None ''' dir = os.path.dirname(os.path.realpath(__file__)) outdir = os.path.join(dir, os.path.dirname(outPath)) name = os.path.basename(outPath) basename, ext = os.path.splitext(name) #NOTE: If using Python > 3.2, this could be replaced with os.makedirs( name, exist_ok=True ) if not os.path.isdir(outdir): os.mkdir(outdir) K.set_learning_phase(0) net_model = load_model(modelPath) # Alias the outputs in the model - this sometimes makes them easier to access in TF tf.identity(net_model.output, name=output) sess = K.get_session() net_model.summary() # Write the graph in human readable f = '{}.reference.pb.ascii'.format(basename) tf.train.write_graph(sess.graph.as_graph_def(), outdir, f, as_text=True) print('Saved the graph definition in ascii format at: ', osp.join(outdir, f)) # Write the graph in binary .pb file from tensorflow.python.framework import graph_util from tensorflow.python.framework import graph_io constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), [output]) graph_io.write_graph(constant_graph, outdir, name, as_text=False) print('Saved the constant graph (ready for inference) at: ', outPath)
Example #23
Source File: cifar10.py From keras-audio with MIT License | 4 votes |
def export_tensorflow_model(self, output_fld, output_model_file=None, output_graphdef_file=None, num_output=None, quantize=False, save_output_graphdef_file=False, output_node_prefix=None): K.set_learning_phase(0) if output_model_file is None: output_model_file = Cifar10AudioClassifier.model_name + '.pb' if output_graphdef_file is None: output_graphdef_file = 'model.ascii' if num_output is None: num_output = 1 if output_node_prefix is None: output_node_prefix = 'output_node' pred = [None] * num_output pred_node_names = [None] * num_output for i in range(num_output): pred_node_names[i] = output_node_prefix + str(i) pred[i] = tf.identity(self.model.outputs[i], name=pred_node_names[i]) print('output nodes names are: ', pred_node_names) sess = K.get_session() if save_output_graphdef_file: tf.train.write_graph(sess.graph.as_graph_def(), output_fld, output_graphdef_file, as_text=True) print('saved the graph definition in ascii format at: ', output_graphdef_file) from tensorflow.python.framework import graph_util from tensorflow.python.framework import graph_io from tensorflow.tools.graph_transforms import TransformGraph if quantize: transforms = ["quantize_weights", "quantize_nodes"] transformed_graph_def = TransformGraph(sess.graph.as_graph_def(), [], pred_node_names, transforms) constant_graph = graph_util.convert_variables_to_constants(sess, transformed_graph_def, pred_node_names) else: constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names) graph_io.write_graph(constant_graph, output_fld, output_model_file, as_text=False) print('saved the freezed graph (ready for inference) at: ', output_model_file)
Example #24
Source File: strip_unused_test.py From auto-alt-text-lambda-api with MIT License | 4 votes |
def testStripUnused(self): input_graph_name = "input_graph.pb" output_graph_name = "output_graph.pb" # We'll create an input graph that has a single constant containing 1.0, # and that then multiplies it by 2. with ops.Graph().as_default(): constant_node = constant_op.constant(1.0, name="constant_node") wanted_input_node = math_ops.subtract(constant_node, 3.0, name="wanted_input_node") output_node = math_ops.multiply( wanted_input_node, 2.0, name="output_node") math_ops.add(output_node, 2.0, name="later_node") sess = session.Session() output = sess.run(output_node) self.assertNear(-4.0, output, 0.00001) graph_io.write_graph(sess.graph, self.get_temp_dir(), input_graph_name) # We save out the graph to disk, and then call the const conversion # routine. input_graph_path = os.path.join(self.get_temp_dir(), input_graph_name) input_binary = False input_node_names = "wanted_input_node" output_binary = True output_node_names = "output_node" output_graph_path = os.path.join(self.get_temp_dir(), output_graph_name) strip_unused_lib.strip_unused_from_files(input_graph_path, input_binary, output_graph_path, output_binary, input_node_names, output_node_names, dtypes.float32.as_datatype_enum) # Now we make sure the variable is now a constant, and that the graph still # produces the expected result. with ops.Graph().as_default(): output_graph_def = graph_pb2.GraphDef() with open(output_graph_path, "rb") as f: output_graph_def.ParseFromString(f.read()) _ = importer.import_graph_def(output_graph_def, name="") self.assertEqual(3, len(output_graph_def.node)) for node in output_graph_def.node: self.assertNotEqual("Add", node.op) self.assertNotEqual("Sub", node.op) if node.name == input_node_names: self.assertTrue("shape" in node.attr) with session.Session() as sess: input_node = sess.graph.get_tensor_by_name("wanted_input_node:0") output_node = sess.graph.get_tensor_by_name("output_node:0") output = sess.run(output_node, feed_dict={input_node: [10.0]}) self.assertNear(20.0, output, 0.00001)
Example #25
Source File: punctuator.py From keras-punctuator with MIT License | 4 votes |
def freeze(): checkpoint_prefix = os.path.join(TMP_DIR, "saved_checkpoint") checkpoint_state_name = "checkpoint_state" input_graph_name = "input_graph.pb" output_graph_name = "freezed.pb" saver_write_version = 1 # We'll create an input graph that has a single variable containing 1.0, # and that then multiplies it by 2. from tensorflow.python.framework import ops with ops.Graph().as_default(): from keras import backend as K K.set_learning_phase(0) model = createModel() model.load_weights(KERAS_WEIGHTS_FILE) sess = K.get_session() from tensorflow.python.framework.graph_util_impl import convert_variables_to_constants # convert_variables_to_constants(sess, sess.graph.as_graph_def(), [model.output.name.split(':')[0]]) testGraph(sess, '') from tensorflow.python.training import saver as saver_lib saver = saver_lib.Saver(write_version=saver_write_version) checkpoint_path = saver.save( sess, checkpoint_prefix, global_step=0, latest_filename=checkpoint_state_name) from tensorflow.python.framework import graph_io graph_io.write_graph(sess.graph, TMP_DIR, input_graph_name) sess.close() # We save out the graph to disk, and then call the const conversion # routine. input_graph_path = os.path.join(TMP_DIR, input_graph_name) input_saver_def_path = "" input_binary = False output_node_names = model.output.name.split(':')[0] restore_op_name = "save/restore_all" filename_tensor_name = "save/Const:0" output_graph_path = os.path.join(MODEL_DATA_DIR, output_graph_name) clear_devices = False from tensorflow.python.tools import freeze_graph freeze_graph.freeze_graph(input_graph_path, input_saver_def_path, input_binary, checkpoint_path, output_node_names, restore_op_name, filename_tensor_name, output_graph_path, clear_devices, "") exportWordIndex(loadWordIndex())
Example #26
Source File: freeze_graph_test.py From auto-alt-text-lambda-api with MIT License | 4 votes |
def _testFreezeGraph(self, saver_write_version): checkpoint_prefix = os.path.join(self.get_temp_dir(), "saved_checkpoint") checkpoint_state_name = "checkpoint_state" input_graph_name = "input_graph.pb" output_graph_name = "output_graph.pb" # We'll create an input graph that has a single variable containing 1.0, # and that then multiplies it by 2. with ops.Graph().as_default(): variable_node = variables.Variable(1.0, name="variable_node") output_node = math_ops.multiply(variable_node, 2.0, name="output_node") sess = session.Session() init = variables.global_variables_initializer() sess.run(init) output = sess.run(output_node) self.assertNear(2.0, output, 0.00001) saver = saver_lib.Saver(write_version=saver_write_version) checkpoint_path = saver.save( sess, checkpoint_prefix, global_step=0, latest_filename=checkpoint_state_name) graph_io.write_graph(sess.graph, self.get_temp_dir(), input_graph_name) # We save out the graph to disk, and then call the const conversion # routine. input_graph_path = os.path.join(self.get_temp_dir(), input_graph_name) input_saver_def_path = "" input_binary = False output_node_names = "output_node" restore_op_name = "save/restore_all" filename_tensor_name = "save/Const:0" output_graph_path = os.path.join(self.get_temp_dir(), output_graph_name) clear_devices = False freeze_graph.freeze_graph(input_graph_path, input_saver_def_path, input_binary, checkpoint_path, output_node_names, restore_op_name, filename_tensor_name, output_graph_path, clear_devices, "") # Now we make sure the variable is now a constant, and that the graph still # produces the expected result. with ops.Graph().as_default(): output_graph_def = graph_pb2.GraphDef() with open(output_graph_path, "rb") as f: output_graph_def.ParseFromString(f.read()) _ = importer.import_graph_def(output_graph_def, name="") self.assertEqual(4, len(output_graph_def.node)) for node in output_graph_def.node: self.assertNotEqual("VariableV2", node.op) self.assertNotEqual("Variable", node.op) with session.Session() as sess: output_node = sess.graph.get_tensor_by_name("output_node:0") output = sess.run(output_node) self.assertNear(2.0, output, 0.00001)
Example #27
Source File: kersa_to_tf.py From face_landmark_dnn with MIT License | 4 votes |
def convertGraph(modelPath, outdir, numoutputs, prefix, name): ''' Converts an HD5F file to a .pb file for use with Tensorflow. Args: modelPath (str): path to the .h5 file outdir (str): path to the output directory numoutputs (int): prefix (str): the prefix of the output aliasing name (str): Returns: None ''' # NOTE: If using Python > 3.2, this could be replaced with os.makedirs( name, exist_ok=True ) if not os.path.isdir(outdir): os.mkdir(outdir) K.set_learning_phase(0) net_model = load_model(modelPath) # Alias the outputs in the model - this sometimes makes them easier to access in TF pred = [None] * numoutputs pred_node_names = [None] * numoutputs for i in range(numoutputs): pred_node_names[i] = prefix + '_' + str(i) pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i]) print('Output nodes names are: ', pred_node_names) sess = K.get_session() # Write the graph in human readable f = 'graph_def_for_reference.pb.ascii' tf.train.write_graph(sess.graph.as_graph_def(), outdir, f, as_text=True) print('Saved the graph definition in ascii format at: ', osp.join(outdir, f)) # Write the graph in binary .pb file from tensorflow.python.framework import graph_util from tensorflow.python.framework import graph_io constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names) graph_io.write_graph(constant_graph, outdir, name, as_text=False) print('Saved the constant graph (ready for inference) at: ', osp.join(outdir, name))
Example #28
Source File: k2tf_convert.py From keras-to-tensorflow with MIT License | 4 votes |
def convertGraph( modelPath, outdir, numoutputs, prefix, name): ''' Converts an HD5F file to a .pb file for use with Tensorflow. Args: modelPath (str): path to the .h5 file outdir (str): path to the output directory numoutputs (int): prefix (str): the prefix of the output aliasing name (str): Returns: None ''' #NOTE: If using Python > 3.2, this could be replaced with os.makedirs( name, exist_ok=True ) if not os.path.isdir(outdir): os.mkdir(outdir) K.set_learning_phase(0) net_model = load_model(modelPath) # Alias the outputs in the model - this sometimes makes them easier to access in TF pred = [None]*numoutputs pred_node_names = [None]*numoutputs for i in range(numoutputs): pred_node_names[i] = prefix+'_'+str(i) pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i]) print('Output nodes names are: ', pred_node_names) sess = K.get_session() # Write the graph in human readable f = 'graph_def_for_reference.pb.ascii' tf.train.write_graph(sess.graph.as_graph_def(), outdir, f, as_text=True) print('Saved the graph definition in ascii format at: ', osp.join(outdir, f)) # Write the graph in binary .pb file from tensorflow.python.framework import graph_util from tensorflow.python.framework import graph_io constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names) graph_io.write_graph(constant_graph, outdir, name, as_text=False) print('Saved the constant graph (ready for inference) at: ', osp.join(outdir, name))