-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheval_airloc.py
164 lines (128 loc) · 5.58 KB
/
eval_airloc.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
164
import pickle
import yaml
import argparse
from scipy.spatial.distance import cdist
import sys
sys.path.append('.')
import torch
from torch.utils import data
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data.sampler import SubsetRandomSampler
from tqdm import tqdm
from model.build_model import build_netvlad, build_airloc_edge, build_gcn
from datasets.mp3d_airloc.mp3d_triplet_v3_edge import mp3d
from datasets.utils.batch_collator import eval_custom_collate
from statistics import mean
from utils.generate_reference import generate,points_to_obj_desc
torch.autograd.set_detect_anomaly(False)
torch.autograd.profiler.profile(False)
torch.autograd.profiler.emit_nvtx(False)
def eval(configs):
base_dir = configs['base_dir']
batch_size = configs['batch_size']
scenes = configs["scenes"]
ap_thres = configs['ap_thres']
m = configs["ap_wght"]
method = configs['method']
test_dataset = mp3d(base_dir=base_dir,test_scenes = scenes)
test_loader = data.DataLoader(dataset=test_dataset, batch_size=batch_size, collate_fn=eval_custom_collate,shuffle = True)
if method in ["airloc","airloc_without_edge",'netvlad_mean','netvlad']:
model = build_netvlad(configs)
model.eval()
if method in ["airloc"]:
edge_model = build_airloc_edge(configs)
edge_model.eval()
if method in ["gcn","gcn_mean"]:
model = build_gcn(configs)
model.eval()
f = open(configs['database']['config_path'], 'r', encoding='utf-8')
ref_configs = yaml.safe_load(f.read())
ref_configs['db_path'] = configs['database']['db_path']
ref_configs['base_dir'] = configs['database']['db_raw_path']
ref_configs['K'] = configs['database']['K']
ref_configs['netvlad_model_path'] = configs['netvlad_model_path']
ref_configs['graph_model_path'] = configs['graph_model_path']
ref_configs['method'] = method
ref = generate(ref_configs)
rooms = []
ref_data = []
ref_points = []
for key in ref.keys():
scene = key.split("_")[1]
if scene in scenes:
rooms.append(key)
ref_data.append(ref[key][0])
ref_points.append(ref[key][1])
with torch.no_grad():
test_accuracy = []
for step, anchor_pts in enumerate(tqdm(test_loader)):
anchor_pt, query = points_to_obj_desc(anchor_pts,model,method)
if query == None:
continue
if method in ["airloc","airloc_without_edge",'gcn']:
room_sim = []
for i in range(len(ref_data)):
ob_sim = []
for j in range(len(query)):
prod = (ref_data[i] @ query[j].T.to(ref_data[i].device))
ref = torch.norm(ref_data[i], dim = 1).to(prod.device) # Normalising
qry = torch.norm(query[j], dim = 1).to(prod.device)
mat = ((prod/qry).T/ref).T
prod = torch.max(mat, dim = 0).values
ob_sim.append(torch.sum(prod))
room_sim.append(torch.stack(ob_sim))
room_sim = torch.stack(room_sim)
elif method in ["netvlad_mean",'gcn_mean','netvlad']:
room_sim = 1-torch.cdist(torch.stack(ref_data),torch.stack(query).to(torch.stack(ref_data)),p = 2)
mInds = torch.argsort(room_sim,axis=0)
mValues, indices = torch.sort(room_sim,axis=0)
positive = 0
if method == "airloc":
for i, obj in enumerate(anchor_pts):
query_room = obj[0]["room_image_name"][0]
if (mValues[-1,i]-mValues[-2,i])> ap_thres :
ref_room = rooms[mInds[-1][i]]
else:
anc_e = edge_model(anchor_pt)
ref_e = edge_model(ref_points)
prod = (ref_e@ anc_e.T)
room_sim_ = m*torch.nn.functional.normalize(room_sim,dim = 0)+torch.nn.functional.normalize(prod,dim = 0).to(room_sim.device)
mInds_ = torch.argsort(room_sim_,axis=0)
ref_room = rooms[mInds_[-1][i]]
if (ref_room==query_room):
positive+=1
elif method in ["airloc_without_edge","netvlad_mean",'gcn','gcn_mean','netvlad'] :
for i, obj in enumerate(anchor_pts):
query_room = obj[0]["room_image_name"][0]
ref_room = rooms[mInds[-1][i]]
if (ref_room==query_room):
positive+=1
acc= positive/len(query)
test_accuracy.append(acc)
print("Test_accuracy : ", mean(test_accuracy) )
def main():
parser = argparse.ArgumentParser(description="Evaluating")
parser.add_argument(
"-c", "--config_file",
dest = "config_file",
type = str,
default = ""
)
parser.add_argument(
"-g", "--gpu",
dest = "gpu",
type = int,
default = 1
)
args = parser.parse_args()
config_file = args.config_file
f = open(config_file, 'r', encoding='utf-8')
configs = f.read()
configs = yaml.safe_load(configs)
configs['use_gpu'] = args.gpu
# for m in ["airloc","netvlad_mean","airloc_without_edge","gcn","gcn_mean","netvlad"]:
# configs["method"] = m
# print("method = ",m)
eval(configs)
if __name__ == "__main__":
main()