How to change the VGG19 network in an existing program into a self-defined training network when performing convolutional neural network visualization training
code show as below
定义一个可视化的函数
def deepcamvis(imagepath):
import matplotlib.pyplot as plt
import numpy as np
import cv2
from keras import backend as K
from keras.applications.vgg19 import VGG19, preprocess_input, decode_predictions
from keras.preprocessing import image
# 导入模型
model = VGG19(weights="imagenet")
# 读取一张待判断的图像
im = image.load_img(imagepath, target_size=(224, 224))
# 转化为数组
imx = image.img_to_array(im)
# 添加一个纬度，得到1*224*224*3的矩阵
imx = np.expand_dims(imx, axis=0)
# 对图像进行预处理，即标准化
imx = preprocess_input(imx)
# 使用VGG19网络进行预测图像的类别
impre = model.predict(imx)
# 对预测的结果进行解码
imprename = decode_predictions(impre, top=5)
# 最有可能的
print(imprename)
# 计算图像所在位置的编码
impreindex = np.argmax(impre)
# 可视化类激活图像
# 计算预测向量种种的老虎元素
impre_output = model.output[:, impreindex]
## 输出的卷基层特征 block5_conv4 (Conv2D)
conv_femap = model.get_layer("block5_conv4")
## 输出特征图的梯度
grads = K.gradients(impre_output, conv_femap.output)[0]
## 计算每个元素在特定通道的梯度平均大小,shape=(512,)的向量
conv_grads = K.mean(grads, axis=(0, 1, 2))
## 访问刚定义变量的数值
iterate = K.function([model.input], [conv_grads, conv_femap.output[0]])
conv_grads_val, conv_femap_val = iterate([imx])
## 将特征图的每个通道乘以类别重要程度
for ii in range(conv_femap_val.shape[2]):
    conv_femap_val[:, :, ii] = conv_femap_val[:, :, ii] * conv_grads_val[ii]
## 计算热力图
heatmap = np.mean(conv_femap_val, axis=-1)
## 对热力图进行标准化，数值到0～1之间
heatmap = np.maximum(heatmap, 0)
heatmap = heatmap / np.max(heatmap)
## 将生成的热力图叠加到原始图像上
imag = cv2.imread(imagepath)
heatmap = np.uint8(cv2.resize(heatmap, (imag.shape[1], imag.shape[0])) * 255)
## 生成伪彩色热力图
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
## 新的图像
imagnew = np.uint8(heatmap * 0.45 + imag)
## OpenCV image to Matplotlib
imagnewrgb = imagnew[..., ::-1]
plt.figure()
plt.imshow(imagnewrgb)
plt.show()
return imagnewrgb