-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmain.py
335 lines (290 loc) · 10.8 KB
/
main.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
import argparse
import datetime
import pathlib
import sys
import time
import cv2
import lietorch
import torch
import tqdm
import yaml
from mast3r_slam.global_opt import FactorGraph
from mast3r_slam.config import load_config, config, set_global_config
from mast3r_slam.dataloader import Intrinsics, load_dataset
import mast3r_slam.evaluate as eval
from mast3r_slam.frame import Mode, SharedKeyframes, SharedStates, create_frame
from mast3r_slam.mast3r_utils import (
load_mast3r,
load_retriever,
mast3r_inference_mono,
)
from mast3r_slam.multiprocess_utils import new_queue, try_get_msg
from mast3r_slam.tracker import FrameTracker
from mast3r_slam.visualization import WindowMsg, run_visualization
import torch.multiprocessing as mp
def relocalization(frame, keyframes, factor_graph, retrieval_database):
# we are adding and then removing from the keyframe, so we need to be careful.
# The lock slows viz down but safer this way...
with keyframes.lock:
kf_idx = []
retrieval_inds = retrieval_database.update(
frame,
add_after_query=False,
k=config["retrieval"]["k"],
min_thresh=config["retrieval"]["min_thresh"],
)
kf_idx += retrieval_inds
successful_loop_closure = False
if kf_idx:
keyframes.append(frame)
n_kf = len(keyframes)
kf_idx = list(kf_idx) # convert to list
frame_idx = [n_kf - 1] * len(kf_idx)
print("RELOCALIZING against kf ", n_kf - 1, " and ", kf_idx)
if factor_graph.add_factors(
frame_idx,
kf_idx,
config["reloc"]["min_match_frac"],
is_reloc=config["reloc"]["strict"],
):
retrieval_database.update(
frame,
add_after_query=True,
k=config["retrieval"]["k"],
min_thresh=config["retrieval"]["min_thresh"],
)
print("Success! Relocalized")
successful_loop_closure = True
keyframes.T_WC[n_kf - 1] = keyframes.T_WC[kf_idx[0]].clone()
else:
keyframes.pop_last()
print("Failed to relocalize")
if successful_loop_closure:
if config["use_calib"]:
factor_graph.solve_GN_calib()
else:
factor_graph.solve_GN_rays()
return successful_loop_closure
def run_backend(cfg, model, states, keyframes, K):
set_global_config(cfg)
device = keyframes.device
factor_graph = FactorGraph(model, keyframes, K, device)
retrieval_database = load_retriever(model)
mode = states.get_mode()
while mode is not Mode.TERMINATED:
mode = states.get_mode()
if mode == Mode.INIT or states.is_paused():
time.sleep(0.01)
continue
if mode == Mode.RELOC:
frame = states.get_frame()
success = relocalization(frame, keyframes, factor_graph, retrieval_database)
if success:
states.set_mode(Mode.TRACKING)
states.dequeue_reloc()
continue
idx = -1
with states.lock:
if len(states.global_optimizer_tasks) > 0:
idx = states.global_optimizer_tasks[0]
if idx == -1:
time.sleep(0.01)
continue
# Graph Construction
kf_idx = []
# k to previous consecutive keyframes
n_consec = 1
for j in range(min(n_consec, idx)):
kf_idx.append(idx - 1 - j)
frame = keyframes[idx]
retrieval_inds = retrieval_database.update(
frame,
add_after_query=True,
k=config["retrieval"]["k"],
min_thresh=config["retrieval"]["min_thresh"],
)
kf_idx += retrieval_inds
lc_inds = set(retrieval_inds)
lc_inds.discard(idx - 1)
if len(lc_inds) > 0:
print("Database retrieval", idx, ": ", lc_inds)
kf_idx = set(kf_idx) # Remove duplicates by using set
kf_idx.discard(idx) # Remove current kf idx if included
kf_idx = list(kf_idx) # convert to list
frame_idx = [idx] * len(kf_idx)
if kf_idx:
factor_graph.add_factors(
kf_idx, frame_idx, config["local_opt"]["min_match_frac"]
)
with states.lock:
states.edges_ii[:] = factor_graph.ii.cpu().tolist()
states.edges_jj[:] = factor_graph.jj.cpu().tolist()
if config["use_calib"]:
factor_graph.solve_GN_calib()
else:
factor_graph.solve_GN_rays()
with states.lock:
if len(states.global_optimizer_tasks) > 0:
idx = states.global_optimizer_tasks.pop(0)
if __name__ == "__main__":
mp.set_start_method("spawn")
torch.backends.cuda.matmul.allow_tf32 = True
torch.set_grad_enabled(False)
device = "cuda:0"
save_frames = False
datetime_now = str(datetime.datetime.now()).replace(" ", "_")
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", default="datasets/tum/rgbd_dataset_freiburg1_desk")
parser.add_argument("--config", default="config/base.yaml")
parser.add_argument("--save-as", default="default")
parser.add_argument("--no-viz", action="store_true")
parser.add_argument("--calib", default="")
args = parser.parse_args()
load_config(args.config)
print(args.dataset)
print(config)
manager = mp.Manager()
main2viz = new_queue(manager, args.no_viz)
viz2main = new_queue(manager, args.no_viz)
dataset = load_dataset(args.dataset)
dataset.subsample(config["dataset"]["subsample"])
h, w = dataset.get_img_shape()[0]
if args.calib:
with open(args.calib, "r") as f:
intrinsics = yaml.load(f, Loader=yaml.SafeLoader)
config["use_calib"] = True
dataset.use_calibration = True
dataset.camera_intrinsics = Intrinsics.from_calib(
dataset.img_size,
intrinsics["width"],
intrinsics["height"],
intrinsics["calibration"],
)
keyframes = SharedKeyframes(manager, h, w)
states = SharedStates(manager, h, w)
if not args.no_viz:
viz = mp.Process(
target=run_visualization,
args=(config, states, keyframes, main2viz, viz2main),
)
viz.start()
model = load_mast3r(device=device)
model.share_memory()
has_calib = dataset.has_calib()
use_calib = config["use_calib"]
if use_calib and not has_calib:
print("[Warning] No calibration provided for this dataset!")
sys.exit(0)
K = None
if use_calib:
K = torch.from_numpy(dataset.camera_intrinsics.K_frame).to(
device, dtype=torch.float32
)
keyframes.set_intrinsics(K)
# remove the trajectory from the previous run
if dataset.save_results:
save_dir, seq_name = eval.prepare_savedir(args, dataset)
traj_file = save_dir / f"{seq_name}.txt"
recon_file = save_dir / f"{seq_name}.ply"
if traj_file.exists():
traj_file.unlink()
if recon_file.exists():
recon_file.unlink()
tracker = FrameTracker(model, keyframes, device)
last_msg = WindowMsg()
backend = mp.Process(target=run_backend, args=(config, model, states, keyframes, K))
backend.start()
i = 0
fps_timer = time.time()
frames = []
while True:
mode = states.get_mode()
msg = try_get_msg(viz2main)
last_msg = msg if msg is not None else last_msg
if last_msg.is_terminated:
states.set_mode(Mode.TERMINATED)
break
if last_msg.is_paused and not last_msg.next:
states.pause()
time.sleep(0.01)
continue
if not last_msg.is_paused:
states.unpause()
if i == len(dataset):
states.set_mode(Mode.TERMINATED)
break
timestamp, img = dataset[i]
if save_frames:
frames.append(img)
# get frames last camera pose
T_WC = (
lietorch.Sim3.Identity(1, device=device)
if i == 0
else states.get_frame().T_WC
)
frame = create_frame(i, img, T_WC, img_size=dataset.img_size, device=device)
if mode == Mode.INIT:
# Initialize via mono inference, and encoded features neeed for database
X_init, C_init = mast3r_inference_mono(model, frame)
frame.update_pointmap(X_init, C_init)
keyframes.append(frame)
states.queue_global_optimization(len(keyframes) - 1)
states.set_mode(Mode.TRACKING)
states.set_frame(frame)
i += 1
continue
if mode == Mode.TRACKING:
add_new_kf, match_info, try_reloc = tracker.track(frame)
if try_reloc:
states.set_mode(Mode.RELOC)
states.set_frame(frame)
elif mode == Mode.RELOC:
X, C = mast3r_inference_mono(model, frame)
frame.update_pointmap(X, C)
states.set_frame(frame)
states.queue_reloc()
# In single threaded mode, make sure relocalization happen for every frame
while config["single_thread"]:
with states.lock:
if states.reloc_sem.value == 0:
break
time.sleep(0.01)
else:
raise Exception("Invalid mode")
if add_new_kf:
keyframes.append(frame)
states.queue_global_optimization(len(keyframes) - 1)
# In single threaded mode, wait for the backend to finish
while config["single_thread"]:
with states.lock:
if len(states.global_optimizer_tasks) == 0:
break
time.sleep(0.01)
# log time
if i % 30 == 0:
FPS = i / (time.time() - fps_timer)
print(f"FPS: {FPS}")
i += 1
if dataset.save_results:
save_dir, seq_name = eval.prepare_savedir(args, dataset)
eval.save_traj(save_dir, f"{seq_name}.txt", dataset.timestamps, keyframes)
eval.save_reconstruction(
save_dir,
f"{seq_name}.ply",
keyframes,
last_msg.C_conf_threshold,
)
eval.save_keyframes(
save_dir / "keyframes" / seq_name, dataset.timestamps, keyframes
)
if save_frames:
savedir = pathlib.Path(f"logs/frames/{datetime_now}")
savedir.mkdir(exist_ok=True, parents=True)
for i, frame in tqdm.tqdm(enumerate(frames), total=len(frames)):
frame = (frame * 255).clip(0, 255)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
cv2.imwrite(f"{savedir}/{i}.png", frame)
print("done")
backend.join()
if not args.no_viz:
viz.join()