-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve.py
163 lines (140 loc) · 6.89 KB
/
retrieve.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
152
153
154
155
156
157
158
159
160
161
162
163
import torch
import h5py
import os
import numpy as np
import gluefactory
from tqdm import tqdm
from pathlib import Path
from evaluate_utils import *
from args import *
from utils import *
from omegaconf import OmegaConf
from datasets import SampleDataset
from torch.utils.data import DataLoader
from torch.nn.functional import cosine_similarity
opt = create_parser()
# check if gpu device is available
opt.device = torch.device('cuda:0' if torch.cuda.is_available() and opt.device != 'cpu' else 'cpu')
torch.set_grad_enabled(False)
# load the data dirs for all
dataset_dirs = OmegaConf.load('dump_datasets/data_dirs.yaml')
# load the query and db lists in the dumped data
overlap_features = Path(opt.dump_dir) / opt.dataset / "overlap_feats.h5"
assert os.path.exists(overlap_features)
if opt.dataset == 'aachen':
opt.dataset_dir = Path(dataset_dirs.get('dataset_dirs')[opt.dataset]) /'images/images_upright/'
with h5py.File(str(overlap_features), 'r') as hfile:
day_query_list = [dq.decode() for dq in hfile['indices']['day_query'].__array__()]
night_query_list = [nq.decode() for nq in hfile['indices']['night_query'].__array__()]
db_list = [db.decode() for db in hfile['indices']['db'].__array__()]
image_list = np.concatenate((day_query_list, night_query_list, db_list))
num_query = len(day_query_list) + len(night_query_list)
elif opt.dataset == 'pitts':
opt.dataset_dir = Path(dataset_dirs.get('dataset_dirs')[opt.dataset])
query_list = np.load(opt.dataset_dir / "pitts30k_test_qImages.npy")
db_list = np.load(opt.dataset_dir / "pitts30k_test_dbImages.npy")
image_list = np.concatenate((query_list, db_list))
num_query = len(query_list)
elif opt.dataset == 'inloc':
opt.dataset_dir = Path(dataset_dirs.get('dataset_dirs')[opt.dataset])
with h5py.File(str(overlap_features), 'r') as hfile:
query_list = [q.decode() for q in hfile['indices']['query'].__array__()]
db_list = [db.decode() for db in hfile['indices']['db'].__array__()]
image_list = np.concatenate((query_list, db_list))
num_query = len(query_list)
else:
raise NotImplementedError("Unknown dataset")
# load the dumped data, prepare for tests
dataset = SampleDataset(overlap_features, image_list, opt.dataset)
data_loader = DataLoader(dataset, batch_size=opt.batch_size, shuffle=False, num_workers=opt.num_workers)
if not os.path.exists(f"outputs/training/{opt.model}"):
download_best(opt.model)
# use the trained model to encode the input embeddings
model = gluefactory.load_experiment(opt.model).to(opt.device).eval()
all_des = []
cls_tokens = []
for d in tqdm(data_loader):
batch_data_cuda = {k: v.cuda() for k, v in d.items()}
pred = model.matcher({**batch_data_cuda})
if model.conf.matcher['add_cls_tokens']:
des0 = pred['desc0'][:, 1:, :]
cls_tokens0 = pred['desc0'][:, 0, :]
else:
des0 = pred['desc0']
cls_tokens0 = batch_data_cuda['global_descriptor0']
all_des += [des0]
cls_tokens += [cls_tokens0]
all_des = torch.concat(all_des)
cls_tokens = torch.concat(cls_tokens)
N = len(image_list)
output_dir = Path(opt.output_dir) / opt.model / opt.dataset
if opt.cls:
output_dir = output_dir / Path('cls_' + str(opt.pre_filter))
output_dir.mkdir(exist_ok=True, parents=True)
# Step 1 (optional): global retrieval
if not os.path.exists(output_dir / Path("results_cls.npz")) or opt.overwrite:
cls_scores = torch.einsum('id,jd->ij', cls_tokens, cls_tokens)
diagonal = torch.eye(N, N).bool().to(cls_scores.device)
cls_scores.masked_fill_(diagonal, -torch.inf)
cls_scores[:num_query, :num_query] = torch.zeros_like(cls_scores[:num_query, :num_query] )
np.savez(output_dir / Path("results_cls.npz"), **{'scores':cls_scores.cpu().numpy()})
# Step 2: patch-level retrieval: radius search + votings
mask = np.load(output_dir / Path("results_cls.npz"))['scores'][:num_query]
filtered_indices = torch.topk(torch.from_numpy(mask), opt.pre_filter).indices - num_query
query_des = all_des[:num_query]
db_des = all_des[num_query:]
for radius in [opt.radius]:
if radius == -1:
sample_des = torch.concat((query_des[:50], db_des[:50]))
similarities = cosine_similarity(sample_des.unsqueeze(2), sample_des.unsqueeze(1))
radius = round((torch.round(similarities.median() * 10) / 10).item(), 2)
with open(output_dir / "median_radius.txt",'w') as txtfile:
txtfile.write(f"{radius}\n")
query_fun = Voting(radius, num_patches=all_des.shape[1], weighted=opt.weighted)
retrieved = query_fun.rerank(query_des, db_des, filtered_indices=filtered_indices)
np.savez(output_dir / (str(radius) + "_results.npz"), **retrieved)
print(f"successfully test model {opt.model} on {opt.dataset} with radius of {radius}.")
# Step 3: save the retrieved image list
overlap_pairs = output_dir / f"top{opt.k}_{radius}_overlap_pairs.txt"
if not os.path.exists(overlap_pairs) or opt.overwrite:
scores = np.load(output_dir / (str(radius) + '_results.npz'))['scores'][1] if opt.weighted else np.load(output_dir / (str(radius) + '_results.npz'))['scores'][0]
assert scores.shape[0] > radius
voting_topk = torch.topk(torch.from_numpy(scores[:num_query]), opt.k)
with open(overlap_pairs, "w") as doc:
if opt.dataset == 'aachen':
for i, name in enumerate(day_query_list):
for j in voting_topk.indices[i]:
pairs_i = db_list[j]
try:
name = str(name).split("'")[1]
except:
name=name
try:
pairs_i = str(pairs_i).split("'")[1]
except:
pairs_i = pairs_i
doc.write(f"{name} {pairs_i}\n")
for i, name in enumerate(night_query_list):
for j in voting_topk.indices[i+len(day_query_list)]:
pairs_i = db_list[j]
try:
name = str(name).split("'")[1]
except:
name=name
try:
pairs_i = str(pairs_i).split("'")[1]
except:
pairs_i = pairs_i
doc.write(f"{name} {pairs_i}\n")
else:
for i, name in enumerate(query_list):
for j in voting_topk.indices[i]:
pairs_i = db_list[j]
try:
name = str(name).split("'")[1]
pairs_i = str(pairs_i).split("'")[1]
except:
name=name
pairs_i = pairs_i
doc.write(f"{name} {pairs_i}\n")
print(f"successfully save the retrieved image list to {overlap_pairs}.")