keras得到每層的系數(shù)方式
使用keras搭建好一個模型,訓(xùn)練好,怎么得到每層的系數(shù)呢:
weights = np.array(model.get_weights()) print(weights) print(weights[0].shape) print(weights[1].shape)
這樣系數(shù)就被存放到一個np中了。
補充知識:使用keras框架編寫的深度模型 輸出及每一層的特征可視化
使用訓(xùn)練好的模型進行預(yù)測的時候,為分析效果,通常需要對特征提取過程中的特征映射做可視化操作
本文以keras為例,對特征可視化操作進行詳解。
一、首先,對模型的最后輸出層進行特征可視化
from keras import models
#使用matlpotlib模塊進行繪圖的操作
import matplotlib.pylot as plt
#images是一個batch的輸入圖像,batch_input[batch圖像數(shù)量,尺寸高,尺寸寬,3(rgb通道數(shù)量)]
#model是訓(xùn)練好的模型
#model = load_model('path')
nb_images = len(images)
batch_input = np.zeros((nb_images, net_h, net_w, 3))
# preprocess the input
for i in range(nb_images):
batch_input[i] = preprocess_input(images[i], net_h, net_w)
# run the prediction
#batch_output為一個樣本的所有通道輸出特征映射,本文應(yīng)用特征金字塔結(jié)構(gòu),有三個維度的特征提取層
#batch_output[0]是第一個維度的特征提取層所有通道的輸出特征映射,四維,本文例子中為[1, 52, 52, 72]
#[一個樣本,尺寸,尺寸,通道數(shù)]
#也可以是batch_output = model.predict(batch_input)
batch_output = model.predict_on_batch(batch_input)
batch_boxes = [None]*nb_images
print(batch_output[0].shape)
#display feature map
#下面為歸一化到0-255空間內(nèi)
xx = batch_output[0]
max = np.max(xx)
print(max,"max value is :")
X_output = X_output .astype("float32") / max * 255
#下面的30為第30個通道
X_output = xx[0,:,:,30]
#使用matplotlib顯示圖像
plt.figure()
plt.imshow(X_output, cmap='viridis')
plt.show()
#輸出結(jié)果
原始圖像

輸出層的特征可視化

二、可視化某一層的特征映射
from keras import backend as k
from keras import models
import matplotlib.pylot as plt
model = load_model('...')
layer_1 =k.function([model.layers[0].input], [model.layers[1].output])
#第2個model,layers[]改成你想顯示的層數(shù)
f1 = layer_1[input_image][0]
f1.image = f1[0,:,:,channel]
plt,matshow(f1.image, camp='viridis')
plt.show()
示例:
from keras import models
import matplotlib.pylot as plt
from keras import backend as k
#images是一個batch的輸入圖像,batch_input[batch圖像數(shù)量,尺寸高,尺寸寬,3(rgb通道數(shù)量)]
#model是訓(xùn)練好的模型
#model = load_model('path')
nb_images = len(images)
batch_input = np.zeros((nb_images, net_h, net_w, 3))
# preprocess the input
for i in range(nb_images):
batch_input[i] = preprocess_input(images[i], net_h, net_w)
#display feature map
#可視化第一層的特征映射
layer_1 = K.function([model.layers[0].input], [model.layers[1].output])
f1 = layer_1([batch_input])[0]
print(f1.shape)
max = np.max(f1)
f1 =f1.astype("float32") / max * 255
plt.figure()
#顯示第一層網(wǎng)絡(luò)前5個通道的特征映射
for i in range(5):
plt.subplot(2, 3, i+1)
plt.imshow(f1[0,:,:,i], cmap='viridis')
plt.show()
輸出結(jié)果:

以上這篇keras得到每層的系數(shù)方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JupyterNotebook 如何調(diào)整輸出窗口的顯示效果
這篇文章主要介紹了JupyterNotebook 輸出窗口的顯示效果調(diào)整實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
linux之文件查找指定文件中包含關(guān)鍵字的行信息方式
這篇文章主要介紹了linux之文件查找指定文件中包含關(guān)鍵字的行信息方式,具有很好的參考價值,希望對大家有所幫助。2023-06-06
Django配置Bootstrap, js實現(xiàn)過程詳解
這篇文章主要介紹了Django配置Bootstrap, js實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
Python中實現(xiàn)優(yōu)雅的switch操作的方法小結(jié)
這篇文章主要為大家詳細介紹了如何在Python中優(yōu)雅地實現(xiàn)?switch?操作,并提供豐富的示例代碼,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02

