heatmap-視覺化你的模型辨識特徵
本模型使用ResNet20訓練cifar10資料集保存的權重。
載點點我論文出處:Grad-CAM Visual Explanations from Deep Networks via Gradient-Based Localization
簡單來說cnn可以萃取圖片的特徵,越深的捲積層,越可以表現重要,可讓神經網路學習的特徵,Grad-CAM的目的就是提取最後一層的特徵,並且resize為原圖的大小,這就可以視覺化你的模型辨識特徵
程式碼如下:
import cv2 import numpy as np from keras.models import load_model #用來啟動 NN from keras import backend as K from matplotlib import pyplot as plt test_image = cv2.resize(cv2.imread('./plane1.jpg'),(32,32)) test_image = np.asarray(test_image.astype("float32")) test_image = test_image/255. test_image = test_image.reshape((1,32,32,3)) model=load_model('./resnet_softmax.h5') preds = model.predict(test_image) print("所有結果resnet_softmax=",preds) print("第"+str(np.argmax(preds[0])+1)+"類") #返回數值最大的索引 #model.summary() model_output = model.output[:, np.argmax(preds[0])] last_conv_layer = model.get_layer('conv2d_22') grads = K.gradients(model_output, last_conv_layer.output)[0] pooled_grads = K.mean(grads, axis=(0, 1, 2)) iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]]) pooled_grads_value, conv_layer_output_value = iterate([test_image]) for i in range(256): #256是conv2d_22的維度 conv_layer_output_value[:, :, i] *= pooled_grads_value[i] heatmap = np.mean(conv_layer_output_value, axis=-1) #這裡你可以print一下heatmap #print(heatmap) heatmap = np.maximum(heatmap, 0) heatmap /= np.max(heatmap) '''plt.matshow(heatmap) plt.savefig('bob1.png') plt.show()''' img = cv2.imread('./plane1.jpg') heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0])) heatmap = np.uint8(255 * heatmap) heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) superimposed_img = heatmap * 0.4 + img cv2.imwrite('23.jpg', superimposed_img) imgheat=cv2.imread('23.jpg') cv2.imshow('imgheat', imgheat) cv2.waitKey(0) cv2.destroyAllWindows()
留言
張貼留言