keras模型可視化,層可視化及kernel可視化實(shí)例
keras模型可視化:
model: model = Sequential() # input: 100x100 images with 3 channels -> (100, 100, 3) tensors. # this applies 32 convolution filters of size 3x3 each. model.add(ZeroPadding2D((1,1), input_shape=(38, 38, 1))) model.add(Conv2D(32, (3, 3), activation='relu', padding='same')) # model.add(Conv2D(32, (3, 3), activation='relu', padding='same')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), activation='relu', padding='same',)) # model.add(Conv2D(64, (3, 3), activation='relu', padding='same',)) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(128, (3, 3), activation='relu', padding='same',)) # model.add(Conv2D(128, (3, 3), activation='relu', padding='same',)) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(AveragePooling2D((5,5))) model.add(Flatten()) # model.add(Dense(512, activation='relu')) # model.add(Dropout(0.5)) model.add(Dense(label_size, activation='softmax'))
1.層可視化:
test_x = [] img_src = cv2.imdecode(np.fromfile(r'c:\temp.tif', dtype=np.uint8), cv2.IMREAD_GRAYSCALE) img = cv2.resize(img_src, (38, 38), interpolation=cv2.INTER_CUBIC) # img = np.random.randint(0,255,(38,38)) img = (255 - img) / 255 img = np.reshape(img, (38, 38, 1)) test_x.append(img) ################################################################### layer = model.layers[1] weight = layer.get_weights() # print(weight) print(np.asarray(weight).shape) model_v1 = Sequential() # input: 100x100 images with 3 channels -> (100, 100, 3) tensors. # this applies 32 convolution filters of size 3x3 each. model_v1.add(ZeroPadding2D((1, 1), input_shape=(38, 38, 1))) model_v1.add(Conv2D(32, (3, 3), activation='relu', padding='same')) # model.add(Conv2D(32, (3, 3), activation='relu', padding='same')) model_v1.layers[1].set_weights(weight) re = model_v1.predict(np.array(test_x)) print(np.shape(re)) re = np.transpose(re, (0,3,1,2)) for i in range(32): plt.subplot(4,8,i+1) plt.imshow(re[0][i]) #, cmap='gray' plt.show() ################################################################## model_v2 = Sequential() # input: 100x100 images with 3 channels -> (100, 100, 3) tensors. # this applies 32 convolution filters of size 3x3 each. model_v2.add(ZeroPadding2D((1, 1), input_shape=(38, 38, 1))) model_v2.add(Conv2D(32, (3, 3), activation='relu', padding='same')) # model.add(Conv2D(32, (3, 3), activation='relu', padding='same')) model_v2.add(BatchNormalization()) model_v2.add(MaxPooling2D(pool_size=(2, 2))) model_v2.add(Dropout(0.25)) model_v2.add(Conv2D(64, (3, 3), activation='relu', padding='same', )) print(len(model_v2.layers)) layer1 = model.layers[1] weight1 = layer1.get_weights() model_v2.layers[1].set_weights(weight1) layer5 = model.layers[5] weight5 = layer5.get_weights() model_v2.layers[5].set_weights(weight5) re2 = model_v2.predict(np.array(test_x)) re2 = np.transpose(re2, (0,3,1,2)) for i in range(64): plt.subplot(8,8,i+1) plt.imshow(re2[0][i]) #, cmap='gray' plt.show() ################################################################## model_v3 = Sequential() # input: 100x100 images with 3 channels -> (100, 100, 3) tensors. # this applies 32 convolution filters of size 3x3 each. model_v3.add(ZeroPadding2D((1, 1), input_shape=(38, 38, 1))) model_v3.add(Conv2D(32, (3, 3), activation='relu', padding='same')) # model.add(Conv2D(32, (3, 3), activation='relu', padding='same')) model_v3.add(BatchNormalization()) model_v3.add(MaxPooling2D(pool_size=(2, 2))) model_v3.add(Dropout(0.25)) model_v3.add(Conv2D(64, (3, 3), activation='relu', padding='same', )) # model.add(Conv2D(64, (3, 3), activation='relu', padding='same',)) model_v3.add(BatchNormalization()) model_v3.add(MaxPooling2D(pool_size=(2, 2))) model_v3.add(Dropout(0.25)) model_v3.add(Conv2D(128, (3, 3), activation='relu', padding='same', )) print(len(model_v3.layers)) layer1 = model.layers[1] weight1 = layer1.get_weights() model_v3.layers[1].set_weights(weight1) layer5 = model.layers[5] weight5 = layer5.get_weights() model_v3.layers[5].set_weights(weight5) layer9 = model.layers[9] weight9 = layer9.get_weights() model_v3.layers[9].set_weights(weight9) re3 = model_v3.predict(np.array(test_x)) re3 = np.transpose(re3, (0,3,1,2)) for i in range(121): plt.subplot(11,11,i+1) plt.imshow(re3[0][i]) #, cmap='gray' plt.show()
2.kernel可視化:
def process(x): res = np.clip(x, 0, 1) return res def dprocessed(x): res = np.zeros_like(x) res += 1 res[x < 0] = 0 res[x > 1] = 0 return res def deprocess_image(x): x -= x.mean() x /= (x.std() + 1e-5) x *= 0.1 x += 0.5 x = np.clip(x, 0, 1) x *= 255 x = np.clip(x, 0, 255).astype('uint8') return x for i_kernal in range(64): input_img=model.input loss = K.mean(model.layers[5].output[:, :,:,i_kernal]) # loss = K.mean(model.output[:, i_kernal]) # compute the gradient of the input picture wrt this loss grads = K.gradients(loss, input_img)[0] # normalization trick: we normalize the gradient grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5) # this function returns the loss and grads given the input picture iterate = K.function([input_img, K.learning_phase()], [loss, grads]) # we start from a gray image with some noise np.random.seed(0) num_channels=1 img_height=img_width=38 input_img_data = (255- np.random.randint(0,255,(1, img_height, img_width, num_channels))) / 255. failed = False # run gradient ascent print('####################################',i_kernal+1) loss_value_pre=0 for i in range(10000): # processed = process(input_img_data) # predictions = model.predict(input_img_data) loss_value, grads_value = iterate([input_img_data,1]) # grads_value *= dprocessed(input_img_data[0]) if i%1000 == 0: # print(' predictions: ' , np.shape(predictions), np.argmax(predictions)) print('Iteration %d/%d, loss: %f' % (i, 10000, loss_value)) print('Mean grad: %f' % np.mean(grads_value)) if all(np.abs(grads_val) < 0.000001 for grads_val in grads_value.flatten()): failed = True print('Failed') break # print('Image:\n%s' % str(input_img_data[0,0,:,:])) if loss_value_pre != 0 and loss_value_pre > loss_value: break if loss_value_pre == 0: loss_value_pre = loss_value # if loss_value > 0.99: # break input_img_data += grads_value * 1 #e-3 plt.subplot(8, 8, i_kernal+1) # plt.imshow((process(input_img_data[0,:,:,0])*255).astype('uint8'), cmap='Greys') #cmap='Greys' img_re = deprocess_image(input_img_data[0]) img_re = np.reshape(img_re, (38,38)) plt.imshow(img_re, cmap='Greys') #cmap='Greys' # plt.show() plt.show()
model.layers[1]
model.layers[5]
model.layers[-1]
以上這篇keras模型可視化,層可視化及kernel可視化實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- keras獲得model中某一層的某一個(gè)Tensor的輸出維度教程
- keras獲得某一層或者某層權(quán)重的輸出實(shí)例
- 淺談keras的深度模型訓(xùn)練過程及結(jié)果記錄方式
- 關(guān)于Keras模型可視化教程及關(guān)鍵問題的解決
- 基于keras 模型、結(jié)構(gòu)、權(quán)重保存的實(shí)現(xiàn)
- 利用keras加載訓(xùn)練好的.H5文件,并實(shí)現(xiàn)預(yù)測(cè)圖片
- keras 特征圖可視化實(shí)例(中間層)
- 基于keras輸出中間層結(jié)果的2種實(shí)現(xiàn)方式
- PyTorch和Keras計(jì)算模型參數(shù)的例子
- 在keras中獲取某一層上的feature map實(shí)例
相關(guān)文章
Python實(shí)現(xiàn)PS濾鏡特效之扇形變換效果示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS濾鏡特效之扇形變換效果,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)PS濾鏡扇形變換效果的原理與相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Python(wordcloud)如何根據(jù)文本數(shù)據(jù)(.txt文件)繪制詞云圖
這篇文章主要給大家介紹了關(guān)于Python(wordcloud)如何根據(jù)文本數(shù)據(jù)(.txt文件)繪制詞云圖的相關(guān)資料,詞云Wordcloud是文本數(shù)據(jù)的一種可視化表示方式,它通過設(shè)置不同的字體大小或顏色來表現(xiàn)每個(gè)術(shù)語的重要性,需要的朋友可以參考下2024-05-05Python?paddleocr快速使用及參數(shù)配置詳解
PaddleOCR是基于PaddlePaddle深度學(xué)習(xí)框架的開源OCR工具,但它提供了推理模型/訓(xùn)練模型/預(yù)訓(xùn)練模型,用戶可以直接使用推理模型進(jìn)行識(shí)別,也可以對(duì)訓(xùn)練模型或預(yù)訓(xùn)練模型進(jìn)行再訓(xùn)練,這篇文章主要介紹了Python?paddleocr快速使用及參數(shù)詳解,需要的朋友可以參考下2024-06-06Python mplfinance庫繪制金融圖表實(shí)現(xiàn)數(shù)據(jù)可視化實(shí)例探究
mplfinance(Matplotlib Finance),它是基于Matplotlib的庫,專門用于創(chuàng)建金融圖表和交互式金融數(shù)據(jù)可視化,本文將深入介紹?mplfinance,包括其基本概念、功能特性以及如何使用示例代碼創(chuàng)建各種金融圖表2024-01-01python爬蟲獲取小區(qū)經(jīng)緯度以及結(jié)構(gòu)化地址
這篇文章主要為大家詳細(xì)介紹了python爬蟲獲取小區(qū)經(jīng)緯度,以及結(jié)構(gòu)化的地址,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12django的模型類管理器——數(shù)據(jù)庫操作的封裝詳解
這篇文章主要介紹了django的模型類管理器——數(shù)據(jù)庫操作的封裝詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04