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代理工具mitmproxy使用指南

python代理工具mitmproxy使用指南

前言 mitmproxy 是 man-in-the-middle proxy 的简称,译为中间人代理工具,可以用来拦截、修改、保存 HTTP/HTTPS 请求。以命令行终端形式呈现,操作...

Python2与Python3的区别点整理

python解释器默认编码(python2与python3的区别一) python2 解释器默认编码:ascii python3 解释器默认编码:utf-8 输入(python2与p...

python 实时遍历日志文件

open 遍历一个大日志文件 使用 readlines() 还是 readline() ? 总体上 readlines() 不慢于python 一次次调用 readline(...

Java实现的执行python脚本工具类示例【使用jython.jar】

本文实例讲述了Java实现的执行python脚本工具类。分享给大家供大家参考,具体如下: 这里java中执行python脚本工具类,需要使用jython.jar java中执行pytho...

基于Python的关键字监控及告警

为了解决日志文件监控的问题, 使用python脚本完成了基于关键字的告警功能 环境 python 2.7 依赖包 time \ traceback \ filelock \ loggin...