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