新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
keras特征圖可視化的示例分析-創(chuàng)新互聯(lián)
這篇文章將為大家詳細講解有關(guān)keras特征圖可視化的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)長期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為南安企業(yè)提供專業(yè)的網(wǎng)站設(shè)計制作、成都做網(wǎng)站,南安網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。使用的比較簡單的一個模型:
def simple_cnn(): input_data = Input(shape=(28, 28, 1)) x = Conv2D(64, kernel_size=3, padding='same', activation='relu', name='conv1')(input_data) x = MaxPooling2D(pool_size=2, strides=2, name='maxpool1')(x) x = Conv2D(32, kernel_size=3, padding='same', activation='relu', name='conv2')(x) x = MaxPooling2D(pool_size=2, strides=2, name='maxpool2')(x) x = Dropout(0.25)(x) # 獲得最后一層卷積層的輸出 # 添加自己的全連接 x = Flatten(name='flatten')(x) x = Dense(128, activation='relu', name='fc1')(x) x = Dropout(0.25)(x) x = Dense(10, activation='softmax', name='fc2')(x) model = Model(inputs=input_data, outputs=x)
此模型已經(jīng)訓(xùn)練好了,跑了10個epoch,驗證集0.33
這里的效果還是很好的,┓( ´?` )┏
下面在網(wǎng)上搞了張手寫數(shù)字
使用網(wǎng)絡(luò)進行預(yù)測,這里就先給出如何可視化第一層的卷積層的輸出吧,哇哈哈
代碼:
input_data = Input(shape=(28, 28, 1)) x = Conv2D(64, kernel_size=3, padding='same', activation='relu', name='conv1')(input_data) x = MaxPooling2D(pool_size=2, strides=2, name='maxpool1')(x) x = Conv2D(32, kernel_size=3, padding='same', activation='relu', name='conv2')(x) x = MaxPooling2D(pool_size=2, strides=2, name='maxpool2')(x) x = Dropout(0.25)(x) x = Flatten(name='flatten')(x) x = Dense(128, activation='relu', name='fc1')(x) x = Dropout(0.25)(x) x = Dense(10, activation='softmax', name='fc2')(x) model = Model(inputs=input_data, outputs=x) model.load_weights('final_model_mnist_2019_1_28.h6') raw_img = cv2.imread('test.png') test_img = load_img('test.png', color_mode='grayscale', target_size=(28, 28)) test_img = np.array(test_img) test_img = np.expand_dims(test_img, axis=0) test_img = np.expand_dims(test_img, axis=3) conv1_layer = Model(inputs=input_data, outputs=model.get_layer(index=1).output) conv1_output = conv1_layer.predict(test_img) for i in range(64): show_img = conv1_output[:, :, :, i] print(show_img.shape) show_img.shape = [28,28] cv2.imshow('img', show_img) cv2.waitKey(0)
核心方法就是通過加載模型后,新建Model,將輸出部分換為你想要查看的網(wǎng)絡(luò)層數(shù)即可,當(dāng)然get_layer()包括了name和index兩個參數(shù)。最后通過遍歷當(dāng)前卷積層的所有特征映射,將每一個都展示出來。就可以了。
關(guān)于“keras特征圖可視化的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
文章題目:keras特征圖可視化的示例分析-創(chuàng)新互聯(lián)
路徑分享:http://fisionsoft.com.cn/article/cedsde.html