keras CNN卷積核可視化,熱度圖教程
卷積核可視化
import matplotlib.pyplot as plt import numpy as np from keras import backend as K from keras.models import load_model # 將浮點(diǎn)圖像轉(zhuǎn)換成有效圖像 def deprocess_image(x): # 對(duì)張量進(jìn)行規(guī)范化 x -= x.mean() x /= (x.std() + 1e-5) x *= 0.1 x += 0.5 x = np.clip(x, 0, 1) # 轉(zhuǎn)化到RGB數(shù)組 x *= 255 x = np.clip(x, 0, 255).astype('uint8') return x # 可視化濾波器 def kernelvisual(model, layer_target=1, num_iterate=100): # 圖像尺寸和通道 img_height, img_width, num_channels = K.int_shape(model.input)[1:4] num_out = K.int_shape(model.layers[layer_target].output)[-1] plt.suptitle('[%s] convnet filters visualizing' % model.layers[layer_target].name) print('第%d層有%d個(gè)通道' % (layer_target, num_out)) for i_kernal in range(num_out): input_img = model.input # 構(gòu)建一個(gè)損耗函數(shù),使所考慮的層的第n個(gè)濾波器的激活最大化,-1層softmax層 if layer_target == -1: loss = K.mean(model.output[:, i_kernal]) else: loss = K.mean(model.layers[layer_target].output[:, :, :, i_kernal]) # m*28*28*128 # 計(jì)算圖像對(duì)損失函數(shù)的梯度 grads = K.gradients(loss, input_img)[0] # 效用函數(shù)通過其L2范數(shù)標(biāo)準(zhǔn)化張量 grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5) # 此函數(shù)返回給定輸入圖像的損耗和梯度 iterate = K.function([input_img], [loss, grads]) # 從帶有一些隨機(jī)噪聲的灰色圖像開始 np.random.seed(0) # 隨機(jī)圖像 # input_img_data = np.random.randint(0, 255, (1, img_height, img_width, num_channels)) # 隨機(jī) # input_img_data = np.zeros((1, img_height, img_width, num_channels)) # 零值 input_img_data = np.random.random((1, img_height, img_width, num_channels)) * 20 + 128. # 隨機(jī)灰度 input_img_data = np.array(input_img_data, dtype=float) failed = False # 運(yùn)行梯度上升 print('####################################', i_kernal + 1) loss_value_pre = 0 # 運(yùn)行梯度上升num_iterate步 for i in range(num_iterate): loss_value, grads_value = iterate([input_img_data]) if i % int(num_iterate/5) == 0: print('Iteration %d/%d, loss: %f' % (i, num_iterate, 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 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 img_re = deprocess_image(input_img_data[0]) if num_channels == 1: img_re = np.reshape(img_re, (img_height, img_width)) else: img_re = np.reshape(img_re, (img_height, img_width, num_channels)) plt.subplot(np.ceil(np.sqrt(num_out)), np.ceil(np.sqrt(num_out)), i_kernal + 1) plt.imshow(img_re) # , cmap='gray' plt.axis('off') plt.show()
運(yùn)行
model = load_model('train3.h5') kernelvisual(model,-1) # 對(duì)最終輸出可視化 kernelvisual(model,6) # 對(duì)第二個(gè)卷積層可視化
熱度圖
import cv2 import matplotlib.pyplot as plt import numpy as np from keras import backend as K from keras.preprocessing import image def heatmap(model, data_img, layer_idx, img_show=None, pred_idx=None): # 圖像處理 if data_img.shape.__len__() != 4: # 由于用作輸入的img需要預(yù)處理,用作顯示的img需要原圖,因此分開兩個(gè)輸入 if img_show is None: img_show = data_img # 縮放 input_shape = K.int_shape(model.input)[1:3] # (28,28) data_img = image.img_to_array(image.array_to_img(data_img).resize(input_shape)) # 添加一個(gè)維度->(1, 224, 224, 3) data_img = np.expand_dims(data_img, axis=0) if pred_idx is None: # 預(yù)測 preds = model.predict(data_img) # 獲取最高預(yù)測項(xiàng)的index pred_idx = np.argmax(preds[0]) # 目標(biāo)輸出估值 target_output = model.output[:, pred_idx] # 目標(biāo)層的輸出代表各通道關(guān)注的位置 last_conv_layer_output = model.layers[layer_idx].output # 求最終輸出對(duì)目標(biāo)層輸出的導(dǎo)數(shù)(優(yōu)化目標(biāo)層輸出),代表目標(biāo)層輸出對(duì)結(jié)果的影響 grads = K.gradients(target_output, last_conv_layer_output)[0] # 將每個(gè)通道的導(dǎo)數(shù)取平均,值越高代表該通道影響越大 pooled_grads = K.mean(grads, axis=(0, 1, 2)) iterate = K.function([model.input], [pooled_grads, last_conv_layer_output[0]]) pooled_grads_value, conv_layer_output_value = iterate([data_img]) # 將各通道關(guān)注的位置和各通道的影響乘起來 for i in range(conv_layer_output_value.shape[-1]): conv_layer_output_value[:, :, i] *= pooled_grads_value[i] # 對(duì)各通道取平均得圖片位置對(duì)結(jié)果的影響 heatmap = np.mean(conv_layer_output_value, axis=-1) # 規(guī)范化 heatmap = np.maximum(heatmap, 0) heatmap /= np.max(heatmap) # plt.matshow(heatmap) # plt.show() # 疊加圖片 # 縮放成同等大小 heatmap = cv2.resize(heatmap, (img_show.shape[1], img_show.shape[0])) heatmap = np.uint8(255 * heatmap) # 將熱圖應(yīng)用于原始圖像.由于opencv熱度圖為BGR,需要轉(zhuǎn)RGB superimposed_img = img_show + cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)[:,:,::-1] * 0.4 # 截取轉(zhuǎn)uint8 superimposed_img = np.minimum(superimposed_img, 255).astype('uint8') return superimposed_img, heatmap # 顯示圖片 # plt.imshow(superimposed_img) # plt.show() # 保存為文件 # superimposed_img = img + cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) * 0.4 # cv2.imwrite('ele.png', superimposed_img) # 生成所有卷積層的熱度圖 def heatmaps(model, data_img, img_show=None): if img_show is None: img_show = np.array(data_img) # Resize input_shape = K.int_shape(model.input)[1:3] # (28,28,1) data_img = image.img_to_array(image.array_to_img(data_img).resize(input_shape)) # 添加一個(gè)維度->(1, 224, 224, 3) data_img = np.expand_dims(data_img, axis=0) # 預(yù)測 preds = model.predict(data_img) # 獲取最高預(yù)測項(xiàng)的index pred_idx = np.argmax(preds[0]) print("預(yù)測為:%d(%f)" % (pred_idx, preds[0][pred_idx])) indexs = [] for i in range(model.layers.__len__()): if 'conv' in model.layers[i].name: indexs.append(i) print('模型共有%d個(gè)卷積層' % indexs.__len__()) plt.suptitle('heatmaps for each conv') for i in range(indexs.__len__()): ret = heatmap(model, data_img, indexs[i], img_show=img_show, pred_idx=pred_idx) plt.subplot(np.ceil(np.sqrt(indexs.__len__()*2)), np.ceil(np.sqrt(indexs.__len__()*2)), i*2 + 1)\ .set_title(model.layers[indexs[i]].name) plt.imshow(ret[0]) plt.axis('off') plt.subplot(np.ceil(np.sqrt(indexs.__len__()*2)), np.ceil(np.sqrt(indexs.__len__()*2)), i*2 + 2)\ .set_title(model.layers[indexs[i]].name) plt.imshow(ret[1]) plt.axis('off') plt.show()
運(yùn)行
from keras.applications.vgg16 import VGG16 from keras.applications.vgg16 import preprocess_input model = VGG16(weights='imagenet') data_img = image.img_to_array(image.load_img('elephant.png')) # VGG16預(yù)處理:RGB轉(zhuǎn)BGR,并對(duì)每一個(gè)顏色通道去均值中心化 data_img = preprocess_input(data_img) img_show = image.img_to_array(image.load_img('elephant.png')) heatmaps(model, data_img, img_show)
elephant.png
結(jié)語
踩坑踩得我腳疼
以上這篇keras CNN卷積核可視化,熱度圖教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python去除字符串中某個(gè)字符的多種實(shí)現(xiàn)方式比較
python中字符串是不可變的,所以無法直接刪除字符串之間的特定字符,下面這篇文章主要給大家介紹了關(guān)于使用Python去除字符串中某個(gè)字符的多種實(shí)現(xiàn)方式比較的相關(guān)資料,需要的朋友可以參考下2022-06-06python實(shí)現(xiàn)數(shù)據(jù)寫入excel表格
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)數(shù)據(jù)寫入excel表格,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model詳解
這篇文章主要給大家介紹了關(guān)于django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-08-08python獲取點(diǎn)擊的坐標(biāo)畫圖形的方法
今天小編就為大家分享一篇python獲取點(diǎn)擊的坐標(biāo)畫圖形的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python wxPython庫使用wx.ListBox創(chuàng)建列表框示例
這篇文章主要介紹了Python wxPython庫使用wx.ListBox創(chuàng)建列表框,結(jié)合實(shí)例形式分析了wxPython庫使用wx.ListBox創(chuàng)建列表框的簡單實(shí)現(xiàn)方法及ListBox函數(shù)相關(guān)選項(xiàng)的功能,需要的朋友可以參考下2018-09-09Python編程中對(duì)文件和存儲(chǔ)器的讀寫示例
這篇文章主要介紹了Python編程中對(duì)文件和存儲(chǔ)器的讀寫示例,包括使用cPickle儲(chǔ)存器存儲(chǔ)對(duì)象的例子,需要的朋友可以參考下2016-01-01python機(jī)器學(xué)習(xí)pytorch自定義數(shù)據(jù)加載器
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)pytorch自定義數(shù)據(jù)加載器使用示例學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10