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

yipeiwu_com5年前Python基础

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())

以上这篇tensorflow ckpt模型和pb模型获取节点名称,及ckpt转pb模型实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python之指数与E记法的区别详解

不要把自乘得到幂(也称为求幂)和E记法弄混了 3**5表示3的5次幂,也就是3*3*3*3*3,等于243 3e5表示3乘以10的5次幂,也就是3*10*10*10*10*10,结果等于...

从CentOS安装完成到生成词云python的实例

从CentOS安装完成到生成词云python的实例

前言 人生苦短,我用python。学习python怎么能不搞一下词云呢是不是(ง •̀_•́)ง 于是便有了这篇边实践...

Python全栈之列表数据类型详解

前言 列表(list)同字符串一样都是有序的,因为他们都可以通过切片和索引进行数据访问,且列表是可变的。 创建列表的几种方法 第一种 name_list = ['Python',...

python实现数据写入excel表格

本文实例为大家分享了python数据写入excel表格的具体代码,供大家参考,具体内容如下 安装: xlsxwriter第三方库 code: #!/usr/bin/env/pytho...

python解释器spython使用及原理解析

python解释器spython使用及原理解析

简介 出于个人爱好和某种需求,我再16年对python的解释器产生了浓厚兴趣,并且下定决心重新实现一个版本。我个人再游戏服务器开发中,对c++嵌入lua和python都有着丰富应用经验...