欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在keras中獲取某一層上的feature map實例

 更新時間:2020年01月24日 10:55:43   作者:今天好好吃飯了嗎  
今天小編就為大家分享一篇在keras中獲取某一層上的feature map實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在深度學(xué)習(xí)中,如果我們想獲得某一個層上的feature map,就像下面的圖這樣,怎么做呢?

我們的代碼是使用keras寫的VGG16網(wǎng)絡(luò),網(wǎng)絡(luò)結(jié)構(gòu)如圖:

那么我們隨便抽取一層的數(shù)據(jù)吧,就拿第四層的pooling以后的結(jié)果作為輸出吧,參考上面的網(wǎng)絡(luò)結(jié)構(gòu),得到的結(jié)果維度應(yīng)該是[1,56,56,128]的尺度。

怎么做呢?

首先通過keras構(gòu)建模型:

model = VGG16(include_top=True, weights='imagenet')

然后設(shè)置輸入和輸出為:原始的輸入和該層對應(yīng)的輸出,然后使用predict函數(shù)得到對應(yīng)的結(jié)果

dense_result = Model(inputs=model.input,outputs=model.get_layer("block2_pool").output) 
dense_res = dense_result.predict(x)#使用predict得到該層結(jié)果

設(shè)置隨機數(shù)(或者固定的數(shù)字)來獲取某一層的結(jié)果

rand_layer = random.randint(10,128)
x_output = dense_res[0,:,:,rand_layer] #獲取某一層的數(shù)據(jù):因為原始數(shù)據(jù)維度是[1,x,x,depths]的,我們僅僅提取某一個depth對應(yīng)的[x,x]維度的信息
# 獲取最大值,然后對該層數(shù)據(jù)進行歸一化之后投影到0-255之間
max = np.max(x_output)
print(max,"max value is :")
# 然后進行歸一化操作
x_output =x_output.astype("float32") / max * 255
print(x_output.shape)

最后對該層的feature進行顯示,我們使用Pillow庫

# 把圖像轉(zhuǎn)換成image可以表示的方式進行顯示
from PIL import Image as PILImage
x_output =PILImage.fromarray(np.asarray(x_output)) 
x_output1 = x_output.resize((400,400)) 
x_output1.show() 
print(np.asarray(x_output1))

結(jié)果如上圖所示啦~

以上這篇在keras中獲取某一層上的feature map實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論