/
debugger_point.py
151 lines (109 loc) · 5.12 KB
/
debugger_point.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import torch
import data
import utils
import viz
import numpy as np
import cv2
from networks.unsuperpoint import SiameseUnsuperPoint, UnsuperLoss, brute_force_match
from matplotlib import pyplot as plt
class DebuggerBase():
def __init__(self, args):
self.DEVICE = args.device
self.loader = data.get_coco_batch_loader_split(args)["test"]
self.model, self.loss_fn = self._setup_model_and_loss()
checkpoint = torch.load(args.load, map_location=torch.device(args.device))
self.model.load_state_dict(checkpoint["model"])
def _setup_model_and_loss(self):
pass
def _debug_step(self, loss, data):
pass
def run(self):
self.model.eval()
utils.iterate_loader(self.DEVICE, self.loader, self._step_fn)
def _step_fn(self, step, inputs):
# Forward pass and loss
with torch.no_grad():
loss, data = utils.forward_pass(self.model, self.loss_fn, inputs)
print(list(data.keys()))
print(f"loss {loss.item():.3f}")
self._debug_step(loss, data)
class DebuggerPointBase(DebuggerBase):
def __init__(self, args):
super().__init__(args)
self.b = 0 # current batch for rendering
def _compute_debug(self):
pass
def _debug_step(self, loss, data):
while True:
print(f"b = {self.b}")
self.prel1 = utils.torch_to_numpy(data["A"]["Prel"][self.b].transpose(0,1))
self.prel2 = utils.torch_to_numpy(data["B"]["Prel"][self.b].transpose(0,1))
self.prelflat = np.concatenate((self.prel1.flatten(), self.prel2.flatten()))
self.img = viz.tensor2img(data["img"][self.b])
self.warp = viz.tensor2img(data["warp"][self.b])
self.AF = data["A"]["F"]
self.BF = data["B"]["F"]
self.B = self.BF.shape[0]
self.des1 = utils.torch_to_numpy(self.AF[self.b].transpose(0,1))
self.des2 = utils.torch_to_numpy(self.BF[self.b].transpose(0,1))
self.s1 = utils.torch_to_numpy(data["A"]["S"][self.b])
self.s2 = utils.torch_to_numpy(data["B"]["S"][self.b])
self.p1 = utils.torch_to_numpy(data["A"]["P"][self.b].transpose(0,1))
self.p2 = utils.torch_to_numpy(data["B"]["P"][self.b].transpose(0,1))
self.img_matches = []
self.inliers = []
if True: # cv2 match descriptor
bf = cv2.BFMatcher.create(cv2.NORM_L2, crossCheck=True)
matches = bf.match(self.des1, self.des2)
#matches = sorted(matches, key = lambda x: -x.distance)
print(self.des1.shape, len(matches))
#matches = matches[:20]
kp1 = [cv2.KeyPoint(xy[0], xy[1], 2) for xy in self.p1]
kp2 = [cv2.KeyPoint(xy[0], xy[1], 2) for xy in self.p2]
self.img_matches.append(viz.draw_text("CV2 BFMatcher", cv2.drawMatches(self.img, kp1, self.warp, kp2, matches, flags=2, outImg=None)))
self._compute_debug(loss, data)
self.img_matches = np.concatenate([img for img in self.img_matches])
plt.ion()
fig = plt.figure(1)
plt.clf()
while True:
key = cv2.waitKey(10)
if key == 27: # esc
print("exit")
exit()
elif key == 119: # w
self.b = min(self.b+1, self.B-1)
break
elif key == 115: # s
self.b = max(self.b-1, 0)
break
elif key == 32: # space
print("next")
return
cv2.imshow("matches", self.img_matches)
plt.hist(self.prelflat, 200, (0.,1.), color=(0,0,1))
#plt.hist(inliers, 10, (0.,1.), color=(0,0,1))
fig.canvas.flush_events()
class DebuggerPoint(DebuggerPointBase):
def __init__(self, args):
super().__init__(args)
def _setup_model_and_loss(self):
return (
SiameseUnsuperPoint().to(self.DEVICE),
UnsuperLoss()
)
def _compute_debug(self, loss, data):
if True: # match descriptors using pytorch
ids, mask = brute_force_match(self.AF, self.BF)
ids = utils.torch_to_numpy(ids[self.b])
mask = utils.torch_to_numpy(mask[self.b])
print(self.p1[mask].shape)
self.img_matches.append(viz.draw_text("PyTorch Matcher", viz.draw_matches(self.img, self.warp, self.p1[mask], self.p2[ids][mask])))
print(ids.shape, mask.sum())
if True: # debug match using ids
ids = utils.torch_to_numpy(data["ids"][self.b])
mask = utils.torch_to_numpy(data["mask"][self.b])
APh = utils.torch_to_numpy(data["APh"][self.b])
p1h = utils.torch_to_numpy(data["APh"][self.b].transpose(0,1))
self.img_matches.append(viz.draw_text("Matched ids", viz.draw_matches(self.img, self.warp, self.p1[ids][mask], self.p2[mask])))
#self.img_matches.append(viz.draw_matches(img, warp, p1, p1h))