场景:假设已训练好model,并选了最佳模型best_net,现在想提取网络层的特征并绘出热力图。
1、oriImg = cv2.imread(image_path) #读取一张图片,image_path是图片路径
2、data = torch.from_numpy(oriImg).type(torch.FloatTensor).cuda()
output = best_net(data) #前向计算,这是best_net上权值结合data生成了网络层的特征值。
3、获取网络层特征值:
activation = {}
def get_activation(name):
def hook(model, input, output):
activation[name] = output.detach()
return hook
# normalizing the output
def normalize_output(img):
img = img - img.min()
img = img / img.max()
return img
#conv1
best_net.conv1.register_forward_hook(get_activation('conv1'))#maxpool
feature = activation['conv1'].squeeze()
feature_0 = feature[0].cpu().numpy()
假设这里获取卷积层conv1的特征层,然后提取第一个通道。
4、可视化特征值,即可显示特征热力图。
feature_0 = normalize_output(feature_0)
feature_0 = np.uint8(255 * feature_0)
#plot
height, width, _ = oriImg.shape
featuremap = cv2.applyColorMap(cv2.resize(feature_0,(width, height)), cv2.COLORMAP_JET)
#featuremap = featuremap * 0.3 + oriImg * 0.5
plt.imshow(featuremap)
plt.axis('off')
结果: