forked from Xbbei/super-colmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchers.py
78 lines (62 loc) · 2.71 KB
/
matchers.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
import torch
# Mutual nearest neighbors matcher for L2 normalized descriptors.
def mutual_nn_matcher(descriptors1, descriptors2, device="cuda"):
des1 = torch.from_numpy(descriptors1).to(device)
des2 = torch.from_numpy(descriptors2).to(device)
sim = des1 @ des2.t()
nn12 = torch.max(sim, dim=1)[1]
nn21 = torch.max(sim, dim=0)[1]
ids1 = torch.arange(0, sim.shape[0], device=device)
mask = ids1 == nn21[nn12]
matches = torch.stack([ids1[mask], nn12[mask]]).t()
return matches.data.cpu().numpy()
# Symmetric Lowe's ratio test matcher for L2 normalized descriptors.
def ratio_matcher(descriptors1, descriptors2, ratio=0.8, device="cuda"):
des1 = torch.from_numpy(descriptors1).to(device)
des2 = torch.from_numpy(descriptors2).to(device)
sim = des1 @ des2.t()
# Retrieve top 2 nearest neighbors 1->2.
nns_sim, nns = torch.topk(sim, 2, dim=1)
nns_dist = torch.sqrt(2 - 2 * nns_sim)
# Compute Lowe's ratio.
ratios12 = nns_dist[:, 0] / (nns_dist[:, 1] + 1e-8)
# Save first NN.
nn12 = nns[:, 0]
# Retrieve top 2 nearest neighbors 1->2.
nns_sim, nns = torch.topk(sim.t(), 2, dim=1)
nns_dist = torch.sqrt(2 - 2 * nns_sim)
# Compute Lowe's ratio.
ratios21 = nns_dist[:, 0] / (nns_dist[:, 1] + 1e-8)
# Save first NN.
nn21 = nns[:, 0]
# Symmetric ratio test.
ids1 = torch.arange(0, sim.shape[0], device=device)
mask = torch.min(ratios12 <= ratio, ratios21[nn12] <= ratio)
# Final matches.
matches = torch.stack([ids1[mask], nn12[mask]], dim=-1)
return matches.data.cpu().numpy()
# Mutual NN + symmetric Lowe's ratio test matcher for L2 normalized descriptors.
def mutual_nn_ratio_matcher(descriptors1, descriptors2, ratio=0.8, device="cuda"):
des1 = torch.from_numpy(descriptors1).to(device)
des2 = torch.from_numpy(descriptors2).to(device)
sim = des1 @ des2.t()
# Retrieve top 2 nearest neighbors 1->2.
nns_sim, nns = torch.topk(sim, 2, dim=1)
nns_dist = torch.sqrt(2 - 2 * nns_sim)
# Compute Lowe's ratio.
ratios12 = nns_dist[:, 0] / (nns_dist[:, 1] + 1e-8)
# Save first NN and match similarity.
nn12 = nns[:, 0]
# Retrieve top 2 nearest neighbors 1->2.
nns_sim, nns = torch.topk(sim.t(), 2, dim=1)
nns_dist = torch.sqrt(2 - 2 * nns_sim)
# Compute Lowe's ratio.
ratios21 = nns_dist[:, 0] / (nns_dist[:, 1] + 1e-8)
# Save first NN.
nn21 = nns[:, 0]
# Mutual NN + symmetric ratio test.
ids1 = torch.arange(0, sim.shape[0], device=device)
mask = torch.min(ids1 == nn21[nn12], torch.min(ratios12 <= ratio, ratios21[nn12] <= ratio))
# Final matches.
matches = torch.stack([ids1[mask], nn12[mask]], dim=-1)
return matches.data.cpu().numpy()