美文网首页
转换tensorflow的模型到openvino

转换tensorflow的模型到openvino

作者: InnoTech | 来源:发表于2020-08-24 20:09 被阅读0次

感觉openvino更新的比较缓慢,tensorflow的好多新的模型在转成openvino的IR模型,转换失败。
查了很多资料之后 发现生成的tensorflow模型,接着转成v1的,然后再用openvino的optimizer转换成IR模型

转化成v1的方式如下:

import tensorflow as tf
import tensorflow.compat.v1 as tf1
tf1.reset_default_graph()
tf1.keras.backend.set_learning_phase(0) #调用模型前一定要执行该命令
tf1.disable_v2_behavior() #禁止tensorflow2.0的行为

#https://blog.csdn.net/u011119817/article/details/103264080
#https://github.com/openvinotoolkit/openvino/issues/294


saved_pb_model = tf.keras.models.load_model("./face-mask.h5")
def freeze_session(session,keep_var_names=None,output_names=None,clear_devices=True):
    graph = session.graph
    with graph.as_default():
#         freeze_var_names = list(set(v.op.name for v in tf1.global_variables()).difference(keep_var_names or []))
        output_names = output_names or []
#         output_names += [v.op.name for v in tf1.global_variables()]
        print("output_names",output_names)
        input_graph_def = graph.as_graph_def()
#         for node in input_graph_def.node:
#             print('node:', node.name)
        print("len node1",len(input_graph_def.node))
        if clear_devices:
            for node in input_graph_def.node:
                node.device = ""
        frozen_graph =  tf1.graph_util.convert_variables_to_constants(session, input_graph_def,
                                                      output_names)
        
        outgraph = tf1.graph_util.remove_training_nodes(frozen_graph)#云掉与推理无关的内容
        print("##################################################################")
        for node in outgraph.node:
            print('node:', node.name)
        print("len node1",len(outgraph.node))
        return outgraph

frozen_graph = freeze_session(tf1.keras.backend.get_session(),output_names=[out.op.name for out in saved_pb_model.outputs])
tf1.train.write_graph(frozen_graph, './', "face-mask.pb", as_text=False)



# # Convert pb model to IR model using BASH



相关文章

网友评论

      本文标题:转换tensorflow的模型到openvino

      本文链接:https://www.haomeiwen.com/subject/bnqojktx.html