简体   繁体   English

在导出为 tflite 格式之前修复冻结图的输入节点

[英]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.

相关问题 无法将冻结推理图转换为 .tflite - Can't convert Frozen Inference Graph to .tflite 如何将对象检测模型(在其冻结图形中)转换为.tflite,而不需要任何输入和输出数组的知识 - How to convert an object detection model, in it's frozen graph, to a .tflite, without any knowledge of input and output arrays 使用TFLiteConverter(Python API)将冻结的图形转换为用于珊瑚的tflite - Convert frozen graph to tflite for Coral using TFLiteConverter (Python API) 无法将使用 Docker 创建的冻结 Inception graph.pb 转换为 .tflite - Can't convert frozen Inception graph .pb created with Docker to .tflite 无法将自定义训练的冻结模型转换为tflite格式 - Unable to convert custom trained frozen model into tflite format 无法从tf.keras模型 - >量化的冻结图 - > .tflite与TOCO - Unable to go from tf.keras model -> quantized frozen graph -> .tflite with TOCO 将 keras 模型导出到 tflite - Exporting keras model into tflite 可视化 TFLite 图并获取特定节点的中间值? - Visualize TFLite graph and get intermediate values of a particular node? 使用MultiIndexing修复导出到Excel - Fixing Exporting to Excel with MultiIndexing 将冻结值从冻结图复制到另一个冻结图 - Copy Frozen Values From A Frozen Graph to Another Frozen Graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM