From 353bb0daf76abd7acdd6c0c654941dd9bc8785c5 Mon Sep 17 00:00:00 2001 From: Matt Tancik Date: Tue, 31 May 2022 09:47:54 -0700 Subject: [PATCH 1/2] Default renderers to use random background color. --- radiance/nerf/graph/instant_ngp.py | 2 +- radiance/nerf/renderers.py | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/radiance/nerf/graph/instant_ngp.py b/radiance/nerf/graph/instant_ngp.py index 983c616c6..d2dd45171 100644 --- a/radiance/nerf/graph/instant_ngp.py +++ b/radiance/nerf/graph/instant_ngp.py @@ -24,7 +24,7 @@ from radiance.utils import visualization, writer class NGPGraph(Graph): - """NeRF-W graph""" + """Instant NGP graph""" def __init__(self, field_implementation="torch", intrinsics=None, camera_to_world=None, **kwargs) -> None: assert field_implementation in field_implementation_to_class diff --git a/radiance/nerf/renderers.py b/radiance/nerf/renderers.py index ce6933e64..91b630b59 100644 --- a/radiance/nerf/renderers.py +++ b/radiance/nerf/renderers.py @@ -17,7 +17,7 @@ class RGBRenderer(nn.Module): def __init__(self, background_color: Optional[TensorType[3]] = None) -> None: """ Args: - background_color (TensorType[3], optional): Background color as RGB. Defaults to black. + background_color (TensorType[3], optional): Background color as RGB. Defaults to random. """ super().__init__() self.background_color = background_color @@ -34,14 +34,17 @@ class RGBRenderer(nn.Module): Args: rgb (TensorType[..., "num_samples", -1]): RGB for each sample weights (TensorType[..., "num_samples"]): Weights for each sample + background_color (TensorType[3], optional): Background color as RGB. Defaults to random. Returns: TensorType[..., 3]: Outputs rgb values. """ rgb = torch.sum(weights[..., None] * rgb, dim=-2) - if background_color is not None: - rgb = rgb + background_color.to(weights.device)[None, ...] * (1.0 - torch.sum(weights, dim=-1)[..., None]) + if background_color is None: + background_color = torch.rand_like(rgb).to(rgb.device) + + rgb = rgb + background_color.to(weights.device) * (1.0 - torch.sum(weights, dim=-1)[..., None]) return rgb @@ -74,7 +77,7 @@ class SHRenderer(nn.Module): ) -> None: """ Args: - background_color (TensorType[3], optional): Background color as RGB. Defaults to black. + background_color (TensorType[3], optional): Background color as RGB. Defaults to random. activation (Optional[nn.Module], optional): Output activation. Defaults to Sigmoid(). """ super().__init__() From eb8434ace939a7597b70b9218f55137151af2f1d Mon Sep 17 00:00:00 2001 From: Matt Tancik Date: Tue, 31 May 2022 10:05:20 -0700 Subject: [PATCH 2/2] Update test sensitivity --- tests/nerf/test_renderers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/nerf/test_renderers.py b/tests/nerf/test_renderers.py index d238893ee..bdd87f4c2 100644 --- a/tests/nerf/test_renderers.py +++ b/tests/nerf/test_renderers.py @@ -21,7 +21,7 @@ def test_rgb_renderer(): assert torch.max(rgb) > 0.9 rgb = rgb_renderer(rgb=rgb_samples * 0, weights=weights) - assert torch.max(rgb) == pytest.approx(0) + assert torch.max(rgb) == pytest.approx(0, abs=1e-6) def test_sh_renderer():