-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathslam.py
executable file
·154 lines (133 loc) · 5.33 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
import os
from argparse import ArgumentParser
from utils.config_utils import read_config
parser = ArgumentParser(description="Training script parameters")
parser.add_argument("--config", type=str, default="configs/replica/office0.yaml")
args = parser.parse_args()
config_path = args.config
args = read_config(config_path)
os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(str(device) for device in args.device_list)
import torch
import json
from utils.camera_utils import loadCam
from arguments import DatasetParams, MapParams, OptimizationParams
from scene import Dataset
from SLAM.multiprocess.mapper import Mapping
from SLAM.multiprocess.tracker import Tracker
from SLAM.utils import *
from SLAM.eval import eval_frame
from utils.general_utils import safe_state
from utils.monitor import Recorder
torch.set_printoptions(4, sci_mode=False)
def main():
# set visible devices
time_recorder = Recorder(args.device_list[0])
optimization_params = OptimizationParams(parser)
dataset_params = DatasetParams(parser, sentinel=True)
map_params = MapParams(parser)
safe_state(args.quiet)
optimization_params = optimization_params.extract(args)
dataset_params = dataset_params.extract(args)
map_params = map_params.extract(args)
# Initialize dataset
dataset = Dataset(
dataset_params,
shuffle=False,
resolution_scales=dataset_params.resolution_scales,
)
record_mem = args.record_mem
gaussian_map = Mapping(args, time_recorder)
gaussian_map.create_workspace()
gaussian_tracker = Tracker(args)
# save config file
prepare_cfg(args)
# set time log
tracker_time_sum = 0
mapper_time_sum = 0
# start SLAM
for frame_id, frame_info in enumerate(dataset.scene_info.train_cameras):
curr_frame = loadCam(
dataset_params, frame_id, frame_info, dataset_params.resolution_scales[0]
)
print("\n========== curr frame is: %d ==========\n" % frame_id)
move_to_gpu(curr_frame)
start_time = time.time()
# tracker process
frame_map = gaussian_tracker.map_preprocess(curr_frame, frame_id)
gaussian_tracker.tracking(curr_frame, frame_map)
tracker_time = time.time()
tracker_consume_time = tracker_time - start_time
time_recorder.update_mean("tracking", tracker_consume_time, 1)
tracker_time_sum += tracker_consume_time
print(f"[LOG] tracker cost time: {tracker_time - start_time}")
mapper_start_time = time.time()
new_poses = gaussian_tracker.get_new_poses()
gaussian_map.update_poses(new_poses)
# mapper process
gaussian_map.mapping(curr_frame, frame_map, frame_id, optimization_params)
gaussian_map.get_render_output(curr_frame)
gaussian_tracker.update_last_status(
curr_frame,
gaussian_map.model_map["render_depth"],
gaussian_map.frame_map["depth_map"],
gaussian_map.model_map["render_normal"],
gaussian_map.frame_map["normal_map_w"],
)
mapper_time = time.time()
mapper_consume_time = mapper_time - mapper_start_time
time_recorder.update_mean("mapping", mapper_consume_time, 1)
mapper_time_sum += mapper_consume_time
print(f"[LOG] mapper cost time: {mapper_time - tracker_time}")
if record_mem:
time_recorder.watch_gpu()
# report eval loss
if ((gaussian_map.time + 1) % gaussian_map.save_step == 0) or (
gaussian_map.time == 0
):
eval_frame(
gaussian_map,
curr_frame,
os.path.join(gaussian_map.save_path, "eval_render"),
min_depth=gaussian_map.min_depth,
max_depth=gaussian_map.max_depth,
save_picture=True,
run_pcd=False
)
gaussian_map.save_model(save_data=True)
gaussian_map.time += 1
move_to_cpu(curr_frame)
torch.cuda.empty_cache()
print("\n========== main loop finish ==========\n")
print(
"[LOG] stable num: {:d}, unstable num: {:d}".format(
gaussian_map.get_stable_num, gaussian_map.get_unstable_num
)
)
print("[LOG] processed frame: ", gaussian_map.optimize_frames_ids)
print("[LOG] keyframes: ", gaussian_map.keyframe_ids)
print("[LOG] mean tracker process time: ", tracker_time_sum / (frame_id + 1))
print("[LOG] mean mapper process time: ", mapper_time_sum / (frame_id + 1))
new_poses = gaussian_tracker.get_new_poses()
gaussian_map.update_poses(new_poses)
gaussian_map.global_optimization(optimization_params, is_end=True)
eval_frame(
gaussian_map,
gaussian_map.keyframe_list[-1],
os.path.join(gaussian_map.save_path, "eval_render"),
min_depth=gaussian_map.min_depth,
max_depth=gaussian_map.max_depth,
save_picture=True,
run_pcd=False
)
gaussian_map.save_model(save_data=True)
gaussian_tracker.save_traj(args.save_path)
time_recorder.cal_fps()
time_recorder.save(args.save_path)
gaussian_map.time += 1
if args.pcd_densify:
densify_pcd = gaussian_map.stable_pointcloud.densify(1, 30, 5)
o3d.io.write_point_cloud(
os.path.join(args.save_path, "save_model", "pcd_densify.ply"), densify_pcd
)
if __name__ == "__main__":
main()