-
Notifications
You must be signed in to change notification settings - Fork 18
/
coslam_mp.py
354 lines (287 loc) · 13.3 KB
/
coslam_mp.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
import os
#os.environ['TCNN_CUDA_ARCHITECTURES'] = '86'
# Package imports
import torch
import torch.optim as optim
import numpy as np
import random
import torch.nn.functional as F
import argparse
import shutil
import json
import time
from torch.utils.data import DataLoader
from tqdm import tqdm
# Local imports
import config
from model.scene_rep import JointEncoding
from model.keyframe import KeyFrameDatabase
from datasets.dataset import get_dataset
from utils import coordinates, extract_mesh
from tools.eval_ate import pose_evaluation
from optimization.utils import at_to_transform_matrix, qt_to_transform_matrix, matrix_to_axis_angle, matrix_to_quaternion
# Multiprocessing imports
import torch.multiprocessing as mp
mp.set_sharing_strategy('file_system')
from mp_slam.tracker import Tracker
from mp_slam.mapper import Mapper
class CoSLAM():
def __init__(self, config):
self.config = config
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.dataset = get_dataset(config)
self.create_bounds()
self.create_pose_data()
self.get_pose_representation()
try:
mp.set_start_method('spawn', force=True)
except RuntimeError:
pass
self.create_share_data()
self.keyframeDatabase = self.create_kf_database(config)
self.model = JointEncoding(config, self.bounding_box).to(self.device).share_memory()
self.create_optimizer()
self.tracker = Tracker(config, self)
self.mapper = Mapper(config, self)
def pose_eval_func(self):
return pose_evaluation
def create_share_data(self):
self.create_pose_data()
self.mapping_first_frame = torch.zeros((1)).int().share_memory_()
self.mapping_idx = torch.zeros((1)).share_memory_()
self.tracking_idx = torch.zeros((1)).share_memory_()
def seed_everything(self, seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
def get_pose_representation(self):
'''
Get the pose representation axis-angle or quaternion
'''
if self.config['training']['rot_rep'] == 'axis_angle':
self.matrix_to_tensor = matrix_to_axis_angle
self.matrix_from_tensor = at_to_transform_matrix
elif self.config['training']['rot_rep'] == "quat":
print("Using quaternion as rotation representation")
self.matrix_to_tensor = matrix_to_quaternion
self.matrix_from_tensor = qt_to_transform_matrix
else:
raise NotImplementedError
def create_pose_data(self):
'''
Create the pose data
'''
num_frames = self.dataset.num_frames
self.est_c2w_data = torch.zeros((num_frames, 4, 4)).to(self.device).share_memory_()
self.est_c2w_data_rel = torch.zeros((num_frames, 4, 4)).to(self.device).share_memory_()
self.load_gt_pose()
def create_bounds(self):
'''
Get the pre-defined bounds for the scene
'''
self.bounding_box = torch.from_numpy(np.array(self.config['mapping']['bound'])).to(self.device)
self.marching_cube_bound = torch.from_numpy(np.array(self.config['mapping']['marching_cubes_bound'])).to(self.device)
def create_kf_database(self, config):
'''
Create the keyframe database
'''
num_kf = int(self.dataset.num_frames // self.config['mapping']['keyframe_every'] + 1)
print('#kf:', num_kf)
print('#Pixels to save:', self.dataset.num_rays_to_save)
return KeyFrameDatabase(config,
self.dataset.H,
self.dataset.W,
num_kf,
self.dataset.num_rays_to_save,
self.device)
def load_gt_pose(self):
'''
Load the ground truth pose
'''
self.pose_gt = torch.zeros((self.dataset.num_frames, 4, 4))
for i, pose in enumerate(self.dataset.poses):
self.pose_gt[i] = pose
def save_state_dict(self, save_path):
torch.save(self.model.state_dict(), save_path)
def load(self, load_path):
self.model.load_state_dict(torch.load(load_path))
def save_ckpt(self, save_path):
'''
Save the model parameters and the estimated pose
'''
save_dict = {'pose': self.est_c2w_data,
'pose_rel': self.est_c2w_data_rel,
'model': self.model.state_dict()}
torch.save(save_dict, save_path)
print('Save the checkpoint')
def load_ckpt(self, load_path):
'''
Load the model parameters and the estimated pose
'''
dict = torch.load(load_path)
self.model.load_state_dict(dict['model'])
self.est_c2w_data = dict['pose']
self.est_c2w_data_rel = dict['pose_rel']
def select_samples(self, H, W, samples):
'''
randomly select samples from the image
'''
#indice = torch.randint(H*W, (samples,))
indice = random.sample(range(H * W), int(samples))
indice = torch.tensor(indice)
return indice
def get_loss_from_ret(self, ret, rgb=True, sdf=True, depth=True, fs=True, smooth=False):
'''
Get the training loss
'''
loss = 0
if rgb:
loss += self.config['training']['rgb_weight'] * ret['rgb_loss']
if depth:
loss += self.config['training']['depth_weight'] * ret['depth_loss']
if sdf:
loss += self.config['training']['sdf_weight'] * ret["sdf_loss"]
if fs:
loss += self.config['training']['fs_weight'] * ret["fs_loss"]
if smooth:
loss += self.config['training']['smooth_weight'] * self.smoothness(self.config['training']['smooth_pts'],
self.config['training']['smooth_vox'],
margin=self.config['training']['smooth_margin'])
return loss
def smoothness(self, sample_points=256, voxel_size=0.1, margin=0.05, color=False):
'''
Smoothness loss of feature grid
'''
volume = self.bounding_box[:, 1] - self.bounding_box[:, 0]
grid_size = (sample_points-1) * voxel_size
offset_max = self.bounding_box[:, 1]-self.bounding_box[:, 0] - grid_size - 2 * margin
offset = torch.rand(3).to(offset_max) * offset_max + margin
coords = coordinates(sample_points - 1, 'cpu', flatten=False).float().to(volume)
pts = (coords + torch.rand((1,1,1,3)).to(volume)) * voxel_size + self.bounding_box[:, 0] + offset
if self.config['grid']['tcnn_encoding']:
pts_tcnn = (pts - self.bounding_box[:, 0]) / (self.bounding_box[:, 1] - self.bounding_box[:, 0])
sdf = self.model.query_sdf(pts_tcnn, embed=True)
tv_x = torch.pow(sdf[1:,...]-sdf[:-1,...], 2).sum()
tv_y = torch.pow(sdf[:,1:,...]-sdf[:,:-1,...], 2).sum()
tv_z = torch.pow(sdf[:,:,1:,...]-sdf[:,:,:-1,...], 2).sum()
loss = (tv_x + tv_y + tv_z)/ (sample_points**3)
return loss
def get_rays_from_batch(self, batch, c2w_est, indices):
'''
Get the rays from the batch
Params:
batch['c2w']: [1, 4, 4]
batch['rgb']: [1, H, W, 3]
batch['depth']: [1, H, W, 1]
batch['direction']: [1, H, W, 3]
c2w_est: [4, 4]
indices: [N]
Returns:
rays_o: [N, 3]
rays_d: [N, 3]
target_s: [N, 3]
target_d: [N, 1]
c2w_gt: [4, 4]
'''
rays_d_cam = batch['direction'].reshape(-1, 3)[indices].to(self.device)
target_s = batch['rgb'].reshape(-1, 3)[indices].to(self.device)
target_d = batch['depth'].reshape(-1, 1)[indices].to(self.device)
rays_d = torch.sum(rays_d_cam[..., None, :] * c2w_est[:3, :3], -1)
rays_o = c2w_est[None, :3, -1].repeat(rays_d.shape[0], 1)
c2w_gt = batch['c2w'][0].to(self.device)
if torch.sum(torch.isnan(rays_d_cam)):
print('warning rays_d_cam')
if torch.sum(torch.isnan(c2w_est)):
print('warning c2w_est')
return rays_o, rays_d, target_s, target_d, c2w_gt
def update_pose_array(self, frame_id):
if torch.sum(torch.isnan(self.est_c2w_data[frame_id])):
print('tracking warning')
self.model.pose_array.add_params(self.est_c2w_data[frame_id].to(self.device), frame_id)
def convert_relative_pose(self):
poses = {}
for i in range(len(self.est_c2w_data)):
if i % self.config['mapping']['keyframe_every'] == 0:
poses[i] = self.est_c2w_data[i]
else:
kf_id = i // self.config['mapping']['keyframe_every']
kf_frame_id = kf_id * self.config['mapping']['keyframe_every']
c2w_key = self.est_c2w_data[kf_frame_id]
delta = self.est_c2w_data_rel[i]
poses[i] = delta @ c2w_key
return poses
def create_optimizer(self):
'''
Create optimizer for mapping
'''
trainable_parameters = [{'params': self.model.decoder.parameters(), 'weight_decay': 1e-6, 'lr': self.config['mapping']['lr_decoder']},
{'params': self.model.embed_fn.parameters(), 'eps': 1e-15, 'lr': self.config['mapping']['lr_embed']}]
if not self.config['grid']['oneGrid']:
trainable_parameters.append({'params': self.model.embed_fn_color.parameters(), 'eps': 1e-15, 'lr': self.config['mapping']['lr_embed_color']})
self.map_optimizer = optim.Adam(trainable_parameters, betas=(0.9, 0.99))
def save_mesh(self, i, voxel_size=0.05):
mesh_savepath = os.path.join(self.config['data']['output'], self.config['data']['exp_name'], 'mesh_track{}.ply'.format(i))
if self.config['mesh']['render_color']:
color_func = self.model.render_surface_color
else:
color_func = self.model.query_color
extract_mesh(self.model.query_sdf,
self.config,
self.bounding_box,
color_func=color_func,
marching_cube_bound=self.marching_cube_bound,
voxel_size=voxel_size,
mesh_savepath=mesh_savepath)
def get_pose_param_optim(self, poses, mapping=True):
task = 'mapping' if mapping else 'tracking'
cur_trans = torch.nn.parameter.Parameter(poses[:, :3, 3])
cur_rot = torch.nn.parameter.Parameter(self.matrix_to_tensor(poses[:, :3, :3]))
pose_optimizer = torch.optim.Adam([{"params": cur_rot, "lr": self.config[task]['lr_rot']},
{"params": cur_trans, "lr": self.config[task]['lr_trans']}])
return cur_rot, cur_trans, pose_optimizer
def tracking(self, rank):
while True:
if self.mapping_first_frame[0] == 1:
print('Start tracking')
break
time.sleep(0.5)
self.tracker.run()
def mapping(self, rank):
self.mapper.run()
def run(self):
processes = []
for rank in range(2):
if rank == 1:
p = mp.Process(target=self.tracking, args=(rank, ))
elif rank == 0:
p = mp.Process(target=self.mapping, args=(rank, ))
time.sleep(2)
p.start()
processes.append(p)
for p in processes:
p.join()
if __name__ == '__main__':
print('Start running...')
parser = argparse.ArgumentParser(
description='Arguments for running the NICE-SLAM/iMAP*.'
)
parser.add_argument('--config', type=str, help='Path to config file.')
parser.add_argument('--input_folder', type=str,
help='input folder, this have higher priority, can overwrite the one in config file')
parser.add_argument('--output', type=str,
help='output folder, this have higher priority, can overwrite the one in config file')
args = parser.parse_args()
cfg = config.load_config(args.config)
if args.output is not None:
cfg['data']['output'] = args.output
print("Saving config and script...")
save_path = os.path.join(cfg["data"]["output"], cfg['data']['exp_name'])
if not os.path.exists(save_path):
os.makedirs(save_path)
shutil.copy("coslam.py", os.path.join(save_path, 'coslam.py'))
with open(os.path.join(save_path, 'config.json'),"w", encoding='utf-8') as f:
f.write(json.dumps(cfg, indent=4))
slam = CoSLAM(cfg)
slam.run()