forked from IIPCVLAB/LCCNet
-
Notifications
You must be signed in to change notification settings - Fork 14
/
DatasetLidarCamera.py
496 lines (433 loc) · 22 KB
/
DatasetLidarCamera.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# -------------------------------------------------------------------
# Copyright (C) 2020 Università degli studi di Milano-Bicocca, iralab
# Author: Daniele Cattaneo ([email protected])
# Released under Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# http://creativecommons.org/licenses/by-nc-sa/4.0/
# -------------------------------------------------------------------
# Modified Author: Xudong Lv
# based on github.com/cattaneod/CMRNet/blob/master/DatasetVisibilityKitti.py
import csv
import os
from math import radians
import cv2
import h5py
import mathutils
import numpy as np
import pandas as pd
import torch
import torchvision.transforms.functional as TTF
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms
from utils import invert_pose, rotate_forward, quaternion_from_matrix, read_calib_file
from pykitti import odometry
import pykitti
class DatasetLidarCameraKittiOdometry(Dataset):
def __init__(self, dataset_dir, transform=None, augmentation=False, use_reflectance=False,
max_t=1.5, max_r=20., split='val', device='cpu', val_sequence='00', suf='.png'):
super(DatasetLidarCameraKittiOdometry, self).__init__()
self.use_reflectance = use_reflectance
self.maps_folder = ''
self.device = device
self.max_r = max_r
self.max_t = max_t
self.augmentation = augmentation
self.root_dir = dataset_dir
self.transform = transform
self.split = split
self.GTs_R = {}
self.GTs_T = {}
self.GTs_T_cam02_velo = {}
self.K = {}
self.suf = suf
self.all_files = []
self.sequence_list = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
'11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21']
# self.model = CameraModel()
# self.model.focal_length = [7.18856e+02, 7.18856e+02]
# self.model.principal_point = [6.071928e+02, 1.852157e+02]
# for seq in ['00', '03', '05', '06', '07', '08', '09']:
for seq in self.sequence_list:
odom = odometry(self.root_dir, seq)
calib = odom.calib
T_cam02_velo_np = calib.T_cam2_velo #gt pose from cam02 to velo_lidar (T_cam02_velo: 4x4)
self.K[seq] = calib.K_cam2 # 3x3
# T_cam02_velo = torch.from_numpy(T_cam02_velo_np)
# GT_R = quaternion_from_matrix(T_cam02_velo[:3, :3])
# GT_T = T_cam02_velo[3:, :3]
# self.GTs_R[seq] = GT_R # GT_R = np.array([row['qw'], row['qx'], row['qy'], row['qz']])
# self.GTs_T[seq] = GT_T # GT_T = np.array([row['x'], row['y'], row['z']])
self.GTs_T_cam02_velo[seq] = T_cam02_velo_np #gt pose from cam02 to velo_lidar (T_cam02_velo: 4x4)
image_list = os.listdir(os.path.join(dataset_dir, 'sequences', seq, 'image_2'))
image_list.sort()
for image_name in image_list:
if not os.path.exists(os.path.join(dataset_dir, 'sequences', seq, 'velodyne',
str(image_name.split('.')[0])+'.bin')):
continue
if not os.path.exists(os.path.join(dataset_dir, 'sequences', seq, 'image_2',
str(image_name.split('.')[0])+suf)):
continue
if seq == val_sequence:
if split.startswith('val') or split == 'test':
self.all_files.append(os.path.join(seq, image_name.split('.')[0]))
elif (not seq == val_sequence) and split == 'train':
self.all_files.append(os.path.join(seq, image_name.split('.')[0]))
self.val_RT = []
if split == 'val' or split == 'test':
# val_RT_file = os.path.join(dataset_dir, 'sequences',
# f'val_RT_seq{val_sequence}_{max_r:.2f}_{max_t:.2f}.csv')
val_RT_file = os.path.join(dataset_dir, 'sequences',
f'val_RT_left_seq{val_sequence}_{max_r:.2f}_{max_t:.2f}.csv')
if os.path.exists(val_RT_file):
print(f'VAL SET: Using this file: {val_RT_file}')
df_test_RT = pd.read_csv(val_RT_file, sep=',')
for index, row in df_test_RT.iterrows():
self.val_RT.append(list(row))
else:
print(f'VAL SET - Not found: {val_RT_file}')
print("Generating a new one")
val_RT_file = open(val_RT_file, 'w')
val_RT_file = csv.writer(val_RT_file, delimiter=',')
val_RT_file.writerow(['id', 'tx', 'ty', 'tz', 'rx', 'ry', 'rz'])
for i in range(len(self.all_files)):
rotz = np.random.uniform(-max_r, max_r) * (3.141592 / 180.0)
roty = np.random.uniform(-max_r, max_r) * (3.141592 / 180.0)
rotx = np.random.uniform(-max_r, max_r) * (3.141592 / 180.0)
transl_x = np.random.uniform(-max_t, max_t)
transl_y = np.random.uniform(-max_t, max_t)
transl_z = np.random.uniform(-max_t, max_t)
# transl_z = np.random.uniform(-max_t, min(max_t, 1.))
val_RT_file.writerow([i, transl_x, transl_y, transl_z,
rotx, roty, rotz])
self.val_RT.append([float(i), float(transl_x), float(transl_y), float(transl_z),
float(rotx), float(roty), float(rotz)])
assert len(self.val_RT) == len(self.all_files), "Something wrong with test RTs"
def get_ground_truth_poses(self, sequence, frame):
return self.GTs_T[sequence][frame], self.GTs_R[sequence][frame]
def custom_transform(self, rgb, img_rotation=0., flip=False):
to_tensor = transforms.ToTensor()
normalization = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
#rgb = crop(rgb)
if self.split == 'train':
color_transform = transforms.ColorJitter(0.1, 0.1, 0.1)
rgb = color_transform(rgb)
if flip:
rgb = TTF.hflip(rgb)
rgb = TTF.rotate(rgb, img_rotation)
#io.imshow(np.array(rgb))
#io.show()
rgb = to_tensor(rgb)
rgb = normalization(rgb)
return rgb
def __len__(self):
return len(self.all_files)
def __getitem__(self, idx):
item = self.all_files[idx]
seq = str(item.split('/')[0])
rgb_name = str(item.split('/')[1])
img_path = os.path.join(self.root_dir, 'sequences', seq, 'image_2', rgb_name+self.suf)
lidar_path = os.path.join(self.root_dir, 'sequences', seq, 'velodyne', rgb_name+'.bin')
lidar_scan = np.fromfile(lidar_path, dtype=np.float32)
pc = lidar_scan.reshape((-1, 4))
valid_indices = pc[:, 0] < -3.
valid_indices = valid_indices | (pc[:, 0] > 3.)
valid_indices = valid_indices | (pc[:, 1] < -3.)
valid_indices = valid_indices | (pc[:, 1] > 3.)
pc = pc[valid_indices].copy()
pc_org = torch.from_numpy(pc.astype(np.float32))
# if self.use_reflectance:
# reflectance = pc[:, 3].copy()
# reflectance = torch.from_numpy(reflectance).float()
RT = self.GTs_T_cam02_velo[seq].astype(np.float32)
if pc_org.shape[1] == 4 or pc_org.shape[1] == 3:
pc_org = pc_org.t()
if pc_org.shape[0] == 3:
homogeneous = torch.ones(pc_org.shape[1]).unsqueeze(0)
pc_org = torch.cat((pc_org, homogeneous), 0)
elif pc_org.shape[0] == 4:
if not torch.all(pc_org[3, :] == 1.):
pc_org[3, :] = 1.
else:
raise TypeError("Wrong PointCloud shape")
pc_rot = np.matmul(RT, pc_org.numpy())
pc_rot = pc_rot.astype(np.float32).copy()
pc_in = torch.from_numpy(pc_rot)
# pc_rot = np.matmul(RT, pc.T)
# pc_rot = pc_rot.astype(np.float).T.copy()
# pc_in = torch.from_numpy(pc_rot.astype(np.float32))#.float()
# if pc_in.shape[1] == 4 or pc_in.shape[1] == 3:
# pc_in = pc_in.t()
# if pc_in.shape[0] == 3:
# homogeneous = torch.ones(pc_in.shape[1]).unsqueeze(0)
# pc_in = torch.cat((pc_in, homogeneous), 0)
# elif pc_in.shape[0] == 4:
# if not torch.all(pc_in[3,:] == 1.):
# pc_in[3,:] = 1.
# else:
# raise TypeError("Wrong PointCloud shape")
h_mirror = False
# if np.random.rand() > 0.5 and self.split == 'train':
# h_mirror = True
# pc_in[1, :] *= -1
img = Image.open(img_path)
# img = cv2.imread(img_path)
img_rotation = 0.
# if self.split == 'train':
# img_rotation = np.random.uniform(-5, 5)
try:
img = self.custom_transform(img, img_rotation, h_mirror)
except OSError:
new_idx = np.random.randint(0, self.__len__())
return self.__getitem__(new_idx)
# Rotate PointCloud for img_rotation
if self.split == 'train':
R = mathutils.Euler((radians(img_rotation), 0, 0), 'XYZ')
T = mathutils.Vector((0., 0., 0.))
pc_in = rotate_forward(pc_in, R, T)
if self.split == 'train':
max_angle = self.max_r
rotz = np.random.uniform(-max_angle, max_angle) * (3.141592 / 180.0)
roty = np.random.uniform(-max_angle, max_angle) * (3.141592 / 180.0)
rotx = np.random.uniform(-max_angle, max_angle) * (3.141592 / 180.0)
transl_x = np.random.uniform(-self.max_t, self.max_t)
transl_y = np.random.uniform(-self.max_t, self.max_t)
transl_z = np.random.uniform(-self.max_t, self.max_t)
# transl_z = np.random.uniform(-self.max_t, min(self.max_t, 1.))
else:
initial_RT = self.val_RT[idx]
rotz = initial_RT[6]
roty = initial_RT[5]
rotx = initial_RT[4]
transl_x = initial_RT[1]
transl_y = initial_RT[2]
transl_z = initial_RT[3]
# 随机设置一定范围内的标定参数扰动值
# train的时候每次都随机生成,每个epoch使用不同的参数
# test则在初始化的时候提前设置好,每个epoch都使用相同的参数
R = mathutils.Euler((rotx, roty, rotz), 'XYZ')
T = mathutils.Vector((transl_x, transl_y, transl_z))
R, T = invert_pose(R, T)
R, T = torch.tensor(R), torch.tensor(T)
#io.imshow(depth_img.numpy(), cmap='jet')
#io.show()
calib = self.K[seq]
if h_mirror:
calib[2] = (img.shape[2] / 2)*2 - calib[2]
if self.split == 'test':
sample = {'rgb': img, 'point_cloud': pc_in, 'calib': calib,
'tr_error': T, 'rot_error': R, 'seq': int(seq), 'img_path': img_path,
'rgb_name': rgb_name + '.png', 'item': item, 'extrin': RT,
'initial_RT': initial_RT}
else:
sample = {'rgb': img, 'point_cloud': pc_in, 'calib': calib,
'tr_error': T, 'rot_error': R, 'seq': int(seq),
'rgb_name': rgb_name, 'item': item, 'extrin': RT}
return sample
class DatasetLidarCameraKittiRaw(Dataset):
def __init__(self, dataset_dir, transform=None, augmentation=False, use_reflectance=False,
max_t=1.5, max_r=15.0, split='val', device='cpu', val_sequence='2011_09_26_drive_0117_sync'):
super(DatasetLidarCameraKittiRaw, self).__init__()
self.use_reflectance = use_reflectance
self.maps_folder = ''
self.device = device
self.max_r = max_r
self.max_t = max_t
self.augmentation = augmentation
self.root_dir = dataset_dir
self.transform = transform
self.split = split
self.GTs_R = {}
self.GTs_T = {}
self.GTs_T_cam02_velo = {}
self.max_depth = 80
self.K_list = {}
self.all_files = []
date_list = ['2011_09_26', '2011_09_28', '2011_09_29', '2011_09_30', '2011_10_03']
data_drive_list = ['0001', '0002', '0004', '0016', '0027']
self.calib_date = {}
for i in range(len(date_list)):
date = date_list[i]
data_drive = data_drive_list[i]
data = pykitti.raw(self.root_dir, date, data_drive)
calib = {'K2': data.calib.K_cam2, 'K3': data.calib.K_cam3,
'RT2': data.calib.T_cam2_velo, 'RT3': data.calib.T_cam3_velo}
self.calib_date[date] = calib
# date = val_sequence[:10]
# seq = val_sequence
# image_list = os.listdir(os.path.join(dataset_dir, date, seq, 'image_02/data'))
# image_list.sort()
#
# for image_name in image_list:
# if not os.path.exists(os.path.join(dataset_dir, date, seq, 'velodyne_points/data',
# str(image_name.split('.')[0]) + '.bin')):
# continue
# if not os.path.exists(os.path.join(dataset_dir, date, seq, 'image_02/data',
# str(image_name.split('.')[0]) + '.jpg')): # png
# continue
# self.all_files.append(os.path.join(date, seq, 'image_02/data', image_name.split('.')[0]))
date = val_sequence[:10]
test_list = ['2011_09_26_drive_0005_sync', '2011_09_26_drive_0070_sync', '2011_10_03_drive_0027_sync']
seq_list = os.listdir(os.path.join(self.root_dir, date))
for seq in seq_list:
if not os.path.isdir(os.path.join(dataset_dir, date, seq)):
continue
image_list = os.listdir(os.path.join(dataset_dir, date, seq, 'image_02/data'))
image_list.sort()
for image_name in image_list:
if not os.path.exists(os.path.join(dataset_dir, date, seq, 'velodyne_points/data',
str(image_name.split('.')[0])+'.bin')):
continue
if not os.path.exists(os.path.join(dataset_dir, date, seq, 'image_02/data',
str(image_name.split('.')[0])+'.jpg')): # png
continue
if seq == val_sequence and (not split == 'train'):
self.all_files.append(os.path.join(date, seq, 'image_02/data', image_name.split('.')[0]))
elif (not seq == val_sequence) and split == 'train' and seq not in test_list:
self.all_files.append(os.path.join(date, seq, 'image_02/data', image_name.split('.')[0]))
self.val_RT = []
if split == 'val' or split == 'test':
val_RT_file = os.path.join(dataset_dir,
f'val_RT_seq{val_sequence}_{max_r:.2f}_{max_t:.2f}.csv')
if os.path.exists(val_RT_file):
print(f'VAL SET: Using this file: {val_RT_file}')
df_test_RT = pd.read_csv(val_RT_file, sep=',')
for index, row in df_test_RT.iterrows():
self.val_RT.append(list(row))
else:
print(f'TEST SET - Not found: {val_RT_file}')
print("Generating a new one")
val_RT_file = open(val_RT_file, 'w')
val_RT_file = csv.writer(val_RT_file, delimiter=',')
val_RT_file.writerow(['id', 'tx', 'ty', 'tz', 'rx', 'ry', 'rz'])
for i in range(len(self.all_files)):
rotz = np.random.uniform(-max_r, max_r) * (3.141592 / 180.0)
roty = np.random.uniform(-max_r, max_r) * (3.141592 / 180.0)
rotx = np.random.uniform(-max_r, max_r) * (3.141592 / 180.0)
transl_x = np.random.uniform(-max_t, max_t)
transl_y = np.random.uniform(-max_t, max_t)
transl_z = np.random.uniform(-max_t, max_t)
# transl_z = np.random.uniform(-max_t, min(max_t, 1.))
val_RT_file.writerow([i, transl_x, transl_y, transl_z,
rotx, roty, rotz])
self.val_RT.append([float(i), transl_x, transl_y, transl_z,
rotx, roty, rotz])
assert len(self.val_RT) == len(self.all_files), "Something wrong with test RTs"
def get_ground_truth_poses(self, sequence, frame):
return self.GTs_T[sequence][frame], self.GTs_R[sequence][frame]
def custom_transform(self, rgb, img_rotation=0., flip=False):
to_tensor = transforms.ToTensor()
normalization = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
#rgb = crop(rgb)
if self.split == 'train':
color_transform = transforms.ColorJitter(0.1, 0.1, 0.1)
# color_transform = transforms.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=0.3 / 3.14)
rgb = color_transform(rgb)
if flip:
rgb = TTF.hflip(rgb)
rgb = TTF.rotate(rgb, img_rotation)
#io.imshow(np.array(rgb))
#io.show()
rgb = to_tensor(rgb)
rgb = normalization(rgb)
return rgb
def __len__(self):
return len(self.all_files)
# self.all_files.append(os.path.join(date, seq, 'image_2/data', image_name.split('.')[0]))
def __getitem__(self, idx):
item = self.all_files[idx]
date = str(item.split('/')[0])
seq = str(item.split('/')[1])
rgb_name = str(item.split('/')[4])
img_path = os.path.join(self.root_dir, date, seq, 'image_02/data', rgb_name+'.jpg') # png
lidar_path = os.path.join(self.root_dir, date, seq, 'velodyne_points/data', rgb_name+'.bin')
lidar_scan = np.fromfile(lidar_path, dtype=np.float32)
pc = lidar_scan.reshape((-1, 4))
valid_indices = pc[:, 0] < -3.
valid_indices = valid_indices | (pc[:, 0] > 3.)
valid_indices = valid_indices | (pc[:, 1] < -3.)
valid_indices = valid_indices | (pc[:, 1] > 3.)
pc = pc[valid_indices].copy()
pc_lidar = pc.copy()
pc_org = torch.from_numpy(pc.astype(np.float32))
if self.use_reflectance:
reflectance = pc[:, 3].copy()
reflectance = torch.from_numpy(reflectance).float()
calib = self.calib_date[date]
RT_cam02 = calib['RT2'].astype(np.float32)
# camera intrinsic parameter
calib_cam02 = calib['K2'] # 3x3
E_RT = RT_cam02
if pc_org.shape[1] == 4 or pc_org.shape[1] == 3:
pc_org = pc_org.t()
if pc_org.shape[0] == 3:
homogeneous = torch.ones(pc_org.shape[1]).unsqueeze(0)
pc_org = torch.cat((pc_org, homogeneous), 0)
elif pc_org.shape[0] == 4:
if not torch.all(pc_org[3, :] == 1.):
pc_org[3, :] = 1.
else:
raise TypeError("Wrong PointCloud shape")
pc_rot = np.matmul(E_RT, pc_org.numpy())
pc_rot = pc_rot.astype(np.float32).copy()
pc_in = torch.from_numpy(pc_rot)
h_mirror = False
# if np.random.rand() > 0.5 and self.split == 'train':
# h_mirror = True
# pc_in[0, :] *= -1
img = Image.open(img_path)
# print(img_path)
# img = cv2.imread(img_path)
img_rotation = 0.
# if self.split == 'train':
# img_rotation = np.random.uniform(-5, 5)
try:
img = self.custom_transform(img, img_rotation, h_mirror)
except OSError:
new_idx = np.random.randint(0, self.__len__())
return self.__getitem__(new_idx)
# Rotate PointCloud for img_rotation
# if self.split == 'train':
# R = mathutils.Euler((radians(img_rotation), 0, 0), 'XYZ')
# T = mathutils.Vector((0., 0., 0.))
# pc_in = rotate_forward(pc_in, R, T)
if self.split == 'train':
max_angle = self.max_r
rotz = np.random.uniform(-max_angle, max_angle) * (3.141592 / 180.0)
roty = np.random.uniform(-max_angle, max_angle) * (3.141592 / 180.0)
rotx = np.random.uniform(-max_angle, max_angle) * (3.141592 / 180.0)
transl_x = np.random.uniform(-self.max_t, self.max_t)
transl_y = np.random.uniform(-self.max_t, self.max_t)
transl_z = np.random.uniform(-self.max_t, self.max_t)
# transl_z = np.random.uniform(-self.max_t, min(self.max_t, 1.))
initial_RT = 0
else:
initial_RT = self.val_RT[idx]
rotz = initial_RT[6]
roty = initial_RT[5]
rotx = initial_RT[4]
transl_x = initial_RT[1]
transl_y = initial_RT[2]
transl_z = initial_RT[3]
# 随机设置一定范围内的标定参数扰动值
# train的时候每次都随机生成,每个epoch使用不同的参数
# test则在初始化的时候提前设置好,每个epoch都使用相同的参数
R = mathutils.Euler((rotx, roty, rotz), 'XYZ')
T = mathutils.Vector((transl_x, transl_y, transl_z))
R, T = invert_pose(R, T)
R, T = torch.tensor(R), torch.tensor(T)
#io.imshow(depth_img.numpy(), cmap='jet')
#io.show()
calib = calib_cam02
# calib = get_calib_kitti_odom(int(seq))
if h_mirror:
calib[2] = (img.shape[2] / 2)*2 - calib[2]
# sample = {'rgb': img, 'point_cloud': pc_in, 'calib': calib, 'pc_org': pc_org, 'img_path': img_path,
# 'tr_error': T, 'rot_error': R, 'seq': int(seq), 'rgb_name': rgb_name, 'item': item,
# 'extrin': E_RT, 'initial_RT': initial_RT}
sample = {'rgb': img, 'point_cloud': pc_in, 'calib': calib, 'pc_org': pc_org, 'img_path': img_path,
'tr_error': T, 'rot_error': R, 'rgb_name': rgb_name + '.png', 'item': item,
'extrin': E_RT, 'initial_RT': initial_RT, 'pc_lidar': pc_lidar}
return sample