TensorFlow實現(xiàn)打印每一層的輸出
在test.py中可以通過如下代碼直接生成帶weight的pb文件,也可以通過tf官方的freeze_graph.py將ckpt轉(zhuǎn)為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函數(shù)可以獲取每一層的輸出
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實現(xiàn)打印每一層的輸出就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python用requests模塊實現(xiàn)動態(tài)網(wǎng)頁爬蟲
大家好,本篇文章主要講的是Python用requests模塊實現(xiàn)動態(tài)網(wǎng)頁爬蟲,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02
淺談Python使用pickle模塊序列化數(shù)據(jù)優(yōu)化代碼的方法
這篇文章主要介紹了淺談Python使用pickle模塊序列化數(shù)據(jù)優(yōu)化代碼的方法,pickle模塊可以對多種Python對象進(jìn)行序列化和反序列化,序列化稱為pickling,反序列化稱為unpickling,需要的朋友可以參考下2023-07-07
python異步編程之a(chǎn)syncio低階API的使用詳解
asyncio中低階API的種類很多,涉及到開發(fā)的5個方面,這篇文章主要為大家詳細(xì)介紹了這些低階API的具體使用,感興趣的小伙伴可以學(xué)習(xí)一下2024-01-01
python自動統(tǒng)計zabbix系統(tǒng)監(jiān)控覆蓋率的示例代碼
這篇文章主要介紹了python自動統(tǒng)計zabbix系統(tǒng)監(jiān)控覆蓋率的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

