├── .gitignore ├── README.md ├── evaluate.py ├── generating_queries ├── generate_inference_sets.py ├── generate_test_sets.py └── generate_training_tuples_baseline.py ├── inference.py ├── loupe.py ├── models └── lpd_FNSF.py ├── network_architecture.png ├── prepare_data.py ├── train_lpdnet.py └── utils ├── loading_pointclouds.py ├── tf_util.py └── transform_nets.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.pickle 3 | .idea 4 | log* 5 | results* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LPD-Net: 3D Point Cloud Learning for Large-Scale Place Recognition and Environment Analysis 2 | 3 | **[LPD-Net: 3D Point Cloud Learning for Large-Scale Place Recognition and Environment Analysis](http://openaccess.thecvf.com/content_ICCV_2019/papers/Liu_LPD-Net_3D_Point_Cloud_Learning_for_Large-Scale_Place_Recognition_and_ICCV_2019_paper.pdf)** ICCV 2019, Seoul, Korea 4 | 5 | Zhe Liu1, Shunbo Zhou1, Chuanzhe Suo1, Peng Yin3, Wen Chen1, Hesheng Wang2,Haoang Li1, Yun-Hui Liu1 6 | 7 | 1The Chinese University of Hong Kong, 2Shanghai Jiao Tong University, 3Carnegie Mellon University 8 | 9 | 10 | ![pic-network](network_architecture.png) 11 | 12 | ## Introduction 13 | Point cloud based Place Recognition is still an open issue due to the difficulty in extracting local features from the raw 3D point cloud and generating the global descriptor,and it’s even harder in the large-scale dynamic environments. We develop a novel deep neural network, named LPD-Net (Large-scale Place Description Network), which can extract discriminative and generalizable 14 | global descriptors from the raw 3D point cloud. The arXiv version of LPD-Net can be found [here](https://arxiv.org/abs/1812.07050). 15 | ``` 16 | @InProceedings{Liu_2019_ICCV, 17 | author = {Liu, Zhe and Zhou, Shunbo and Suo, Chuanzhe and Yin, Peng and Chen, Wen and Wang, Hesheng and Li, Haoang and Liu, Yun-Hui}, 18 | title = {LPD-Net: 3D Point Cloud Learning for Large-Scale Place Recognition and Environment Analysis}, 19 | booktitle = {The IEEE International Conference on Computer Vision (ICCV)}, 20 | month = {October}, 21 | year = {2019} 22 | } 23 | ``` 24 | ## Benchmark Datasets 25 | The benchmark datasets introdruced in this work can be downloaded [here](https://drive.google.com/open?id=1H9Ep76l8KkUpwILY-13owsEMbVCYTmyx), which created by PointNetVLAD for point cloud based retrieval for place recognition from the open-source [Oxford RobotCar](https://robotcar-dataset.robots.ox.ac.uk/). Details can be found in [PointNetVLAD](https://arxiv.org/abs/1804.03492). 26 | * All submaps are in binary file format 27 | * Ground truth GPS coordinate of the submaps are found in the corresponding csv files for each run 28 | * Filename of the submaps are their timestamps which is consistent with the timestamps in the csv files 29 | * Use CSV files to define positive and negative point clouds 30 | * All submaps are preprocessed with the road removed and downsampled to 4096 points 31 | 32 | ### Oxford Dataset 33 | * 45 sets in total of full and partial runs 34 | * Used both full and partial runs for training but only used full runs for testing/inference 35 | * Training submaps are found in the folder "pointcloud_20m_10overlap/" and its corresponding csv file is "pointcloud_locations_20m_10overlap.csv" 36 | * Training submaps are not mutually disjoint per run 37 | * Each training submap ~20m of car trajectory and subsequent submaps are ~10m apart 38 | * Test/Inference submaps found in the folder "pointcloud_20m/" and its corresponding csv file is "pointcloud_locations_20m.csv" 39 | * Test/Inference submaps are mutually disjoint 40 | 41 | 42 | ## Project Code 43 | 44 | ### Pre-requisites 45 | * Python 46 | * CUDA 47 | * Tensorflow 48 | * Scipy 49 | * Pandas 50 | * Sklearn 51 | 52 | Code was tested using Python 3 on Tensorflow 1.4.0 with CUDA 8.0 and Tensorflow 1.12.0 with CUDA 9.0 53 | 54 | ``` 55 | sudo apt-get install python3-pip python3-dev python-virtualenv 56 | virtualenv --system-site-packages -p python3 ~/tensorflow 57 | source ~/tensorflow/bin/activate 58 | easy_install -U pip 59 | pip3 install --upgrade tensorflow-gpu==1.4.0 60 | pip install scipy, pandas, sklearn 61 | pip install glog 62 | ``` 63 | ### Dataset set-up 64 | Download the zip file of the benchmark datasets found [here](https://drive.google.com/open?id=1H9Ep76l8KkUpwILY-13owsEMbVCYTmyx). Extract the folder on the same directory as the project code. Thus, on that directory you must have two folders: 1) benchmark_datasets/ and 2) LPD_net/ 65 | 66 | ### Data pre-processing 67 | We preprocess the benchmark datasets at first and store the features of point clouds on bin files to save the training time. The files only need to be generated once and used as input of networks. The generation of these files may take a few hours. 68 | ``` 69 | # For pre-processing dataset to generate pointcloud with features 70 | python prepare_data.py 71 | 72 | # Parse Arguments: --k_start 20 --k_end 100 --k_step 10 --bin_core_num 10 73 | # KNN Neighbor size from 20 to 100 with interval 10, parallel process pool core numbers:10 74 | ``` 75 | 76 | ### Generate pickle files 77 | We store the positive and negative point clouds to each anchor on pickle files that are used in our training and evaluation codes. The files only need to be generated once. The generation of these files may take a few minutes. 78 | 79 | ``` 80 | cd generating_queries/ 81 | 82 | # For training tuples in LPD-Net 83 | python generate_training_tuples_baseline.py 84 | 85 | # For network evaluation 86 | python generate_test_sets.py 87 | 88 | # For network inference 89 | python generate_inference_sets.py 90 | # Need to modify the variables (folders or index_list) to specify the folder 91 | ``` 92 | 93 | ### Model Training and Evaluation 94 | To train our network, run the following command: 95 | ``` 96 | python train_lpdnet.py 97 | # Parse Arguments: --model lpd_FNSF --log_dir log/ --restore 98 | 99 | # For example, Train lpd_FNSF network from scratch 100 | python train_lpdnet.py --model lpd_FNSF 101 | 102 | # Retrain lpd_FNSF network with pretrained model 103 | python train_lpdnet.py --log_dir log/ --restore 104 | ``` 105 | To evaluate the model, run the following command: 106 | ``` 107 | python evaluate.py --log_dir log/ 108 | # The resulst.txt will be saved in results/ 109 | ``` 110 | To infer the model, run the following command to get global descriptors: 111 | ``` 112 | python inference.py --log_dir log/ 113 | # The inference_vectors.bin will be saved in LPD_net folder 114 | ``` 115 | 116 | ## Pre-trained Models 117 | The pre-trained models for lpd_FNSF networks can be downloaded [here](https://drive.google.com/open?id=1H9Ep76l8KkUpwILY-13owsEMbVCYTmyx). Put it under the ```/log```folder 118 | 119 | ## License 120 | This repository is released under MIT License (see LICENSE file for details). 121 | -------------------------------------------------------------------------------- /evaluate.py: -------------------------------------------------------------------------------- 1 | # Author: Chuanzhe Suo (suo_ivy@foxmail.com) 10/26/2018 2 | # Thanks to Mikaela Angelina Uy, modified from PointNetVLAD 3 | # Reference: LPD-Net: 3D Point Cloud Learning for Large-Scale Place Recognition and Environment Analysis, ICCV 2019 4 | 5 | import argparse 6 | import os 7 | import sys 8 | import importlib 9 | import tensorflow as tf 10 | import numpy as np 11 | BASE_DIR = os.path.dirname(os.path.abspath(__file__)) 12 | sys.path.append(BASE_DIR) 13 | sys.path.append(os.path.join(BASE_DIR, 'models')) 14 | sys.path.append(os.path.join(BASE_DIR, 'utils')) 15 | from loading_pointclouds import * 16 | from sklearn.neighbors import KDTree 17 | 18 | #params 19 | parser = argparse.ArgumentParser() 20 | parser.add_argument('--gpu', type=int, default=0, help='GPU to use [default: GPU 1]') 21 | parser.add_argument('--log_dir', default='log/', help='Log dir [default: log]') 22 | parser.add_argument('--positives_per_query', type=int, default=4, help='Number of potential positives in each training tuple [default: 2]') 23 | parser.add_argument('--negatives_per_query', type=int, default=10, help='Number of definite negatives in each training tuple [default: 20]') 24 | parser.add_argument('--batch_num_queries', type=int, default=3, help='Batch Size during training [default: 1]') 25 | parser.add_argument('--dimension', type=int, default=256) 26 | parser.add_argument('--decay_step', type=int, default=200000, help='Decay step for lr decay [default: 200000]') 27 | parser.add_argument('--decay_rate', type=float, default=0.7, help='Decay rate for lr decay [default: 0.8]') 28 | FLAGS = parser.parse_args() 29 | 30 | #BATCH_SIZE = FLAGS.batch_size 31 | BATCH_NUM_QUERIES = FLAGS.batch_num_queries 32 | EVAL_BATCH_SIZE = 1 33 | NUM_POINTS = 4096 34 | POSITIVES_PER_QUERY= FLAGS.positives_per_query 35 | NEGATIVES_PER_QUERY= FLAGS.negatives_per_query 36 | GPU_INDEX = FLAGS.gpu 37 | DECAY_STEP = FLAGS.decay_step 38 | DECAY_RATE = FLAGS.decay_rate 39 | 40 | DATABASE_FILE= 'generating_queries/oxford_evaluation_database.pickle' 41 | QUERY_FILE= 'generating_queries/oxford_evaluation_query.pickle' 42 | 43 | LOG_DIR = FLAGS.log_dir 44 | model = LOG_DIR.split('/')[1] 45 | RESULTS_FOLDER=os.path.join("results/", model) 46 | model = model.split('-')[0] 47 | print(LOG_DIR) 48 | MODEL = importlib.import_module(model) 49 | if not os.path.exists(RESULTS_FOLDER): os.makedirs(RESULTS_FOLDER) 50 | output_file= RESULTS_FOLDER +'/results.txt' 51 | model_file= "model.ckpt" 52 | 53 | DATABASE_SETS= get_sets_dict(DATABASE_FILE) 54 | QUERY_SETS= get_sets_dict(QUERY_FILE) 55 | 56 | global DATABASE_VECTORS 57 | DATABASE_VECTORS=[] 58 | 59 | global QUERY_VECTORS 60 | QUERY_VECTORS=[] 61 | 62 | BN_INIT_DECAY = 0.5 63 | BN_DECAY_DECAY_RATE = 0.5 64 | BN_DECAY_DECAY_STEP = float(DECAY_STEP) 65 | BN_DECAY_CLIP = 0.99 66 | 67 | def get_bn_decay(batch): 68 | bn_momentum = tf.train.exponential_decay( 69 | BN_INIT_DECAY, 70 | batch*BATCH_NUM_QUERIES, 71 | BN_DECAY_DECAY_STEP, 72 | BN_DECAY_DECAY_RATE, 73 | staircase=True) 74 | bn_decay = tf.minimum(BN_DECAY_CLIP, 1 - bn_momentum) 75 | return bn_decay 76 | 77 | def evaluate(): 78 | global DATABASE_VECTORS 79 | global QUERY_VECTORS 80 | 81 | with tf.Graph().as_default(): 82 | with tf.device('/gpu:'+str(GPU_INDEX)): 83 | print("In Graph") 84 | query= MODEL.placeholder_inputs(BATCH_NUM_QUERIES, 1, NUM_POINTS) 85 | positives= MODEL.placeholder_inputs(BATCH_NUM_QUERIES, POSITIVES_PER_QUERY, NUM_POINTS) 86 | negatives= MODEL.placeholder_inputs(BATCH_NUM_QUERIES, NEGATIVES_PER_QUERY, NUM_POINTS) 87 | eval_queries= MODEL.placeholder_inputs(EVAL_BATCH_SIZE, 1, NUM_POINTS) 88 | 89 | is_training_pl = tf.placeholder(tf.bool, shape=()) 90 | print(is_training_pl) 91 | 92 | batch = tf.Variable(0) 93 | bn_decay = get_bn_decay(batch) 94 | 95 | with tf.variable_scope("query_triplets") as scope: 96 | vecs= tf.concat([query, positives, negatives],1) 97 | print(vecs) 98 | out_vecs= MODEL.forward(vecs, is_training_pl, bn_decay=bn_decay) 99 | q_vec, pos_vecs, neg_vecs= tf.split(out_vecs, [1,POSITIVES_PER_QUERY,NEGATIVES_PER_QUERY],1) 100 | print(q_vec) 101 | print(pos_vecs) 102 | print(neg_vecs) 103 | 104 | saver = tf.train.Saver() 105 | 106 | # Create a session 107 | gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95) 108 | config = tf.ConfigProto(gpu_options=gpu_options) 109 | config.gpu_options.allow_growth = True 110 | config.allow_soft_placement = True 111 | config.log_device_placement = False 112 | sess = tf.Session(config=config) 113 | 114 | 115 | saver.restore(sess, os.path.join(LOG_DIR, model_file)) 116 | print("Model restored:{}".format(os.path.join(LOG_DIR, model_file))) 117 | 118 | ops = {'query': query, 119 | 'positives': positives, 120 | 'negatives': negatives, 121 | 'is_training_pl': is_training_pl, 122 | 'eval_queries': eval_queries, 123 | 'q_vec':q_vec, 124 | 'pos_vecs': pos_vecs, 125 | 'neg_vecs': neg_vecs} 126 | recall= np.zeros(25) 127 | count=0 128 | similarity=[] 129 | one_percent_recall=[] 130 | for i in range(len(DATABASE_SETS)): 131 | DATABASE_VECTORS.append(get_latent_vectors(sess, ops, DATABASE_SETS[i])) 132 | 133 | for j in range(len(QUERY_SETS)): 134 | QUERY_VECTORS.append(get_latent_vectors(sess, ops, QUERY_SETS[j])) 135 | 136 | for m in range(len(QUERY_SETS)): 137 | for n in range(len(QUERY_SETS)): 138 | if(m==n): 139 | continue 140 | pair_recall, pair_similarity, pair_opr = get_recall(sess, ops, m, n) 141 | recall+=np.array(pair_recall) 142 | count+=1 143 | one_percent_recall.append(pair_opr) 144 | for x in pair_similarity: 145 | similarity.append(x) 146 | 147 | print() 148 | ave_recall=recall/count 149 | print('ave_recallrecall') 150 | print(ave_recall) 151 | 152 | #print('similarity:') 153 | #print(similarity) 154 | average_similarity= np.mean(similarity) 155 | print('average_similarity') 156 | print(average_similarity) 157 | 158 | ave_one_percent_recall= np.mean(one_percent_recall) 159 | print('ave_one_percent_recall') 160 | print(ave_one_percent_recall) 161 | 162 | 163 | #filename=RESULTS_FOLDER +'average_recall_oxford_netmax_sg(finetune_conv5).txt' 164 | with open(output_file, "a") as output: 165 | output.write(model) 166 | output.write("\n\n") 167 | output.write("Average Recall @N:\n") 168 | output.write(str(ave_recall)) 169 | output.write("\n\n") 170 | output.write("Average Similarity:\n") 171 | output.write(str(average_similarity)) 172 | output.write("\n\n") 173 | output.write("Average Top 1% Recall:\n") 174 | output.write(str(ave_one_percent_recall)) 175 | output.write("\n\n") 176 | 177 | 178 | def get_latent_vectors(sess, ops, dict_to_process): 179 | is_training=False 180 | train_file_idxs = np.arange(0, len(dict_to_process.keys())) 181 | #print(len(train_file_idxs)) 182 | batch_num= BATCH_NUM_QUERIES*(1+POSITIVES_PER_QUERY+NEGATIVES_PER_QUERY) 183 | q_output = [] 184 | for q_index in range(len(train_file_idxs)//batch_num): 185 | file_indices=train_file_idxs[q_index*batch_num:(q_index+1)*(batch_num)] 186 | file_names=[] 187 | for index in file_indices: 188 | file_names.append(dict_to_process[index]["query"]) 189 | queries=load_pc_files(file_names) 190 | # queries= np.expand_dims(queries,axis=1) 191 | q1=queries[0:BATCH_NUM_QUERIES] 192 | q1=np.expand_dims(q1,axis=1) 193 | #print(q1.shape) 194 | 195 | q2=queries[BATCH_NUM_QUERIES:BATCH_NUM_QUERIES*(POSITIVES_PER_QUERY+1)] 196 | q2=np.reshape(q2,(BATCH_NUM_QUERIES,POSITIVES_PER_QUERY,NUM_POINTS,13)) 197 | 198 | q3=queries[BATCH_NUM_QUERIES*(POSITIVES_PER_QUERY+1):BATCH_NUM_QUERIES*(NEGATIVES_PER_QUERY+POSITIVES_PER_QUERY+1)] 199 | q3=np.reshape(q3,(BATCH_NUM_QUERIES,NEGATIVES_PER_QUERY,NUM_POINTS,13)) 200 | feed_dict={ops['query']:q1, ops['positives']:q2, ops['negatives']:q3, ops['is_training_pl']:is_training} 201 | o1, o2, o3=sess.run([ops['q_vec'], ops['pos_vecs'], ops['neg_vecs']], feed_dict=feed_dict) 202 | 203 | o1=np.reshape(o1,(-1,o1.shape[-1])) 204 | o2=np.reshape(o2,(-1,o2.shape[-1])) 205 | o3=np.reshape(o3,(-1,o3.shape[-1])) 206 | 207 | out=np.vstack((o1,o2,o3)) 208 | q_output.append(out) 209 | 210 | q_output=np.array(q_output) 211 | if(len(q_output)!=0): 212 | q_output=q_output.reshape(-1,q_output.shape[-1]) 213 | #print(q_output.shape) 214 | 215 | #handle edge case 216 | for q_index in range((len(train_file_idxs)//batch_num*batch_num),len(dict_to_process.keys())): 217 | index=train_file_idxs[q_index] 218 | queries=load_pc_files([dict_to_process[index]["query"]]) 219 | queries= np.expand_dims(queries,axis=1) 220 | #print(query.shape) 221 | #exit() 222 | fake_queries=np.zeros((BATCH_NUM_QUERIES-1,1,NUM_POINTS,13)) 223 | fake_pos=np.zeros((BATCH_NUM_QUERIES,POSITIVES_PER_QUERY,NUM_POINTS,13)) 224 | fake_neg=np.zeros((BATCH_NUM_QUERIES,NEGATIVES_PER_QUERY,NUM_POINTS,13)) 225 | q=np.vstack((queries,fake_queries)) 226 | #print(q.shape) 227 | feed_dict={ops['query']:q, ops['positives']:fake_pos, ops['negatives']:fake_neg, ops['is_training_pl']:is_training} 228 | output=sess.run(ops['q_vec'], feed_dict=feed_dict) 229 | #print(output.shape) 230 | output=output[0] 231 | output=np.squeeze(output) 232 | if (q_output.shape[0]!=0): 233 | q_output=np.vstack((q_output,output)) 234 | else: 235 | q_output=output 236 | 237 | #q_output=np.array(q_output) 238 | #q_output=q_output.reshape(-1,q_output.shape[-1]) 239 | print(q_output.shape) 240 | return q_output 241 | 242 | def get_recall(sess, ops, m, n): 243 | global DATABASE_VECTORS 244 | global QUERY_VECTORS 245 | 246 | database_output= DATABASE_VECTORS[m] 247 | queries_output= QUERY_VECTORS[n] 248 | 249 | print(len(queries_output)) 250 | database_nbrs = KDTree(database_output) 251 | 252 | num_neighbors=25 253 | recall=[0]*num_neighbors 254 | 255 | top1_similarity_score=[] 256 | one_percent_retrieved=0 257 | threshold=max(int(round(len(database_output)/100.0)),1) 258 | 259 | num_evaluated=0 260 | for i in range(len(queries_output)): 261 | true_neighbors= QUERY_SETS[n][i][m] 262 | if(len(true_neighbors)==0): 263 | continue 264 | num_evaluated+=1 265 | distances, indices = database_nbrs.query(np.array([queries_output[i]]),k=num_neighbors) 266 | for j in range(len(indices[0])): 267 | if indices[0][j] in true_neighbors: 268 | if(j==0): 269 | similarity= np.dot(queries_output[i],database_output[indices[0][j]]) 270 | top1_similarity_score.append(similarity) 271 | recall[j]+=1 272 | break 273 | 274 | if len(list(set(indices[0][0:threshold]).intersection(set(true_neighbors))))>0: 275 | one_percent_retrieved+=1 276 | 277 | one_percent_recall=(one_percent_retrieved/float(num_evaluated))*100 278 | recall=(np.cumsum(recall)/float(num_evaluated))*100 279 | print('recall') 280 | print(recall) 281 | print('top1_simlar_score') 282 | print(np.mean(top1_similarity_score)) 283 | print('one_percent_recall') 284 | print(one_percent_recall) 285 | return recall, top1_similarity_score, one_percent_recall 286 | 287 | def get_similarity(sess, ops, m, n): 288 | global DATABASE_VECTORS 289 | global QUERY_VECTORS 290 | 291 | database_output= DATABASE_VECTORS[m] 292 | queries_output= QUERY_VECTORS[n] 293 | 294 | threshold= len(queries_output) 295 | print(len(queries_output)) 296 | database_nbrs = KDTree(database_output) 297 | 298 | similarity=[] 299 | for i in range(len(queries_output)): 300 | distances, indices = database_nbrs.query(np.array([queries_output[i]]),k=1) 301 | for j in range(len(indices[0])): 302 | q_sim= np.dot(q_output[i], database_output[indices[0][j]]) 303 | similarity.append(q_sim) 304 | average_similarity=np.mean(similarity) 305 | print(average_similarity) 306 | return average_similarity 307 | 308 | 309 | if __name__ == "__main__": 310 | evaluate() 311 | -------------------------------------------------------------------------------- /generating_queries/generate_inference_sets.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | import os 4 | import pandas as pd 5 | from sklearn.neighbors import KDTree 6 | import pickle 7 | import random 8 | 9 | #####For training and test data split##### 10 | x_width=150 11 | y_width=150 12 | 13 | #For Oxford 14 | p1=[5735712.768124,620084.402381] 15 | p2=[5735611.299219,620540.270327] 16 | p3=[5735237.358209,620543.094379] 17 | p4=[5734749.303802,619932.693364] 18 | 19 | #For University Sector 20 | p5=[363621.292362,142864.19756] 21 | p6=[364788.795462,143125.746609] 22 | p7=[363597.507711,144011.414174] 23 | 24 | #For Residential Area 25 | p8=[360895.486453,144999.915143] 26 | p9=[362357.024536,144894.825301] 27 | p10=[361368.907155,145209.663042] 28 | 29 | p_dict={"oxford":[p1,p2,p3,p4], "university":[p5,p6,p7], "residential": [p8,p9,p10], "business":[]} 30 | 31 | def check_in_test_set(northing, easting, points, x_width, y_width): 32 | in_test_set=False 33 | for point in points: 34 | if(point[0]-x_width5 and i%700 ==29): 370 | #update cached feature vectors 371 | TRAINING_LATENT_VECTORS=get_latent_vectors(sess, ops, TRAINING_QUERIES) 372 | print("Updated cached feature vectors") 373 | 374 | if(i%1000==101): 375 | save_path = saver.save(sess, os.path.join(LOG_DIR, "model.ckpt")) 376 | log_string("Model saved in file: %s" % save_path) 377 | 378 | 379 | def get_feature_representation(filename, sess, ops): 380 | is_training=False 381 | queries=load_pc_files([filename]) 382 | queries= np.expand_dims(queries,axis=1) 383 | if(BATCH_NUM_QUERIES-1>0): 384 | fake_queries=np.zeros((BATCH_NUM_QUERIES-1,1,NUM_POINTS,13)) 385 | q=np.vstack((queries,fake_queries)) 386 | else: 387 | q=queries 388 | fake_pos=np.zeros((BATCH_NUM_QUERIES,POSITIVES_PER_QUERY,NUM_POINTS,13)) 389 | fake_neg=np.zeros((BATCH_NUM_QUERIES,NEGATIVES_PER_QUERY,NUM_POINTS,13)) 390 | fake_other_neg=np.zeros((BATCH_NUM_QUERIES,1,NUM_POINTS,13)) 391 | feed_dict={ops['query']:q, ops['positives']:fake_pos, ops['negatives']:fake_neg, ops['other_negatives']: fake_other_neg, ops['is_training_pl']:is_training} 392 | output=sess.run(ops['q_vec'], feed_dict=feed_dict) 393 | output=output[0] 394 | output=np.squeeze(output) 395 | return output 396 | 397 | def get_random_hard_negatives(query_vec, random_negs, num_to_take): 398 | global TRAINING_LATENT_VECTORS 399 | 400 | latent_vecs=[] 401 | for j in range(len(random_negs)): 402 | latent_vecs.append(TRAINING_LATENT_VECTORS[random_negs[j]]) 403 | 404 | latent_vecs=np.array(latent_vecs) 405 | nbrs = KDTree(latent_vecs) 406 | distances, indices = nbrs.query(np.array([query_vec]),k=num_to_take) 407 | hard_negs=np.squeeze(np.array(random_negs)[indices[0]]) 408 | hard_negs= hard_negs.tolist() 409 | return hard_negs 410 | 411 | def get_latent_vectors(sess, ops, dict_to_process): 412 | is_training=False 413 | train_file_idxs = np.arange(0, len(dict_to_process.keys())) 414 | 415 | batch_num= BATCH_NUM_QUERIES*(1+POSITIVES_PER_QUERY+NEGATIVES_PER_QUERY+1) 416 | q_output = [] 417 | for q_index in range(len(train_file_idxs)//batch_num): 418 | file_indices=train_file_idxs[q_index*batch_num:(q_index+1)*(batch_num)] 419 | file_names=[] 420 | for index in file_indices: 421 | file_names.append(dict_to_process[index]["query"]) 422 | queries=load_pc_files(file_names) 423 | 424 | q1=queries[0:BATCH_NUM_QUERIES] 425 | q1=np.expand_dims(q1,axis=1) 426 | 427 | q2=queries[BATCH_NUM_QUERIES:BATCH_NUM_QUERIES*(POSITIVES_PER_QUERY+1)] 428 | q2=np.reshape(q2,(BATCH_NUM_QUERIES,POSITIVES_PER_QUERY,NUM_POINTS,13)) 429 | 430 | q3=queries[BATCH_NUM_QUERIES*(POSITIVES_PER_QUERY+1):BATCH_NUM_QUERIES*(NEGATIVES_PER_QUERY+POSITIVES_PER_QUERY+1)] 431 | q3=np.reshape(q3,(BATCH_NUM_QUERIES,NEGATIVES_PER_QUERY,NUM_POINTS,13)) 432 | 433 | q4=queries[BATCH_NUM_QUERIES*(NEGATIVES_PER_QUERY+POSITIVES_PER_QUERY+1):BATCH_NUM_QUERIES*(NEGATIVES_PER_QUERY+POSITIVES_PER_QUERY+2)] 434 | q4=np.expand_dims(q4,axis=1) 435 | 436 | feed_dict={ops['query']:q1, ops['positives']:q2, ops['negatives']:q3,ops['other_negatives']:q4, ops['is_training_pl']:is_training} 437 | o1, o2, o3, o4=sess.run([ops['q_vec'], ops['pos_vecs'], ops['neg_vecs'], ops['other_neg_vec']], feed_dict=feed_dict) 438 | 439 | o1=np.reshape(o1,(-1,o1.shape[-1])) 440 | o2=np.reshape(o2,(-1,o2.shape[-1])) 441 | o3=np.reshape(o3,(-1,o3.shape[-1])) 442 | o4=np.reshape(o4,(-1,o4.shape[-1])) 443 | 444 | out=np.vstack((o1,o2,o3,o4)) 445 | q_output.append(out) 446 | 447 | q_output=np.array(q_output) 448 | if(len(q_output)!=0): 449 | q_output=q_output.reshape(-1,q_output.shape[-1]) 450 | 451 | #handle edge case 452 | for q_index in range((len(train_file_idxs)//batch_num*batch_num),len(dict_to_process.keys())): 453 | index=train_file_idxs[q_index] 454 | queries=load_pc_files([dict_to_process[index]["query"]]) 455 | queries= np.expand_dims(queries,axis=1) 456 | 457 | if(BATCH_NUM_QUERIES-1>0): 458 | fake_queries=np.zeros((BATCH_NUM_QUERIES-1,1,NUM_POINTS,13)) 459 | q=np.vstack((queries,fake_queries)) 460 | else: 461 | q=queries 462 | 463 | fake_pos=np.zeros((BATCH_NUM_QUERIES,POSITIVES_PER_QUERY,NUM_POINTS,13)) 464 | fake_neg=np.zeros((BATCH_NUM_QUERIES,NEGATIVES_PER_QUERY,NUM_POINTS,13)) 465 | fake_other_neg=np.zeros((BATCH_NUM_QUERIES,1,NUM_POINTS,13)) 466 | feed_dict={ops['query']:q, ops['positives']:fake_pos, ops['negatives']:fake_neg, ops['other_negatives']:fake_other_neg, ops['is_training_pl']:is_training} 467 | output=sess.run(ops['q_vec'], feed_dict=feed_dict) 468 | output=output[0] 469 | output=np.squeeze(output) 470 | if (q_output.shape[0]!=0): 471 | q_output=np.vstack((q_output,output)) 472 | else: 473 | q_output=output 474 | 475 | print(q_output.shape) 476 | return q_output 477 | 478 | if __name__ == "__main__": 479 | train() 480 | -------------------------------------------------------------------------------- /utils/loading_pointclouds.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pickle 3 | import numpy as np 4 | import random 5 | 6 | UTILS_DIR = os.path.dirname(os.path.abspath(__file__)) 7 | DATASET_PATH=os.path.join(UTILS_DIR, '../../benchmark_datasets/') 8 | print(DATASET_PATH) 9 | 10 | 11 | def get_queries_dict(filename): 12 | #key:{'query':file,'positives':[files],'negatives:[files], 'neighbors':[keys]} 13 | with open(filename, 'rb') as handle: 14 | queries = pickle.load(handle) 15 | print("Queries Loaded.") 16 | return queries 17 | 18 | def get_sets_dict(filename): 19 | #[key_dataset:{key_pointcloud:{'query':file,'northing':value,'easting':value}},key_dataset:{key_pointcloud:{'query':file,'northing':value,'easting':value}}, ...} 20 | with open(filename, 'rb') as handle: 21 | trajectories = pickle.load(handle) 22 | print("Trajectories Loaded.") 23 | return trajectories 24 | 25 | def load_pc_file(filename): 26 | #returns Nx13 matrix (3 pose 10 handcraft features) 27 | pc=np.fromfile(os.path.join(DATASET_PATH,filename), dtype=np.float64) 28 | 29 | if(pc.shape[0]!= 4096*13): 30 | print("Error in pointcloud shape") 31 | print(pc.shape) 32 | print(filename) 33 | #return np.array([]) 34 | return np.zeros([4096,13]) 35 | 36 | pc=np.reshape(pc,(pc.shape[0]//13,13)) 37 | 38 | # preprocessing data 39 | # Normalization 40 | pc[:,3:12] = ((pc-pc.min(axis=0))/(pc.max(axis=0)-pc.min(axis=0)))[:,3:12] 41 | pc[np.isnan(pc)] = 0.0 42 | pc[np.isinf(pc)] = 1.0 43 | 44 | return pc 45 | 46 | def load_pc_files(filenames): 47 | pcs=[] 48 | for filename in filenames: 49 | #print(filename) 50 | pc=load_pc_file(filename) 51 | if(pc.shape[0]!=4096): 52 | continue 53 | pcs.append(pc) 54 | pcs=np.array(pcs) 55 | return pcs 56 | 57 | def rotate_point_cloud(batch_data): 58 | """ Randomly rotate the point clouds to augument the dataset 59 | rotation is per shape based along up direction 60 | Input: 61 | BxNx3 array, original batch of point clouds 62 | Return: 63 | BxNx3 array, rotated batch of point clouds 64 | """ 65 | rotated_data = np.zeros(batch_data.shape, dtype=np.float32) 66 | for k in range(batch_data.shape[0]): 67 | #rotation_angle = np.random.uniform() * 2 * np.pi 68 | #-90 to 90 69 | rotation_angle = (np.random.uniform()*np.pi)- np.pi/2.0 70 | cosval = np.cos(rotation_angle) 71 | sinval = np.sin(rotation_angle) 72 | rotation_matrix = np.array([[cosval, -sinval, 0], 73 | [sinval, cosval, 0], 74 | [0, 0, 1]]) 75 | shape_pc = batch_data[k, ...] 76 | rotated_data[k, ...] = np.dot(shape_pc.reshape((-1, 3)), rotation_matrix) 77 | return rotated_data 78 | 79 | def jitter_point_cloud(batch_data, sigma=0.005, clip=0.05): 80 | """ Randomly jitter points. jittering is per point. 81 | Input: 82 | BxNx3 array, original batch of point clouds 83 | Return: 84 | BxNx3 array, jittered batch of point clouds 85 | """ 86 | B, N, C = batch_data.shape 87 | assert(clip > 0) 88 | jittered_data = np.clip(sigma * np.random.randn(B, N, C), -1*clip, clip) 89 | jittered_data += batch_data 90 | return jittered_data 91 | 92 | def get_query_tuple(dict_value, num_pos, num_neg, QUERY_DICT, hard_neg=[], other_neg=False): 93 | #get query tuple for dictionary entry 94 | #return list [query,positives,negatives] 95 | 96 | query=load_pc_file(dict_value["query"]) #Nx13 97 | 98 | random.shuffle(dict_value["positives"]) 99 | pos_files=[] 100 | 101 | for i in range(num_pos): 102 | pos_files.append(QUERY_DICT[dict_value["positives"][i]]["query"]) 103 | #positives= load_pc_files(dict_value["positives"][0:num_pos]) 104 | positives=load_pc_files(pos_files) 105 | 106 | neg_files=[] 107 | neg_indices=[] 108 | if(len(hard_neg)==0): 109 | random.shuffle(dict_value["negatives"]) 110 | for i in range(num_neg): 111 | neg_files.append(QUERY_DICT[dict_value["negatives"][i]]["query"]) 112 | neg_indices.append(dict_value["negatives"][i]) 113 | 114 | else: 115 | random.shuffle(dict_value["negatives"]) 116 | for i in hard_neg: 117 | neg_files.append(QUERY_DICT[i]["query"]) 118 | neg_indices.append(i) 119 | j=0 120 | while(len(neg_files)