forked from valeoai/LOST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizations.py
executable file
·120 lines (105 loc) · 3.77 KB
/
visualizations.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import cv2
import torch
import skimage.io
import numpy as np
import torch.nn as nn
from PIL import Image
import matplotlib.pyplot as plt
def visualize_predictions(image, pred, seed, scales, dims, vis_folder, im_name, plot_seed=False):
"""
Visualization of the predicted box and the corresponding seed patch.
"""
w_featmap, h_featmap = dims
# Plot the box
cv2.rectangle(
image,
(int(pred[0]), int(pred[1])),
(int(pred[2]), int(pred[3])),
(255, 0, 0), 3,
)
# Plot the seed
if plot_seed:
s_ = np.unravel_index(seed.cpu().numpy(), (w_featmap, h_featmap))
size_ = np.asarray(scales) / 2
cv2.rectangle(
image,
(int(s_[1] * scales[1] - (size_[1] / 2)), int(s_[0] * scales[0] - (size_[0] / 2))),
(int(s_[1] * scales[1] + (size_[1] / 2)), int(s_[0] * scales[0] + (size_[0] / 2))),
(0, 255, 0), -1,
)
pltname = f"{vis_folder}/LOST_{im_name}.png"
Image.fromarray(image).save(pltname)
print(f"Predictions saved at {pltname}.")
def visualize_fms(A, seed, scores, dims, scales, output_folder, im_name):
"""
Visualization of the maps presented in Figure 2 of the paper.
"""
w_featmap, h_featmap = dims
# Binarized similarity
binA = A.copy()
binA[binA < 0] = 0
binA[binA > 0] = 1
# Get binarized correlation for this pixel and make it appear in gray
im_corr = np.zeros((3, len(scores)))
where = binA[seed, :] > 0
im_corr[:, where] = np.array([128 / 255, 133 / 255, 133 / 255]).reshape((3, 1))
# Show selected pixel in green
im_corr[:, seed] = [204 / 255, 37 / 255, 41 / 255]
# Reshape and rescale
im_corr = im_corr.reshape((3, w_featmap, h_featmap))
im_corr = (
nn.functional.interpolate(
torch.from_numpy(im_corr).unsqueeze(0),
scale_factor=scales,
mode="nearest",
)[0].cpu().numpy()
)
# Save correlations
skimage.io.imsave(
fname=f"{output_folder}/corr_{im_name}.png",
arr=im_corr.transpose((1, 2, 0)),
)
print(f"Image saved at {output_folder}/corr_{im_name}.png .")
# Save inverse degree
im_deg = (
nn.functional.interpolate(
torch.from_numpy(1 / binA.sum(-1)).reshape(1, 1, w_featmap, h_featmap),
scale_factor=scales,
mode="nearest",
)[0][0].cpu().numpy()
)
plt.imsave(fname=f"{output_folder}/deg_{im_name}.png", arr=im_deg)
print(f"Image saved at {output_folder}/deg_{im_name}.png .")
def visualize_seed_expansion(image, pred, seed, pred_seed, scales, dims, vis_folder, im_name):
"""
Visualization of the seed expansion presented in Figure 3 of the paper.
"""
w_featmap, h_featmap = dims
# Before expansion
cv2.rectangle(
image,
(int(pred_seed[0]), int(pred_seed[1])),
(int(pred_seed[2]), int(pred_seed[3])),
(204, 204, 0), # Yellow
3,
)
# After expansion
cv2.rectangle(
image,
(int(pred[0]), int(pred[1])),
(int(pred[2]), int(pred[3])),
(204, 0, 204), # Magenta
3,
)
# Position of the seed
center = np.unravel_index(seed.cpu().numpy(), (w_featmap, h_featmap))
start_1 = center[0] * scales[0]
end_1 = center[0] * scales[0] + scales[0]
start_2 = center[1] * scales[1]
end_2 = center[1] * scales[1] + scales[1]
image[start_1:end_1, start_2:end_2, 0] = 204
image[start_1:end_1, start_2:end_2, 1] = 37
image[start_1:end_1, start_2:end_2, 2] = 41
pltname = f"{vis_folder}/LOST_seed_expansion_{im_name}.png"
Image.fromarray(image).save(pltname)
print(f"Image saved at {pltname}.")