-
Notifications
You must be signed in to change notification settings - Fork 16
/
loading_pointclouds.py
357 lines (285 loc) · 11.6 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import os
import pickle
import numpy as np
import random
import config as cfg
import struct
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import gputransform
import time
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 convert(x_s, y_s, z_s):
scaling = 0.005 # 5 mm
offset = -100.0
x = x_s * scaling + offset
y = y_s * scaling + offset
z = z_s * scaling + offset
return x, y, z
def load_lidar_file_nclt(file_path):
n_vec = 4
f_bin = open(file_path,'rb')
hits = []
while True:
x_str = f_bin.read(2)
if x_str == b"": # eof
break
x = struct.unpack('<H', x_str)[0]
y = struct.unpack('<H', f_bin.read(2))[0]
z = struct.unpack('<H', f_bin.read(2))[0]
i = struct.unpack('B', f_bin.read(1))[0]
l = struct.unpack('B', f_bin.read(1))[0]
x, y, z = convert(x, y, z)
s = "%5.3f, %5.3f, %5.3f, %d, %d" % (x, y, z, i, l)
# filter and normalize the point cloud to -1 ~ 1
if np.abs(x) < 70. and z > -20. and z < -2. and np.abs(y) < 70. and not(np.abs(x) < 5. and np.abs(y) < 5.):
hits += [[x/70., y/70., z/20.]]
f_bin.close()
hits = np.asarray(hits)
hits[:, 2] = -hits[:, 2]
return hits
def load_pc_file_infer(filename):
# returns Nx3 matrix
pc = load_lidar_file_nclt(filename)
size = pc.shape[0]
pc_img = np.zeros([cfg.num_height * cfg.num_ring * cfg.num_sector])
pc = pc.transpose().flatten().astype(np.float32)
transer = gputransform.GPUTransformer(pc, size, cfg.max_length, cfg.max_height, cfg.num_ring, cfg.num_sector, cfg.num_height, 1)
transer.transform()
point_t = transer.retreive()
point_t = point_t.reshape(-1, 3)
point_t = point_t[...,2]
pc_img = point_t.reshape(cfg.num_height, cfg.num_ring, cfg.num_sector)
return pc_img
def load_pc_infer(pc):
# returns Nx3 matrix
pc = np.array(pc, dtype=np.float32)
# filter in inference step
hits = pc[np.where((np.abs(pc[:,0]) < 70.)&(np.abs(pc[:,1]) < 70.)&(np.abs(pc[:,2]) < 20.)&(np.abs(pc[:,2]) > 2.)&(np.abs(pc[:,0]) > 5.)&(np.abs(pc[:,1]) > 5.))]
hits[...,0] = hits[...,0] / 70.
hits[...,1] = hits[...,1] / 70.
hits[...,2] = hits[...,2] / 20.
hits = hits.transpose((1,0))
pc = np.array(hits, dtype=np.float32)
size = pc.shape[0]
pc_img = np.zeros([cfg.num_height * cfg.num_ring * cfg.num_sector])
pc = pc.transpose().flatten().astype(np.float32)
transer = gputransform.GPUTransformer(pc, size, cfg.max_length, cfg.max_height, cfg.num_ring, cfg.num_sector, cfg.num_height, 1)
transer.transform()
point_t = transer.retreive()
point_t = point_t.reshape(-1, 3)
point_t = point_t[...,2]
pc_img = point_t.reshape(cfg.num_height, cfg.num_ring, cfg.num_sector)
return pc_img
def load_pc_file(filename):
filename = filename.replace('.bin','.npy')
filename = filename.replace('/velo_trans/','/occ_0.5m/')
pc_img = np.load(filename)
return pc_img
def load_pc_files(filenames):
pcs = []
for filename in filenames:
pc = load_pc_file(filename)
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]
heading = []
query = load_pc_file(dict_value["query"]) # Nx3
heading.append(dict_value["heading"])
random.shuffle(dict_value["positives"])
pos_files = []
for i in range(num_pos):
pos_files.append(QUERY_DICT[dict_value["positives"][i]]["query"])
heading.append(QUERY_DICT[dict_value["positives"][i]]["heading"])
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])
heading.append(QUERY_DICT[dict_value["negatives"][i]]["heading"])
else:
random.shuffle(dict_value["negatives"])
for i in hard_neg:
neg_files.append(QUERY_DICT[i]["query"])
heading.append(QUERY_DICT[i]["heading"])
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"])
heading.append(QUERY_DICT[dict_value["negatives"][j]]["heading"])
neg_indices.append(dict_value["negatives"][j])
j += 1
negatives = load_pc_files(neg_files)
if other_neg is False:
return [query, positives, negatives, heading]
# 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([]), heading]
neg2 = load_pc_file(QUERY_DICT[possible_negs[0]]["query"])
heading.append(QUERY_DICT[possible_negs[0]]["heading"])
heading = np.array(heading)
return [query, positives, negatives, neg2, heading]
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 is 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_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(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 is 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]