Skip to content

Commit cdcfd90

Browse files
author
Jianbo Ye
committed
Add average_init_density to improve robustness of nerfacto training
1 parent 1aba4ea commit cdcfd90

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

nerfstudio/fields/density_fields.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __init__(
5656
base_res: int = 16,
5757
log2_hashmap_size: int = 18,
5858
features_per_level: int = 2,
59+
average_init_density: float = 1.0,
5960
implementation: Literal["tcnn", "torch"] = "tcnn",
6061
) -> None:
6162
super().__init__()
@@ -66,6 +67,7 @@ def __init__(
6667
self.register_buffer("max_res", torch.tensor(max_res))
6768
self.register_buffer("num_levels", torch.tensor(num_levels))
6869
self.register_buffer("log2_hashmap_size", torch.tensor(log2_hashmap_size))
70+
self.register_buffer("average_init_density", torch.tensor(average_init_density))
6971

7072
self.encoding = HashEncoding(
7173
num_levels=num_levels,
@@ -111,7 +113,7 @@ def get_density(self, ray_samples: RaySamples) -> Tuple[Tensor, None]:
111113
# Rectifying the density with an exponential is much more stable than a ReLU or
112114
# softplus, because it enables high post-activation (float32) density outputs
113115
# from smaller internal (float16) parameters.
114-
density = trunc_exp(density_before_activation)
116+
density = self.average_init_density * trunc_exp(density_before_activation)
115117
density = density * selector[..., None]
116118
return density, None
117119

nerfstudio/fields/nerfacto_field.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def __init__(
9595
use_pred_normals: bool = False,
9696
use_average_appearance_embedding: bool = False,
9797
spatial_distortion: Optional[SpatialDistortion] = None,
98+
average_init_density: float = 1.0,
9899
implementation: Literal["tcnn", "torch"] = "tcnn",
99100
) -> None:
100101
super().__init__()
@@ -105,6 +106,7 @@ def __init__(
105106
self.register_buffer("max_res", torch.tensor(max_res))
106107
self.register_buffer("num_levels", torch.tensor(num_levels))
107108
self.register_buffer("log2_hashmap_size", torch.tensor(log2_hashmap_size))
109+
self.register_buffer("average_init_density", torch.tensor(average_init_density))
108110

109111
self.spatial_distortion = spatial_distortion
110112
self.num_images = num_images
@@ -218,7 +220,7 @@ def get_density(self, ray_samples: RaySamples) -> Tuple[Tensor, Tensor]:
218220
# Rectifying the density with an exponential is much more stable than a ReLU or
219221
# softplus, because it enables high post-activation (float32) density outputs
220222
# from smaller internal (float16) parameters.
221-
density = trunc_exp(density_before_activation.to(positions))
223+
density = self.average_init_density * trunc_exp(density_before_activation.to(positions))
222224
density = density * selector[..., None]
223225
return density, base_mlp_out
224226

nerfstudio/models/nerfacto.py

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class NerfactoModelConfig(ModelConfig):
124124
"""Which implementation to use for the model."""
125125
appearance_embed_dim: int = 32
126126
"""Dimension of the appearance embedding."""
127+
average_init_density: float = 0.01
128+
"""Average initial density output from MLP. """
127129
camera_optimizer: CameraOptimizerConfig = field(default_factory=lambda: CameraOptimizerConfig(mode="SO3xR3"))
128130
"""Config of the camera optimizer to use"""
129131

@@ -162,6 +164,7 @@ def populate_modules(self):
162164
use_pred_normals=self.config.predict_normals,
163165
use_average_appearance_embedding=self.config.use_average_appearance_embedding,
164166
appearance_embedding_dim=self.config.appearance_embed_dim,
167+
average_init_density=self.config.average_init_density,
165168
implementation=self.config.implementation,
166169
)
167170

@@ -179,6 +182,7 @@ def populate_modules(self):
179182
self.scene_box.aabb,
180183
spatial_distortion=scene_contraction,
181184
**prop_net_args,
185+
average_init_density=self.config.average_init_density,
182186
implementation=self.config.implementation,
183187
)
184188
self.proposal_networks.append(network)
@@ -190,6 +194,7 @@ def populate_modules(self):
190194
self.scene_box.aabb,
191195
spatial_distortion=scene_contraction,
192196
**prop_net_args,
197+
average_init_density=self.config.average_init_density,
193198
implementation=self.config.implementation,
194199
)
195200
self.proposal_networks.append(network)

0 commit comments

Comments
 (0)