TensorFlow获取加载模型中的全部张量名称代码


Posted in Python onFebruary 11, 2020

核心代码如下:

[tensor.name for tensor in tf.get_default_graph().as_graph_def().node]

实例代码:(加载了Inceptino_v3的模型,并获取该模型所有节点的名称)

# -*- coding: utf-8 -*-
 
import tensorflow as tf
import os
 
model_dir = 'C:/Inception_v3'
model_name = 'output_graph.pb'
 
# 读取并创建一个图graph来存放训练好的 Inception_v3模型(函数)
def create_graph():
 with tf.gfile.FastGFile(os.path.join(
   model_dir, model_name), 'rb') as f:
  # 使用tf.GraphDef()定义一个空的Graph
  graph_def = tf.GraphDef()
  graph_def.ParseFromString(f.read())
  # Imports the graph from graph_def into the current default Graph.
  tf.import_graph_def(graph_def, name='')
 
# 创建graph
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')

输出结果:

mixed_8/tower/conv_1/batchnorm/moving_variance 

mixed_8/tower/conv_1/batchnorm 

r_1/mixed/conv_1/batchnorm 

.

.

.

mixed_10/tower_1/mixed/conv_1/CheckNumerics 

mixed_10/tower_1/mixed/conv_1/control_dependency 

mixed_10/tower_1/mixed/conv_1 

pool_3 

pool_3/_reshape/shape 

pool_3/_reshape 

input/BottleneckInputPlaceholder 
.
.
.
.
final_training_ops/weights/final_weights 

final_training_ops/weights/final_weights/read 

final_training_ops/biases/final_biases 

final_training_ops/biases/final_biases/read 

final_training_ops/Wx_plus_b/MatMul 

final_training_ops/Wx_plus_b/add 

final_result

由于结果太长了,就省略了一些。

如果不想这样print输出也可以将其写入txt 查看。

写入txt代码如下:

tensor_name_list = [tensor.name for tensor in tf.get_default_graph().as_graph_def().node]
 
txt_path = './txt/节点名称'
full_path = txt_path+ '.txt'
 
for tensor_name in tensor_name_list:
 name = tensor_name + '\n'
 file = open(full_path,'a+')
file.write(name)
file.close()

以上这篇TensorFlow获取加载模型中的全部张量名称代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python字符串替换示例
Apr 24 Python
Tornado服务器中绑定域名、虚拟主机的方法
Aug 22 Python
python itchat实现微信好友头像拼接图的示例代码
Aug 14 Python
Python网络爬虫神器PyQuery的基本使用教程
Feb 03 Python
python获取磁盘号下盘符步骤详解
Jun 19 Python
python找出因数与质因数的方法
Jul 25 Python
python装饰器代替set get方法实例
Dec 19 Python
python每5分钟从kafka中提取数据的例子
Dec 23 Python
Pytoch之torchvision.transforms图像变换实例
Dec 30 Python
python使用正则表达式(Regular Expression)方法超详细
Dec 30 Python
浅谈Python访问MySQL的正确姿势
Jan 07 Python
分享PyCharm最新激活码(真永久激活方法)不用每月找安装参数或最新激活码了
Dec 27 Python
tensorflow 获取checkpoint中的变量列表实例
Feb 11 #Python
python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解
Feb 11 #Python
python 函数中的参数类型
Feb 11 #Python
python正则过滤字母、中文、数字及特殊字符方法详解
Feb 11 #Python
python3正则模块re的使用方法详解
Feb 11 #Python
Python版中国省市经纬度
Feb 11 #Python
Python终端输出彩色字符方法详解
Feb 11 #Python
You might like
PHP命名空间(Namespace)简明教程
2014/06/11 PHP
Laravel框架实现的上传图片到七牛功能详解
2019/09/06 PHP
飞鱼(shqlsl) javascript作品集
2006/12/16 Javascript
js玩一玩WSH吧
2007/02/23 Javascript
Cookie 注入是怎样产生的
2009/04/08 Javascript
JavaScript 语法集锦 脚本之家基础推荐
2009/11/15 Javascript
jquery禁用右键示例
2014/04/28 Javascript
js获取内联样式的方法
2015/01/27 Javascript
浅谈JavaScript 数据属性和访问器属性
2016/09/01 Javascript
JavaScript定义函数_动力节点Java学院整理
2017/06/27 Javascript
ui-router中使用ocLazyLoad和resolve的具体方法
2017/10/18 Javascript
layui实现文件或图片上传记录
2018/08/28 Javascript
通过js实现压缩图片上传功能
2020/02/25 Javascript
利用webpack理解CommonJS和ES Modules的差异区别
2020/06/16 Javascript
python进阶教程之函数对象(函数也是对象)
2014/08/30 Python
python 计算数组中每个数字出现多少次--“Bucket”桶的思想
2017/12/19 Python
python字符串与url编码的转换实例
2018/05/10 Python
Python做智能家居温湿度报警系统
2018/09/25 Python
使用Python进行目录的对比方法
2018/11/01 Python
python3.6 tkinter实现屏保小程序
2019/07/30 Python
ubuntu上安装python的实例方法
2019/09/30 Python
python画蝴蝶曲线图的实例
2019/11/21 Python
django formset实现数据表的批量操作的示例代码
2019/12/06 Python
使用Tkinter制作信息提示框
2020/02/18 Python
Python tcp传输代码实例解析
2020/03/18 Python
pandas DataFrame 数据选取,修改,切片的实现
2020/04/24 Python
DRF框架API版本管理实现方法解析
2020/08/21 Python
Python的信号库Blinker用法详解
2020/12/31 Python
详解Canvas 实现炫丽的粒子运动效果(粒子生成文字)
2018/02/01 HTML / CSS
Chemist Warehouse官方海外旗舰店:澳洲第一连锁大药房
2017/08/25 全球购物
德国综合购物网站:OTTO
2018/11/13 全球购物
三星新西兰官网:Samsung新西兰
2019/03/05 全球购物
党风廉正建设个人工作总结
2015/03/06 职场文书
企业员工辞职信范文
2015/05/12 职场文书
单位收入证明范本
2015/06/18 职场文书
医院感染管理制度
2015/08/05 职场文书