Add boolean visualization (#225)

This commit is contained in:
Matthew Tancik authored and GitHub committed 2022-07-28 10:50:56 -07:00
1 parent 87df978582
commit 395b03f48c
4 files changed
+80 -19

No files matched your search

+3 -6
View File
@@ -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"]
+37 -13
View File
@@ -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
+1
View File
@@ -35,6 +35,7 @@ disable = [
"too-many-locals",
"too-many-instance-attributes",
"duplicate-code",
"unnecessary-ellipsis",
]
#pytest
+39
View File
@@ -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