-
Notifications
You must be signed in to change notification settings - Fork 8
/
kitti_odometry.py
executable file
·617 lines (545 loc) · 22.2 KB
/
kitti_odometry.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# Copyright (C) Huangying Zhan 2019. All rights reserved.
# https://github.com/Huangying-Zhan/kitti-odom-eval
import copy
from matplotlib import pyplot as plt
import numpy as np
import os
from glob import glob
def scale_lse_solver(X, Y):
"""Least-sqaure-error solver
Compute optimal scaling factor so that s(X)-Y is minimum
Args:
X (KxN array): current data
Y (KxN array): reference data
Returns:
scale (float): scaling factor
"""
scale = np.sum(X * Y) / np.sum(X ** 2)
return scale
def umeyama_alignment(x, y, with_scale=False):
"""
Computes the least squares solution parameters of an Sim(m) matrix
that minimizes the distance between a set of registered points.
Umeyama, Shinji: Least-squares estimation of transformation parameters
between two point patterns. IEEE PAMI, 1991
:param x: mxn matrix of points, m = dimension, n = nr. of data points
:param y: mxn matrix of points, m = dimension, n = nr. of data points
:param with_scale: set to True to align also the scale (default: 1.0 scale)
:return: r, t, c - rotation matrix, translation vector and scale factor
"""
if x.shape != y.shape:
assert False, "x.shape not equal to y.shape"
# m = dimension, n = nr. of data points
m, n = x.shape
# means, eq. 34 and 35
mean_x = x.mean(axis=1)
mean_y = y.mean(axis=1)
# variance, eq. 36
# "transpose" for column subtraction
sigma_x = 1.0 / n * (np.linalg.norm(x - mean_x[:, np.newaxis]) ** 2)
# covariance matrix, eq. 38
outer_sum = np.zeros((m, m))
for i in range(n):
outer_sum += np.outer((y[:, i] - mean_y), (x[:, i] - mean_x))
cov_xy = np.multiply(1.0 / n, outer_sum)
# SVD (text betw. eq. 38 and 39)
u, d, v = np.linalg.svd(cov_xy)
# S matrix, eq. 43
s = np.eye(m)
if np.linalg.det(u) * np.linalg.det(v) < 0.0:
# Ensure a RHS coordinate system (Kabsch algorithm).
s[m - 1, m - 1] = -1
# rotation, eq. 40
r = u.dot(s).dot(v)
# scale & translation, eq. 42 and 41
c = 1 / sigma_x * np.trace(np.diag(d).dot(s)) if with_scale else 1.0
t = mean_y - np.multiply(c, r.dot(mean_x))
return r, t, c
class KittiEvalOdom:
"""Evaluate odometry result
Usage example:
vo_eval = KittiEvalOdom()
vo_eval.eval(gt_pose_txt_dir, result_pose_txt_dir)
"""
def __init__(self):
self.lengths = [100, 200, 300, 400, 500, 600, 700, 800]
self.num_lengths = len(self.lengths)
def load_poses_from_txt(self, file_name):
"""Load poses from txt (KITTI format)
Each line in the file should follow one of the following structures
(1) idx pose(3x4 matrix in terms of 12 numbers)
(2) pose(3x4 matrix in terms of 12 numbers)
Args:
file_name (str): txt file path
Returns:
poses (dict): {idx: 4x4 array}
"""
f = open(file_name, "r")
if not f:
print("error to open gt files on {}".format(file_name))
return
s = f.readlines()
f.close()
poses = {}
for cnt, line in enumerate(s):
P = np.eye(4)
line_split = [float(i) for i in line.split(" ") if i != ""]
withIdx = len(line_split) == 13
for row in range(3):
for col in range(4):
P[row, col] = line_split[row * 4 + col + withIdx]
if withIdx:
frame_idx = line_split[0]
else:
frame_idx = cnt
poses[frame_idx] = P
return poses
def trajectory_distances(self, poses):
"""Compute distance for each pose w.r.t frame-0
Args:
poses (dict): {idx: 4x4 array}
Returns:
dist (float list): distance of each pose w.r.t frame-0
"""
dist = [0]
sort_frame_idx = sorted(poses.keys())
for i in range(len(sort_frame_idx) - 1):
cur_frame_idx = sort_frame_idx[i]
next_frame_idx = sort_frame_idx[i + 1]
P1 = poses[cur_frame_idx]
P2 = poses[next_frame_idx]
dx = P1[0, 3] - P2[0, 3]
dy = P1[1, 3] - P2[1, 3]
dz = P1[2, 3] - P2[2, 3]
dist.append(dist[i] + np.sqrt(dx ** 2 + dy ** 2 + dz ** 2))
return dist
def rotation_error(self, pose_error):
"""Compute rotation error
Args:
pose_error (4x4 array): relative pose error
Returns:
rot_error (float): rotation error
"""
a = pose_error[0, 0]
b = pose_error[1, 1]
c = pose_error[2, 2]
d = 0.5 * (a + b + c - 1.0)
rot_error = np.arccos(max(min(d, 1.0), -1.0))
return rot_error
def translation_error(self, pose_error):
"""Compute translation error
Args:
pose_error (4x4 array): relative pose error
Returns:
trans_error (float): translation error
"""
dx = pose_error[0, 3]
dy = pose_error[1, 3]
dz = pose_error[2, 3]
trans_error = np.sqrt(dx ** 2 + dy ** 2 + dz ** 2)
return trans_error
def last_frame_from_segment_length(self, dist, first_frame, length):
"""Find frame (index) that away from the first_frame with
the required distance
Args:
dist (float list): distance of each pose w.r.t frame-0
first_frame (int): start-frame index
length (float): required distance
Returns:
i (int) / -1: end-frame index. if not found return -1
"""
for i in range(first_frame, len(dist), 1):
if dist[i] > (dist[first_frame] + length):
return i
return -1
def calc_sequence_errors(self, poses_gt, poses_result):
"""calculate sequence error
Args:
poses_gt (dict): {idx: 4x4 array}, ground truth poses
poses_result (dict): {idx: 4x4 array}, predicted poses
Returns:
err (list list): [first_frame, rotation error, translation error, length, speed]
- first_frame: frist frame index
- rotation error: rotation error per length
- translation error: translation error per length
- length: evaluation trajectory length
- speed: car speed (#FIXME: 10FPS is assumed)
"""
err = []
dist = self.trajectory_distances(poses_gt)
self.step_size = 10
for first_frame in range(0, len(poses_gt), self.step_size):
for i in range(self.num_lengths):
len_ = self.lengths[i]
last_frame = self.last_frame_from_segment_length(
dist, first_frame, len_
)
# Continue if sequence not long enough
if (
last_frame == -1
or not (last_frame in poses_result.keys())
or not (first_frame in poses_result.keys())
):
continue
# compute rotational and translational errors
pose_delta_gt = np.dot(
np.linalg.inv(poses_gt[first_frame]), poses_gt[last_frame]
)
pose_delta_result = np.dot(
np.linalg.inv(poses_result[first_frame]), poses_result[last_frame]
)
pose_error = np.dot(np.linalg.inv(pose_delta_result), pose_delta_gt)
r_err = self.rotation_error(pose_error)
t_err = self.translation_error(pose_error)
# compute speed
num_frames = last_frame - first_frame + 1.0
speed = len_ / (0.1 * num_frames)
err.append([first_frame, r_err / len_, t_err / len_, len_, speed])
return err
def save_sequence_errors(self, err, file_name):
"""Save sequence error
Args:
err (list list): error information
file_name (str): txt file for writing errors
"""
fp = open(file_name, "w")
for i in err:
line_to_write = " ".join([str(j) for j in i])
fp.writelines(line_to_write + "\n")
fp.close()
def compute_overall_err(self, seq_err):
"""Compute average translation & rotation errors
Args:
seq_err (list list): [[r_err, t_err],[r_err, t_err],...]
- r_err (float): rotation error
- t_err (float): translation error
Returns:
ave_t_err (float): average translation error
ave_r_err (float): average rotation error
"""
t_err = 0
r_err = 0
seq_len = len(seq_err)
if seq_len > 0:
for item in seq_err:
r_err += item[1]
t_err += item[2]
ave_t_err = t_err / seq_len
ave_r_err = r_err / seq_len
return ave_t_err, ave_r_err
else:
return 0, 0
def plot_trajectory(self, poses_gt, poses_result, file_name):
"""Plot trajectory for both GT and prediction
Args:
poses_gt (dict): {idx: 4x4 array}; ground truth poses
poses_result (dict): {idx: 4x4 array}; predicted poses
file_name (str): the results file named.
"""
print("plot_trajectory")
plot_keys = ["Ground Truth", file_name]
fontsize_ = 20
poses_dict = {}
poses_dict["Ground Truth"] = poses_gt
poses_dict[file_name] = poses_result
fig = plt.figure()
ax = plt.gca()
ax.set_aspect("equal")
for key in plot_keys:
pos_xz = []
frame_idx_list = sorted(poses_dict[file_name].keys())
for frame_idx in frame_idx_list:
# pose = np.linalg.inv(poses_dict[key][frame_idx_list[0]]) @ poses_dict[key][frame_idx]
pose = poses_dict[key][frame_idx]
pos_xz.append([pose[0, 3], pose[2, 3]])
pos_xz = np.asarray(pos_xz)
plt.plot(pos_xz[:, 0], pos_xz[:, 1], label=key)
plt.legend(loc="upper right", prop={"size": fontsize_})
plt.xticks(fontsize=fontsize_)
plt.yticks(fontsize=fontsize_)
plt.xlabel("x (m)", fontsize=fontsize_)
plt.ylabel("z (m)", fontsize=fontsize_)
fig.set_size_inches(10, 10)
png_title = "{}".format(file_name)
fig_pdf = self.plot_path_dir + "/" + png_title + ".pdf"
plt.savefig(fig_pdf, bbox_inches="tight", pad_inches=0)
plt.close(fig)
def plot_error(self, avg_segment_errs, file_name):
"""Plot per-length error
Args:
avg_segment_errs (dict): {100:[avg_t_err, avg_r_err],...}
file_name (str): the results file named.
"""
# Translation error
print("plot_error")
plot_y = []
plot_x = []
for len_ in self.lengths:
plot_x.append(len_)
if len(avg_segment_errs[len_]) > 0:
plot_y.append(avg_segment_errs[len_][0] * 100)
else:
plot_y.append(0)
fontsize_ = 10
fig = plt.figure()
plt.plot(plot_x, plot_y, "bs-", label="Translation Error")
plt.ylabel("Translation Error (%)", fontsize=fontsize_)
plt.xlabel("Path Length (m)", fontsize=fontsize_)
plt.legend(loc="upper right", prop={"size": fontsize_})
fig.set_size_inches(5, 5)
fig_pdf = self.plot_error_dir + "/trans_err_{}.pdf".format(file_name)
plt.savefig(fig_pdf, bbox_inches="tight", pad_inches=0)
plt.close(fig)
# Rotation error
plot_y = []
plot_x = []
for len_ in self.lengths:
plot_x.append(len_)
if len(avg_segment_errs[len_]) > 0:
plot_y.append(avg_segment_errs[len_][1] / np.pi * 180 * 100)
else:
plot_y.append(0)
fontsize_ = 10
fig = plt.figure()
plt.plot(plot_x, plot_y, "bs-", label="Rotation Error")
plt.ylabel("Rotation Error (deg/100m)", fontsize=fontsize_)
plt.xlabel("Path Length (m)", fontsize=fontsize_)
plt.legend(loc="upper right", prop={"size": fontsize_})
fig.set_size_inches(5, 5)
fig_pdf = self.plot_error_dir + "/rot_err_{}.pdf".format(file_name)
plt.savefig(fig_pdf, bbox_inches="tight", pad_inches=0)
plt.close(fig)
def compute_segment_error(self, seq_errs):
"""This function calculates average errors for different segment.
Args:
seq_errs (list list): list of errs; [first_frame, rotation error, translation error, length, speed]
- first_frame: frist frame index
- rotation error: rotation error per length
- translation error: translation error per length
- length: evaluation trajectory length
- speed: car speed (#FIXME: 10FPS is assumed)
Returns:
avg_segment_errs (dict): {100:[avg_t_err, avg_r_err],...}
"""
segment_errs = {}
avg_segment_errs = {}
for len_ in self.lengths:
segment_errs[len_] = []
# Get errors
for err in seq_errs:
len_ = err[3]
t_err = err[2]
r_err = err[1]
segment_errs[len_].append([t_err, r_err])
# Compute average
for len_ in self.lengths:
if segment_errs[len_] != []:
avg_t_err = np.mean(np.asarray(segment_errs[len_])[:, 0])
avg_r_err = np.mean(np.asarray(segment_errs[len_])[:, 1])
avg_segment_errs[len_] = [avg_t_err, avg_r_err]
else:
avg_segment_errs[len_] = []
return avg_segment_errs
def compute_ATE(self, gt, pred):
"""Compute RMSE of ATE
Args:
gt (4x4 array dict): ground-truth poses
pred (4x4 array dict): predicted poses
"""
errors = []
idx_0 = list(pred.keys())[0]
gt_0 = gt[idx_0]
pred_0 = pred[idx_0]
for i in pred:
# cur_gt = np.linalg.inv(gt_0) @ gt[i]
cur_gt = gt[i]
gt_xyz = cur_gt[:3, 3]
# cur_pred = np.linalg.inv(pred_0) @ pred[i]
cur_pred = pred[i]
pred_xyz = cur_pred[:3, 3]
align_err = gt_xyz - pred_xyz
# print('i: ', i)
# print("gt: ", gt_xyz)
# print("pred: ", pred_xyz)
# input("debug")
errors.append(np.sqrt(np.sum(align_err ** 2)))
ate = np.sqrt(np.mean(np.asarray(errors) ** 2))
return ate
def compute_RPE(self, gt, pred):
"""Compute RPE
Args:
gt (4x4 array dict): ground-truth poses
pred (4x4 array dict): predicted poses
Returns:
rpe_trans
rpe_rot
"""
trans_errors = []
rot_errors = []
for i in list(pred.keys())[:-1]:
if (i + 1 in pred.keys()) and (i and i + 1 in gt.keys()):
gt1 = gt[i]
gt2 = gt[i + 1]
gt_rel = np.linalg.inv(gt1) @ gt2
pred1 = pred[i]
pred2 = pred[i + 1]
pred_rel = np.linalg.inv(pred1) @ pred2
rel_err = np.linalg.inv(gt_rel) @ pred_rel
trans_errors.append(self.translation_error(rel_err))
rot_errors.append(self.rotation_error(rel_err))
# rpe_trans = np.sqrt(np.mean(np.asarray(trans_errors) ** 2))
# rpe_rot = np.sqrt(np.mean(np.asarray(rot_errors) ** 2))
rpe_trans = np.mean(np.asarray(trans_errors))
rpe_rot = np.mean(np.asarray(rot_errors))
return rpe_trans, rpe_rot
def scale_optimization(self, gt, pred):
"""Optimize scaling factor
Args:
gt (4x4 array dict): ground-truth poses
pred (4x4 array dict): predicted poses
Returns:
new_pred (4x4 array dict): predicted poses after optimization
"""
pred_updated = copy.deepcopy(pred)
xyz_pred = []
xyz_ref = []
for i in pred:
pose_pred = pred[i]
pose_ref = gt[i]
xyz_pred.append(pose_pred[:3, 3])
xyz_ref.append(pose_ref[:3, 3])
xyz_pred = np.asarray(xyz_pred)
xyz_ref = np.asarray(xyz_ref)
scale = scale_lse_solver(xyz_pred, xyz_ref)
for i in pred_updated:
pred_updated[i][:3, 3] *= scale
return pred_updated
def write_result(self, f, seq, errs):
"""Write result into a txt file
Args:
f (IOWrapper)
seq (int): sequence number
errs (list): [ave_t_err, ave_r_err, ate, rpe_trans, rpe_rot]
"""
ave_t_err, ave_r_err, ate, rpe_trans, rpe_rot = errs
lines = []
lines.append("Sequence: \t {} \n".format(seq))
lines.append("Trans. err. (%): \t {:.3f} \n".format(ave_t_err * 100))
lines.append(
"Rot. err. (deg/100m): \t {:.3f} \n".format(ave_r_err / np.pi * 180 * 100)
)
lines.append("ATE (m): \t {:.3f} \n".format(ate))
lines.append("RPE (m): \t {:.3f} \n".format(rpe_trans))
lines.append("RPE (deg): \t {:.3f} \n\n".format(rpe_rot * 180 / np.pi))
for line in lines:
f.writelines(line)
def eval(self, args):
"""Evaulate required/available sequences
Args:
gt_dir (str): ground truth poses txt files path
result_dir (str): pose predictions txt files directory
alignment (str): if not None, optimize poses by
- scale: optimize scale factor for trajectory alignment and evaluation
- scale_7dof: optimize 7dof for alignment and use scale for trajectory evaluation
- 7dof: optimize 7dof for alignment and evaluation
- 6dof: optimize 6dof for alignment and evaluation
"""
# Initialization
gt_pose_path = args.gt_pose_txt
result_dir = args.dest
alignment = args.align
ave_t_errs = []
ave_r_errs = []
seq_ate = []
seq_rpe_trans = []
seq_rpe_rot = []
if args.named is None:
file_name = "eval"
else:
file_name = args.named
# Create result directory
error_dir = result_dir + "/errors"
self.plot_path_dir = result_dir + "/plot_path"
self.plot_error_dir = result_dir + "/plot_error"
result_txt = os.path.join(result_dir, "pose_result.txt")
f = open(result_txt, "w")
if not os.path.exists(error_dir):
os.makedirs(error_dir)
if not os.path.exists(self.plot_path_dir):
os.makedirs(self.plot_path_dir)
if not os.path.exists(self.plot_error_dir):
os.makedirs(self.plot_error_dir)
poses_gt = self.load_poses_from_txt(gt_pose_path)
poses_result = self.load_poses_from_txt(result_dir + "/pose.txt")
self.result_file_name = result_dir + "/eval_pose.txt"
# Pose alignment to first frame
idx_0 = sorted(list(poses_result.keys()))[0]
pred_0 = poses_result[idx_0]
gt_0 = poses_gt[idx_0]
for cnt in poses_result:
poses_result[cnt] = np.linalg.inv(pred_0) @ poses_result[cnt]
poses_gt[int(cnt)] = np.linalg.inv(gt_0) @ poses_gt[cnt]
if alignment == "scale":
poses_result = self.scale_optimization(poses_gt, poses_result)
elif alignment == "scale_7dof" or alignment == "7dof" or alignment == "6dof":
# get XYZ
xyz_gt = []
xyz_result = []
for cnt in poses_result:
xyz_gt.append(
[poses_gt[cnt][0, 3], poses_gt[cnt][1, 3], poses_gt[cnt][2, 3]]
)
xyz_result.append(
[
poses_result[cnt][0, 3],
poses_result[cnt][1, 3],
poses_result[cnt][2, 3],
]
)
xyz_gt = np.asarray(xyz_gt).transpose(1, 0)
xyz_result = np.asarray(xyz_result).transpose(1, 0)
r, t, scale = umeyama_alignment(xyz_result, xyz_gt, alignment != "6dof")
align_transformation = np.eye(4)
align_transformation[:3:, :3] = r
align_transformation[:3, 3] = t
for cnt in poses_result:
poses_result[cnt][:3, 3] *= scale
if alignment == "7dof" or alignment == "6dof":
poses_result[cnt] = align_transformation @ poses_result[cnt]
# compute sequence errors
seq_err = self.calc_sequence_errors(poses_gt, poses_result)
self.save_sequence_errors(seq_err, error_dir + "/" + file_name)
# Compute segment errors
avg_segment_errs = self.compute_segment_error(seq_err)
# compute overall error
ave_t_err, ave_r_err = self.compute_overall_err(seq_err)
print("Translational error (%): ", ave_t_err * 100)
print("Rotational error (deg/100m): ", ave_r_err / np.pi * 180 * 100)
ave_t_errs.append(ave_t_err)
ave_r_errs.append(ave_r_err)
# Compute ATE
ate = self.compute_ATE(poses_gt, poses_result)
seq_ate.append(ate)
print("ATE (m): ", ate)
# Compute RPE
rpe_trans, rpe_rot = self.compute_RPE(poses_gt, poses_result)
seq_rpe_trans.append(rpe_trans)
seq_rpe_rot.append(rpe_rot)
print("RPE (m): ", rpe_trans)
print("RPE (deg): ", rpe_rot * 180 / np.pi)
# Plotting
if not args.is_bash:
self.plot_trajectory(poses_gt, poses_result, file_name)
self.plot_error(avg_segment_errs, file_name)
# Save result summary
self.write_result(
f, file_name, [ave_t_err, ave_r_err, ate, rpe_trans, rpe_rot]
)
f.close()
print("-------------------- For Copying ------------------------------")
for i in range(len(ave_t_errs)):
print("{0:.2f}".format(ave_t_errs[i] * 100))
print("{0:.2f}".format(ave_r_errs[i] / np.pi * 180 * 100))
print("{0:.2f}".format(seq_ate[i]))
print("{0:.3f}".format(seq_rpe_trans[i]))
print("{0:.3f}".format(seq_rpe_rot[i] * 180 / np.pi))