Created
February 28, 2019 09:39
-
-
Save DibyaranjanSathua/a660d5daa32a252320624f7b2a332337 to your computer and use it in GitHub Desktop.
Convert the input placeholders to Constant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from tensorflow.core.framework import graph_pb2 | |
import copy | |
def load_graph(filename): | |
""" Use to read the tensorflow protobuf graph file. """ | |
graph_def = tf.GraphDef() | |
with tf.gfile.FastGFile(filename, 'rb') as f: | |
graph_def.ParseFromString(f.read()) | |
return graph_def | |
def convert_to_constant(input_graph, output_graph): | |
""" Use to replace the placeholders in graph with Constant nodes. """ | |
graph_def = load_graph(input_graph) | |
keep_prob = tf.constant(1.0, dtype=tf.float32, shape=[], name='keep_prob') | |
weight_factor = tf.constant(1.0, dtype=tf.float32, shape=[], name='weight_factor') | |
is_training = tf.constant(False, dtype=tf.bool, shape=[], name='is_training') | |
new_graph_def = graph_pb2.GraphDef() | |
# Iterating over each node in the graph | |
for node in graph_def.node: | |
if node.name == 'keep_prob': | |
new_graph_def.node.extend([keep_prob.op.node_def]) | |
elif node.name == 'weight_factor': | |
new_graph_def.node.extend([weight_factor.op.node_def]) | |
elif node.name == 'is_training': | |
new_graph_def.node.extend([is_training.op.node_def]) | |
else: | |
new_graph_def.node.extend([copy.deepcopy(node)]) | |
# save new graph | |
with tf.gfile.GFile(output_graph, "wb") as f: | |
f.write(new_graph_def.SerializeToString()) | |
if __name__ == '__main__': | |
output_transform_graph = "/Users/dibyaranjan/Python/Projects/LicensePlate/OCR_Model/AlphaDigitAdam30Epochs36Classes_V2/transformed_graph.pb" | |
remove_placeholder_graph = "/Users/dibyaranjan/Python/Projects/LicensePlate/OCR_Model/AlphaDigitAdam30Epochs36Classes_V2/removed_placeholder_graph.pb" | |
convert_to_constant(output_transform_graph, remove_placeholder_graph) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment