From 395b03f48c3e0c953dc6117755742108207af01b Mon Sep 17 00:00:00 2001 From: Matthew Tancik Date: Thu, 28 Jul 2022 10:50:56 -0700 Subject: [PATCH] Add boolean visualization (#225) --- nerfactory/graphs/instant_ngp.py | 9 ++---- nerfactory/utils/visualization.py | 50 +++++++++++++++++++++++-------- pyproject.toml | 1 + tests/utils/test_visualization.py | 39 ++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 tests/utils/test_visualization.py diff --git a/nerfactory/graphs/instant_ngp.py b/nerfactory/graphs/instant_ngp.py index efc316ec4..049b0ec00 100644 --- a/nerfactory/graphs/instant_ngp.py +++ b/nerfactory/graphs/instant_ngp.py @@ -134,15 +134,12 @@ class NGPGraph(Graph): """Do preprocessing to make images valid""" # TODO: make log_test_image_outputs use this directly # TODO: implement across all the different graph implementations - acc = visualization.apply_colormap(outputs["accumulation"]) - depth = visualization.apply_depth_colormap( + outputs["accumulation"] = visualization.apply_colormap(outputs["accumulation"]) + outputs["depth"] = visualization.apply_depth_colormap( outputs["depth"], accumulation=outputs["accumulation"], ) - combined_acc = torch.cat([acc], dim=1) - outputs["accumulation"] = combined_acc - combined_depth = torch.cat([depth], dim=1) - outputs["depth"] = combined_depth + outputs["alive_ray_mask"] = visualization.apply_boolean_colormap(outputs["alive_ray_mask"]) def log_test_image_outputs(self, image_idx, step, batch, outputs): image = batch["image"] diff --git a/nerfactory/utils/visualization.py b/nerfactory/utils/visualization.py index 11d00193a..e4183293e 100644 --- a/nerfactory/utils/visualization.py +++ b/nerfactory/utils/visualization.py @@ -20,16 +20,18 @@ import torch from matplotlib import cm from torchtyping import TensorType +from nerfactory.utils import colors -def apply_colormap(image: TensorType[..., 1], cmap="viridis") -> TensorType[..., 3]: + +def apply_colormap(image: TensorType["bs":..., 1], cmap="viridis") -> TensorType["bs":..., "rgb":3]: """Convert single channel to a color image. Args: - image (TensorType[..., 1]): Single channel image. - cmap (str, optional): Colormap for image. Defaults to 'turbo'. + image: Single channel image. + cmap: Colormap for image. Defaults to 'turbo'. Returns: - TensorType[..., 3]: Colored image + TensorType: Colored image """ colormap = cm.get_cmap(cmap) @@ -43,23 +45,23 @@ def apply_colormap(image: TensorType[..., 1], cmap="viridis") -> TensorType[..., def apply_depth_colormap( - depth: TensorType[..., 1], - accumulation: Optional[TensorType[..., 1]] = None, + depth: TensorType["bs":..., 1], + accumulation: Optional[TensorType["bs":..., 1]] = None, near_plane: Optional[float] = None, far_plane: Optional[float] = None, cmap="turbo", -) -> TensorType[..., 3]: +) -> TensorType["bs":..., "rgb":3]: """Converts a depth image to color for easier analysis. Args: - depth (TensorType[..., 1]): Depth image. - accumulation (TensorType[..., 1], optional): Ray accumulation used for masking vis. Defaults to None. - near_plane (float, optional): Closest depth to consider. If None, use min image value. Defaults to None. - far_plane (float, optional): Furthest depth to consider. If None, use max image value. Defaults to None. - cmap (str, optional): Colormap to apply. Defaults to "turbo". + depth: Depth image. + accumulation: Ray accumulation used for masking vis. Defaults to None. + near_plane: Closest depth to consider. If None, use min image value. Defaults to None. + far_plane: Furthest depth to consider. If None, use max image value. Defaults to None. + cmap: Colormap to apply. Defaults to "turbo". Returns: - TensorType[..., 3]: Colored depth image + Colored depth image """ near_plane = near_plane or torch.min(depth) @@ -75,3 +77,25 @@ def apply_depth_colormap( colored_image = colored_image * accumulation + (1 - accumulation) return colored_image + + +def apply_boolean_colormap( + image: TensorType["bs":..., 1, bool], + true_color: TensorType["bs":..., "rgb":3] = colors.WHITE, + false_color: TensorType["bs":..., "rgb":3] = colors.BLACK, +) -> TensorType["bs":..., "rgb":3]: + """Converts a depth image to color for easier analysis. + + Args: + image: Boolean image. + true_color: Color to use for True. Defaults to white. + false_color: Color to use for False. Defaults to black. + + Returns: + Colored boolean image + """ + + colored_image = torch.ones(image.shape[:-1] + (3,)) + colored_image[image[..., 0], :] = true_color + colored_image[~image[..., 0], :] = false_color + return colored_image diff --git a/pyproject.toml b/pyproject.toml index 138dcf001..e9c000bb4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,7 @@ disable = [ "too-many-locals", "too-many-instance-attributes", "duplicate-code", + "unnecessary-ellipsis", ] #pytest diff --git a/tests/utils/test_visualization.py b/tests/utils/test_visualization.py new file mode 100644 index 000000000..4b5e832b5 --- /dev/null +++ b/tests/utils/test_visualization.py @@ -0,0 +1,39 @@ +""" +Test visualizations +""" +import torch + +from nerfactory.utils import visualization + + +def test_apply_colormap(): + """Test adding a colormap to data""" + data = torch.rand((10, 20, 1)) + colored_data = visualization.apply_colormap(data) + + assert colored_data.shape == (10, 20, 3) + assert torch.min(colored_data) >= 0 + assert torch.max(colored_data) <= 1 + + +def test_apply_depth_colormap(): + """Test adding a colormap to depth data""" + data = torch.rand((10, 20, 1)) + accum = torch.rand((10, 20, 1)) + accum = accum / torch.max(accum) + colored_data = visualization.apply_depth_colormap(depth=data, accumulation=accum) + + assert colored_data.shape == (10, 20, 3) + assert torch.min(colored_data) >= 0 + assert torch.max(colored_data) <= 1 + + +def test_apply_boolean_colormap(): + """Test adding a colormap to boolean data""" + data = torch.rand((10, 20, 1)) + data = data > 0.5 + colored_data = visualization.apply_boolean_colormap(data) + + assert colored_data.shape == (10, 20, 3) + assert torch.min(colored_data) >= 0 + assert torch.max(colored_data) <= 1