grad-cam提取图片敏感特征信息的python代码
时间: 2023-09-16 09:03:35 浏览: 155
图像特征提取源码
Grad-CAM是一种用于解释深度学习模型预测结果的方法,它可以帮助我们理解模型在做出决策时所关注的图像区域。以下是用Python编写的Grad-CAM代码:
```python
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras import backend as K
def grad_cam(input_model, image, class_index):
# 获取模型输入和输出张量
img_width, img_height = image.shape[1], image.shape[0]
layer_name = input_model.layers[-1].name
last_conv_layer = input_model.get_layer(layer_name)
last_conv_layer_model = tf.keras.Model(inputs=input_model.inputs,
outputs=last_conv_layer.output)
classifier_input = tf.keras.Input(shape=last_conv_layer.output.shape[1:])
x = classifier_input
preds = input_model.layers[-1](x)
grad_model = tf.keras.Model(inputs=classifier_input, outputs=preds)
# 计算梯度
with tf.GradientTape() as tape:
last_conv_layer_output = last_conv_layer_model(image[np.newaxis, ...])
tape.watch(last_conv_layer_output)
preds = grad_model(last_conv_layer_output)
output = preds[:, class_index]
grads = tape.gradient(output, last_conv_layer_output)
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
# 计算权重
heatmap = tf.reduce_mean(last_conv_layer_output, axis=-1)
heatmap = tf.maximum(heatmap, 0)
heatmap /= tf.reduce_max(heatmap)
# 生成Grad-CAM图像
heatmap = tf.expand_dims(heatmap, axis=-1)
heatmap = tf.image.resize(heatmap, (img_height, img_width))
heatmap = tf.cast(heatmap, dtype=tf.float32)
heatmap = tf.reshape(heatmap, (1, img_height, img_width, 1))
image = tf.cast(image, dtype=tf.float32)
heatmap = tf.image.resize(heatmap, (img_height, img_width))
cam = tf.multiply(heatmap, image)
cam /= tf.reduce_max(cam)
return cam.numpy()[0]
# 加载预训练的VGG16模型
model = VGG16(weights='imagenet')
# 加载图像
img_path = 'image.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# 获取Grad-CAM图像
cam = grad_cam(model, x, 0) # 选取第0类作为敏感特征
# 可视化Grad-CAM图像
cam = cv2.resize(cam, (img.shape[1], img.shape[0]))
heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)
output_image = cv2.addWeighted(cv2.cvtColor(img.astype('uint8'), cv2.COLOR_RGB2BGR), 0.7, heatmap, 0.3, 0)
cv2.imshow("Grad-CAM", output_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
以上代码中调用了VGG16模型作为示例,可以根据自己的需求替换为其他模型。这段代码会加载一张图像,提取出对应类别的Grad-CAM图像,并最终可视化出来。
阅读全文