首先透過load_model將模型載入後,用model.layers取得模型的層數.

在將每一層的特徵圖逐一進行顯示,程式碼如下:

from keras import backend as K
from keras.models import load_model
import matplotlib.pyplot as plt

model = load_model('real_model_path')
def show_feature_map(idx,X):
        layer = K.function([model.layers[0].input], [model.layers[idx].output])
        f1 = layer([X])[0]

        for _ in range(f1.shape[3]):
            show_img = f1[0, :, :, _]
            title_str = "layer " + str(idx) + ", map " + str(_) + "shape="+str(show_img.shape[0]) +"x"+ str(show_img.shape[1])
            print "layer ",idx,show_img.shape

            plt.title(title_str)
            plt.imshow(show_img, cmap='gray')

for i in range(1,len(model.layers)):
    show_feature_map(idx=i,X=imageToUse)

 

程式內的 f1.shape[3] 中的3為資料的維度值(當輸入資料為2D的資料時,則寫成 f1.shape[2]),程式中的 show_img = f1[0, :, :, _] 依照實際輸入的維度進行調整。

 

 

arrow
arrow
    文章標籤
    tensorflow keras feature-map
    全站熱搜

    Lung-Yu,Tsai 發表在 痞客邦 留言(0) 人氣()