-
Notifications
You must be signed in to change notification settings - Fork 4
/
evaluate.py
432 lines (369 loc) · 14.5 KB
/
evaluate.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
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "8"
import sys
import glob
import yaml
import json
import torch
import argparse
import torchvision
import numpy as np
import os.path as osp
from tqdm import tqdm
from pathlib import Path
from evo.core import sync
from functools import partial
import evo.main_ape as main_ape
from evo.core.metrics import PoseRelation
from evo.core.trajectory import PoseTrajectory3D
from utils.seed_everything import seed_everything
from ramp.data_readers.TartanEvent import TartanEvent
from utils.rotation_error_with_euler import rot_error_with_alignment_from_pose3d
from utils.eval_utils import (
read_eds_format_poses,
read_stereodavis_format_poses,
read_tartan_format_poses,
read_moonlanding_format_poses
)
from data import H5EventHandle
from ramp.utils import (
input_resize,
normalize_image,
)
from ramp.config import cfg as VO_cfg
from ramp.Ramp_vo import Ramp_vo
seed_everything(seed=1234)
sys.setrecursionlimit(100000)
def set_global_params(K_path=None, standard_pose_format=False, resize_to=None):
global fx, fy, cx, cy
if K_path is None or not os.path.exists(K_path):
fx, fy, cx, cy = [320, 320, 320, 240]
print("Using default intrinsics", [fx, fy, cx, cy])
return (fx, fy, cx, cy)
else:
# Load the YAML file
with open(K_path, "r") as file:
data = yaml.safe_load(file)
# Extract the intrinsics
intrinsics = data["cam0"]["intrinsics"]
# Extract the individual components
fx, fy, cx, cy = intrinsics
if resize_to is not None:
resolution = data["cam0"]["resolution"]
slack = np.array(resize_to) - np.array(resolution)
d_cx, d_cy = slack[0] / 2, slack[1] / 2
cx = cx + d_cx
cy = cy + d_cy
print("Using intrinsics from {}".format(K_path), (fx, fy, cx, cy))
return (fx, fy, cx, cy)
def save_results(
traj_ref, traj_est, scene, j=0, eval_type="None"
):
# save poses for finer evaluations
save_dir = osp.join(
os.getcwd(),
"trajectory_evaluation",
f"{eval_type}",
"trial_" + str(j),
scene,
)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
time_ref = (traj_ref.timestamps * 10 ** -9)[..., np.newaxis]
time_est = (traj_est.timestamps * 10 ** -9)[..., np.newaxis]
np.savetxt(
osp.join(save_dir, "stamped_groundtruth.txt"),
np.concatenate((time_ref, traj_ref.positions_xyz, traj_ref.orientations_quat_wxyz), axis=1),
)
np.savetxt(
osp.join(save_dir, "stamped_traj_estimate.txt"),
np.concatenate((time_est, traj_est.positions_xyz, traj_est.orientations_quat_wxyz), axis=1),
)
def data_loader_all_events(
config, full_scene, downsample_fact=1, norm_to=None, extension=".png"
):
images_paths = osp.join(full_scene, "image_left", "*{}".format(extension))
imfiles = sorted(glob.glob(images_paths))
evfile = osp.join(full_scene, "events.h5")
intrinsics = torch.as_tensor([fx, fy, cx, cy])
TartanEvent_loader = TartanEvent(config=config, path=full_scene)
timestamps = np.loadtxt(osp.join(full_scene, "timestamps.txt"))
# skip first element (no events for it)
image_files = sorted(imfiles)[1 :: downsample_fact]
corresponding_timestamps = timestamps[1 :: downsample_fact]
# load events and compute how many are they
event = H5EventHandle.from_path(Path(evfile))
n_events = len(event.t)
n_events_selected = TartanEvent_loader.num_events_selected
n_events_voxels = n_events // n_events_selected
corr_events_timestamps = event.t[0:n_events:n_events_selected][1::]
time_vicinity = (
np.subtract.outer(corr_events_timestamps, corresponding_timestamps) ** 2
)
corresponding_frame_indices = np.argmin(time_vicinity, axis=1)
corresponding_events_indices = np.argmin(time_vicinity, axis=0)
print("import images and events ...")
data_list = []
masks = []
i1 = 0
for i in tqdm(range(n_events_voxels)):
i0 = i1
i1 = i1 + n_events_selected
event_voxel = TartanEvent_loader.events_from_indices(
event=event, i_start=i0, i_stop=i1
)
frame_ind = corresponding_frame_indices[i]
imfile = image_files[frame_ind]
image = torchvision.io.read_image(imfile)
image = normalize_image(images=image, norm_img_to=norm_to)
# plot_events(event, image, i0, i1, i)
# the index of the smallest error between the event voxel timestamp and the image timestamp is event index
event_ind = corresponding_events_indices[frame_ind]
if event_ind == i:
mask = True
else:
mask = False
masks.append(mask)
data_list.append((image, event_voxel, intrinsics, torch.tensor([mask])))
# frame_indices = list(set(corresponding_frame_indices))
# Check this masking operation
frame_indices = list(set(corresponding_frame_indices[masks]))
return data_list, frame_indices
def _data_iterator(data_list):
for image, events, intrinsics, mask in data_list:
im = image[None, None, ...].cuda()
ev = events[None, None, ...].float().cuda()
intr = intrinsics.cuda()
mask.cuda()
yield im, ev, intr, mask
def resize_input(image, events):
default_shape = torch.tensor([480, 640])
data_shape = image.shape[-2:]
if data_shape != default_shape:
image, events = input_resize(
image, events, desired_ht=data_shape[0] + 1, desired_wh=data_shape[1] + 1
)
image = (
torch.stack((image, image, image), dim=3)[0, ...]
if image.shape[-3] == 1
else image
)
image.squeeze(0).squeeze(0)
return image, events
@torch.no_grad()
def run_pose_pred(cfg_VO, network, eval_cfg, data_list, t_horizon_to_pred, t_to_pred, deg_approx=4):
"""Run the slam on the given data_list using pose prediction algorithm
for bootstrapping and return the trajectory and timestamps.
Pose prediction typically slows down VO frequency.
Args:
cfg_VO: config for the slam
network: the network to use for the slam
eval_cfg: config for the evaluation
data_list: list of tuples (image, events, intrinsics)
t_horizon_to_pred: the time horizon to predict the future
t_to_pred: the time to start predicting the future
deg_approx: the degree of the polynomial to use for the prediction
Returns:
traj_est: the estimated trajectory
tstamps: the timestamps of the estimated trajectory
"""
train_cfg = eval_cfg["data_loader"]["train"]["args"]
slam = Ramp_vo(cfg=cfg_VO, network=network, train_cfg=train_cfg)
for t, (image, events, intrinsics, mask) in enumerate(tqdm(_data_iterator(data_list))):
image, events = resize_input(image, events)
if t < t_to_pred or t_to_pred < 0:
slam(t, input_tensor=(events, image, mask), intrinsics=intrinsics)
last_keyframe_number = slam.n
if t == t_to_pred and t_to_pred > 0:
for _ in range(12):
slam.update()
if t >= t_to_pred and t_to_pred > 0:
sec_to_pred_future = t - t_to_pred
slam.predict_future_pose(
last_keyframe_number=last_keyframe_number,
sec_to_pred_future=sec_to_pred_future,
abs_time=t,
deg=deg_approx,
)
if t == t_to_pred + t_horizon_to_pred:
break
for _ in range(12):
slam.update()
return slam.terminate()
@torch.no_grad()
def run(cfg_VO, network, eval_cfg, data_list):
"""Run the slam on the given data_list and return the trajectory and timestamps
Args:
cfg_VO: config for the slam
network: the network to use for the slam
eval_cfg: config for the evaluation
data_list: list of tuples (image, events, intrinsics)
Returns:
traj_est: the estimated trajectory
tstamps: the timestamps of the estimated trajectory
"""
train_cfg = eval_cfg["data_loader"]["train"]["args"]
slam = Ramp_vo(cfg=cfg_VO, network=network, train_cfg=train_cfg)
for t, (image, events, intrinsics, mask) in enumerate(
tqdm(_data_iterator(data_list))
):
image, events = resize_input(image, events)
slam(t, input_tensor=(events, image, mask), intrinsics=intrinsics)
for _ in range(12):
slam.update()
return slam.terminate()
def evaluate_sequence(
config_VO, net, eval_cfg, data_list, traj_ref, use_pose_pred, img_timestamps,
):
if use_pose_pred:
# Tune starting_t_to_pred and t_horizon_to_pred accordingly for your dataset
starting_t_to_pred = traj_ref.num_poses // 2
t_horizon_to_pred = traj_ref.num_poses - starting_t_to_pred
traj_est, tstamps = run_pose_pred(
cfg_VO=config_VO,
network=net,
eval_cfg=eval_cfg,
data_list=data_list,
t_to_pred=starting_t_to_pred,
t_horizon_to_pred=t_horizon_to_pred,
deg_approx=4,
)
else:
traj_est, tstamps = run(
cfg_VO=config_VO, network=net, eval_cfg=eval_cfg, data_list=data_list
)
traj_est_ = PoseTrajectory3D(
positions_xyz=traj_est[:, :3],
orientations_quat_wxyz=traj_est[:, 3:][:, (1, 2, 3, 0)],
timestamps=img_timestamps,
)
try:
traj_ref, traj_est = sync.associate_trajectories(traj_ref, traj_est_)
result = main_ape.ape(
traj_ref=traj_ref,
traj_est=traj_est,
est_name="traj",
pose_relation=PoseRelation.translation_part,
align=True,
correct_scale=True,
)
ate_score = result.stats["rmse"]
rot_score = rot_error_with_alignment_from_pose3d(
ref=traj_ref, est=traj_est, correct_scale=True
)
except:
ate_score = 1000
rot_score = [1000, 1000, 1000]
return ate_score, rot_score, traj_est, traj_ref
@torch.no_grad()
def evaluate(
net, trials=1, downsample_fact=1, config_VO=None, eval_cfg=None, results_path=None
):
test_ = eval_cfg["data_loader"]["test"]
train_ = eval_cfg["data_loader"]["train"]["args"]
norm_to = train_["norm_to"] if train_.get("norm_to") else None
test_split = test_["test_split"]
dataset_name = test_["dataset_name"]
use_pose_pred = test_["use_pose_pred"]
if config_VO is None:
config_VO = VO_cfg
config_VO.merge_from_file("config/default.yaml")
results = {}
for scene in test_split:
print(f"loading training data ... scene:{scene}")
if not os.path.exists(scene):
raise FileNotFoundError(f"scene {scene} not found")
traj_ref_path = osp.join(scene, "pose_left.txt")
scene_name = os.path.basename(scene) if os.path.isdir(scene) else scene
timestamps_path = osp.join(scene, "timestamps.txt")
img_timestamps = np.loadtxt(timestamps_path)
if "Tartan" in dataset_name:
set_global_params(K_path=osp.join(scene, "K.yaml"))
traj_ref = read_tartan_format_poses(
traj_path=traj_ref_path, timestamps_path=timestamps_path
)
elif "StereoDavis" in dataset_name:
set_global_params(
K_path=osp.join(scene, "K.yaml"),
standard_pose_format=True,
)
img_timestamps = img_timestamps / 1e6
traj_ref = read_stereodavis_format_poses(
traj_path=osp.join(scene, "poses.txt"),
timestamps_path=osp.join(scene, "timestamps_poses.txt"),
)
elif "EDS" in dataset_name:
set_global_params(
K_path=osp.join(scene, "K.yaml"),
standard_pose_format=True,
)
img_timestamps = img_timestamps / 1e6
traj_ref = read_eds_format_poses(traj_ref_path)
elif "MoonLanding" in dataset_name:
set_global_params(K_path=osp.join(scene, "K.yaml"))
traj_ref = read_moonlanding_format_poses(
traj_path=traj_ref_path, timestamps_path=timestamps_path
)
else:
raise NotImplementedError("dataset not supported")
data_list, frame_indices = data_loader_all_events(
config=eval_cfg,
full_scene=scene,
downsample_fact=downsample_fact,
norm_to=norm_to,
)
eval_subtraj = partial(
evaluate_sequence,
config_VO=config_VO,
net=net,
eval_cfg=eval_cfg,
data_list=data_list,
traj_ref=traj_ref,
use_pose_pred=use_pose_pred,
img_timestamps=img_timestamps[frame_indices],
)
save_res = partial(save_results, scene=scene_name, eval_type="full_data")
results[scene] = {}
for j in range(trials):
ate_error, rot_error, traj_est, traj_ref = eval_subtraj()
print("\n full_data ate ------->", ate_error)
print("\n full_data rot ------->", rot_error)
save_res(traj_est=traj_est, traj_ref=traj_ref, j=j)
results[scene][f"trial_{j}"] = {
"ate": ate_error,
"rot_err": list(rot_error),
}
if results_path is not None:
with open(results_path, "w") as json_file:
json.dump(results, json_file, indent=4)
if results_path is not None:
with open(results_path, "w") as json_file:
results["test_info"] = [
{"config_VO": dict(config_VO)},
train_,
test_,
]
json.dump(results, json_file, indent=4)
return results
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--weights", default="dpvo.pth")
parser.add_argument("--config_VO", default="config/default.yaml")
parser.add_argument("--config_eval", type=str, default="config/TartanEvent.json")
parser.add_argument("--trials", type=int, default=1)
parser.add_argument("--downsample_fact", type=int, default=1)
parser.add_argument("--results_path", type=str, default=None)
args = parser.parse_args()
VO_cfg.merge_from_file(args.config_VO)
eval_cfg = json.load(open(args.config_eval))
print("Running evaluation...")
results = evaluate(
config_VO=VO_cfg,
eval_cfg=eval_cfg,
net=args.weights,
trials=args.trials,
downsample_fact=args.downsample_fact,
results_path=args.results_path,
)
for k in results:
print(k, results[k])