-
Notifications
You must be signed in to change notification settings - Fork 22
/
densification.py
181 lines (157 loc) · 7.61 KB
/
densification.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
import torch
import utils.general_utils as utils
def densification(iteration, scene, gaussians, batched_screenspace_pkg):
args = utils.get_args()
timers = utils.get_timers()
log_file = utils.get_log_file()
# Densification
if not args.disable_auto_densification and iteration <= args.densify_until_iter:
# Keep track of max radii in image-space for pruning
timers.start("densification")
timers.start("densification_update_stats")
for radii, visibility_filter, screenspace_mean2D in zip(
batched_screenspace_pkg["batched_locally_preprocessed_radii"],
batched_screenspace_pkg["batched_locally_preprocessed_visibility_filter"],
batched_screenspace_pkg["batched_locally_preprocessed_mean2D"],
):
gaussians.max_radii2D[visibility_filter] = torch.max(
gaussians.max_radii2D[visibility_filter], radii[visibility_filter]
)
gaussians.add_densification_stats(screenspace_mean2D, visibility_filter)
timers.stop("densification_update_stats")
if iteration > args.densify_from_iter and utils.check_update_at_this_iter(
iteration, args.bsz, args.densification_interval, 0
):
assert (
args.stop_update_param == False
), "stop_update_param must be false for densification; because it is a flag for debugging."
# utils.print_rank_0("iteration: {}, bsz: {}, update_interval: {}, update_residual: {}".format(iteration, args.bsz, args.densification_interval, 0))
timers.start("densify_and_prune")
size_threshold = 20 if iteration > args.opacity_reset_interval else None
gaussians.densify_and_prune(
args.densify_grad_threshold,
args.min_opacity,
scene.cameras_extent,
size_threshold,
)
timers.stop("densify_and_prune")
# redistribute after densify_and_prune, because we have new gaussians to distribute evenly.
if utils.get_denfify_iter() % args.redistribute_gaussians_frequency == 0:
num_3dgs_before_redistribute = gaussians.get_xyz.shape[0]
timers.start("redistribute_gaussians")
gaussians.redistribute_gaussians()
timers.stop("redistribute_gaussians")
num_3dgs_after_redistribute = gaussians.get_xyz.shape[0]
log_file.write(
"iteration[{},{}) redistribute. Now num of 3dgs before redistribute: {}. Now num of 3dgs after redistribute: {}. \n".format(
iteration,
iteration + args.bsz,
num_3dgs_before_redistribute,
num_3dgs_after_redistribute,
)
)
utils.check_memory_usage(
log_file, args, iteration, gaussians, before_densification_stop=True
)
utils.inc_densify_iter()
if (
utils.check_update_at_this_iter(
iteration, args.bsz, args.opacity_reset_interval, 0
)
and iteration + args.bsz <= args.opacity_reset_until_iter
):
timers.start("reset_opacity")
gaussians.reset_opacity()
timers.stop("reset_opacity")
timers.stop("densification")
else:
if iteration > args.densify_from_iter and utils.check_update_at_this_iter(
iteration, args.bsz, args.densification_interval, 0
):
utils.check_memory_usage(
log_file, args, iteration, gaussians, before_densification_stop=False
)
def gsplat_densification(iteration, scene, gaussians, batched_screenspace_pkg):
args = utils.get_args()
timers = utils.get_timers()
log_file = utils.get_log_file()
# Densification
if not args.disable_auto_densification and iteration <= args.densify_until_iter:
# Keep track of max radii in image-space for pruning
timers.start("densification")
timers.start("densification_update_stats")
image_width = batched_screenspace_pkg["image_width"]
image_height = batched_screenspace_pkg["image_height"]
batched_screenspace_mean2D_grad = batched_screenspace_pkg[
"batched_locally_preprocessed_mean2D"
].grad
for i, (radii, visibility_filter) in enumerate(
zip(
batched_screenspace_pkg["batched_locally_preprocessed_radii"],
batched_screenspace_pkg[
"batched_locally_preprocessed_visibility_filter"
],
)
):
gaussians.max_radii2D[visibility_filter] = torch.max(
gaussians.max_radii2D[visibility_filter], radii[visibility_filter]
)
gaussians.gsplat_add_densification_stats(
batched_screenspace_mean2D_grad[i],
visibility_filter,
image_width,
image_height,
)
timers.stop("densification_update_stats")
if iteration > args.densify_from_iter and utils.check_update_at_this_iter(
iteration, args.bsz, args.densification_interval, 0
):
assert (
args.stop_update_param == False
), "stop_update_param must be false for densification; because it is a flag for debugging."
# utils.print_rank_0("iteration: {}, bsz: {}, update_interval: {}, update_residual: {}".format(iteration, args.bsz, args.densification_interval, 0))
timers.start("densify_and_prune")
size_threshold = 20 if iteration > args.opacity_reset_interval else None
gaussians.densify_and_prune(
args.densify_grad_threshold,
args.min_opacity,
scene.cameras_extent,
size_threshold,
)
timers.stop("densify_and_prune")
# redistribute after densify_and_prune, because we have new gaussians to distribute evenly.
if utils.get_denfify_iter() % args.redistribute_gaussians_frequency == 0:
num_3dgs_before_redistribute = gaussians.get_xyz.shape[0]
timers.start("redistribute_gaussians")
gaussians.redistribute_gaussians()
timers.stop("redistribute_gaussians")
num_3dgs_after_redistribute = gaussians.get_xyz.shape[0]
log_file.write(
"iteration[{},{}) redistribute. Now num of 3dgs before redistribute: {}. Now num of 3dgs after redistribute: {}. \n".format(
iteration,
iteration + args.bsz,
num_3dgs_before_redistribute,
num_3dgs_after_redistribute,
)
)
utils.check_memory_usage(
log_file, args, iteration, gaussians, before_densification_stop=True
)
utils.inc_densify_iter()
if (
utils.check_update_at_this_iter(
iteration, args.bsz, args.opacity_reset_interval, 0
)
and iteration + args.bsz <= args.opacity_reset_until_iter
):
timers.start("reset_opacity")
gaussians.reset_opacity()
timers.stop("reset_opacity")
timers.stop("densification")
else:
if iteration > args.densify_from_iter and utils.check_update_at_this_iter(
iteration, args.bsz, args.densification_interval, 0
):
utils.check_memory_usage(
log_file, args, iteration, gaussians, before_densification_stop=False
)