-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
272 lines (218 loc) · 9.36 KB
/
evaluate.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import argparse
import math
import numpy as np
import socket
import importlib
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "3"
import sys
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.backends import cudnn
from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors import KDTree
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
from loading_pointclouds import *
import models.PointNetVlad as PNV
from tensorboardX import SummaryWriter
import loss.pointnetvlad_loss
import config as cfg
cudnn.enabled = True
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def evaluate():
model = PNV.PointNetVlad(global_feat=True, feature_transform=True, max_pool=False,
output_dim=cfg.FEATURE_OUTPUT_DIM, num_points=cfg.NUM_POINTS)
model = model.to(device)
resume_filename = cfg.LOG_DIR + "checkpoint.pth.tar"
print("Resuming From ", resume_filename)
checkpoint = torch.load(resume_filename)
saved_state_dict = checkpoint['state_dict']
model.load_state_dict(saved_state_dict)
#model = nn.DataParallel(model)
ave_one_percent_recall = evaluate_model(model)
print("ave_one_percent_recall:"+str(ave_one_percent_recall))
def evaluate_model(model,save=False):
if save:
torch.save({
'state_dict': model.state_dict(),
}, cfg.LOG_DIR + "checkpoint.pth.tar")
#checkpoint = torch.load(cfg.LOG_DIR + "checkpoint.pth.tar")
#saved_state_dict = checkpoint['state_dict']
#model.load_state_dict(saved_state_dict)
DATABASE_SETS = get_sets_dict(cfg.EVAL_DATABASE_FILE)
QUERY_SETS = get_sets_dict(cfg.EVAL_QUERY_FILE)
'''
QUERY_SETS = []
for i in range(4):
QUERY = {}
for j in range(len(QUERY_SETS_temp)//4):
#QUERY[len(QUERY.keys())] = {"query":QUERY_SETS_temp[i][j]['query'],
# "x":float(QUERY_SETS_temp[i][j]['x']),
# "y":float(QUERY_SETS_temp[i][j]['y']),
# }
QUERY[len(QUERY.keys())] = QUERY_SETS_temp[i][j]
QUERY_SETS.append(QUERY)
'''
if not os.path.exists(cfg.RESULTS_FOLDER):
os.mkdir(cfg.RESULTS_FOLDER)
recall = np.zeros(25)
count = 0
similarity = []
one_percent_recall = []
DATABASE_VECTORS = []
QUERY_VECTORS = []
for i in range(len(DATABASE_SETS)):
DATABASE_VECTORS.append(get_latent_vectors(model, DATABASE_SETS[i]))
for j in range(len(QUERY_SETS)):
QUERY_VECTORS.append(get_latent_vectors(model, QUERY_SETS[j]))
#############
for m in range(len(QUERY_SETS)):
for n in range(len(QUERY_SETS)):
if (m == n):
continue
pair_recall, pair_similarity, pair_opr = get_recall(
m, n, DATABASE_VECTORS, QUERY_VECTORS, QUERY_SETS)
recall += np.array(pair_recall)
count += 1
one_percent_recall.append(pair_opr)
for x in pair_similarity:
similarity.append(x)
#########
### Save Evaluate vectors
file_name = os.path.join(cfg.RESULTS_FOLDER, "database.npy")
np.save(file_name, np.array(DATABASE_VECTORS))
print("saving for DATABASE_VECTORS to "+str(file_name))
ave_recall = recall / count
# print(ave_recall)
# print(similarity)
average_similarity = np.mean(similarity)
# print(average_similarity)
ave_one_percent_recall = np.mean(one_percent_recall)
# print(ave_one_percent_recall)
#print("os.path.join(/home/cc/PointNet-torch2,cfg.OUTPUT_FILE,log.txt):"+str(os.path.join("/home/cc/PointNet-torch2",cfg.OUTPUT_FILE,"log.txt")))
#assert(0)
with open(os.path.join("/home/cc/Supervised-PointNetVlad",cfg.OUTPUT_FILE), "w") as output:
output.write("Average Recall @N:\n")
output.write(str(ave_recall))
output.write("\n\n")
output.write("Average Similarity:\n")
output.write(str(average_similarity))
output.write("\n\n")
output.write("Average Top 1% Recall:\n")
output.write(str(ave_one_percent_recall))
return ave_one_percent_recall
def get_latent_vectors(model, dict_to_process):
model.eval()
is_training = False
train_file_idxs = np.arange(0, len(dict_to_process.keys()))
batch_num = cfg.EVAL_BATCH_SIZE * \
(1 + cfg.EVAL_POSITIVES_PER_QUERY + cfg.EVAL_NEGATIVES_PER_QUERY)
q_output = []
for q_index in range(len(train_file_idxs)//batch_num):
file_indices = train_file_idxs[q_index *
batch_num:(q_index+1)*(batch_num)]
file_names = []
for index in file_indices:
file_names.append(dict_to_process[index]["query"])
queries = load_pc_files(file_names,True)
with torch.no_grad():
feed_tensor = torch.from_numpy(queries).float()
feed_tensor = feed_tensor.unsqueeze(1)
feed_tensor = feed_tensor.to(device)
out = model(feed_tensor)
out = out.detach().cpu().numpy()
out = np.squeeze(out)
#out = np.vstack((o1, o2, o3, o4))
q_output.append(out)
q_output = np.array(q_output)
if(len(q_output) != 0):
q_output = q_output.reshape(-1, q_output.shape[-1])
# handle edge case
index_edge = len(train_file_idxs) // batch_num * batch_num
if index_edge < len(dict_to_process.keys()):
file_indices = train_file_idxs[index_edge:len(dict_to_process.keys())]
file_names = []
for index in file_indices:
file_names.append(dict_to_process[index]["query"])
queries = load_pc_files(file_names,True)
with torch.no_grad():
feed_tensor = torch.from_numpy(queries).float()
feed_tensor = feed_tensor.unsqueeze(1)
feed_tensor = feed_tensor.to(device)
o1 = model(feed_tensor)
output = o1.detach().cpu().numpy()
output = np.squeeze(output)
if (q_output.shape[0] != 0):
q_output = np.vstack((q_output, output))
else:
q_output = output
model.train()
return q_output
def get_recall(m, n, DATABASE_VECTORS, QUERY_VECTORS, QUERY_SETS):
database_output = DATABASE_VECTORS[m]
queries_output = QUERY_VECTORS[n]
# print(len(queries_output))
database_nbrs = KDTree(database_output)
num_neighbors = 25
recall = [0] * num_neighbors
top1_similarity_score = []
one_percent_retrieved = 0
threshold = max(int(round(len(database_output)/100.0)), 1)
num_evaluated = 0
for i in range(len(queries_output)):
true_neighbors = QUERY_SETS[n][i][m]
if(len(true_neighbors) == 0):
continue
num_evaluated += 1
distances, indices = database_nbrs.query(
np.array([queries_output[i]]),k=num_neighbors)
for j in range(len(indices[0])):
if indices[0][j] in true_neighbors:
if(j == 0):
similarity = np.dot(
queries_output[i], database_output[indices[0][j]])
top1_similarity_score.append(similarity)
recall[j] += 1
break
if len(list(set(indices[0][0:threshold]).intersection(set(true_neighbors)))) > 0:
one_percent_retrieved += 1
one_percent_recall = (one_percent_retrieved/float(num_evaluated))*100
recall = (np.cumsum(recall)/float(num_evaluated))*100
return recall, top1_similarity_score, one_percent_recall
if __name__ == "__main__":
# params
parser = argparse.ArgumentParser()
parser.add_argument('--positives_per_query', type=int, default=4,
help='Number of potential positives in each training tuple [default: 2]')
parser.add_argument('--negatives_per_query', type=int, default=12,
help='Number of definite negatives in each training tuple [default: 20]')
parser.add_argument('--eval_batch_size', type=int, default=12,
help='Batch Size during training [default: 1]')
parser.add_argument('--dimension', type=int, default=256)
parser.add_argument('--decay_step', type=int, default=200000,
help='Decay step for lr decay [default: 200000]')
parser.add_argument('--decay_rate', type=float, default=0.7,
help='Decay rate for lr decay [default: 0.8]')
parser.add_argument('--results_dir', default='results/',
help='results dir [default: results]')
parser.add_argument('--dataset_folder', default='../../dataset/',
help='PointNetVlad Dataset Folder')
FLAGS = parser.parse_args()
#BATCH_SIZE = FLAGS.batch_size
#cfg.EVAL_BATCH_SIZE = FLAGS.eval_batch_size
cfg.NUM_POINTS = 4096
cfg.FEATURE_OUTPUT_DIM = 256
cfg.EVAL_POSITIVES_PER_QUERY = FLAGS.positives_per_query
cfg.EVAL_NEGATIVES_PER_QUERY = FLAGS.negatives_per_query
cfg.DECAY_STEP = FLAGS.decay_step
cfg.DECAY_RATE = FLAGS.decay_rate
cfg.RESULTS_FOLDER = FLAGS.results_dir
cfg.EVAL_DATABASE_FILE = 'generating_queries/evaluation_database.pickle'
cfg.EVAL_QUERY_FILE = 'generating_queries/evaluation_query.pickle'
cfg.LOG_DIR = 'log/'
cfg.OUTPUT_FILE = cfg.RESULTS_FOLDER + 'results.txt'
cfg.MODEL_FILENAME = "model.ckpt"
cfg.DATASET_FOLDER = FLAGS.dataset_folder
evaluate()