一文了解CNN可視化技術(shù)總結(jié)之卷積核可視化
導(dǎo)言:
上篇文章我們介紹了特征圖可視化方法,對(duì)于特征圖可視化的方法(或者說原理)比較容易理解,即把feature map從特征空間通過反卷積網(wǎng)絡(luò)映射回像素空間。
那卷積核怎樣可視化呢,基于什么原理來可視化?卷積核的尺寸一般只有3x3, 5x5大小,如何可視化?本文將介紹這個(gè)兩個(gè)內(nèi)容。
歡迎關(guān)注公眾號(hào) CV技術(shù)指南 ,專注于計(jì)算機(jī)視覺的技術(shù)總結(jié)、最新技術(shù)跟蹤、經(jīng)典論文解讀。
卷積核可視化的原理
卷積核,在網(wǎng)絡(luò)中起到將圖像從像素空間映射到特征空間的作用,可認(rèn)為是一個(gè)映射函數(shù),像素空間中的值經(jīng)過卷積核后得到響應(yīng)值,在特征提取網(wǎng)絡(luò)中,基本都是使用最大池化來選擇最大響應(yīng)值進(jìn)入下一層繼續(xù)卷積,其余響應(yīng)值低的都進(jìn)入待定。也就是說,我們認(rèn)定只有響應(yīng)值大的才會(huì)對(duì)最終的識(shí)別任務(wù)起作用。
根據(jù)這個(gè)思路,給定一個(gè)已經(jīng)訓(xùn)練好的網(wǎng)絡(luò),現(xiàn)在想要可視化某一層的某一個(gè)卷積核,我們隨機(jī)初始化生成一張圖(指的是對(duì)像素值隨機(jī)取值,不是數(shù)據(jù)集中隨機(jī)選一張圖),然后經(jīng)過前向傳播到該層,我們希望這個(gè)隨機(jī)生成的圖在經(jīng)過這一層卷積核時(shí),它的響應(yīng)值能盡可能的大,換句話說,響應(yīng)值比較大的圖像是這個(gè)卷積核比較認(rèn)可的,是與識(shí)別任務(wù)更相關(guān)的。然后不斷調(diào)整圖像像素值,直到響應(yīng)值足夠大,我們就可以認(rèn)為此時(shí)的圖像就是這個(gè)卷積核所認(rèn)可的,從而達(dá)到可視化該卷積核的目的。
理解了它的原理后,它的實(shí)現(xiàn)方法就比較簡單了,設(shè)計(jì)一個(gè)損失函數(shù),即以經(jīng)過該層卷積核后的響應(yīng)值為目標(biāo)函數(shù),使用梯度上升,更新像素值,使響應(yīng)值最大。
實(shí)現(xiàn)代碼
Setup
import numpy as np import tensorflow as tf from tensorflow import keras # The dimensions of our input image img_width = 180 img_height = 180 # Our target layer: we will visualize the filters from this layer. # See `model.summary()` for list of layer names, if you want to change this. layer_name = "conv3_block4_out"
Build a feature extraction model
# Build a ResNet50V2 model loaded with pre-trained ImageNet weights model = keras.a(chǎn)pplications.ResNet50V2(weights="imagenet", include_top=False) # Set up a model that returns the activation values for our target layerlayer = model.get_layer(name=layer_name) feature_extractor = keras.Model(inputs=model.inputs, outputs=layer.output)
Set up the gradient ascent process
loss函數(shù)取最大化指定卷積核的響應(yīng)值的平均值,為了避免邊界的影響,邊界的響應(yīng)值不計(jì)。
def compute_loss(input_image, filter_index): activation = feature_extractor(input_image) # We avoid border artifacts by only involving non-border pixels in the loss. filter_activation = activation[:, 2:-2, 2:-2, filter_index] return tf.reduce_mean(filter_activation)
@tf.function def gradient_ascent_step(img, filter_index, learning_rate): with tf.GradientTape() as tape: tape.watch(img) loss = compute_loss(img, filter_index) # Compute gradients. grads = tape.gradient(loss, img) # Normalize gradients. grads = tf.math.l2_normalize(grads) img += learning_rate * grads return loss, img
Set up the end-to-end filter visualization loop
def initialize_image(): # We start from a gray image with some random noise img = tf.random.uniform((1, img_width, img_height, 3)) # ResNet50V2 expects inputs in the range [-1, +1]. # Here we scale our random inputs to [-0.125, +0.125] return (img - 0.5) * 0.25 def visualize_filter(filter_index): # We run gradient ascent for 20 steps iterations = 30 learning_rate = 10.0 img = initialize_image() for iteration in range(iterations): loss, img = gradient_ascent_step(img, filter_index, learning_rate) # Decode the resulting input image img = deprocess_image(img[0].numpy()) return loss, img def deprocess_image(img): # Normalize array: center on 0., ensure variance is 0.15 img -= img.mean() img /= img.std() + 1e-5 img *= 0.15 # Center crop img = img[25:-25, 25:-25, :] # Clip to [0, 1] img += 0.5 img = np.clip(img, 0, 1) # Convert to RGB array img *= 255 img = np.clip(img, 0, 255).a(chǎn)stype("uint8") return img
可視化效果圖
可視化vgg16卷積核
總結(jié):本節(jié)內(nèi)容介紹了一種可視化卷積核的方法,即通過生成指定卷積核響應(yīng)值盡可能大的圖像來達(dá)到可視化卷積核的目的,使用的方法是梯度上升。
在不少論文的末尾都有可視化卷積核來分析提出的模型,該方法值得了解。
下一篇我們將介紹最常用的可視化方法--CAM系列,其作用是給出圖像中對(duì)類別識(shí)別起作用的區(qū)域的熱力圖。
本文來源于公眾號(hào) CV技術(shù)指南 的技術(shù)總結(jié)系列。
歡迎關(guān)注公眾號(hào) CV技術(shù)指南 ,專注于計(jì)算機(jī)視覺的技術(shù)總結(jié)、最新技術(shù)跟蹤、經(jīng)典論文解讀。

