-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtrain.py
More file actions
1376 lines (1149 loc) · 67.7 KB
/
train.py
File metadata and controls
1376 lines (1149 loc) · 67.7 KB
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact george.drettakis@inria.fr
#
"""
Dynamic Gaussian Splatting training script (release version).
This refactor only adds concise documentation and removes duplicate imports
while keeping the original variable names and overall structure intact.
"""
import torch.nn as nn
from PIL import Image
import cv2
import numpy as np
import random
import os, sys
import torch
from random import randint
from utils.loss_utils import l1_loss, ssim, entropy_loss, structural_ssim, \
ssim_raw, EdgeAwareTV
from gaussian_renderer import render_background, render_foreground, render_mask
# import sys # Duplicate import removed
from scene import Scene, GaussianModel, GaussianModel_dynamic, Scene2gs_mixed
from utils.general_utils import safe_state
import uuid
import torch.nn.functional as F
from tqdm import tqdm
from utils.image_utils import psnr
from argparse import ArgumentParser, Namespace
from arguments import ModelParams, PipelineParams, OptimizationParams, ModelHiddenParams
from torch.utils.data import DataLoader
from utils.timer import Timer
from utils.loader_utils import FineSampler, get_stamp_list
import lpips
from utils.scene_utils import render_training_image
from time import time
import copy
import matplotlib.pyplot as plt
# import matplotlib.pyplot as plt # Duplicate import removed
from matplotlib import font_manager
font_manager.fontManager.addfont('./utils/Times New Roman.ttf')
try:
from torch.utils.tensorboard import SummaryWriter
TENSORBOARD_FOUND = True
except ImportError:
TENSORBOARD_FOUND = False
def get_edge_mask(image, threshold=0.1, dilation_iterations=2):
"""Return a binary edge map using Sobel magnitude.
Parameters
----------
image : torch.Tensor (B,C,H,W)
Input RGB image in range [0,1].
threshold : float, optional
Normalised gradient magnitude above which a pixel is considered edge.
dilation_iterations : int, optional
Number of binary dilation steps to slightly expand the edges. Currently
disabled to avoid extra dependency cost.
"""
from scipy.ndimage import binary_dilation
# --- Sobel kernels -----------------------------------------------------
sobel_x = torch.Tensor([[1, 0, -1], [2, 0, -2], [1, 0, -1]]).unsqueeze(0).unsqueeze(0).to(image.device)
sobel_y = torch.Tensor([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]).unsqueeze(0).unsqueeze(0).to(image.device)
edge_masks = []
for c in range(image.size(1)): # Apply Sobel filter to each channel separately
channel = image[:, c:c + 1, :, :]
grad_x = F.conv2d(channel, sobel_x, padding=1)
grad_y = F.conv2d(channel, sobel_y, padding=1)
grad_mag = torch.sqrt(grad_x ** 2 + grad_y ** 2)
edge_masks.append(grad_mag)
# Combine edge masks from all channels
edge_mask = torch.max(torch.stack(edge_masks, dim=1), dim=1)[0]
# Create binary edge mask (thresholding the gradient magnitude)
edge_mask = (edge_mask > threshold).float()
# Dilate the edge mask
edge_mask_np = edge_mask.cpu().numpy()
# for i in range(edge_mask_np.shape[0]):
# edge_mask_np[i] = binary_dilation(edge_mask_np[i], iterations=dilation_iterations)
return torch.from_numpy(edge_mask_np).to(image.device).float()
def normalize_depth(depth_i):
"""Normalise a depth map to [0,1] for visualisation.
This helper avoids division-by-zero by adding an epsilon to the denominator.
"""
depth_max = depth_i.max()
depth_min = depth_i.min()
return ((depth_i - depth_min) / (depth_max - depth_min + 1e-9))
class BrightnessActivation(nn.Module):
"""Piece-wise linear brightness mapping used by the light-control branch."""
def __init__(self):
super(BrightnessActivation, self).__init__()
def forward(self, x):
"""Map raw [0,1] probability → perceptually-balanced brightness factor.
Below a 0.75 threshold the mapping is identity (no amplification). Above
the threshold we linearly re-scale to the range [0.75,10] to give the
optimisation headroom when compensating for very dark backgrounds.
"""
output = torch.zeros_like(x)
mask1 = (x <= 0.75)
mask2 = (x > 0.75)
# Linear part for x in [0, 0.75]
output[mask1] = x[mask1]
# Linear transformation for x in (0.75, 1]
output[mask2] = 0.75 + (x[mask2] - 0.75) * ((10 - 0.75) / (1 - 0.75))
return output
def scene_reconstruction_degauss(dataset, optimization_params, hypernetwork_config, pipeline_config, testing_iterations,
saving_iterations,
checkpoint_iterations, checkpoint, debug_from,
foreground_gaussians, scene, stage, tb_writer, train_iter, timer,
background_gaussians=None,
expname='debug_2gs'):
# ---------------------------------------------------------------------
# ➤ COARSE / FINE TRAINING LOOP (Foreground & Background Gaussians)
# ---------------------------------------------------------------------
"""Two-stage (coarse→fine) training loop for DeGauss dynamic Gaussian splatting.
This routine orchestrates optimisation of *foreground* (dynamic) and
*background* (static) Gaussian models by alternating between:
1. Coarse stage – establishes scene geometry, aggressive densification.
2. Fine stage – refines colour/opacity, enforces temporal & depth priors.
It supports mixed-resolution datasets, gradient accumulation, adaptive
densification/pruning, brightness control, and a rich loss cocktail.
Parameters
----------
dataset : ModelParams-ready dataset object
opt : argparse.Namespace
All optimisation hyper-parameters.
hyper : ModelHiddenParams
Deformation & temporal smoothness coefficients.
pipe : PipelineParams
Rendering pipeline configuration.
testing_iterations / saving_iterations / checkpoint_iterations : list[int]
Iteration indices for eval / lightweight save / full checkpoint.
checkpoint : str | None
Path to an existing checkpoint to resume from.
debug_from : int
Earliest iteration at which extra debug visualisations are produced.
gaussians : GaussianModel_dynamic
Foreground (dynamic) Gaussian representation.
scene : Scene2gs_mixed
Wrapper that holds cameras + both Gaussian sets.
stage : {"coarse", "fine"}
tb_writer : SummaryWriter | None
train_iter : int
Number of optimisation iterations for this stage.
timer : utils.Timer
gaussians_second : GaussianModel, optional
Background (static) Gaussian representation.
expname : str
Experiment tag used for output folders.
"""
first_iter = 0
# ---- Model Setup ----
foreground_gaussians.training_setup(optimization_params)
background_gaussians.training_setup(optimization_params)
# ---- Checkpoint Resume Logic ----
if checkpoint:
if stage == "coarse" and stage not in checkpoint:
print("start from fine stage, skip coarse stage.")
return
if stage in checkpoint:
(model_params, first_iter) = torch.load(checkpoint)
foreground_gaussians.restore(model_params, optimization_params)
# ---- Background Colors (near-black to avoid numerical issues) ----
bg_color = [1e-7, 1e-7, 1e-7]
if optimization_params.force_white_background:
print("using white background")
background = 1- torch.tensor(bg_color, dtype=torch.float32, device="cuda")
else:
background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
# black_color = 1 - [1e-7, 1e-7, 1e-7]
# black_bg = torch.tensor(black_color, dtype=torch.float32, device="cuda")
# ---- CUDA Timing Events ----
iter_start = torch.cuda.Event(enable_timing=True)
iter_end = torch.cuda.Event(enable_timing=True)
# ---- Gradient Accumulation Setup ----
if stage == "coarse":
accumulation_steps = 1 # No accumulation in coarse stage
else:
accumulation_steps = optimization_params.accumulation_steps
# ---- Training State Initialization ----
viewpoint_stack = None
ema_loss_for_log = 0.0
ema_psnr_for_log = 0.0
ema_lossl1_for_log = 0.0
final_iter = train_iter
progress_bar = tqdm(range(first_iter, final_iter), desc="Training progress")
first_iter += 1
# ---- Camera Data Loading ----
video_cams = scene.getVideoCameras()
test_cams = scene.getTestCameras()
train_cams = scene.getTrainCameras()
val_cams = scene.getValCameras()
viewpoint_stack_index = list(range(len(train_cams)))
if not viewpoint_stack and not optimization_params.dataloader:
# Manual sampling mode - copy camera list
viewpoint_stack = [i for i in train_cams]
viewpoint_stack_index_save = copy.deepcopy(viewpoint_stack_index)
batch_size = optimization_params.batch_size
print("data loading done")
# ---- DataLoader Setup (optional) ----
if optimization_params.dataloader:
viewpoint_stack = scene.getTrainCameras()
if optimization_params.custom_sampler is not None:
sampler = FineSampler(viewpoint_stack)
viewpoint_stack_loader = DataLoader(viewpoint_stack, batch_size=batch_size, sampler=sampler, num_workers=16,
collate_fn=list)
random_loader = False
else:
viewpoint_stack_loader = DataLoader(viewpoint_stack, batch_size=batch_size, shuffle=True, num_workers=16,
collate_fn=list)
random_loader = True
loader = iter(viewpoint_stack_loader)
# ---- Image Masking Setup ----
actual_height = train_cams[0].image_height
actual_width = train_cams[0].image_width
# Camera-specific pixel mask (invalid regions)
if optimization_params.camera_mask:
pixel_valid_mask = cv2.imread(
optimization_params.camera_mask,
cv2.IMREAD_GRAYSCALE)
pixel_valid_mask = cv2.resize(pixel_valid_mask, (actual_width, actual_height), interpolation=cv2.INTER_NEAREST)
pixel_valid_mask = torch.from_numpy(pixel_valid_mask).unsqueeze(0).unsqueeze(0).float().cuda() > 0
else:
pixel_valid_mask = torch.ones(actual_height, actual_width).unsqueeze(0).unsqueeze(0).float().cuda()
# Vignette correction mask (lens distortion)
if optimization_params.vignette_mask:
vignette_rgb_path = optimization_params.vignette_mask
vignette_rgb = Image.open(vignette_rgb_path)
offset_x, offset_y = (32, 32)
w, h, = vignette_rgb.size
vignette_img = vignette_rgb.crop((offset_x, offset_y, w - offset_x, h - offset_y))
vignette_img = vignette_img.resize((actual_width, actual_height))
vignette_img_tensor = torch.from_numpy(np.array(vignette_img)[:, :, :3]).float().cuda() / 255
vignette_mask = vignette_img_tensor.permute(2, 0, 1).unsqueeze(0)
# ---- Special Coarse Stage Initialization ----
if stage == "coarse" and optimization_params.zerostamp_init:
# Filter cameras to timestamp 0 only
temp_list = get_stamp_list(viewpoint_stack, 0)
viewpoint_stack = temp_list.copy()
else:
load_in_memory = False
# ---- Main Training Loop ----
Finish_whose_seq = False
for iteration in range(first_iter, final_iter + 1):
iter_start.record()
# Learning rate scheduling
foreground_gaussians.update_learning_rate(iteration)
background_gaussians.update_learning_rate(iteration)
# Spherical harmonics degree progression
if iteration % optimization_params.foreground_oneupshinterval == 0:
foreground_gaussians.oneupSHdegree()
if iteration % optimization_params.background_oneupshinterval == 0:
background_gaussians.oneupSHdegree()
# ---- Camera Batch Sampling ----
if optimization_params.dataloader:
try:
viewpoint_cams = next(loader)
except StopIteration:
print("reset dataloader into random dataloader.")
if not random_loader:
viewpoint_stack_loader = DataLoader(viewpoint_stack, batch_size=optimization_params.batch_size,
shuffle=True,
num_workers=32, collate_fn=list)
random_loader = True
loader = iter(viewpoint_stack_loader)
else:
idx = 0
viewpoint_cams = []
# Mixed-resolution handling (currently disabled)
handle_different_cam = False
if not handle_different_cam:
# Simple random sampling without replacement
while idx < batch_size:
viewpoint_cam_idx = viewpoint_stack_index.pop(randint(0, len(viewpoint_stack_index) - 1))
viewpoint_cam = viewpoint_stack[viewpoint_cam_idx]
if not viewpoint_stack_index:
# Reset for next epoch
viewpoint_stack_index = viewpoint_stack_index_save.copy()
Finish_whose_seq = True
viewpoint_cams.append(viewpoint_cam)
idx += 1
else:
viewpoint_cams = []
current_resolution = None # Track the resolution for the current batch
batch_resolved = False
while idx < batch_size:
viewpoint_cam_idx = viewpoint_stack_index.pop(randint(0, len(viewpoint_stack_index) - 1))
viewpoint_cam = viewpoint_stack[viewpoint_cam_idx]
# Get the resolution of the current viewpoint camera
cam_resolution = viewpoint_cam.image_width # Assuming viewpoint_cam has a method to get its resolution
if current_resolution is None:
# Set the resolution for the batch if not yet set
current_resolution = cam_resolution
# Check if the viewpoint_cam has the same resolution as the current batch resolution
if cam_resolution == current_resolution:
viewpoint_cams.append(viewpoint_cam)
idx += 1
else:
# If a different resolution is encountered, skip this camera
viewpoint_stack_index.append(viewpoint_cam_idx)
# Check if we are running out of images with the current resolution
remaining_same_resolution = sum(1 for cam_idx in viewpoint_stack_index if
viewpoint_stack[cam_idx].image_width == current_resolution)
if remaining_same_resolution + len(viewpoint_cams) < batch_size:
# If there aren't enough same-resolution images left to fill the batch, exit the loop
print(f"Exiting: Not enough images with resolution {current_resolution} to fill the batch.")
idx += 1
if not viewpoint_stack_index:
viewpoint_stack_index = viewpoint_stack_index_save.copy()
Finish_whose_seq = True
# Continue the rest of your logic
if len(viewpoint_cams) == 0:
continue
# ---- Batch Rendering Setup ----
images = []
gt_images = []
images_second = []
radii_list = []
visibility_filter_list = []
viewspace_point_tensor_list = []
viewspace_point_tensor_list_motion = []
radii_list_second = []
visibility_filter_list_second = []
viewspace_point_tensor_list_second = []
motion_masks = []
foreground_prob_list = []
depth_images = []
depth_images_dy = []
# ---- Multi-Model Rendering Loop ----
for viewpoint_cam in viewpoint_cams:
# Render foreground (dynamic) Gaussians
render_pkg_dynamic_pers = render_foreground(viewpoint_cam, foreground_gaussians, pipeline_config, background,
stage=stage,
cam_type=scene.dataset_type)
image, viewspace_point_tensor, visibility_filter, radii = render_pkg_dynamic_pers["render"], \
render_pkg_dynamic_pers[
"viewspace_points"], render_pkg_dynamic_pers["visibility_filter"], render_pkg_dynamic_pers["radii"]
images.append(image.unsqueeze(0))
depth_images_dy.append(render_pkg_dynamic_pers['depth'].unsqueeze(0))
# Ground truth image
gt_image = viewpoint_cam.original_image.float().cuda() / 255
# Render background (static) Gaussians
render_pkg_second = render_background(viewpoint_cam, background_gaussians, pipeline_config, background,
stage='coarse',
cam_type=scene.dataset_type)
# Render motion probability mask
render_pkg_motion = render_mask(viewpoint_cam, foreground_gaussians, pipeline_config, background,
stage=stage,
cam_type=scene.dataset_type)
image_second = render_pkg_second["render"]
motion_mask = render_pkg_motion["render"]
motion_masks.append(motion_mask.unsqueeze(0))
images_second.append(image_second.unsqueeze(0))
depth_images.append(render_pkg_second['depth'].unsqueeze(0))
foreground_prob_list.append(render_pkg_motion["foreground_prob"].unsqueeze(0))
gt_images.append(gt_image.unsqueeze(0))
radii_list.append(radii.unsqueeze(0))
visibility_filter_list.append(visibility_filter.unsqueeze(0))
viewspace_point_tensor_list.append(viewspace_point_tensor)
viewspace_point_tensor_list_motion.append(render_pkg_motion['viewspace_points'])
radii_list_second.append(render_pkg_second['radii'].unsqueeze(0))
viewspace_point_tensor_list_second.append(render_pkg_second['viewspace_points'])
visibility_filter_list_second.append(render_pkg_second['visibility_filter'].unsqueeze(0))
# ---- Batch Tensor Consolidation ----
radii = torch.cat(radii_list, 0).max(dim=0).values
motion_masks = torch.cat(motion_masks, 0)
visibility_filter = torch.cat(visibility_filter_list).any(dim=0)
# Accumulate visibility filters for background model
if (iteration - 1) % accumulation_steps == 0:
visibility_filter_second = torch.cat(visibility_filter_list_second).any(dim=0)
else:
visibility_filter_second = torch.logical_or(visibility_filter_second,
torch.cat(visibility_filter_list_second).any(dim=0))
image_tensor_first = torch.cat(images, 0)
gt_image_tensor = torch.cat(gt_images, 0)
image_tensor_second = torch.cat(images_second, 0)
radii_second = torch.cat(radii_list_second, 0).max(dim=0).values
depth_images_tensor = torch.cat(depth_images, 0)
depth_images_dy_tensor = torch.cat(depth_images_dy, 0)
foreground_prob_tensor = torch.cat(foreground_prob_list, 0).max(dim=0).values.cuda()
# ---- Pruning Schedule Configuration ----
if optimization_params.prune_small_foreground_visbility:
step_to_prune = optimization_params.iterations / 2
else:
# Never prune based on visibility (2x max iterations)
step_to_prune = optimization_params.iterations * 2
# ---- Motion Probability Processing ----
if stage == "coarse":
motion_pro_first = motion_masks[:, 2:3, :, :] - 0.25
motion_pro_second = motion_masks[:, 1:2, :, :] + 0.25
else:
motion_pro_first = motion_masks[:, 2:3, :, :] + 1e-6
motion_pro_second = motion_masks[:, 1:2, :, :] + 1e-6
# ---- Apply Image Masks ----
if optimization_params.vignette_mask:
image_tensor_second = image_tensor_second * pixel_valid_mask * vignette_mask
gt_image_tensor = gt_image_tensor * pixel_valid_mask
image_tensor_first = image_tensor_first * pixel_valid_mask * vignette_mask
else:
image_tensor_second = image_tensor_second * pixel_valid_mask
gt_image_tensor = gt_image_tensor * pixel_valid_mask
image_tensor_first = image_tensor_first * pixel_valid_mask
# ---- Brightness Control Activation ----
activation_light = BrightnessActivation()
light_var = 0.5 + activation_light(motion_masks[:, 0:1, :, :].repeat(1, 3, 1, 1))
# ---- Background Brightness Adjustment ----
if optimization_params.use_brightness_control:
# Save raw background for debugging/loss computation
image_second_to_show = image_tensor_second.clone().detach().cpu()
image_second_to_show_g = image_tensor_second.clone().detach()
image_tensor_second = image_tensor_second * light_var
image_tensor_second = torch.clamp(image_tensor_second, 0, 1 - 1e-9)
image_tensor_first = image_tensor_first
image_tensor_first = torch.clamp(image_tensor_first, 0, 1 - 1e-9)
else:
image_second_to_show = image_tensor_second.clone().detach().cpu()
image_second_to_show_g = image_tensor_second.clone().detach()
# ---- Probabilistic Motion Mask Normalization ----
distri = motion_pro_first + motion_pro_second
motion_masks_first = motion_pro_first / distri
motion_masks_second = motion_pro_second / distri
motion_masks_first = motion_masks_first * pixel_valid_mask
motion_masks_second = motion_masks_second * pixel_valid_mask
# Motion mask aliases for loss computation
mask_comp_first = motion_masks_first
mask_comp_second = motion_masks_second
# ---- Image Composition ----
image_dy = image_tensor_first * motion_masks_first
image_sta = image_tensor_second * motion_masks_second
vis_thresh = 0.49
# Final composite image
image_tensor = image_dy + image_sta
# ---- Primary Loss Computation ----
Ll1 = l1_loss(image_tensor, gt_image_tensor[:, :3, :, :])
psnr_ = psnr(image_tensor, gt_image_tensor).mean().double()
# ---- Stage-Specific Loss ----
if stage == "coarse":
# Coarse stage: background + mixed component loss
ll2 = l1_loss(image_tensor_second, gt_image_tensor[:, :3, :, :])
###### override with 0.5 compoisition
##### LL1 learns 0.9 ground truth to regularize foreground learning but allowing structrual modeling
if optimization_params.coarse_mean_override:
Ll1 = l1_loss(
image_tensor_first * (0.5) + image_tensor_second.clone().detach() * (
0.5)
, gt_image_tensor[:, :3, :, :] * 0.9)
else:
Ll1 = l1_loss(
image_tensor_first * (motion_masks_first) + image_tensor_second.clone().detach() * (
motion_masks_second)
, gt_image_tensor[:, :3, :, :] * 0.9)
loss = optimization_params.lambda_main_loss * Ll1 + optimization_params.lambda_main_loss * ll2
else:
# Fine stage: focus on composite quality
loss = optimization_params.lambda_main_loss * Ll1
# ---- Brightness Regularization Loss ----
start_to_penal = optimization_params.densify_until_iter // 10
loss_light = 0.0001 * l1_loss(light_var * pixel_valid_mask,
torch.ones_like(light_var).cuda() * pixel_valid_mask)
weight_penal_light_start = 0.001
weight_penal_light_end = optimization_params.weight_penal_light_end
weight_penal_light = weight_penal_light_start + (weight_penal_light_end - weight_penal_light_start) * (
iteration - start_to_penal) / (optimization_params.densify_until_iter - start_to_penal)
if optimization_params.explicitly_update_brightness_control and optimization_params.use_brightness_control:
image_second_without_light_var = image_second_to_show_g.clone().detach()
this_light_var = torch.clamp(image_second_without_light_var * light_var, 0, 1 - 1e-9)
if iteration <= start_to_penal:
loss_light += 0.01 * l1_loss(this_light_var, gt_image_tensor)
else:
if weight_penal_light < 0.01:
loss_light += (0.01 - weight_penal_light) * l1_loss(this_light_var, gt_image_tensor)
ssim_notdense = 0.4 * optimization_params.downscale_ulti_loss * (1.0 - ssim(image_tensor, gt_image_tensor[:, :3, :, :]))
loss_light += ssim_notdense
# ---- Additional Brightness Regularization ----
regularize_light_var = True
if regularize_light_var and stage == "fine" and optimization_params.use_brightness_control:
if iteration > start_to_penal:
loss_light += weight_penal_light * l1_loss(light_var * pixel_valid_mask,
torch.ones_like(light_var).cuda() * pixel_valid_mask)
# ---- Stage-Specific SSIM Loss ----
if stage == 'coarse':
ssim_loss_temp = ssim(image_tensor_second, gt_image_tensor)
ssim_loss_temp1 = ssim(image_tensor_first, gt_image_tensor)
loss += 0.2 * (1.0 - ssim_loss_temp)
loss += 0.2 * (1.0 - ssim_loss_temp1)
# ---- Temporal Smoothness Loss ----
if stage == "fine" and hypernetwork_config.time_smoothness_weight != 0:
tv_loss = foreground_gaussians.compute_regulation(hypernetwork_config.time_smoothness_weight,
hypernetwork_config.l1_time_planes,
hypernetwork_config.plane_tv_weight)
loss += tv_loss
# ---- Depth Separation Losses ----
if optimization_params.separation_high_prob:
if optimization_params.detach_background_separation:
loss_depth_back = torch.maximum(
(
depth_images_dy_tensor - (
depth_images_tensor.clone().detach())) / scene.cameras_extent * pixel_valid_mask * (
mask_comp_first > 0.6).clone().detach(),
torch.tensor(0)).mean()
else:
########### this helps to push floaters further away from camera and be pruned afterwards
loss_depth_back = torch.maximum(
(
depth_images_dy_tensor - depth_images_tensor) / scene.cameras_extent * pixel_valid_mask * (
mask_comp_first > 0.6).clone().detach(),
torch.tensor(0)).mean()
loss += optimization_params.lambda_loss_depth_back * loss_depth_back
if optimization_params.separation_low_prob:
loss_depth_forward = torch.maximum(
(
depth_images_tensor.clone().detach() - depth_images_dy_tensor) / scene.cameras_extent * pixel_valid_mask * (
mask_comp_first < 0.4).clone().detach(),
torch.tensor(0)).mean()
loss += 0.1 * loss_depth_forward
if iteration % 100 == 0:
print("depth loss", loss_depth_back.item())
print("depth forward loss", loss_depth_forward.item())
# ---- Depth Smoothness Loss ----
if optimization_params.use_depth_smoothness_loss:
depth_reg = EdgeAwareTV()
depth_scaled = depth_images_tensor / scene.cameras_extent
loss_smooth_sta = depth_reg(depth_scaled.permute(0, 2, 3, 1), gt_image_tensor.permute(0, 2, 3, 1))
loss += optimization_params.lambda_depth_smoothness * (loss_smooth_sta)
# ---- Dynamic Content Diversity Loss ----
if optimization_params.penalize_dynamic:
dynamic_segment = image_tensor_first
static_disposed = (image_tensor_second).clone().detach().cuda()
d_ssim_str = (structural_ssim(dynamic_segment, static_disposed)) * pixel_valid_mask * (
mask_comp_first > 1 - vis_thresh)
diversity_penalty = d_ssim_str.mean()
loss += 0.01 * diversity_penalty
# ---- Foreground/Background Component Losses ----
if optimization_params.use_foreground_background_loss:
# High-confidence region losses
masked_2d_first = mask_comp_first > 1 - vis_thresh
l1_comp_first = l1_loss(image_tensor_first * masked_2d_first, gt_image_tensor * masked_2d_first)
if optimization_params.ssim_loss_use:
ssim_loss_raw_first = ssim_raw(image_tensor_first, gt_image_tensor)
loss += 0.4 * optimization_params.downscale_ulti_loss * ((1.0 - (ssim_loss_raw_first)) * masked_2d_first * pixel_valid_mask).mean()
loss += 2 * optimization_params.downscale_ulti_loss * l1_comp_first
masked_2d = mask_comp_second > 1 - vis_thresh
l1_comp_second = l1_loss(image_tensor_second * masked_2d, gt_image_tensor * masked_2d)
if optimization_params.ssim_loss_use:
ssim_raw_second = ssim_raw(image_tensor_second, gt_image_tensor)
loss += 0.4*optimization_params.downscale_ulti_loss * ((1.0 - ssim_raw_second) * masked_2d * pixel_valid_mask).mean()
loss += 2*optimization_params.downscale_ulti_loss * l1_comp_second
# ---- Gaussian Scale Regularization ----
if optimization_params.use_penal_large_gaussians:
max_scale = 0.1 * scene.cameras_extent
scale_exp = torch.exp(foreground_gaussians._scaling)
scale_max_size = (torch.maximum(scale_exp.amax(dim=-1),
torch.tensor(max_scale).cuda()) - max_scale)
scale_reg_max = 0.01 * scale_max_size.mean()
loss_light += scale_reg_max
scale_exp_second = torch.exp(background_gaussians._scaling)
scale_max_size_second = (torch.maximum(scale_exp_second.amax(dim=-1),
torch.tensor(max_scale).cuda()) - max_scale)
scale_reg_second_max = 0.01 * scale_max_size_second.mean()
loss_light += scale_reg_second_max
# ---- Gaussian Aspect Ratio Regularization ----
if optimization_params.use_penal_spiky_gaussians:
if iteration % 10 == 0:
scale_exp = torch.exp(foreground_gaussians._scaling)
scale_reg = (
torch.maximum(
scale_exp.amax(dim=-1) / scale_exp.amin(dim=-1),
torch.tensor(5),
)
- 5
)
scale_reg = 0.1 * scale_reg.mean()
loss_light += scale_reg
if iteration % 10 == 0:
scale_exp = torch.exp(background_gaussians._scaling)
scale_reg = (
torch.maximum(
scale_exp.amax(dim=-1) / scale_exp.amin(dim=-1),
torch.tensor(5),
)
- 5
)
scale_reg = 0.1 * scale_reg.mean()
loss_light += scale_reg
# ---- Motion Mask Entropy Loss (second half of training) ----
if iteration > optimization_params.iterations // 2:
loss_light += optimization_params.lambda_entropy_loss * entropy_loss(mask_comp_first)
# ---- Gradient Accumulation & Backpropagation ----
loss = loss / (accumulation_steps)
loss.backward(retain_graph=True)
# Emergency restart on NaN loss
if torch.isnan(loss).any():
print("loss is nan,end training, reexecv program now.")
os.execv(sys.executable, [sys.executable] + sys.argv)
# ---- Gradient Collection from Multi-Model Rendering ----
viewspace_point_tensor_grad = torch.zeros_like(viewspace_point_tensor)
if (iteration - 1) % accumulation_steps == 0:
viewspace_point_tensor_grad_second = torch.zeros_like(render_pkg_second['viewspace_points'])
for idx in range(0, len(viewspace_point_tensor_list)):
if optimization_params.use_motion_grad:
# Include motion mask gradients for foreground
viewspace_point_tensor_grad = viewspace_point_tensor_grad + viewspace_point_tensor_list[
idx].grad.clone() + viewspace_point_tensor_list_motion[idx].grad.clone()
viewspace_point_tensor_grad_second = viewspace_point_tensor_grad_second + \
viewspace_point_tensor_list_second[idx].grad.clone()
else:
viewspace_point_tensor_grad = viewspace_point_tensor_grad + viewspace_point_tensor_list[
idx].grad.clone()
viewspace_point_tensor_grad_second = viewspace_point_tensor_grad_second + \
viewspace_point_tensor_list_second[idx].grad
# ---- Brightness Loss Backpropagation ----
if loss_light:
loss_light = loss_light / (accumulation_steps)
loss_light.backward()
if iteration % 100 == 0:
print('redner_grad: ', torch.norm((viewspace_point_tensor_list[idx].grad).clone().detach()).mean())
print('redner_grad_motion: ',
torch.norm((viewspace_point_tensor_list_motion[idx].grad).clone().detach()).mean())
iter_end.record()
with torch.no_grad():
# Progress bar
ema_loss_for_log = 0.4 * loss.item() + 0.6 * ema_loss_for_log
ema_psnr_for_log = 0.4 * psnr_ + 0.6 * ema_psnr_for_log
ema_lossl1_for_log = 0.4 * Ll1.item() + 0.6 * ema_lossl1_for_log
total_point = foreground_gaussians._xyz.shape[0]
total_point_second = background_gaussians._xyz.shape[0]
if iteration % 10 == 0:
progress_bar.set_postfix({"Loss": f"{ema_loss_for_log:.{7}f}",
"ll1": f"{ema_lossl1_for_log:.{7}f}",
"psnr": f"{psnr_:.{2}f}",
"point": f"{total_point}",
"point_second": f"{total_point_second}"})
progress_bar.update(10)
if iteration == optimization_params.iterations:
progress_bar.close()
# ---- Logging and Saving ----
timer.pause()
if (iteration in saving_iterations):
print("\n[ITER {}] Saving Gaussians".format(iteration))
scene.save(iteration, stage)
# ---- Debug Visualization (every 100 iterations) ----
if iteration % 100 == 0:
out_debug_depth_dir = os.path.join(optimization_params.saving_folder, expname)
os.makedirs(out_debug_depth_dir, exist_ok=True)
fig, ax = plt.subplots(3, 5, figsize=(30, 18))
plt.rcParams['font.family'] = "sans-serif"
ax[0, 0].imshow(np.clip(gt_image_tensor.clone().detach().cpu()[0].permute(1, 2, 0).numpy(), 0, 1))
ax[0, 0].set_title("Ground Truth")
ax[1, 0].imshow(np.clip(image_tensor.clone().detach().cpu()[0].permute(1, 2, 0).numpy(), 0, 1))
ax[1, 0].set_title("Full Predicted")
ax[0, 1].imshow(np.clip(image_tensor_first[0].clone().detach().cpu().permute(1, 2, 0).numpy(), 0, 1))
ax[0, 1].set_title("Dynamic Raw")
ax[1, 1].imshow(np.clip(image_dy[0].clone().detach().cpu().permute(1, 2, 0).numpy(), 0, 1))
ax[1, 1].set_title("Dynamic Final")
ax[0, 2].imshow(np.clip(image_second_to_show[0].permute(1, 2, 0).numpy(), 0, 1))
ax[0, 2].set_title("Static Raw")
ax[1, 2].imshow(np.clip(image_sta[0].clone().detach().cpu().permute(1, 2, 0).numpy(), 0, 1))
ax[1, 2].set_title("Static Final")
ax[0, 3].imshow(
np.clip(
(motion_masks[0, 2:3] * pixel_valid_mask[0]).clone().detach().cpu().permute(1, 2, 0).numpy(), 0,
1),
cmap='jet', vmin=0, vmax=1)
ax[0, 3].set_title("Dynamic Prob")
ax[1, 3].imshow(
np.clip(
(motion_masks[0, 0:1] * pixel_valid_mask[0]).clone().detach().cpu().permute(1, 2, 0).numpy(), 0,
1),
cmap='jet', vmin=0, vmax=1)
ax[1, 3].set_title("Brightness Control")
ax[0, 4].imshow(np.clip(image_tensor_second[0].clone().detach().cpu().permute(1, 2, 0).numpy(), 0, 1))
ax[0, 4].set_title("Static Raw with Brightness Control")
ax[1, 4].imshow(
np.clip(
torch.abs(gt_image_tensor - image_tensor_second).sum(axis=1)[0].clone().detach().cpu().numpy(),
0, 1),
cmap='jet', vmin=0, vmax=1)
ax[1, 4].set_title("Static Raw - GT Error")
ax[2, 0].imshow(
np.clip(normalize_depth((depth_images_dy_tensor * mask_comp_first[0, :1] +
depth_images_tensor * mask_comp_second[0, :1])[
0].clone().detach().cpu().permute(1, 2, 0).numpy()), 0, 1),
cmap='jet', vmin=0, vmax=1)
ax[2, 0].set_title("Depth Image Pred")
ax[2, 1].imshow(
np.clip(normalize_depth(depth_images_dy_tensor[0].clone().detach().cpu().permute(1, 2, 0).numpy()),
0, 1),
cmap='jet', vmin=0, vmax=1)
ax[2, 1].set_title("Depth Image Pred dynamic")
ax[2, 2].imshow(
np.clip(normalize_depth(depth_images_tensor[0].clone().detach().cpu().permute(1, 2, 0).numpy()), 0,
1),
cmap='jet', vmin=0, vmax=1)
ax[2, 2].set_title("Depth Image Pred static")
ax[2, 3].imshow(
np.clip(
(motion_masks[0, 1:2] * pixel_valid_mask[0]).clone().detach().cpu().permute(1, 2, 0).numpy(), 0,
1),
cmap='jet', vmin=0, vmax=1)
ax[2, 3].set_title("Static Raw Prob")
ax[2, 4].imshow(
np.clip((mask_comp_first[0] * pixel_valid_mask[0]).clone().detach().cpu().permute(1, 2, 0).numpy(),
0, 1),
cmap='jet', vmin=0, vmax=1)
ax[2, 4].set_title("Full Probabilistic Mask")
plt.savefig(os.path.join(out_debug_depth_dir, stage + '_' + str(iteration).zfill(6) + ".jpg"))
plt.close()
timer.start()
# ---- Adaptive Gaussian Densification & Pruning ----
if iteration < optimization_params.densify_until_iter:
# Track foreground importance (using probability instead of radii)
foreground_gaussians.max_radii2D[visibility_filter] = torch.max(
foreground_gaussians.max_radii2D[visibility_filter],
foreground_prob_tensor[visibility_filter])
foreground_gaussians.add_densification_stats(viewspace_point_tensor_grad, visibility_filter)
# ---- Threshold Scheduling ----
if stage == "coarse":
opacity_threshold = optimization_params.opacity_threshold_coarse
densify_threshold = optimization_params.densify_grad_threshold_coarse
else:
# Progressive threshold scheduling for fine stage
opacity_threshold = optimization_params.opacity_threshold_fine_init - iteration * (
optimization_params.opacity_threshold_fine_init - optimization_params.opacity_threshold_fine_after) / (
optimization_params.densify_until_iter)
if iteration < optimization_params.densify_until_iter * 0.5:
# First half: gradually reduce threshold
densify_threshold = optimization_params.densify_grad_threshold_fine_init - iteration * (
optimization_params.densify_grad_threshold_fine_init - optimization_params.densify_grad_threshold_after) / (
optimization_params.densify_until_iter)
else:
# Second half: use minimum threshold
densify_threshold = optimization_params.densify_grad_threshold_after
if accumulation_steps > 1:
# Scale threshold for gradient accumulation
densify_threshold = densify_threshold / (accumulation_steps)
if optimization_params.make_foreground_thresh_larger:
# Make foreground densification more aggressive
densify_threshold = densify_threshold * 2
# ---- Densification (Add Gaussians) ----
if iteration > optimization_params.densify_from_iter and iteration % optimization_params.densification_interval == 0 and \
foreground_gaussians.get_xyz.shape[0] < optimization_params.max_gaussian_foreground:
size_threshold = 50 if iteration > optimization_params.opacity_reset_interval else None
foreground_gaussians.densify(densify_threshold, opacity_threshold, scene.cameras_extent,
size_threshold, 5, 5,
scene.model_path, iteration, stage)
# ---- Pruning (Remove Gaussians) ----
if iteration > optimization_params.pruning_from_iter and iteration % optimization_params.pruning_interval == 0 and \
foreground_gaussians.get_xyz.shape[0] > 10000:
size_threshold = 20 if iteration > step_to_prune else None
if Finish_whose_seq:
# End-of-epoch pruning with probability-based criteria
run_pruning_prob = True
foreground_gaussians.prune(densify_threshold, opacity_threshold, scene.cameras_extent,
size_threshold,
run_pruning_prob=run_pruning_prob)
Finish_whose_seq = False
else:
# Standard pruning
foreground_gaussians.prune(densify_threshold, opacity_threshold, scene.cameras_extent,
size_threshold)
# ---- Optional Point Growing ----
if iteration % optimization_params.densification_interval == 0 and foreground_gaussians.get_xyz.shape[
0] < 360000 and optimization_params.add_point:
foreground_gaussians.grow(5, 5, scene.model_path, iteration, stage)
# ---- Opacity Reset (prevent degradation) ----
if iteration % optimization_params.opacity_reset_interval == 0:
print("reset opacity")
foreground_gaussians.reset_opacity_partially_small()
if optimization_params.reset_SH:
foreground_gaussians.reset_sh_partially_first()
# ---- Foreground Model Optimization ----
max_norm = 1.0
# Optional gradient clipping (currently disabled)
# torch.nn.utils.clip_grad_norm_(foreground_gaussians._deformation.parameters(), max_norm)
if iteration < optimization_params.iterations:
foreground_gaussians.optimizer.step()
foreground_gaussians.optimizer.zero_grad(set_to_none=True)
# ---- Background Model Management ----
if iteration < optimization_params.densify_until_iter and iteration % accumulation_steps == 0:
visibility_filter = visibility_filter_second
radii = radii_second
# Track background importance (using actual radii)
background_gaussians.max_radii2D[visibility_filter] = torch.max(
background_gaussians.max_radii2D[visibility_filter],
radii[visibility_filter])
background_gaussians.add_densification_stats(viewspace_point_tensor_grad_second, visibility_filter)
# ---- Background Threshold Configuration ----
if stage == "coarse":
opacity_threshold = optimization_params.opacity_threshold_coarse
densify_threshold = optimization_params.densify_grad_threshold_coarse
else:
opacity_threshold = optimization_params.opacity_threshold_fine_init - iteration * (
optimization_params.opacity_threshold_fine_init - optimization_params.opacity_threshold_fine_after) / (
optimization_params.densify_until_iter)
# Background uses coarse threshold (less aggressive)
densify_threshold = optimization_params.densify_grad_threshold_coarse
if optimization_params.make_background_thresh_larger:
densify_threshold = densify_threshold * 2
# ---- Background Densification ----
if iteration > optimization_params.densify_from_iter and iteration % optimization_params.densification_interval == 0 and \
background_gaussians.get_xyz.shape[0] < optimization_params.max_gaussian_background:
size_threshold = 20 if iteration > optimization_params.opacity_reset_interval else None
background_gaussians.densify(densify_threshold, opacity_threshold, scene.cameras_extent,
size_threshold,
5, 5,
scene.model_path, iteration, stage)
# ---- Background Pruning ----
if iteration > optimization_params.pruning_from_iter and iteration % optimization_params.pruning_interval == 0 and \
background_gaussians.get_xyz.shape[0] > 100000:
size_threshold = 50 if iteration > step_to_prune and iteration < int(
optimization_params.densify_until_iter * 0.8) else None
background_gaussians.prune(densify_threshold, opacity_threshold, scene.cameras_extent,
size_threshold)
# ---- Background Opacity Reset ----
if iteration % optimization_params.opacity_reset_interval == 0:
print("reset opacity")
background_gaussians.reset_opacity_partially()
if optimization_params.reset_SH:
background_gaussians.reset_sh_partially_second()
# ---- Background Model Optimization ----
if iteration < optimization_params.iterations and iteration % accumulation_steps == 0:
background_gaussians.optimizer.step()
background_gaussians.optimizer.zero_grad(set_to_none=True)
# ---- Checkpoint Saving ----
if (iteration in checkpoint_iterations):
print("\n[ITER {}] Saving Checkpoint".format(iteration))
torch.save((foreground_gaussians.capture(), iteration),
scene.model_path + "/chkpnt" + f"_{stage}_" + str(iteration) + ".pth")
if (iteration in checkpoint_iterations):
print("\n[ITER {}] Saving Checkpoint".format(iteration))
torch.save((background_gaussians.capture(), iteration),
scene.model_path + "/chkpnt" + f"_{stage}_gs2_" + str(iteration) + ".pth")
# ---- Post-Training Evaluation (Fine Stage Only) ----
if stage == "fine":
batch_size = 1