• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > TensorFlow实现打印每一层的输出

TensorFlow实现打印每一层的输出

作者:Kluiverthoo 字体:[增加 减小] 来源:互联网

Kluiverthoo 通过本文主要向大家介绍了TensorFlow,打印,一层,输出等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

在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实现打印每一层的输出就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能想查找下面的文章:

  • tensorflow模型保存、加载之变量重命名实例
  • tensorflow模型保存、加载之变量重命名实例
  • 使用tensorflow DataSet实现高效加载变长文本输入
  • TensorFlow——Checkpoint为模型添加检查点的实例
  • TensorFlow实现打印每一层的输出
  • tensorflow ckpt模型和pb模型获取节点名称,及ckpt转pb模型实例
  • tensorflow estimator 使用hook实现finetune方式
  • tensorflow查看ckpt各节点名称实例
  • 浅谈tensorflow中Dataset图片的批量读取及维度的操作详解
  • 浅谈Tensorflow 动态双向RNN的输出问题

相关文章

  • python实现图片批量剪切示例
  • Pycharm学习教程(5) Python快捷键相关设置
  • Python对文件和目录进行操作的方法(file对象/os/os.path/shutil 模块)
  • 在Django中使用Sitemap的方法讲解
  • 详解Python中类的定义与使用
  • Python自定义主从分布式架构实例分析
  • Python中使用logging模块打印log日志详解
  • Python类的专用方法实例分析
  • pyqt4教程之实现windows窗口小示例分享
  • python使用点操作符访问字典(dict)数据的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python测试驱动开发实例
    • wxpython中利用线程防止假死的实现方法
    • python threading模块操作多线程介绍
    • python简单获取数组元素个数的方法
    • python中enumerate的用法实例解析
    • 在Django中使用Sitemap的方法讲解
    • python 多进程通信模块的简单实现
    • python发送邮件接收邮件示例分享
    • 通过实例浅析Python对比C语言的编程思想差异
    • Django框架下在URLconf中指定视图缓存的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有