-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
478 lines (395 loc) · 22.3 KB
/
train.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import os
import torch
import cv2
import kornia
from random import randint
from lib.utils.loss_utils import l1_loss, l2_loss, psnr, ssim
from lib.utils.img_utils import save_img_torch, visualize_depth_numpy
from lib.models.street_gaussian_renderer import StreetGaussianRenderer
from lib.models.street_gaussian_model import StreetGaussianModel
from lib.utils.general_utils import safe_state
from lib.utils.camera_utils import Camera
from lib.utils.cfg_utils import save_cfg
from lib.models.scene import Scene
from lib.datasets.dataset import Dataset
from lib.config import cfg
from tqdm import tqdm
from argparse import ArgumentParser, Namespace
from lib.utils.system_utils import searchForMaxIteration
try:
from torch.utils.tensorboard import SummaryWriter
TENSORBOARD_FOUND = True
except ImportError:
TENSORBOARD_FOUND = False
def training():
training_args = cfg.train
optim_args = cfg.optim
data_args = cfg.data
start_iter = 0
tb_writer = prepare_output_and_logger()
dataset = Dataset()
gaussians = StreetGaussianModel(dataset.scene_info.metadata)
scene = Scene(gaussians=gaussians, dataset=dataset)
gaussians.training_setup()
try:
if cfg.loaded_iter == -1:
loaded_iter = searchForMaxIteration(cfg.trained_model_dir)
else:
loaded_iter = cfg.loaded_iter
ckpt_path = os.path.join(cfg.trained_model_dir, f'iteration_{loaded_iter}.pth')
state_dict = torch.load(ckpt_path)
start_iter = state_dict['iter']
print(f'Loading model from {ckpt_path}')
gaussians.load_state_dict(state_dict)
except:
pass
print(f'Starting from {start_iter}')
save_cfg(cfg, cfg.model_path, epoch=start_iter)
gaussians_renderer = StreetGaussianRenderer()
iter_start = torch.cuda.Event(enable_timing = True)
iter_end = torch.cuda.Event(enable_timing = True)
ema_loss_for_log = 0.0
ema_psnr_for_log = 0.0
psnr_dict = {}
progress_bar = tqdm(range(start_iter, training_args.iterations))
start_iter += 1
viewpoint_stack = None
for iteration in range(start_iter, training_args.iterations + 1):
iter_start.record()
gaussians.update_learning_rate(iteration)
# Every 1000 its we increase the levels of SH up to a maximum degree
if iteration % 1000 == 0:
gaussians.oneupSHdegree()
# Every 1000 iterations upsample
# if iteration % 1000 == 0:
# if resolution_scales:
# scale = resolution_scales.pop()
# if iteration >= cfg.optim.densify_from_iter and iteration <= cfg.optim.densify_until_iter:
gaussians.set_flip()
# Pick a random Camera
if not viewpoint_stack:
viewpoint_stack = scene.getTrainCameras().copy()
viewpoint_cam: Camera = viewpoint_stack.pop(randint(0, len(viewpoint_stack) - 1))
gt_image = viewpoint_cam.original_image.cuda()
if hasattr(viewpoint_cam, 'original_mask'):
mask = viewpoint_cam.original_mask.cuda().bool()
else:
mask = torch.ones_like(gt_image[0:1]).bool()
if hasattr(viewpoint_cam, 'original_sky_mask'):
sky_mask = viewpoint_cam.original_sky_mask.cuda()
else:
sky_mask = None
if hasattr(viewpoint_cam, 'original_ego_mask'):
ego_mask = viewpoint_cam.original_ego_mask.cuda()
else:
ego_mask = None
if ego_mask is not None:
mask = mask & (~ego_mask.bool())
if hasattr(viewpoint_cam, 'original_obj_bound'):
obj_bound = viewpoint_cam.original_obj_bound.cuda().bool()
else:
obj_bound = torch.zeros_like(gt_image[0:1]).bool()
if hasattr(viewpoint_cam, 'original_car_mask'):
car_mask = viewpoint_cam.original_car_mask.cuda().bool()
else:
car_mask = None
# 增加一个逻辑,如果我们不渲染 obj,那么在 mask 上加上 obj_bound(即不让 obj 影响 depth mask 和 l1 mask)
# if not gaussians.include_obj:
# mask = mask & (~obj_bound.bool())
# 现在的逻辑是读取 car loss,然后添加到 mask 上
if not gaussians.include_obj:
mask = mask & (~car_mask.bool())
if iteration % 1000 == 0: # 每1000次迭代保存一次
os.makedirs(f"{cfg.model_path}/mask_debug", exist_ok=True)
# 创建可视化图像
mask_vis = torch.zeros(3, mask.shape[1], mask.shape[2], device=mask.device)
if mask is not None:
mask_vis[0] = mask.float().squeeze(0) # 红色通道显示普通mask
if sky_mask is not None:
mask_vis[1] = sky_mask.float().squeeze(0) # 绿色通道显示sky_mask
if ego_mask is not None:
mask_vis[2] = ego_mask.float().squeeze(0) # 蓝色通道显示ego_mask
# 保存mask可视化结果
save_img_torch(mask_vis, f"{cfg.model_path}/mask_debug/mask_{iteration}.jpg")
# 保存obj_bound可视化结果
if obj_bound is not None:
obj_bound_vis = obj_bound.float().repeat(3, 1, 1)
save_img_torch(obj_bound_vis, f"{cfg.model_path}/mask_debug/obj_bound_{iteration}.jpg")
# 在特定迭代次数时插入断点
# if iteration == training_args.debug_from:
# if iteration == 2000:
# import pdb; pdb.set_trace()
if (iteration - 1) == training_args.debug_from:
cfg.render.debug = True
render_pkg = gaussians_renderer.render(viewpoint_cam, gaussians)
image, acc, viewspace_point_tensor, visibility_filter, radii = render_pkg["rgb"], render_pkg['acc'], render_pkg["viewspace_points"], render_pkg["visibility_filter"], render_pkg["radii"]
depth = render_pkg['depth'] # [1, H, W]
scalar_dict = dict()
# rgb loss
Ll1 = l1_loss(image, gt_image, mask)
scalar_dict['l1_loss'] = Ll1.item()
loss = (1.0 - optim_args.lambda_dssim) * optim_args.lambda_l1 * Ll1 + optim_args.lambda_dssim * (1.0 - ssim(image, gt_image, mask=mask))
# sky loss
if optim_args.lambda_sky > 0 and gaussians.include_sky and sky_mask is not None:
acc = torch.clamp(acc, min=1e-6, max=1.-1e-6)
sky_loss = torch.where(sky_mask, -torch.log(1 - acc), -torch.log(acc)).mean()
if len(optim_args.lambda_sky_scale) > 0:
sky_loss *= optim_args.lambda_sky_scale[viewpoint_cam.meta['cam']]
scalar_dict['sky_loss'] = sky_loss.item()
loss += optim_args.lambda_sky * sky_loss
# # ego car loss 该方案和原本的 ego mask 方案冲突,且未测试效果,之后尝试
# if optim_args.lambda_ego > 0 and ego_mask is not None:
# acc = torch.clamp(acc, min=1e-6, max=1.-1e-6)
# ego_loss = torch.where(ego_mask, -torch.log(1 - acc), -torch.log(acc)).mean()
# scalar_dict['ego_loss'] = ego_loss.item()
# loss += optim_args.lambda_ego * ego_loss
# # 如果有ego车的原始图像,可以直接用来替换
# if hasattr(viewpoint_cam, 'original_ego_image'):
# ego_image = viewpoint_cam.original_ego_image.cuda()
# image = torch.where(ego_mask.repeat(3, 1, 1), ego_image, image)
# semantic loss
if optim_args.lambda_semantic > 0 and data_args.get('use_semantic', False) and 'semantic' in viewpoint_cam.meta:
gt_semantic = viewpoint_cam.meta['semantic'].cuda().long() # [1, H, W]
if torch.all(gt_semantic == -1):
semantic_loss = torch.zeros_like(Ll1)
else:
semantic = render_pkg['semantic'].unsqueeze(0) # [1, S, H, W]
semantic_loss = torch.nn.functional.cross_entropy(
input=semantic,
target=gt_semantic,
ignore_index=-1,
reduction='mean'
)
scalar_dict['semantic_loss'] = semantic_loss.item()
loss += optim_args.lambda_semantic * semantic_loss
if optim_args.lambda_reg > 0 and gaussians.include_obj and iteration >= optim_args.densify_until_iter:
render_pkg_obj = gaussians_renderer.render_object(viewpoint_cam, gaussians)
image_obj, acc_obj = render_pkg_obj["rgb"], render_pkg_obj['acc']
acc_obj = torch.clamp(acc_obj, min=1e-6, max=1.-1e-6)
# box_reg_loss = gaussians.get_box_reg_loss()
# scalar_dict['box_reg_loss'] = box_reg_loss.item()
# loss += optim_args.lambda_reg * box_reg_loss
# 暂时关闭obj acc loss 不过好像不包含 obj也就不会开启这个
obj_acc_loss = torch.where(obj_bound,
-(acc_obj * torch.log(acc_obj) + (1. - acc_obj) * torch.log(1. - acc_obj)),
-torch.log(1. - acc_obj)).mean()
scalar_dict['obj_acc_loss'] = obj_acc_loss.item()
loss += optim_args.lambda_reg * obj_acc_loss
# obj_acc_loss = -((acc_obj * torch.log(acc_obj) + (1. - acc_obj) * torch.log(1. - acc_obj))).mean()
# scalar_dict['obj_acc_loss'] = obj_acc_loss.item()
# loss += optim_args.lambda_reg * obj_acc_loss
# lidar depth loss
if optim_args.lambda_depth_lidar > 0 and 'lidar_depth' in viewpoint_cam.meta:
lidar_depth = viewpoint_cam.meta['lidar_depth'].cuda() # [1, H, W]
depth_mask = torch.logical_and((lidar_depth > 0.), mask)
# depth_mask[obj_bound] = False
if torch.nonzero(depth_mask).any():
expected_depth = depth / (render_pkg['acc'] + 1e-10)
depth_error = torch.abs((expected_depth[depth_mask] - lidar_depth[depth_mask]))
depth_error, _ = torch.topk(depth_error, int(0.95 * depth_error.size(0)), largest=False)
lidar_depth_loss = depth_error.mean()
scalar_dict['lidar_depth_loss'] = lidar_depth_loss
else:
lidar_depth_loss = torch.zeros_like(Ll1)
loss += optim_args.lambda_depth_lidar * lidar_depth_loss
# color correction loss
if optim_args.lambda_color_correction > 0 and gaussians.use_color_correction:
color_correction_reg_loss = gaussians.color_correction.regularization_loss(viewpoint_cam)
scalar_dict['color_correction_reg_loss'] = color_correction_reg_loss.item()
loss += optim_args.lambda_color_correction * color_correction_reg_loss
# pose correction loss
if optim_args.lambda_pose_correction > 0 and gaussians.use_pose_correction:
pose_correction_reg_loss = gaussians.pose_correction.regularization_loss()
scalar_dict['pose_correction_reg_loss'] = pose_correction_reg_loss.item()
loss += optim_args.lambda_pose_correction * pose_correction_reg_loss
# scale flatten loss
if optim_args.lambda_scale_flatten > 0:
scale_flatten_loss = gaussians.background.scale_flatten_loss()
scalar_dict['scale_flatten_loss'] = scale_flatten_loss.item()
loss += optim_args.lambda_scale_flatten * scale_flatten_loss
# opacity sparse loss
if optim_args.lambda_opacity_sparse > 0:
opacity = gaussians.get_opacity
opacity = opacity.clamp(1e-6, 1-1e-6)
log_opacity = opacity * torch.log(opacity)
log_one_minus_opacity = (1-opacity) * torch.log(1 - opacity)
sparse_loss = -1 * (log_opacity + log_one_minus_opacity)[visibility_filter].mean()
scalar_dict['opacity_sparse_loss'] = sparse_loss.item()
loss += optim_args.lambda_opacity_sparse * sparse_loss
# normal loss
if optim_args.lambda_normal_mono > 0 and 'mono_normal' in viewpoint_cam.meta and 'normals' in render_pkg:
if sky_mask is None:
normal_mask = mask
else:
normal_mask = torch.logical_and(mask, ~sky_mask)
normal_mask = normal_mask.squeeze(0)
normal_mask[:50] = False
normal_gt = viewpoint_cam.meta['mono_normal'].permute(1, 2, 0).cuda() # [H, W, 3]
R_c2w = viewpoint_cam.world_view_transform[:3, :3]
normal_gt = torch.matmul(normal_gt, R_c2w.T) # to world space
normal_pred = render_pkg['normals'].permute(1, 2, 0) # [H, W, 3]
normal_l1_loss = torch.abs(normal_pred[normal_mask] - normal_gt[normal_mask]).mean()
normal_cos_loss = (1. - torch.sum(normal_pred[normal_mask] * normal_gt[normal_mask], dim=-1)).mean()
scalar_dict['normal_l1_loss'] = normal_l1_loss.item()
scalar_dict['normal_cos_loss'] = normal_cos_loss.item()
normal_loss = normal_l1_loss + normal_cos_loss
loss += optim_args.lambda_normal_mono * normal_loss
scalar_dict['loss'] = loss.item()
loss.backward()
iter_end.record()
is_save_images = True
if is_save_images and (iteration % 1000 == 0):
# row0: gt_image, image, depth
# row1: acc, image_obj, acc_obj
depth_colored, _ = visualize_depth_numpy(depth.detach().cpu().numpy().squeeze(0))
depth_colored = depth_colored[..., [2, 1, 0]] / 255.
depth_colored = torch.from_numpy(depth_colored).permute(2, 0, 1).float().cuda()
row0 = torch.cat([gt_image, image, depth_colored], dim=2)
acc = acc.repeat(3, 1, 1)
with torch.no_grad():
render_pkg_obj = gaussians_renderer.render_object(viewpoint_cam, gaussians)
image_obj, acc_obj = render_pkg_obj["rgb"], render_pkg_obj['acc']
acc_obj = acc_obj.repeat(3, 1, 1)
row1 = torch.cat([acc, image_obj, acc_obj], dim=2)
image_to_show = torch.cat([row0, row1], dim=1)
image_to_show = torch.clamp(image_to_show, 0.0, 1.0)
os.makedirs(f"{cfg.model_path}/log_images", exist_ok = True)
save_img_torch(image_to_show, f"{cfg.model_path}/log_images/{iteration}.jpg")
with torch.no_grad():
# Log
tensor_dict = dict()
# Progress bar
ema_loss_for_log = 0.4 * loss.item() + 0.6 * ema_loss_for_log
ema_psnr_for_log = 0.4 * psnr(image, gt_image, mask).mean().float() + 0.6 * ema_psnr_for_log
if viewpoint_cam.id not in psnr_dict:
psnr_dict[viewpoint_cam.id] = psnr(image, gt_image, mask).mean().float()
else:
psnr_dict[viewpoint_cam.id] = 0.4 * psnr(image, gt_image, mask).mean().float() + 0.6 * psnr_dict[viewpoint_cam.id]
if iteration % 10 == 0:
progress_bar.set_postfix({"Exp": f"{cfg.task}-{cfg.exp_name}",
"Loss": f"{ema_loss_for_log:.{7}f},",
"PSNR": f"{ema_psnr_for_log:.{4}f}"})
progress_bar.update(10)
if iteration == training_args.iterations:
progress_bar.close()
# Log and save
if (iteration in training_args.save_iterations):
print("\n[ITER {}] Saving Gaussians".format(iteration))
scene.save(iteration)
# Densification
if iteration < optim_args.densify_until_iter:
gaussians.set_visibility(include_list=list(set(gaussians.model_name_id.keys()) - set(['sky'])))
gaussians.parse_camera(viewpoint_cam)
gaussians.set_max_radii2D(radii, visibility_filter)
gaussians.add_densification_stats(viewspace_point_tensor, visibility_filter)
prune_big_points = iteration > optim_args.opacity_reset_interval # TODO 这个步骤会报错
if iteration > optim_args.densify_from_iter:
if iteration % optim_args.densification_interval == 0:
scalars, tensors = gaussians.densify_and_prune(
max_grad=optim_args.densify_grad_threshold,
min_opacity=optim_args.min_opacity,
prune_big_points=prune_big_points,
)
scalar_dict.update(scalars)
tensor_dict.update(tensors)
# Reset opacity
if iteration < optim_args.densify_until_iter:
if iteration % optim_args.opacity_reset_interval == 0:
gaussians.reset_opacity()
if data_args.white_background and iteration == optim_args.densify_from_iter:
gaussians.reset_opacity()
training_report(tb_writer, iteration, scalar_dict, tensor_dict, training_args.test_iterations, scene, gaussians_renderer) # Forbiden due to limited spcae
# Optimizer step
if iteration < training_args.iterations:
gaussians.update_optimizer()
if (iteration in training_args.checkpoint_iterations):
print("\n[ITER {}] Saving Checkpoint".format(iteration))
state_dict = gaussians.save_state_dict(is_final=(iteration == training_args.iterations))
state_dict['iter'] = iteration
ckpt_path = os.path.join(cfg.trained_model_dir, f'iteration_{iteration}.pth')
torch.save(state_dict, ckpt_path)
def prepare_output_and_logger():
# if cfg.model_path == '':
# if os.getenv('OAR_JOB_ID'):
# unique_str = os.getenv('OAR_JOB_ID')
# else:
# unique_str = str(uuid.uuid4())
# cfg.model_path = os.path.join("./output/", unique_str[0:10])
# Set up output folder
print("Output folder: {}".format(cfg.model_path))
os.makedirs(cfg.model_path, exist_ok=True)
os.makedirs(cfg.trained_model_dir, exist_ok=True)
os.makedirs(cfg.record_dir, exist_ok=True)
if not cfg.resume:
os.system('rm -rf {}/*'.format(cfg.record_dir))
os.system('rm -rf {}/*'.format(cfg.trained_model_dir))
with open(os.path.join(cfg.model_path, "cfg_args"), 'w') as cfg_log_f:
viewer_arg = dict()
viewer_arg['sh_degree'] = cfg.model.gaussian.sh_degree
viewer_arg['white_background'] = cfg.data.white_background
viewer_arg['source_path'] = cfg.source_path
viewer_arg['model_path']= cfg.model_path
cfg_log_f.write(str(Namespace(**viewer_arg)))
# Create Tensorboard writer
tb_writer = None
if TENSORBOARD_FOUND:
tb_writer = SummaryWriter(cfg.record_dir)
else:
print("Tensorboard not available: not logging progress")
return tb_writer
def training_report(tb_writer, iteration, scalar_stats, tensor_stats, testing_iterations, scene: Scene, renderer: StreetGaussianRenderer):
if tb_writer:
try:
for key, value in scalar_stats.items():
tb_writer.add_scalar('train/' + key, value, iteration)
for key, value in tensor_stats.items():
tb_writer.add_histogram('train/' + key, value, iteration)
except:
print('Failed to write to tensorboard')
# Report test and samples of training set
if iteration in testing_iterations:
torch.cuda.empty_cache()
validation_configs = ({'name': 'test/test_view', 'cameras' : scene.getTestCameras()},
{'name': 'test/train_view', 'cameras' : [scene.getTrainCameras()[idx % len(scene.getTrainCameras())] for idx in range(5, 30, 5)]})
for config in validation_configs:
if config['cameras'] and len(config['cameras']) > 0:
l1_test = 0.0
psnr_test = 0.0
for idx, viewpoint in enumerate(config['cameras']):
image = torch.clamp(renderer.render(viewpoint, scene.gaussians)["rgb"], 0.0, 1.0)
gt_image = torch.clamp(viewpoint.original_image.to("cuda"), 0.0, 1.0)
if tb_writer and (idx < 5):
tb_writer.add_images(config['name'] + "_{}/render".format(viewpoint.image_name), image[None], global_step=iteration)
if iteration == testing_iterations[0]:
tb_writer.add_images(config['name'] + "_{}/ground_truth".format(viewpoint.image_name), gt_image[None], global_step=iteration)
if hasattr(viewpoint, 'original_mask'):
mask = viewpoint.original_mask.cuda().bool()
else:
mask = torch.ones_like(gt_image[0]).bool()
if hasattr(viewpoint, 'original_ego_mask'):
ego_mask = viewpoint.original_ego_mask.cuda()
else:
ego_mask = None
if ego_mask is not None:
mask = mask & (~ego_mask.bool())
l1_test += l1_loss(image, gt_image, mask).mean().double()
psnr_test += psnr(image, gt_image, mask).mean().double()
psnr_test /= len(config['cameras'])
l1_test /= len(config['cameras'])
print("\n[ITER {}] Evaluating {}: L1 {} PSNR {}".format(iteration, config['name'], l1_test, psnr_test))
if tb_writer:
tb_writer.add_scalar(config['name'] + '/loss_viewpoint - l1_loss', l1_test, iteration)
tb_writer.add_scalar(config['name'] + '/loss_viewpoint - psnr', psnr_test, iteration)
if tb_writer:
tb_writer.add_histogram("test/opacity_histogram", scene.gaussians.get_opacity, iteration)
tb_writer.add_scalar('test/points_total', scene.gaussians.get_xyz.shape[0], iteration)
torch.cuda.empty_cache()
if __name__ == "__main__":
print("Optimizing " + cfg.model_path)
# Initialize system state (RNG)
safe_state(cfg.train.quiet)
# Start GUI server, configure and run training
torch.autograd.set_detect_anomaly(cfg.train.detect_anomaly)
training()
# All done
print("\nTraining complete.")