亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

在keras中獲取某一層上的feature map實(shí)例

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

在深度學(xué)習(xí)中,如果我們想獲得某一個(gè)層上的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è)置輸入和輸出為:原始的輸入和該層對(duì)應(yīng)的輸出,然后使用predict函數(shù)得到對(duì)應(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è)置隨機(jī)數(shù)(或者固定的數(shù)字)來獲取某一層的結(jié)果

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

最后對(duì)該層的feature進(jìn)行顯示,我們使用Pillow庫(kù)

# 把圖像轉(zhuǎn)換成image可以表示的方式進(jìn)行顯示
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實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論