-
Notifications
You must be signed in to change notification settings - Fork 13
/
loading_pointclouds.py
257 lines (219 loc) · 7.94 KB
/
loading_pointclouds.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
#This code is taken from Mika's PointnetVLAD
import os
import pickle
import numpy as np
import random
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_PATH=os.path.join(BASE_DIR, '../benchmark_datasets/')
print(BASE_PATH)
#BASE_PATH = "/media/deep-three/Deep_Store/CVPR2018/benchmark_datasets/"
def get_queries_dict(filename):
#key:{'query':file,'positives':[files],'negatives:[files], 'neighbors':[keys]}
with open(filename, 'rb') as handle:
queries = pickle.load(handle)
print("Queries Loaded.")
return queries
def get_sets_dict(filename):
#[key_dataset:{key_pointcloud:{'query':file,'northing':value,'easting':value}},key_dataset:{key_pointcloud:{'query':file,'northing':value,'easting':value}}, ...}
with open(filename, 'rb') as handle:
trajectories = pickle.load(handle)
print("Trajectories Loaded.")
return trajectories
def load_pc_file(filename):
#returns Nx3 matrix
pc=np.fromfile(os.path.join(BASE_PATH,filename), dtype=np.float64)
if(pc.shape[0]!= 4096*3):
print("Error in pointcloud shape")
return np.array([])
pc=np.reshape(pc,(pc.shape[0]//3,3))
return pc
def load_pc_files(filenames):
pcs=[]
for filename in filenames:
#print(filename)
pc=load_pc_file(filename)
if(pc.shape[0]!=4096):
continue
pcs.append(pc)
pcs=np.array(pcs)
return pcs
def rotate_point_cloud(batch_data):
""" Randomly rotate the point clouds to augument the dataset
rotation is per shape based along up direction
Input:
BxNx3 array, original batch of point clouds
Return:
BxNx3 array, rotated batch of point clouds
"""
rotated_data = np.zeros(batch_data.shape, dtype=np.float32)
for k in range(batch_data.shape[0]):
#rotation_angle = np.random.uniform() * 2 * np.pi
#-90 to 90
rotation_angle = (np.random.uniform()*np.pi)- np.pi/2.0
cosval = np.cos(rotation_angle)
sinval = np.sin(rotation_angle)
rotation_matrix = np.array([[cosval, -sinval, 0],
[sinval, cosval, 0],
[0, 0, 1]])
shape_pc = batch_data[k, ...]
rotated_data[k, ...] = np.dot(shape_pc.reshape((-1, 3)), rotation_matrix)
return rotated_data
def jitter_point_cloud(batch_data, sigma=0.005, clip=0.05):
""" Randomly jitter points. jittering is per point.
Input:
BxNx3 array, original batch of point clouds
Return:
BxNx3 array, jittered batch of point clouds
"""
B, N, C = batch_data.shape
assert(clip > 0)
jittered_data = np.clip(sigma * np.random.randn(B, N, C), -1*clip, clip)
jittered_data += batch_data
return jittered_data
def get_query_tuple(dict_value, num_pos, num_neg, QUERY_DICT, hard_neg=[], other_neg=False):
#get query tuple for dictionary entry
#return list [query,positives,negatives]
query=load_pc_file(dict_value["query"]) #Nx3
random.shuffle(dict_value["positives"])
pos_files=[]
for i in range(num_pos):
pos_files.append(QUERY_DICT[dict_value["positives"][i]]["query"])
#positives= load_pc_files(dict_value["positives"][0:num_pos])
positives=load_pc_files(pos_files)
neg_files=[]
neg_indices=[]
if(len(hard_neg)==0):
random.shuffle(dict_value["negatives"])
for i in range(num_neg):
neg_files.append(QUERY_DICT[dict_value["negatives"][i]]["query"])
neg_indices.append(dict_value["negatives"][i])
else:
random.shuffle(dict_value["negatives"])
for i in hard_neg:
neg_files.append(QUERY_DICT[i]["query"])
neg_indices.append(i)
j=0
while(len(neg_files)<num_neg):
if not dict_value["negatives"][j] in hard_neg:
neg_files.append(QUERY_DICT[dict_value["negatives"][j]]["query"])
neg_indices.append(dict_value["negatives"][j])
j+=1
negatives=load_pc_files(neg_files)
if(other_neg==False):
return [query,positives,negatives]
#For Quadruplet Loss
else:
#get neighbors of negatives and query
neighbors=[]
for pos in dict_value["positives"]:
neighbors.append(pos)
for neg in neg_indices:
for pos in QUERY_DICT[neg]["positives"]:
neighbors.append(pos)
possible_negs= list(set(QUERY_DICT.keys())-set(neighbors))
random.shuffle(possible_negs)
if(len(possible_negs)==0):
return [query, positives, negatives, np.array([])]
neg2= load_pc_file(QUERY_DICT[possible_negs[0]]["query"])
return [query,positives,negatives,neg2]
def get_rotated_tuple(dict_value, num_pos, num_neg, QUERY_DICT, hard_neg=[],other_neg=False):
query=load_pc_file(dict_value["query"]) #Nx3
q_rot= rotate_point_cloud(np.expand_dims(query, axis=0))
q_rot= np.squeeze(q_rot)
random.shuffle(dict_value["positives"])
pos_files=[]
for i in range(num_pos):
pos_files.append(QUERY_DICT[dict_value["positives"][i]]["query"])
#positives= load_pc_files(dict_value["positives"][0:num_pos])
positives=load_pc_files(pos_files)
p_rot= rotate_point_cloud(positives)
neg_files=[]
neg_indices=[]
if(len(hard_neg)==0):
random.shuffle(dict_value["negatives"])
for i in range(num_neg):
neg_files.append(QUERY_DICT[dict_value["negatives"][i]]["query"])
neg_indices.append(dict_value["negatives"][i])
else:
random.shuffle(dict_value["negatives"])
for i in hard_neg:
neg_files.append(QUERY_DICT[i]["query"])
neg_indices.append(i)
j=0
while(len(neg_files)<num_neg):
if not dict_value["negatives"][j] in hard_neg:
neg_files.append(QUERY_DICT[dict_value["negatives"][j]]["query"])
neg_indices.append(dict_value["negatives"][j])
j+=1
negatives=load_pc_files(neg_files)
n_rot=rotate_point_cloud(negatives)
if(other_neg==False):
return [q_rot,p_rot,n_rot]
#For Quadruplet Loss
else:
#get neighbors of negatives and query
neighbors=[]
for pos in dict_value["positives"]:
neighbors.append(pos)
for neg in neg_indices:
for pos in QUERY_DICT[neg]["positives"]:
neighbors.append(pos)
possible_negs= list(set(QUERY_DICT.keys())-set(neighbors))
random.shuffle(possible_negs)
if(len(possible_negs)==0):
return [q_jit, p_jit, n_jit, np.array([])]
neg2= load_pc_file(QUERY_DICT[possible_negs[0]]["query"])
n2_rot= rotate_point_cloud(np.expand_dims(neg2, axis=0))
n2_rot= np.squeeze(n2_rot)
return [q_rot,p_rot,n_rot,n2_rot]
def get_jittered_tuple(dict_value, num_pos, num_neg, QUERY_DICT, hard_neg=[],other_neg=False):
query=load_pc_file(dict_value["query"]) #Nx3
#q_rot= rotate_point_cloud(np.expand_dims(query, axis=0))
q_jit= jitter_point_cloud(np.expand_dims(query, axis=0))
q_jit= np.squeeze(q_jit)
random.shuffle(dict_value["positives"])
pos_files=[]
for i in range(num_pos):
pos_files.append(QUERY_DICT[dict_value["positives"][i]]["query"])
#positives= load_pc_files(dict_value["positives"][0:num_pos])
positives=load_pc_files(pos_files)
p_jit= jitter_point_cloud(positives)
neg_files=[]
neg_indices=[]
if(len(hard_neg)==0):
random.shuffle(dict_value["negatives"])
for i in range(num_neg):
neg_files.append(QUERY_DICT[dict_value["negatives"][i]]["query"])
neg_indices.append(dict_value["negatives"][i])
else:
random.shuffle(dict_value["negatives"])
for i in hard_neg:
neg_files.append(QUERY_DICT[i]["query"])
neg_indices.append(i)
j=0
while(len(neg_files)<num_neg):
if not dict_value["negatives"][j] in hard_neg:
neg_files.append(QUERY_DICT[dict_value["negatives"][j]]["query"])
neg_indices.append(dict_value["negatives"][j])
j+=1
negatives=load_pc_files(neg_files)
n_jit=jitter_point_cloud(negatives)
if(other_neg==False):
return [q_jit,p_jit,n_jit]
#For Quadruplet Loss
else:
#get neighbors of negatives and query
neighbors=[]
for pos in dict_value["positives"]:
neighbors.append(pos)
for neg in neg_indices:
for pos in QUERY_DICT[neg]["positives"]:
neighbors.append(pos)
possible_negs= list(set(QUERY_DICT.keys())-set(neighbors))
random.shuffle(possible_negs)
if(len(possible_negs)==0):
return [q_jit, p_jit, n_jit, np.array([])]
neg2= load_pc_file(QUERY_DICT[possible_negs[0]]["query"])
n2_jit= jitter_point_cloud(np.expand_dims(neg2, axis=0))
n2_jit= np.squeeze(n2_jit)
return [q_jit,p_jit,n_jit,n2_jit]