[英]Fixing input node of frozen graph, before exporting to tflite format
I am able to freeze a graph using the following:我可以使用以下方法冻结图形:
freeze_graph.freeze_graph(input_graph=f"{save_graph_path}/graph.pbtxt",
input_saver="",
input_binary=False,
input_checkpoint=last_ckpt,
output_node_names="network/output_node",
restore_op_name="save/restore_all",
filename_tensor_name="save/Const:0",
output_graph=output_frozen_graph_name,
clear_devices=True,
initializer_nodes="")
However, the graph has two notable input nodes, namely "input/is_training" and "input/input_node".然而,该图有两个值得注意的输入节点,即“input/is_training”和“input/input_node”。
I would like to export this frozen graph to tflite format, but in doing so I need to fix is_training to False (since it is used for tf.layers.batch_normalization).我想将此冻结图导出为 tflite 格式,但这样做时我需要将 is_training 修复为 False(因为它用于 tf.layers.batch_normalization)。
I am aware that setting the is_training placeholder to False will fix this but assuming I just have the frozen graph file and checkpoint, how would I go about doing this?我知道将 is_training 占位符设置为 False 会解决这个问题,但假设我只有冻结的图形文件和检查点,我将如何去做? or is that not possible?
或者这是不可能的?
You can do that by just loading the frozen graph, mapping the value in question to a constant and saving the graph again.您可以通过加载冻结图、将相关值映射到常量并再次保存图来完成此操作。
import tensorflow as tf
with tf.Graph().as_default():
# Make constant False value (name does not need to match)
is_training = tf.constant(False, dtype=tf.bool, name="input/is_training")
# Load frozen graph
gd = tf.GraphDef()
with open(f"{save_graph_path}/graph.pbtxt", "r") as f:
gd.ParseFromString(f.read())
# Load graph mapping placeholder to constant
tf.import_graph_def(gd, name="", input_map={"input/is_training:0": is_training})
# Save graph again
tf.train.write_graph(tf.get_default_graph(), save_graph_path, "graph_modified.pbtxt",
as_text=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.