forked from princeton-vl/DPVO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_euroc.py
154 lines (113 loc) · 4.51 KB
/
evaluate_euroc.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
import cv2
import numpy as np
import glob
import os.path as osp
import os
from pathlib import Path
import datetime
from tqdm import tqdm
from dpvo.utils import Timer
from dpvo.dpvo import DPVO
from dpvo.stream import image_stream
from dpvo.config import cfg
from dpvo.plot_utils import plot_trajectory, save_trajectory_tum_format
import torch
from multiprocessing import Process, Queue
### evo evaluation library ###
import evo
from evo.core.trajectory import PoseTrajectory3D
from evo.tools import file_interface
from evo.core import sync
import evo.main_ape as main_ape
from evo.core.metrics import PoseRelation
SKIP = 0
def show_image(image, t=0):
image = image.permute(1, 2, 0).cpu().numpy()
cv2.imshow('image', image / 255.0)
cv2.waitKey(t)
@torch.no_grad()
def run(cfg, network, imagedir, calib, stride=1, viz=False):
slam = None
queue = Queue(maxsize=8)
reader = Process(target=image_stream, args=(queue, imagedir, calib, stride, 0))
reader.start()
while 1:
(t, image, intrinsics) = queue.get()
if t < 0: break
image = torch.from_numpy(image).permute(2,0,1).cuda()
intrinsics = torch.from_numpy(intrinsics).cuda()
if viz:
show_image(image, 1)
if slam is None:
slam = DPVO(cfg, network, ht=image.shape[1], wd=image.shape[2], viz=viz)
image = image.cuda()
intrinsics = intrinsics.cuda()
with Timer("SLAM", enabled=False):
slam(t, image, intrinsics)
for _ in range(12):
slam.update()
reader.join()
return slam.terminate()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--network', type=str, default='dpvo.pth')
parser.add_argument('--config', default="config/default.yaml")
parser.add_argument('--stride', type=int, default=2)
parser.add_argument('--viz', action="store_true")
parser.add_argument('--trials', type=int, default=1)
parser.add_argument('--eurocdir', default="datasets/EUROC")
parser.add_argument('--plot', action="store_true")
parser.add_argument('--save_trajectory', action="store_true")
args = parser.parse_args()
cfg.merge_from_file(args.config)
print("\nRunning with config...")
print(cfg, "\n")
torch.manual_seed(1234)
euroc_scenes = [
"MH_01_easy",
"MH_02_easy",
"MH_03_medium",
"MH_04_difficult",
"MH_05_difficult",
"V1_01_easy",
"V1_02_medium",
"V1_03_difficult",
"V2_01_easy",
"V2_02_medium",
"V2_03_difficult",
]
results = {}
for scene in euroc_scenes:
imagedir = os.path.join(args.eurocdir, scene, "mav0/cam0/data")
groundtruth = "datasets/euroc_groundtruth/{}.txt".format(scene)
scene_results = []
for i in range(args.trials):
traj_est, timestamps = run(cfg, args.network, imagedir, "calib/euroc.txt", args.stride, args.viz)
images_list = sorted(glob.glob(os.path.join(imagedir, "*.png")))[::args.stride]
tstamps = [float(x.split('/')[-1][:-4]) for x in images_list]
traj_est = PoseTrajectory3D(
positions_xyz=traj_est[:,:3],
orientations_quat_wxyz=traj_est[:,3:],
timestamps=np.array(tstamps))
traj_ref = file_interface.read_tum_trajectory_file(groundtruth)
traj_ref, traj_est = sync.associate_trajectories(traj_ref, traj_est)
result = main_ape.ape(traj_ref, traj_est, est_name='traj',
pose_relation=PoseRelation.translation_part, align=True, correct_scale=True)
ate_score = result.stats["rmse"]
if args.plot:
scene_name = '_'.join(scene.split('/')[1:]).title()
Path("trajectory_plots").mkdir(exist_ok=True)
plot_trajectory(traj_est, traj_ref, f"Euroc {scene} Trial #{i+1} (ATE: {ate_score:.03f})",
f"trajectory_plots/Euroc_{scene}_Trial{i+1:02d}.pdf", align=True, correct_scale=True)
if args.save_trajectory:
Path("saved_trajectories").mkdir(exist_ok=True)
save_trajectory_tum_format(traj_est, f"saved_trajectories/Euroc_{scene}_Trial{i+1:02d}.txt")
scene_results.append(ate_score)
results[scene] = np.median(scene_results)
print(scene, sorted(scene_results))
xs = []
for scene in results:
print(scene, results[scene])
xs.append(results[scene])
print("AVG: ", np.mean(xs))