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

yipeiwu_com4年前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实现贪吃蛇游戏

教你一步步利用python实现贪吃蛇游戏

0 引言 前几天,星球有人提到贪吃蛇,一下子就勾起了我的兴趣,毕竟在那个Nokia称霸的年代,这款游戏可是经典中的经典啊!而用Python(蛇)玩Snake(贪吃蛇),那再合适不过了&...

Python如何读取MySQL数据库表数据

Python如何读取MySQL数据库表数据

本文实例为大家分享了Python读取MySQL数据库表数据的具体代码,供大家参考,具体内容如下 环境:Python 3.6 ,Window 64bit 目的:从MySQL数据库读取目标表...

用python 实现在不确定行数情况下多行输入方法

如下所示: stopword = '' str = '' for line in iter(raw_input, stopword): str += line + '\n' pri...

Python程序设计入门(3)数组的使用

1、Python的数组可分为三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素。定义方式:arr = [元素] (2) Tuple 固定的数组,一旦定义后,其...

python删除列表中重复记录的方法

本文实例讲述了python删除列表中重复记录的方法。分享给大家供大家参考。具体实现方法如下: def removeListDuplicates(seq): seen = set(...