-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloading_pointclouds.py
More file actions
291 lines (237 loc) · 10.1 KB
/
loading_pointclouds.py
File metadata and controls
291 lines (237 loc) · 10.1 KB
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
import os
import pickle
import numpy as np
import random
import config as cfg
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(cfg.DATASET_FOLDER, 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
# dict_value : TRAINING_QUERIES[batch_keys[j]], batch_keys[j] 번째 dict
# dict는 {idx : values}로 구성되어 있으며, values에는 {query, positives, negatives}가 dictionary 형태로 존재
# query는 directory 형식, positives와 negatives는 index가 저장
# num_pos : cfg.TRAIN_POSITIVES_PER_QUERY, positive 개수
# num_neg : cfg.TRAIN_NEGATIVES_PER_QUERY, negative 개수
# QUERY_DICT : TRAINING_QUERIES
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]
# dictionary에 query에 해당하는 pc를 불러온다
# query에는 directory 형식이 저장되어 있다.
query = load_pc_file(dict_value["query"]) # Nx3
random.shuffle(dict_value["positives"])
pos_files = []
for i in range(num_pos):
# i번째 positive에 해당하는 bin파일 경로를 QUERY_DICT를 이용해 획득 후, pos_files에 append
pos_files.append(QUERY_DICT[dict_value["positives"][i]]["query"])
positives = load_pc_files(pos_files)
neg_files = []
neg_indices = []
# positive와 같은 방식으로 진행
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])
# TODO
# hard_neg가 존재할 경우 해당 정보를 이용
# 어느 경우에 존재하지?
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):
# hard_neg에 존재하지 않는 negative도 neg_file에 추가함
# 이때 추가는 파일 directory를 이용
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 is False:
return [query, positives, negatives]
# For Quadruplet Loss
else:
# get neighbors of negatives and query
neighbors = []
# 현재 dict에 해당하는 positive => neighbor
for pos in dict_value["positives"]:
neighbors.append(pos)
# 현재 negative가 가지는 positive => neighbor
for neg in neg_indices:
for pos in QUERY_DICT[neg]["positives"]:
neighbors.append(pos)
# Neighbor를 제외한 나머지에서 possible negative를 검색
possible_negs = list(set(QUERY_DICT.keys())-set(neighbors))
random.shuffle(possible_negs)
if(len(possible_negs) == 0):
return [query, positives, negatives, np.array([])]
# 왜 0? => Random sampling
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 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_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 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]