implementing l1 and tv reg in the original tensorf paper (#1855)

* implementing l1 and tv reg in the original tensorf paper

* added documentation for tv loss

* added test for tv loss

* fix formating

* fix formatting

---------

Co-authored-by: Yan Xiao <yax010@ucsd.edu>
This commit is contained in:
Yan XiaoandYan Xiao authored and GitHub committed 2023-05-09 09:32:51 -07:00
1 parent f92275603a
commit 38cbb0f358
4 files changed
+77 -3

No files matched your search

+29 -2
View File
@@ -43,7 +43,7 @@ from nerfstudio.field_components.encodings import (
)
from nerfstudio.field_components.field_heads import FieldHeadNames
from nerfstudio.fields.tensorf_field import TensoRFField
from nerfstudio.model_components.losses import MSELoss
from nerfstudio.model_components.losses import MSELoss, tv_loss
from nerfstudio.model_components.ray_samplers import PDFSampler, UniformSampler
from nerfstudio.model_components.renderers import (
AccumulationRenderer,
@@ -67,7 +67,14 @@ class TensoRFModelConfig(ModelConfig):
"""final render resolution"""
upsampling_iters: Tuple[int, ...] = (2000, 3000, 4000, 5500, 7000)
"""specifies a list of iteration step numbers to perform upsampling"""
loss_coefficients: Dict[str, float] = to_immutable_dict({"rgb_loss": 1.0})
loss_coefficients: Dict[str, float] = to_immutable_dict(
{
"rgb_loss": 1.0,
"tv_reg_density": 1e-3,
"tv_reg_color": 1e-4,
"l1_reg": 5e-4,
}
)
"""Loss specific weights."""
num_samples: int = 50
"""Number of samples in field evaluation"""
@@ -80,6 +87,8 @@ class TensoRFModelConfig(ModelConfig):
appearance_dim: int = 27
"""Number of channels for color encoding"""
tensorf_encoding: Literal["triplane", "vm", "cp"] = "vm"
regularization: Literal["none", "l1", "tv"] = "l1"
"""Regularization method used in tensorf paper"""
class TensoRFModel(Model):
@@ -241,6 +250,10 @@ class TensoRFModel(Model):
if self.config.enable_collider:
self.collider = AABBBoxCollider(scene_box=self.scene_box)
# regularizations
if self.config.tensorf_encoding == "cp" and self.config.regularization == "tv":
raise RuntimeError("TV reg not supported for CP decomposition")
def get_param_groups(self) -> Dict[str, List[Parameter]]:
param_groups = {}
@@ -295,6 +308,20 @@ class TensoRFModel(Model):
rgb_loss = self.rgb_loss(image, outputs["rgb"])
loss_dict = {"rgb_loss": rgb_loss}
if self.config.regularization == "l1":
l1_parameters = []
for parameter in self.field.density_encoding.parameters():
l1_parameters.append(parameter.view(-1))
loss_dict["l1_reg"] = torch.abs(torch.cat(l1_parameters)).mean()
elif self.config.regularization == "tv":
loss_dict["tv_reg_density"] = tv_loss(self.field.density_encoding.plane_coef)
loss_dict["tv_reg_color"] = tv_loss(self.field.color_encoding.plane_coef)
elif self.config.regularization == "none":
pass
else:
raise ValueError(f"Regularization {self.config.regularization} not supported")
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
return loss_dict