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设计】。

相关文章

Pytorch中的variable, tensor与numpy相互转化的方法

Pytorch中的variable, tensor与numpy相互转化的方法

在使用pytorch作为深度学习的框架时,经常会遇到变量variable、张量tensor与矩阵numpy的类型的相互转化的问题,本章结合这实际图像对此转化方法进行实现。 1.加载需要用...

Python matplotlib生成图片背景透明的示例代码

Python matplotlib生成图片背景透明的示例代码

使用matplotlib生成图片,想要背景透明,而且图例部分也显示透明效果,找到了大概的设置方法,特此记录。 # coding=utf-8 # matplotlib背景透明示例图 #...

python 阶乘累加和的实例

阶乘:也是数学里的一种术语;阶乘指从1乘以2乘以3乘以4一直乘到所要求的数;在表达阶乘时,就使用“!”来表示。如h阶乘,就表示为h!;阶乘一般很难计算,因为积都很大。 提问:求1+2!+...

Python3中类、模块、错误与异常、文件的简易教程

Python3中类、模块、错误与异常、文件的简易教程

和旧版的Python 2.7教程相比,新增内容包括: 新增命名关键字参数的使用; 新增StringIO和BytesIO; 新增datetime的使用; 新增urllib...

python出现"IndentationError: unexpected indent"错误解决办法

python出现"IndentationError: unexpected indent"错误解决办法

python出现"IndentationError: unexpected indent"错误解决办法 Python是一种对缩进非常敏感的语言,最常见的情况是tab和空格的混用会导致错误...