tensorflow ckpt模型和pb模型获取节点名称,及ckpt转pb模型实例

作者:三寸光阴___ 时间:2021-05-23 10:22:53 

ckpt


from tensorflow.python import pywrap_tensorflow
checkpoint_path = 'model.ckpt-8000'
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
for key in var_to_shape_map:
print("tensor_name: ", key)

pb


import tensorflow as tf
import os

model_name = './mobilenet_v2_140_inf_graph.pb'

def create_graph():
with tf.gfile.FastGFile(model_name, 'rb') as f:
 graph_def = tf.GraphDef()
 graph_def.ParseFromString(f.read())
 tf.import_graph_def(graph_def, name='')

create_graph()
tensor_name_list = [tensor.name for tensor in tf.get_default_graph().as_graph_def().node]
for tensor_name in tensor_name_list:
print(tensor_name,'\n')

ckpt转pb


def freeze_graph(input_checkpoint,output_graph):
'''
:param input_checkpoint:
:param output_graph: PB模型保存路径
:return:
'''
output_node_names = "xxx"
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 = 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:
  f.write(output_graph_def.SerializeToString())
 print("%d ops in the final graph." % len(output_graph_def.node))

for op in graph.get_operations():
  print(op.name, op.values())

来源:https://blog.csdn.net/qq_38109843/article/details/88841306

标签:tensorflow,节点名称,ckpt,pb
0
投稿

猜你喜欢

  • Asp操作Xml的精炼类,含示例代码

    2011-02-28 11:11:00
  • MySQL中隐藏空间问题浅析

    2009-11-24 09:04:00
  • php 常用算法和时间复杂度

    2023-11-05 10:30:49
  • Go语言转换所有字符串为大写或者小写的方法

    2023-06-21 19:48:07
  • 百度百科的图片轮播代码

    2009-05-06 12:58:00
  • JS页内查找关键词的高亮显示

    2013-07-18 21:13:54
  • mysql 存储过程 使用小结

    2010-10-25 20:02:00
  • python使用JSON模块进行数据处理(编码解码)

    2024-01-01 21:52:42
  • Golang利用casbin实现权限验证详解

    2023-08-06 23:18:45
  • 使用jQuery简化Ajax开发

    2010-04-11 21:09:00
  • 使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子

    2023-08-26 23:12:32
  • asp是什么格式 asp文件用什么打开

    2020-06-30 16:04:48
  • Python时间和字符串转换操作实例分析

    2023-04-15 22:58:08
  • 使用python脚本自动生成K8S-YAML的方法示例

    2023-09-19 06:12:17
  • Dreamweaver使用快技法十三则总结

    2008-05-01 17:32:00
  • MySQL最新漏洞分析

    2012-07-11 15:41:10
  • 网站防止采集方法全攻略

    2007-09-05 19:57:00
  • SQL Server 数据页缓冲区的内存瓶颈分析

    2012-08-21 10:49:11
  • 用ASP打开远端MDB数据库

    2007-10-13 06:56:00
  • 如何在ASP里面创建GUID

    2008-01-08 19:13:00
  • asp之家 网络编程 m.aspxhome.com