Spaces:
Build error
Build error
edits
Browse files
app.py
CHANGED
@@ -58,7 +58,25 @@ transform = transforms.Compose([
|
|
58 |
|
59 |
# ---------------------------------------
|
60 |
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
# sf_idx_ = [55, 14, 5, 4, 52, 57, 40, 9]
|
64 |
|
@@ -97,6 +115,12 @@ def generate_matching_superfeatures(im1, im2, scale_id=6, threshold=50, sf_ids='
|
|
97 |
feats2 = output2[0][0]
|
98 |
attns2 = output2[1][0]
|
99 |
strenghts2 = output2[2][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
# outputs = []
|
101 |
# for im_tensor in loader:
|
102 |
# outputs.append(net.get_superfeatures(im_tensor.to(device), scales=[scales[scale_id]]))
|
|
|
58 |
|
59 |
# ---------------------------------------
|
60 |
|
61 |
+
def match(query_feat, pos_feat, LoweRatioTh=0.9):
|
62 |
+
# first perform reciprocal nn
|
63 |
+
dist = torch.cdist(query_feat, pos_feat)
|
64 |
+
best1 = torch.argmin(dist, dim=1)
|
65 |
+
best2 = torch.argmin(dist, dim=0)
|
66 |
+
arange = torch.arange(best2.size(0), device=best2.device)
|
67 |
+
reciprocal = best1[best2]==arange
|
68 |
+
# check Lowe ratio test
|
69 |
+
dist2 = dist.clone()
|
70 |
+
dist2[best2,arange] = float('Inf')
|
71 |
+
dist2_second2 = torch.argmin(dist2, dim=0)
|
72 |
+
ratio1to2 = dist[best2,arange] / dist2_second2
|
73 |
+
valid = torch.logical_and(reciprocal, ratio1to2<=LoweRatioTh)
|
74 |
+
pindices = torch.where(valid)[0]
|
75 |
+
qindices = best2[pindices]
|
76 |
+
# keep only the ones with same indices
|
77 |
+
valid = pindices==qindices
|
78 |
+
return pindices[valid]
|
79 |
+
|
80 |
|
81 |
# sf_idx_ = [55, 14, 5, 4, 52, 57, 40, 9]
|
82 |
|
|
|
115 |
feats2 = output2[0][0]
|
116 |
attns2 = output2[1][0]
|
117 |
strenghts2 = output2[2][0]
|
118 |
+
|
119 |
+
feats1n = F.normalize(feats1, dim=1)
|
120 |
+
feats2n = F.normalize(feats2, dim=1)
|
121 |
+
ind_match = match(feats1n, feats2n)
|
122 |
+
print('ind', ind_match)
|
123 |
+
print('ind.shape', ind_match.shape)
|
124 |
# outputs = []
|
125 |
# for im_tensor in loader:
|
126 |
# outputs.append(net.get_superfeatures(im_tensor.to(device), scales=[scales[scale_id]]))
|