TensorFlow实现打印每一层的输出


Posted in Python onJanuary 21, 2020

在test.py中可以通过如下代码直接生成带weight的pb文件,也可以通过tf官方的freeze_graph.py将ckpt转为pb文件。

constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def,['net_loss/inference/encode/conv_output/conv_output'])
with tf.gfile.FastGFile('net_model.pb', mode='wb') as f:
  f.write(constant_graph.SerializeToString())

tf1.0中通过带weight的pb文件与get_tensor_by_name函数可以获取每一层的输出

import os
import os.path as ops
import argparse
import time
import math
 
import tensorflow as tf
import glob
import numpy as np
import matplotlib.pyplot as plt
import cv2
 
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
 
gragh_path = './model.pb'
image_path = './lvds1901.JPG'
inputtensorname = 'input_tensor:0'
tensorname = 'loss/inference/encode/resize_images/ResizeBilinear'
filepath='./net_output.txt'
HEIGHT=256
WIDTH=256
VGG_MEAN = [103.939, 116.779, 123.68]
 
with tf.Graph().as_default():
  graph_def = tf.GraphDef()
  with tf.gfile.GFile(gragh_path, 'rb') as fid:
    serialized_graph = fid.read()
    graph_def.ParseFromString(serialized_graph)
 
    tf.import_graph_def(graph_def, name='')
 
    image = cv2.imread(image_path)
    image = cv2.resize(image, (WIDTH, HEIGHT), interpolation=cv2.INTER_CUBIC)
    image_np = np.array(image)
    image_np = image_np - VGG_MEAN
    image_np_expanded = np.expand_dims(image_np, axis=0)
 
    with tf.Session() as sess:
      ops = tf.get_default_graph().get_operations()
      tensor_name = tensorname + ':0'
      tensor_dict = tf.get_default_graph().get_tensor_by_name(tensor_name)
      image_tensor = tf.get_default_graph().get_tensor_by_name(inputtensorname)
      output = sess.run(tensor_dict, feed_dict={image_tensor: image_np_expanded})
      
      ftxt = open(filepath,'w')
      transform = output.transpose(0, 3, 1, 2)
      transform = transform.flatten()
      weight_count = 0
      for i in transform:
        if weight_count % 10 == 0 and weight_count != 0:
          ftxt.write('\n')
        ftxt.write(str(i) + ',')
        weight_count += 1
      ftxt.close()

以上这篇TensorFlow实现打印每一层的输出就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python splitlines使用技巧
Sep 06 Python
Python的Flask站点中集成xhEditor文本编辑器的教程
Jun 13 Python
Python 编码处理-str与Unicode的区别
Sep 06 Python
浅谈numpy中linspace的用法 (等差数列创建函数)
Jun 07 Python
Python中max函数用于二维列表的实例
Apr 03 Python
浅述python中深浅拷贝原理
Sep 18 Python
python 列表递归求和、计数、求最大元素的实例
Nov 28 Python
Python设计模式之桥接模式原理与用法实例分析
Jan 10 Python
python GUI库图形界面开发之PyQt5单行文本框控件QLineEdit详细使用方法与实例
Feb 27 Python
pandas 强制类型转换 df.astype实例
Apr 09 Python
浅谈优化Django ORM中的性能问题
Jul 09 Python
python基础之文件处理知识总结
May 23 Python
NumPy排序的实现
Jan 21 #Python
tensorflow实现在函数中用tf.Print输出中间值
Jan 21 #Python
Python实现随机生成任意数量车牌号
Jan 21 #Python
tensorflow模型继续训练 fineturn实例
Jan 21 #Python
tensorflow ckpt模型和pb模型获取节点名称,及ckpt转pb模型实例
Jan 21 #Python
tensorflow查看ckpt各节点名称实例
Jan 21 #Python
python同义词替换的实现(jieba分词)
Jan 21 #Python
You might like
FleaPHP的安全设置方法
2008/09/15 PHP
攻克CakePHP系列二 表单数据显示
2008/10/22 PHP
PHP XML数据解析代码
2010/05/26 PHP
PHP开发不能违背的安全规则 过滤用户输入
2011/05/01 PHP
PHP简洁函数(PHP简单明了函数语法)
2012/06/10 PHP
探讨php中防止SQL注入最好的方法是什么
2013/06/10 PHP
Laravel 4 初级教程之安装及入门
2014/10/30 PHP
PHP调用Mailgun发送邮件的方法
2017/05/04 PHP
PHP框架自动加载类文件原理详解
2017/06/06 PHP
php处理多图上传压缩代码功能
2018/06/13 PHP
PHP安装BCMath扩展的方法
2019/02/13 PHP
25个好玩的JavaScript小游戏分享
2011/04/22 Javascript
js中的this关键字详解
2013/09/25 Javascript
JS使用for循环遍历Table的所有单元格内容
2014/08/21 Javascript
js点击选择文本的方法
2015/02/09 Javascript
Express实现前端后端通信上传图片之存储数据库(mysql)傻瓜式教程(一)
2015/12/10 Javascript
javascript实现简单计算器效果【推荐】
2016/04/19 Javascript
AngularJS中的表单简单入门
2016/07/28 Javascript
JavaScript之Vue.js【入门基础】
2016/12/06 Javascript
vue.js指令v-model使用方法
2017/03/20 Javascript
js canvas实现适用于移动端的百分比仪表盘dashboard
2017/07/18 Javascript
在vue项目中,使用axios跨域处理
2018/03/07 Javascript
对layui中table组件工具栏的使用详解
2019/09/19 Javascript
初步解析Python下的多进程编程
2015/04/28 Python
学习python之编写简单乘法口诀表实现代码
2016/02/27 Python
详解使用pymysql在python中对mysql的增删改查操作(综合)
2017/01/18 Python
python-视频分帧&多帧合成视频实例
2019/12/10 Python
OpenCV哈里斯(Harris)角点检测的实现
2020/01/15 Python
在Matplotlib图中插入LaTex公式实例
2020/04/17 Python
python实现密码验证合格程序的思路详解
2020/06/01 Python
numpy的Fancy Indexing和array比较详解
2020/06/11 Python
安全目标管理责任书
2014/07/25 职场文书
带刀到教室的检讨书
2014/10/04 职场文书
表扬通报怎么写
2015/01/16 职场文书
第一军规观后感
2015/06/12 职场文书
iPhone13将有八大升级
2021/04/15 数码科技