显示feature的heatmap热力图
程序员文章站
2024-01-04 08:46:22
...
显示feature的heatmap
import cv2
import numpy as np
import scipy.misc
# feature:numpy array; 从网络的forward过程当中保存下来的feature map
# row_image:numpy array; 3*224*224
# output_jpg_name: eg. "out.jpg"
def show_heatmap(feature,output_jpg_name,row_image):
data = feature
heatmap = data.sum(0)/data.shape[0]
heatmap = np.maximum(heatmap, 0)
heatmap /= np.max(heatmap)
heatmap = 1.0 - heatmap # 也可以不写,就是蓝色红色互换的作用
heatmap = cv2.resize(heatmap, (224,224)) # (224,224)指的是图像的size,需要resize到原图大小
heatmap = np.uint8(255 * heatmap)
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
row = row_image
row = row.transpose(1,2,0)
superimposed_img = heatmap*1.0 + row*0.5 # 1.0 和 0.5代表heatmap和row image的强度占比,可调整
scipy.misc.imsave(output_jpg_name,superimposed_img)
推荐阅读