-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_KITTI_vec.py
216 lines (181 loc) · 7.95 KB
/
gen_KITTI_vec.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
import argparse
import math
import numpy as np
import tensorflow as tf
import socket
import importlib
import os
import sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
from pointnetvlad_cls import *
from loading_pointclouds_kitti import *
from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors import KDTree
from tqdm import tqdm
import time
#params
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=0, help='GPU to use [default: GPU 0]')
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('--batch_num_queries', type=int, default=3, 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]')
FLAGS = parser.parse_args()
#BATCH_SIZE = FLAGS.batch_size
BATCH_NUM_QUERIES = FLAGS.batch_num_queries
EVAL_BATCH_SIZE = 1
NUM_POINTS = 4096
POSITIVES_PER_QUERY= FLAGS.positives_per_query
NEGATIVES_PER_QUERY= FLAGS.negatives_per_query
GPU_INDEX = FLAGS.gpu
DECAY_STEP = FLAGS.decay_step
DECAY_RATE = FLAGS.decay_rate
# LOG_DIR = '../models/refine'
LOG_DIR = 'log_fold08/' # todo
RESULTS_FOLDER=LOG_DIR
# RESULTS_FOLDER="pretrained_results/"
if not os.path.exists(RESULTS_FOLDER): os.mkdir(RESULTS_FOLDER)
OUT_FEATURE_FOLDER = os.path.join(RESULTS_FOLDER, "feature_database/test_time") # todo
if not os.path.exists(OUT_FEATURE_FOLDER): os.makedirs(OUT_FEATURE_FOLDER)
# DATABASE_FILE= 'generating_queries/oxford_evaluation_database.pickle'
# QUERY_FILE= 'generating_queries/oxford_evaluation_query.pickle'
KITTI_submap_dir = "/media/work/data/kitti/odometry/submap_seg_bin"
output_file= RESULTS_FOLDER +'results.txt'
# model_file= "model_refine.ckpt"
model_file= "model.ckpt"
# DATABASE_SETS= get_sets_dict(DATABASE_FILE)
# QUERY_SETS= get_sets_dict(QUERY_FILE)
# global DATABASE_VECTORS
# DATABASE_VECTORS=[]
#
# global QUERY_VECTORS
# QUERY_VECTORS=[]
BN_INIT_DECAY = 0.5
BN_DECAY_DECAY_RATE = 0.5
BN_DECAY_DECAY_STEP = float(DECAY_STEP)
BN_DECAY_CLIP = 0.99
def get_bn_decay(batch):
bn_momentum = tf.train.exponential_decay(
BN_INIT_DECAY,
batch*BATCH_NUM_QUERIES,
BN_DECAY_DECAY_STEP,
BN_DECAY_DECAY_RATE,
staircase=True)
bn_decay = tf.minimum(BN_DECAY_CLIP, 1 - bn_momentum)
return bn_decay
def evaluate():
# global DATABASE_VECTORS
# global QUERY_VECTORS
with tf.Graph().as_default():
with tf.device('/gpu:'+str(GPU_INDEX)):
print("In Graph")
query= placeholder_inputs(BATCH_NUM_QUERIES, 1, NUM_POINTS)
positives= placeholder_inputs(BATCH_NUM_QUERIES, POSITIVES_PER_QUERY, NUM_POINTS)
negatives= placeholder_inputs(BATCH_NUM_QUERIES, NEGATIVES_PER_QUERY, NUM_POINTS)
eval_queries= placeholder_inputs(EVAL_BATCH_SIZE, 1, NUM_POINTS)
is_training_pl = tf.placeholder(tf.bool, shape=())
print(is_training_pl)
batch = tf.Variable(0)
bn_decay = get_bn_decay(batch)
with tf.variable_scope("query_triplets") as scope:
vecs= tf.concat([query, positives, negatives],1)
print(vecs)
out_vecs= forward(vecs, is_training_pl, bn_decay=bn_decay)
print(out_vecs)
q_vec, pos_vecs, neg_vecs= tf.split(out_vecs, [1,POSITIVES_PER_QUERY,NEGATIVES_PER_QUERY],1)
print(q_vec)
print(pos_vecs)
print(neg_vecs)
saver = tf.train.Saver()
# Create a session
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
config = tf.ConfigProto(gpu_options=gpu_options)
config.gpu_options.allow_growth = True
config.allow_soft_placement = True
config.log_device_placement = False
sess = tf.Session(config=config)
saver.restore(sess, os.path.join(LOG_DIR, model_file))
print("Model restored.")
ops = {'query': query,
'positives': positives,
'negatives': negatives,
'is_training_pl': is_training_pl,
'eval_queries': eval_queries,
'q_vec':q_vec,
'pos_vecs': pos_vecs,
'neg_vecs': neg_vecs}
# recall= np.zeros(25)
# count=0
# similarity=[]
# one_percent_recall=[]
# sequences = ["00", "02", "05", "08", "06", "07"]
sequences = ["08"] # todo
for sq in sequences:
sq_dir = os.path.join(KITTI_submap_dir, sq)
t1 = time.time()
feature_db = get_latent_vectors(sess, ops, sq_dir)
t2 = time.time()
print("time: ", t2 - t1)
feature_db_name = os.path.join(OUT_FEATURE_FOLDER, sq + "_PV_" + sq + ".npy")
def get_latent_vectors(sess, ops, sq_dir):
train_file_idxs = []
listDir(sq_dir, train_file_idxs)
train_file_idxs.sort()
is_training=False
#print(len(train_file_idxs))
batch_num= BATCH_NUM_QUERIES*(1+POSITIVES_PER_QUERY+NEGATIVES_PER_QUERY)
q_output = []
for q_index in tqdm(range(len(train_file_idxs)//batch_num)):
file_names=train_file_idxs[q_index*batch_num:(q_index+1)*(batch_num)]
queries=load_pc_files(file_names)
# queries= np.expand_dims(queries,axis=1)
q1=queries[0:BATCH_NUM_QUERIES]
q1=np.expand_dims(q1,axis=1)
#print(q1.shape)
q2=queries[BATCH_NUM_QUERIES:BATCH_NUM_QUERIES*(POSITIVES_PER_QUERY+1)]
q2=np.reshape(q2,(BATCH_NUM_QUERIES,POSITIVES_PER_QUERY,NUM_POINTS,3))
q3=queries[BATCH_NUM_QUERIES*(POSITIVES_PER_QUERY+1):BATCH_NUM_QUERIES*(NEGATIVES_PER_QUERY+POSITIVES_PER_QUERY+1)]
q3=np.reshape(q3,(BATCH_NUM_QUERIES,NEGATIVES_PER_QUERY,NUM_POINTS,3))
feed_dict={ops['query']:q1, ops['positives']:q2, ops['negatives']:q3, ops['is_training_pl']:is_training}
o1, o2, o3=sess.run([ops['q_vec'], ops['pos_vecs'], ops['neg_vecs']], feed_dict=feed_dict)
o1=np.reshape(o1,(-1,o1.shape[-1]))
o2=np.reshape(o2,(-1,o2.shape[-1]))
o3=np.reshape(o3,(-1,o3.shape[-1]))
out=np.vstack((o1,o2,o3))
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])
#print(q_output.shape)
#handle edge case
for q_index in tqdm(range((len(train_file_idxs)//batch_num*batch_num),len(train_file_idxs))):
file_names=train_file_idxs[q_index]
queries=load_pc_files([file_names])
queries= np.expand_dims(queries,axis=1)
#print(query.shape)
#exit()
fake_queries=np.zeros((BATCH_NUM_QUERIES-1,1,NUM_POINTS,3))
fake_pos=np.zeros((BATCH_NUM_QUERIES,POSITIVES_PER_QUERY,NUM_POINTS,3))
fake_neg=np.zeros((BATCH_NUM_QUERIES,NEGATIVES_PER_QUERY,NUM_POINTS,3))
q=np.vstack((queries,fake_queries))
#print(q.shape)
feed_dict={ops['query']:q, ops['positives']:fake_pos, ops['negatives']:fake_neg, ops['is_training_pl']:is_training}
output=sess.run(ops['q_vec'], feed_dict=feed_dict)
#print(output.shape)
output=output[0]
output=np.squeeze(output)
if (q_output.shape[0]!=0):
q_output=np.vstack((q_output,output))
else:
q_output=output
#q_output=np.array(q_output)
#q_output=q_output.reshape(-1,q_output.shape[-1])
print(q_output.shape)
print(len(train_file_idxs))
assert q_output.shape[0] == len(train_file_idxs)
return q_output
if __name__ == "__main__":
evaluate()