heatmap_python, used to determine the attention weight of different layer networks

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"


feature = cv2.imread('hdr_output_case4.png')
row_image = cv2.imread('hdr_output_case4.png')
row_image = cv2.resize(row_image,(224,224))
output_jpg_name =  "out.jpg"
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.1  # 1.0 和 0.5代表heatmap和row image的强度占比,可调整
scipy.misc.imsave(output_jpg_name, superimposed_img)

row_image is the original image, and feature is the feature map output by different network layers. The above code is only for testing. In actual use, the pixels of the feature feature map should be sent directly.

The picture below is the heatmap result. The above code needs to be slightly adjusted according to your actual situation.

 The following code was added to the model when I actually used it. It is easy to understand. Analyze it yourself. . .

feature = full_mask
row_image = cv2.imread('data/test_out/22-006-col.png')
row_image = cv2.resize(row_image, (512, 512))
output_jpg_name = "./out.png"
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, (512, 512))  # (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.7  # 1.0 和 0.5代表heatmap和row image的强度占比,可调整
#scipy.misc.imsave(output_jpg_name, superimposed_img)
cv2.imwrite(output_jpg_name,superimposed_img)

Guess you like

Origin blog.csdn.net/qq_40962125/article/details/130058194