-
Notifications
You must be signed in to change notification settings - Fork 7
/
datasets.py
462 lines (380 loc) · 18.3 KB
/
datasets.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
import glob
import os
import cv2
import numpy as np
import pandas as pd
import torch
import tqdm
from PIL import Image
from kornia.geometry.conversions import (
rotation_matrix_to_quaternion,
quaternion_to_rotation_matrix,
QuaternionCoeffOrder
)
from torch.nn.functional import normalize
from torch.utils.data import Dataset
from torchvision import transforms
from colmap.scripts.python.read_write_model import read_model
# Image preprocessing pipeline according to PyTorch implementation
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
def collate_fn(views):
"""
Transforms list of dicts [{key1: value1, key2:value2}, {key1: value3, key2:value4}]
into a dict of lists {key1: [value1, value3], key2: [value2, value4]}.
Then stacks batch-compatible values into tensor batchs.
"""
batch = {key: [] for key in views[0].keys()}
for view in views:
for key, value in view.items():
batch[key].append(value)
for key, value in batch.items():
if key not in ['w_P', 'c_p', 'image_file']:
batch[key] = torch.stack(value)
return batch
class RelocDataset(Dataset):
"""
Dataset template class for use with PyTorch DataLoader class.
"""
def __init__(self, dataset):
"""
`dataset` must be a list of dicts providing localization data for each image.
Dicts must provide:
{
'image_file': name of image file
'image': torch.tensor image with shape (3, height, width)
'w_t_c': torch.tensor camera-to-world translation with shape (3, 1)
'c_q_w': torch.tensor world-to-camera quaternion rotation with shape (4,) in format wxyz
'c_R_w': torch.tensor world-to-camera rotation matrix with shape (3, 3)
(can be computed with quaternion_to_R)
'K': torch.tensor camera intrinsics matrix with shape (3, 3)
'w_P': torch.tensor 3D observations of the image in the world frame with shape (*, 3)
'c_p': reprojections of the 3D observations in the camera view (in pixels) with shape (*, 2)
'xmin': minimum depth of observations
'xmax': maximum depth of observations
}
"""
self.data = dataset
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return {
'image_file': self.data[idx]['image_file'],
'image': self.data[idx]['image'],
'w_t_c': self.data[idx]['w_t_c'],
'c_q_w': self.data[idx]['c_q_w'],
'c_R_w': self.data[idx]['c_R_w'],
'K': self.data[idx]['K'],
'w_P': self.data[idx]['w_P'],
'c_p': self.data[idx]['c_p'],
'xmin': self.data[idx]['xmin'],
'xmax': self.data[idx]['xmax']
}
class CambridgeDataset:
"""
Template class to load every scene of Cambridge dataset.
"""
def __init__(self, path, xmin_percentile, xmax_percentile):
"""
`path` is the path to the dataset directory,
e.g. for King's College: "/home/data/KingsCollege".
Creates 6 attributes:
- 2 lists of dicts (train and test) providing localization data for each image.
- 4 parameters (train and test) for minimum and maximum depths of observations.
"""
views = []
scene_coordinates = []
with open(os.path.join(path, 'reconstruction.nvm'), mode='r') as file:
# Skip first two lines
for _ in range(2):
file.readline()
# `n_views` is the number of images
n_views = int(file.readline())
# For each image, NVM format is:
# <File name> <focal length> <quaternion WXYZ> <camera center> <radial distortion> 0
for _ in range(n_views):
line = file.readline().split()
f = float(line[1])
K = torch.tensor([
[f, 0, 1920 / 2],
[0, f, 1080 / 2],
[0, 0, 1]
], dtype=torch.float32)
views.append({
'image_file': line[0],
'K': K,
'observations_ids': []
})
# Skip one line
file.readline()
# `n_points` is the number of scene coordinates
n_points = int(file.readline())
# For each scene coordinate, SVM format is:
# <XYZ> <RGB> <number of measurements> <List of Measurements>
for i in range(n_points):
line = file.readline().split()
scene_coordinates.append(torch.tensor(list(map(float, line[:3]))))
# `n_obs` is the number of images where the scene coordinate is observed
n_obs = int(line[6])
# Each measurement is
# <Image index> <Feature Index> <xy>
for n in range(n_obs):
views[int(line[7 + n * 4])]['observations_ids'].append(i)
views = {view.pop('image_file'): view for view in views}
scene_coordinates = torch.stack(scene_coordinates)
train_df = pd.read_csv(os.path.join(path, 'dataset_train.txt'), sep=' ', skiprows=1)
test_df = pd.read_csv(os.path.join(path, 'dataset_test.txt'), sep=' ', skiprows=1)
train_data = []
test_data = []
train_global_depths = []
test_global_depths = []
print('Loading images from dataset. This may take a while...')
for data, df, global_depths in [(train_data, train_df, train_global_depths),
(test_data, test_df, test_global_depths)]:
for line in tqdm.tqdm(df.values):
image_file = line[0]
image = preprocess(Image.open(os.path.join(path, image_file)))
w_t_c = torch.tensor(line[1:4].tolist()).view(3, 1)
c_q_w = normalize(torch.tensor(line[4:8].tolist()), dim=0)
c_R_w = quaternion_to_rotation_matrix(c_q_w, order=QuaternionCoeffOrder.WXYZ)
view = views[os.path.splitext(image_file)[0] + '.jpg']
w_P = scene_coordinates[view['observations_ids']]
c_P = c_R_w @ (w_P.T - w_t_c)
c_p = view['K'] @ c_P
c_p = c_p[:2] / c_p[2]
args_inliers = torch.where(torch.logical_and(
torch.logical_and(
torch.logical_and(c_P[2] > 0.2, c_P[2] < 1000),
torch.logical_and(c_P[0].abs() < 1000, c_P[1].abs() < 1000)
),
torch.logical_and(
torch.logical_and(c_p[0] > 0, c_p[0] < 1920),
torch.logical_and(c_p[1] > 0, c_p[1] < 1080)
)
))[0]
if args_inliers.shape[0] < 10:
tqdm.tqdm.write(f'Not using image {image_file}: [{args_inliers.shape[0]}/{w_P.shape[0]}] scene '
f'coordinates inliers')
elif w_t_c.abs().max() > 1000:
tqdm.tqdm.write(f'Not using image {image_file}: t is {w_t_c.numpy()}')
else:
if args_inliers.shape[0] != w_P.shape[0]:
tqdm.tqdm.write(f'Eliminating outliers in image {image_file}: '
f'[{args_inliers.shape[0]}/{w_P.shape[0]}] scene coordinates inliers')
depths = torch.sort(c_P.T[args_inliers][:, 2]).values
global_depths.append(depths)
data.append({
'image_file': image_file,
'image': image,
'w_t_c': w_t_c,
'c_q_w': c_q_w,
'c_R_w': c_R_w,
'w_P': w_P[args_inliers],
'c_p': c_p.T[args_inliers],
'K': view['K'],
'xmin': depths[int(xmin_percentile * (depths.shape[0] - 1))],
'xmax': depths[int(xmax_percentile * (depths.shape[0] - 1))]
})
train_global_depths = torch.sort(torch.hstack(train_global_depths)).values
test_global_depths = torch.sort(torch.hstack(test_global_depths)).values
self.train_global_xmin = train_global_depths[int(xmin_percentile * (train_global_depths.shape[0] - 1))]
self.train_global_xmax = train_global_depths[int(xmax_percentile * (train_global_depths.shape[0] - 1))]
self.test_global_xmin = test_global_depths[int(xmin_percentile * (test_global_depths.shape[0] - 1))]
self.test_global_xmax = test_global_depths[int(xmax_percentile * (test_global_depths.shape[0] - 1))]
self.train_data = train_data
self.test_data = test_data
class SevenScenesDataset:
"""
Template class to load every scene from 7-Scenes dataset
"""
def __init__(self, path, xmin_percentile, xmax_percentile):
# Camera intrinsics
K = np.array([
[585, 0, 320],
[0, 585, 240],
[0, 0, 1]
], dtype=np.float64)
K_inv = np.linalg.inv(K)
K_torch = torch.tensor(K, dtype=torch.float32)
# Grid of pixels
u = np.arange(640) + 0.5
v = np.arange(480) + 0.5
u, v = np.meshgrid(u, v)
# Array of all pixel positions in pixels
c_p_px = np.hstack([
u.reshape(-1, 1),
v.reshape(-1, 1),
np.ones((u.size, 1))
])
c_p_px_torch = torch.tensor(c_p_px[:, :2], dtype=torch.float32)
# Array of all pixels in the sensor plane
c_p = K_inv @ c_p_px.T
train_data = []
test_data = []
train_global_depths = []
test_global_depths = []
for data, file, global_depths in [(train_data, 'TrainSplit.txt', train_global_depths),
(test_data, 'TestSplit.txt', test_global_depths)]:
with open(os.path.join(path, file), mode='r') as f:
seqs = [int(line[8:]) for line in f]
for seq in seqs:
seq_dir = os.path.join(path, f'seq-{seq:02d}')
print(f'Loading seq-{seq:02d}')
for frame in tqdm.tqdm(glob.glob(os.path.join(seq_dir, '*.color.png'))):
frame = os.path.basename(frame).split('.')[0]
image_path = os.path.join(seq_dir, f'{frame}.color.png')
pose_path = os.path.join(seq_dir, f'{frame}.pose.txt')
depth_path = os.path.join(seq_dir, f'{frame}.depth.png')
image = preprocess(Image.open(image_path))
# Read camera-to-world pose
w_M_c = np.zeros((4, 4))
with open(pose_path, mode='r') as f:
for i, line in enumerate(f):
w_M_c[i] = list(map(float, line.strip().split('\t')))
# Read depth map
Z = np.array(Image.open(depth_path)).reshape(-1, 1)
# Filter outliers
args_inliers = np.logical_and(Z > 0, Z != 65535).squeeze()
# Unproject pixels
c_P = c_p.T[args_inliers] * (Z[args_inliers] / 1000)
# Convert 3D points from camera to world frame
w_P = w_M_c[:3, :3] @ c_P.T + w_M_c[:3, 3:4]
# Building rotation matrix and its quaternion
w_M_c = torch.tensor(w_M_c)
c_R_w = w_M_c[:3, :3].T.contiguous()
c_q_w = rotation_matrix_to_quaternion(c_R_w, order=QuaternionCoeffOrder.WXYZ)
# Keep the quaternion on the top hypersphere
if c_q_w[0] < 0:
c_q_w *= -1
# Sort depths
depths = Z[args_inliers].flatten()
global_depths.append(depths)
depths = np.sort(depths)
data.append({
'image_file': f'seq-{seq:02d}/{frame}.color.png',
'image': image,
'w_t_c': w_M_c[:3, 3:4].float(),
'c_q_w': c_q_w.float(),
'c_R_w': c_R_w.float(),
'w_P': torch.tensor(w_P.T, dtype=torch.float32),
'c_p': c_p_px_torch[args_inliers],
'K': K_torch,
'xmin': torch.tensor(
depths[int(xmin_percentile * (depths.size - 1))] / 1000, dtype=torch.float32
),
'xmax': torch.tensor(
depths[int(xmax_percentile * (depths.size - 1))] / 1000, dtype=torch.float32
)
})
# Sort global depths
print('Sorting depths, this may take a while...')
train_global_depths = np.sort(np.hstack(train_global_depths))
test_global_depths = np.sort(np.hstack(test_global_depths))
self.train_global_xmin = torch.tensor(
train_global_depths[int(xmin_percentile * (train_global_depths.size - 1))] / 1000,
dtype=torch.float32
)
self.train_global_xmax = torch.tensor(
train_global_depths[int(xmax_percentile * (train_global_depths.size - 1))] / 1000,
dtype=torch.float32
)
self.test_global_xmin = torch.tensor(
test_global_depths[int(xmin_percentile * (test_global_depths.size - 1))] / 1000,
dtype=torch.float32
)
self.test_global_xmax = torch.tensor(
test_global_depths[int(xmax_percentile * (test_global_depths.size - 1))] / 1000,
dtype=torch.float32
)
self.train_data = train_data
self.test_data = test_data
class COLMAPDataset:
"""
WIP class to load COLMAP scenes. Only RADIAL camera model is supported.
"""
def __init__(self, path, xmin_percentile, xmax_percentile):
"""
`path` to a folder containing:
- COLMAP model
- an `images` directory containing all images
- two lists named `list_db.txt` and `list_query.txt` containing
respectively the names of database and query images (one name per line)
"""
print('COLMAPDataset is work in progress, only supports RADIAL camera model!')
images_path = os.path.join(path, 'images')
list_query = os.path.join(path, 'list_query.txt')
list_db = os.path.join(path, 'list_db.txt')
cameras, images, points3D = read_model(path)
image_name_to_id = {image.name: i for i, image in images.items()}
scene_coordinates = torch.zeros(max(points3D.keys()) + 1, 3, dtype=torch.float64)
for i, point3D in points3D.items():
scene_coordinates[i] = torch.tensor(point3D.xyz)
train_data = []
test_data = []
train_global_depths = []
test_global_depths = []
for data, file, global_depths in zip([train_data, test_data],
[list_db, list_query],
[train_global_depths, test_global_depths]):
with open(file, 'r') as f:
image_names = f.read().splitlines()
for image_name in tqdm.tqdm(image_names):
image = images[image_name_to_id[image_name]]
camera = cameras[image.camera_id]
im = cv2.imread(os.path.join(images_path, image_name))
f, u0, v0, k1, k2 = camera.params
K = np.array([
[f, 0, u0],
[0, f, v0],
[0, 0, 1]
])
dist_coeffs = np.array([k1, k2, 0, 0])
new_K, roi = cv2.getOptimalNewCameraMatrix(
cameraMatrix=K,
distCoeffs=dist_coeffs,
imageSize=im.shape[:2][::-1],
alpha=0,
centerPrincipalPoint=True
)
new_K = torch.tensor(new_K)
new_K[0, 2] = camera.width / 2
new_K[1, 2] = camera.height / 2
# Undistort image and center its principal point
im = cv2.undistort(im, K, dist_coeffs, newCameraMatrix=new_K.numpy())
im = preprocess(Image.fromarray(im[:, :, ::-1]))
c_t_w = torch.tensor(image.tvec).view(3, 1)
c_q_w = torch.tensor(image.qvec)
# Keep the quaternion on the top hypersphere
if c_q_w[0] < 0:
c_q_w *= -1
c_R_w = quaternion_to_rotation_matrix(c_q_w, order=QuaternionCoeffOrder.WXYZ)
w_t_c = -c_R_w.T @ c_t_w
w_P = scene_coordinates[[i for i in image.point3D_ids if i != -1]]
c_P = c_R_w @ (w_P.T - w_t_c)
c_p = new_K @ c_P
c_p = c_p[:2] / c_p[2]
depths = torch.sort(c_P[2]).values
global_depths.append(depths.float())
data.append({
'image_file': image_name,
'image': im,
'w_t_c': w_t_c.float(),
'c_q_w': c_q_w.float(),
'c_R_w': c_R_w.float(),
'w_P': w_P.float(),
'c_p': c_p.T.float(),
'K': new_K.float(),
'xmin': depths[int(xmin_percentile * (depths.shape[0] - 1))].float(),
'xmax': depths[int(xmax_percentile * (depths.shape[0] - 1))].float()
})
train_global_depths = torch.sort(torch.hstack(train_global_depths)).values
test_global_depths = torch.sort(torch.hstack(test_global_depths)).values
self.train_global_xmin = train_global_depths[int(xmin_percentile * (train_global_depths.shape[0] - 1))]
self.train_global_xmax = train_global_depths[int(xmax_percentile * (train_global_depths.shape[0] - 1))]
self.test_global_xmin = test_global_depths[int(xmin_percentile * (test_global_depths.shape[0] - 1))]
self.test_global_xmax = test_global_depths[int(xmax_percentile * (test_global_depths.shape[0] - 1))]
self.train_data = train_data
self.test_data = test_data