卷积神经网络 特征热力图可视化

获取卷积神经网络的特征热力图。
实现方式:Grad-GAM(注:不过多展开讲Grad-GAM,具体请上网搜)
话不多说,直接上代码!

import cv2
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import models
import tensorflow.keras.backend as K
from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions

VGG16_model = tf.keras.applications.VGG16(include_top=True,weights='imagenet')
VGG16_model.summary()

def prepocess(x):
    x = tf.io.read_file(x)
    x = tf.image.decode_jpeg(x, channels=3)
    x = tf.image.resize(x, [224,224])
    x =tf.expand_dims(x, 0) # 扩维
    x = preprocess_input(x)
    return x


img_path='dog.jpg'
img=prepocess(img_path)
Predictions = VGG16_model.predict(img)
print('Predicted:', decode_predictions(Predictions, top=3)[0])


last_conv_layer = VGG16_model.get_layer('block5_pool')
heatmap_model =models.Model([VGG16_model.inputs], [last_conv_layer.output, VGG16_model.output])


with tf.GradientTape() as gtape:
    conv_output, Predictions = heatmap_model(img)
    prob = Predictions[:, np.argmax(Predictions[0])] # 最大可能性类别的预测概率
    grads = gtape.gradient(prob, conv_output)  # 类别与卷积层的梯度 (1,14,14,512)
    pooled_grads = K.mean(grads, axis=(0,1,2)) # 特征层梯度的全局平均代表每个特征层权重
    
heatmap = tf.reduce_mean(tf.multiply(pooled_grads, conv_output), axis=-1) #权重与特征层相乘,512层求和平均


heatmap = np.maximum(heatmap, 0)
max_heat = np.max(heatmap)
if max_heat == 0:
    max_heat = 1e-10
heatmap /= max_heat


original_img=cv2.imread(img_path)
heatmap1 = cv2.resize(heatmap[0], (original_img.shape[1], original_img.shape[0]), interpolation=cv2.INTER_CUBIC)

heatmap1 = np.maximum(heatmap1,0)
heatmap1 = heatmap1/np.max(heatmap1)

heatmap1 = np.uint8(255*heatmap1)
heatmap1 = cv2.applyColorMap(heatmap1, cv2.COLORMAP_JET)
frame_out=cv2.addWeighted(original_img,0.5,heatmap1,0.5,0)
# frame_out = heatmap1*0.4 + original_img
cv2.imwrite('./heatmap.jpg', frame_out)

plt.figure()
plt.imshow(frame_out)

原图如下:
在这里插入图片描述

特征热力图如下:
在这里插入图片描述

  • 9
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值