發(fā)表評(píng)論
請輸入評(píng)論內(nèi)容...
請輸入評(píng)論/評(píng)論長度6~500個(gè)字
最新活動(dòng)更多
-
6月20日立即下載>> 【白皮書】精準(zhǔn)測量 安全高效——福祿克光伏行業(yè)解決方案
-
7月3日立即報(bào)名>> 【在線會(huì)議】英飛凌新一代智能照明方案賦能綠色建筑與工業(yè)互聯(lián)
-
7月22-29日立即報(bào)名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會(huì)
-
7.30-8.1火熱報(bào)名中>> 全數(shù)會(huì)2025(第六屆)機(jī)器人及智能工廠展
-
7月31日免費(fèi)預(yù)約>> OFweek 2025具身機(jī)器人動(dòng)力電池技術(shù)應(yīng)用大會(huì)
-
免費(fèi)參會(huì)立即報(bào)名>> 7月30日- 8月1日 2025全數(shù)會(huì)工業(yè)芯片與傳感儀表展
推薦專題
- 1 AI 眼鏡讓百萬 APP「集體失業(yè)」?
- 2 大廠紛紛入局,百度、阿里、字節(jié)搶奪Agent話語權(quán)
- 3 深度報(bào)告|中國AI產(chǎn)業(yè)正在崛起成全球力量,市場潛力和關(guān)鍵挑戰(zhàn)有哪些?
- 4 上海跑出80億超級(jí)獨(dú)角獸:獲上市公司戰(zhàn)投,干人形機(jī)器人
- 5 國家數(shù)據(jù)局局長劉烈宏調(diào)研格創(chuàng)東智
- 6 下一代入口之戰(zhàn):大廠為何紛紛押注智能體?
- 7 百億AI芯片訂單,瘋狂傾銷中東?
- 8 Robotaxi新消息密集釋放,量產(chǎn)元年誰在領(lǐng)跑?
- 9 格斗大賽出圈!人形機(jī)器人致命短板曝光:頭腦過于簡單
- 10 “搶灘”家用機(jī)器人領(lǐng)域,聯(lián)通、海爾、美的等紛紛入局