matplotlib绘制图像注意力

通过matplotlib可以在图像表层对图像注意力机制进行可视化绘制,即将图像注意力叠加在图像表层。

matplotlib绘制图像注意力

1 效果展示

2 实现原理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import matplotlib.pyplot as plt
import cv2
import numpy as np

# read target image
demo_img_path = r"res\img\36979.jpg"
demo_img = plt.imread(demo_img_path)
demo_img_h, demo_img_w, demo_img_c = demo_img.shape

demo_img_att = np.array([
[0.1, 0.4, 0.4, 0, 0],
[0, 0.4, 0.4, 0.4, 0],
[0.4, 0, 0, 0.3, 0.4],
[0.2, 0.1, 0, 0, 0.4],
[0.3, 0.4, 0.1, 0, 0],
])
# resize the image attention to the target image size with interpolation
demo_img_att = cv2.resize(demo_img_att,
dsize=(demo_img_w, demo_img_h),
interpolation=cv2.INTER_CUBIC)

# plot with matplotlib
plt.figure(figsize=(9, 5))

# plot target image
plt.subplot(1, 2, 1)
plt.imshow(demo_img)
plt.axis("off")
plt.title("image")

# plot image with attention masked on it
plt.subplot(1, 2, 2)
plt.imshow(demo_img)
plt.imshow(demo_img_att, alpha=0.8, cmap="gray")
plt.axis("off")
plt.title("image with attention")

# shrink padding etc. to a tight layout
plt.tight_layout()

# save figure and show on display
plt.savefig("demo_img_att.png") # to disk
plt.show() # on display

主要的绘制要点在于:

  1. 绘制前,需要对注意力层进行插值,调整到与原图相同的尺寸;
  2. 绘制时,先绘制原图,再绘制插值后的注意力层,且绘制时设置好color map(cmap)。