1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| """ keras神经网络的可视化 """
from tensorflow.keras.applications.vgg16 import preprocess_input from tensorflow.keras.models import load_model
from tensorflow.keras import preprocessing from tensorflow.keras import backend as K from tensorflow.keras import models
import tensorflow as tf import numpy as np import cv2 image_size = 224
model_path = "模型地址" img_path = "图片地址"
model = load_model(model_path) image = cv2.imread(img_path) img_tensor = preprocessing.image.img_to_array(image) img_tensor = np.expand_dims(img_tensor, axis=0) img_tensor = preprocess_input(img_tensor)
conv_layer = model.get_layer("conv2d_93") heatmap_model = models.Model([model.inputs], [conv_layer.output, model.output])
with tf.GradientTape() as gtape: conv_output, predictions = heatmap_model(img_tensor) loss = predictions[:, np.argmax(predictions[0])] grads = gtape.gradient(loss, conv_output)[0] pooled_grads = K.mean(grads, axis=(0, 1, 2))
heatmap = tf.reduce_mean(tf.multiply(pooled_grads, conv_output), axis=-1) heatmap = np.maximum(heatmap, 0) max_heat = np.max(heatmap) if max_heat == 0: max_heat = 1e-10 heatmap /= max_heat heatmap = np.uint8(255 * heatmap)
img = cv2.imread(img_path) heatmap1 = cv2.resize(heatmap[0], (img.shape[1], img.shape[0]), interpolation=cv2.INTER_CUBIC) heatmap1 = np.uint8(255*heatmap1) heatmap1 = cv2.applyColorMap(heatmap1, cv2.COLORMAP_JET) frame_out = cv2.addWeighted(img, 0.5, heatmap1, 0.5, 0) cv2.imwrite('热力图保存路径', frame_out)
print("finish")
|