Stop adding all ones alpha channel and clean up blend_background

This commit is contained in:
Nicholas Pfaff committed 2023-07-04 16:22:42 +01:00
1 parent 0bbaba6f1a
commit e24f15baad
3 files changed
+23 -19

No files matched your search

-3
View File
@@ -86,9 +86,6 @@ class InputDataset(Dataset):
image = torch.from_numpy(self.get_numpy_image(image_idx).astype("float32") / 255.0) image = torch.from_numpy(self.get_numpy_image(image_idx).astype("float32") / 255.0)
if self._dataparser_outputs.alpha_color is not None and image.shape[-1] == 4: if self._dataparser_outputs.alpha_color is not None and image.shape[-1] == 4:
image = image[:, :, :3] * image[:, :, -1:] + self._dataparser_outputs.alpha_color * (1.0 - image[:, :, -1:]) image = image[:, :, :3] * image[:, :, -1:] + self._dataparser_outputs.alpha_color * (1.0 - image[:, :, -1:])
elif image.shape[-1] == 3:
alpha_channel = torch.ones(size=(*image.shape[:2], 1), dtype=image.dtype)
image = torch.concatenate([image, alpha_channel], dim=2)
return image return image
def get_data(self, image_idx: int) -> Dict: def get_data(self, image_idx: int) -> Dict:
+13 -13
View File
@@ -28,7 +28,7 @@ Example:
""" """
import contextlib import contextlib
import math import math
from typing import Generator, Literal, Optional, Union, Dict from typing import Generator, Literal, Optional, Union
import nerfacc import nerfacc
import torch import torch
@@ -131,25 +131,25 @@ class RGBRenderer(nn.Module):
@classmethod @classmethod
def blend_background( def blend_background(
cls, cls,
image: Tensor, rgb: Tensor,
outputs: Dict[str, Union[Tensor, list]], opacity: Tensor,
background_color: Optional[Tensor] = None,
) -> Float[Tensor, "*bs 3"]: ) -> Float[Tensor, "*bs 3"]:
"""Blends the background color into the image using the alpha channel. Removes """Blends the background color into the image.
the alpha channel if there is no `background_color` in `outputs`.
Args: Args:
image: RGBA. rgb: RGB per pixel.
outputs: Dict containing the model outputs. opacity: Alpha opacity per pixel.
background_color: Background color.
Returns: Returns:
Blended RGB. Blended RGB.
""" """
if outputs.get("background_color", None) is not None: if len(opacity.shape) == 1:
background_color = outputs["background_color"] opacity = opacity.unsqueeze(-1)
assert isinstance(background_color, Tensor) blended_rgb = (
blended_rgb = image[:, :3] * image[:, 3:] + background_color.to(image.device) * (1 - image[:, 3:]) rgb * opacity + background_color.to(rgb.device) * (1 - opacity) if background_color is not None else rgb
else: )
blended_rgb = image[:, :3]
return blended_rgb return blended_rgb
def forward( def forward(
+10 -3
View File
@@ -301,7 +301,7 @@ class NerfactoModel(Model):
"accumulation": accumulation, "accumulation": accumulation,
"depth": depth, "depth": depth,
} }
if self.config.use_alpha_transparency_training: if background_color is not None:
outputs["background_color"] = background_color outputs["background_color"] = background_color
if self.config.predict_normals: if self.config.predict_normals:
@@ -340,8 +340,15 @@ class NerfactoModel(Model):
def get_loss_dict(self, outputs, batch, metrics_dict=None): def get_loss_dict(self, outputs, batch, metrics_dict=None):
loss_dict = {} loss_dict = {}
image = self.renderer_rgb.blend_background(batch["image"].to(self.device), outputs) image = batch["image"].to(self.device)
loss_dict["rgb_loss"] = self.rgb_loss(image, outputs["rgb"]) blended_rgb = (
self.renderer_rgb.blend_background(
rgb=image[..., :3], opacity=image[..., 3], background_color=outputs["background_color"]
)
if "background_color" in outputs
else image[..., :3]
)
loss_dict["rgb_loss"] = self.rgb_loss(blended_rgb, outputs["rgb"])
if self.training: if self.training:
loss_dict["interlevel_loss"] = self.config.interlevel_loss_mult * interlevel_loss( loss_dict["interlevel_loss"] = self.config.interlevel_loss_mult * interlevel_loss(
outputs["weights_list"], outputs["ray_samples_list"] outputs["weights_list"], outputs["ray_samples_list"]