如何将tf模型 checkpoint保存的文件转存为pb单文件

def freeze_graph(input_checkpoint, output_graph):
    '''
    :param input_checkpoint: xxx.ckpt(千万不要加后面的xxx.ckpt.data这种,到ckpt就行了!)
    :param output_graph: PB模型保存路径
    :return:
    '''
    # checkpoint = tf.train.get_checkpoint_state(model_folder) #检查目录下ckpt文件状态是否可用
    # input_checkpoint = checkpoint.model_checkpoint_path #得ckpt文件路径
    # 指定输出的节点名称,该节点名称必须是原模型中存在的节点
    #output_node_names = "rois/Reshape/shape" # 模型输入节点,根据情况自定义
    output_node_names = 'rpn_bbox_pred/Shape,rpn_cls_prob'
    #clearsess
    #if sess 中有模型
    #saver = tf.train.Saver()
    #else:
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=True)
    
    graph = tf.get_default_graph() # 获得默认的图
    input_graph_def = graph.as_graph_def()  # 返回一个序列化的图代表当前的图

    with tf.Session() as sess:
        saver.restore(sess, input_checkpoint) # 恢复图并得到数据
        output_graph_def = tf.graph_util.convert_variables_to_constants(  # 模型持久化,将变量值固定
            sess=sess,
            input_graph_def=input_graph_def,# 等于:sess.graph_def
            output_node_names=output_node_names.split(","))# 如果有多个输出节点,以逗号隔开

        #with tf.gfile.GFile(output_graph, "wb") as f: #保存模型
        with tf.io.gfile.GFile(output_graph, "wb") as f: #保存模型            
            f.write(output_graph_def.SerializeToString()) #序列化输出
        print("%d ops in the final graph." % len(output_graph_def.node)) #得到当前图有几个操作节点
# ————————————————
# 版权声明:本文为CSDN博主「sooner高」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
# 原文链接:https://blog.csdn.net/g11d111/article/details/84136404



def freeze_sess_graph(sess,output_graph):
    '''
    :param output_graph: PB模型保存路径,将当前sess中的模型保存为pb
    :return:
    '''
    output_node_names = 'rpn_bbox_pred/Shape,rpn_cls_prob'#根据模型输出填写,
    #site-packages/tensorflow_core/python/framework/graph_util_impl.py
    #中的197:_assert_nodes_are_present(name_to_node, dest_nodes) #name_to_node
    #clearsess
    #if sess 中有模型
    #saver = tf.train.Saver()
    #else:
    #saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=True)
    
    graph = tf.get_default_graph() # 获得默认的图
    input_graph_def = graph.as_graph_def()  # 返回一个序列化的图代表当前的图

    #with tf.Session() as sess:
    if 1:
        output_graph_def = tf.graph_util.convert_variables_to_constants(  # 模型持久化,将变量值固定
            sess=sess,
            input_graph_def=input_graph_def,# 等于:sess.graph_def
            output_node_names=output_node_names.split(","))# 如果有多个输出节点,以逗号隔开

        #with tf.gfile.GFile(output_graph, "wb") as f: #保存模型
        with tf.io.gfile.GFile(output_graph, "wb") as f: #保存模型            
            f.write(output_graph_def.SerializeToString()) #序列化输出
        print("%d ops in the final graph." % len(output_graph_def.node)) #得到当前图有几个操作节点
# ————————————————

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值