-
Notifications
You must be signed in to change notification settings - Fork 128
/
slam.py
258 lines (220 loc) · 8.87 KB
/
slam.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
import os
import sys
import time
from argparse import ArgumentParser
from datetime import datetime
import torch
import torch.multiprocessing as mp
import yaml
from munch import munchify
import wandb
from gaussian_splatting.scene.gaussian_model import GaussianModel
from gaussian_splatting.utils.system_utils import mkdir_p
from gui import gui_utils, slam_gui
from utils.config_utils import load_config
from utils.dataset import load_dataset
from utils.eval_utils import eval_ate, eval_rendering, save_gaussians
from utils.logging_utils import Log
from utils.multiprocessing_utils import FakeQueue
from utils.slam_backend import BackEnd
from utils.slam_frontend import FrontEnd
class SLAM:
def __init__(self, config, save_dir=None):
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
self.config = config
self.save_dir = save_dir
model_params = munchify(config["model_params"])
opt_params = munchify(config["opt_params"])
pipeline_params = munchify(config["pipeline_params"])
self.model_params, self.opt_params, self.pipeline_params = (
model_params,
opt_params,
pipeline_params,
)
self.live_mode = self.config["Dataset"]["type"] == "realsense"
self.monocular = self.config["Dataset"]["sensor_type"] == "monocular"
self.use_spherical_harmonics = self.config["Training"]["spherical_harmonics"]
self.use_gui = self.config["Results"]["use_gui"]
if self.live_mode:
self.use_gui = True
self.eval_rendering = self.config["Results"]["eval_rendering"]
model_params.sh_degree = 3 if self.use_spherical_harmonics else 0
self.gaussians = GaussianModel(model_params.sh_degree, config=self.config)
self.gaussians.init_lr(6.0)
self.dataset = load_dataset(
model_params, model_params.source_path, config=config
)
self.gaussians.training_setup(opt_params)
bg_color = [0, 0, 0]
self.background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
frontend_queue = mp.Queue()
backend_queue = mp.Queue()
q_main2vis = mp.Queue() if self.use_gui else FakeQueue()
q_vis2main = mp.Queue() if self.use_gui else FakeQueue()
self.config["Results"]["save_dir"] = save_dir
self.config["Training"]["monocular"] = self.monocular
self.frontend = FrontEnd(self.config)
self.backend = BackEnd(self.config)
self.frontend.dataset = self.dataset
self.frontend.background = self.background
self.frontend.pipeline_params = self.pipeline_params
self.frontend.frontend_queue = frontend_queue
self.frontend.backend_queue = backend_queue
self.frontend.q_main2vis = q_main2vis
self.frontend.q_vis2main = q_vis2main
self.frontend.set_hyperparams()
self.backend.gaussians = self.gaussians
self.backend.background = self.background
self.backend.cameras_extent = 6.0
self.backend.pipeline_params = self.pipeline_params
self.backend.opt_params = self.opt_params
self.backend.frontend_queue = frontend_queue
self.backend.backend_queue = backend_queue
self.backend.live_mode = self.live_mode
self.backend.set_hyperparams()
self.params_gui = gui_utils.ParamsGUI(
pipe=self.pipeline_params,
background=self.background,
gaussians=self.gaussians,
q_main2vis=q_main2vis,
q_vis2main=q_vis2main,
)
backend_process = mp.Process(target=self.backend.run)
if self.use_gui:
gui_process = mp.Process(target=slam_gui.run, args=(self.params_gui,))
gui_process.start()
time.sleep(5)
backend_process.start()
self.frontend.run()
backend_queue.put(["pause"])
end.record()
torch.cuda.synchronize()
# empty the frontend queue
N_frames = len(self.frontend.cameras)
FPS = N_frames / (start.elapsed_time(end) * 0.001)
Log("Total time", start.elapsed_time(end) * 0.001, tag="Eval")
Log("Total FPS", N_frames / (start.elapsed_time(end) * 0.001), tag="Eval")
if self.eval_rendering:
self.gaussians = self.frontend.gaussians
kf_indices = self.frontend.kf_indices
ATE = eval_ate(
self.frontend.cameras,
self.frontend.kf_indices,
self.save_dir,
0,
final=True,
monocular=self.monocular,
)
rendering_result = eval_rendering(
self.frontend.cameras,
self.gaussians,
self.dataset,
self.save_dir,
self.pipeline_params,
self.background,
kf_indices=kf_indices,
iteration="before_opt",
)
columns = ["tag", "psnr", "ssim", "lpips", "RMSE ATE", "FPS"]
metrics_table = wandb.Table(columns=columns)
metrics_table.add_data(
"Before",
rendering_result["mean_psnr"],
rendering_result["mean_ssim"],
rendering_result["mean_lpips"],
ATE,
FPS,
)
# re-used the frontend queue to retrive the gaussians from the backend.
while not frontend_queue.empty():
frontend_queue.get()
backend_queue.put(["color_refinement"])
while True:
if frontend_queue.empty():
time.sleep(0.01)
continue
data = frontend_queue.get()
if data[0] == "sync_backend" and frontend_queue.empty():
gaussians = data[1]
self.gaussians = gaussians
break
rendering_result = eval_rendering(
self.frontend.cameras,
self.gaussians,
self.dataset,
self.save_dir,
self.pipeline_params,
self.background,
kf_indices=kf_indices,
iteration="after_opt",
)
metrics_table.add_data(
"After",
rendering_result["mean_psnr"],
rendering_result["mean_ssim"],
rendering_result["mean_lpips"],
ATE,
FPS,
)
wandb.log({"Metrics": metrics_table})
save_gaussians(self.gaussians, self.save_dir, "final_after_opt", final=True)
backend_queue.put(["stop"])
backend_process.join()
Log("Backend stopped and joined the main thread")
if self.use_gui:
q_main2vis.put(gui_utils.GaussianPacket(finish=True))
gui_process.join()
Log("GUI Stopped and joined the main thread")
def run(self):
pass
if __name__ == "__main__":
# Set up command line argument parser
parser = ArgumentParser(description="Training script parameters")
parser.add_argument("--config", type=str)
parser.add_argument("--eval", action="store_true")
args = parser.parse_args(sys.argv[1:])
mp.set_start_method("spawn")
with open(args.config, "r") as yml:
config = yaml.safe_load(yml)
config = load_config(args.config)
save_dir = None
if args.eval:
Log("Running MonoGS in Evaluation Mode")
Log("Following config will be overriden")
Log("\tsave_results=True")
config["Results"]["save_results"] = True
Log("\tuse_gui=False")
config["Results"]["use_gui"] = False
Log("\teval_rendering=True")
config["Results"]["eval_rendering"] = True
Log("\tuse_wandb=True")
config["Results"]["use_wandb"] = True
if config["Results"]["save_results"]:
mkdir_p(config["Results"]["save_dir"])
current_datetime = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
path = config["Dataset"]["dataset_path"].split("/")
save_dir = os.path.join(
config["Results"]["save_dir"], path[-3] + "_" + path[-2], current_datetime
)
tmp = args.config
tmp = tmp.split(".")[0]
config["Results"]["save_dir"] = save_dir
mkdir_p(save_dir)
with open(os.path.join(save_dir, "config.yml"), "w") as file:
documents = yaml.dump(config, file)
Log("saving results in " + save_dir)
run = wandb.init(
project="MonoGS",
name=f"{tmp}_{current_datetime}",
config=config,
mode=None if config["Results"]["use_wandb"] else "disabled",
)
wandb.define_metric("frame_idx")
wandb.define_metric("ate*", step_metric="frame_idx")
slam = SLAM(config, save_dir=save_dir)
slam.run()
wandb.finish()
# All done
Log("Done.")