mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-05 23:38:42 +08:00
Add multiple color options
This commit is contained in:
1 parent
cc6fd15927
commit
fc282f8857
7 files changed
+81
-24
No files matched your search
@@ -3,7 +3,7 @@ dataset:
|
|||||||
data_directory: data/blender/lego
|
data_directory: data/blender/lego
|
||||||
dataset_type: blender
|
dataset_type: blender
|
||||||
downscale_factor: 4
|
downscale_factor: 4
|
||||||
white_background: True
|
alpha_color: white
|
||||||
|
|
||||||
# validation options
|
# validation options
|
||||||
validation_image_indices: [0, 10] # Which images to evaluate on.
|
validation_image_indices: [0, 10] # Which images to evaluate on.
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
Some dataset code.
|
Some dataset code.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import numpy.typing as npt
|
import numpy.typing as npt
|
||||||
@@ -10,6 +10,7 @@ import PIL
|
|||||||
import torch
|
import torch
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from torch.utils.data import default_collate
|
from torch.utils.data import default_collate
|
||||||
|
from torchtyping import TensorType
|
||||||
|
|
||||||
|
|
||||||
def collate_batch(batch_list, num_rays_per_batch, keep_full_image: bool = False):
|
def collate_batch(batch_list, num_rays_per_batch, keep_full_image: bool = False):
|
||||||
@@ -50,19 +51,21 @@ def collate_batch(batch_list, num_rays_per_batch, keep_full_image: bool = False)
|
|||||||
class ImageDataset(torch.utils.data.Dataset):
|
class ImageDataset(torch.utils.data.Dataset):
|
||||||
"""Dataset that returns images."""
|
"""Dataset that returns images."""
|
||||||
|
|
||||||
def __init__(self, image_filenames: List[str], downscale_factor: int = 1, white_background: bool = False):
|
def __init__(
|
||||||
|
self, image_filenames: List[str], downscale_factor: int = 1, alpha_color: Optional[TensorType[3]] = None
|
||||||
|
):
|
||||||
"""_summary_
|
"""_summary_
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
image_filenames (List[str]): List of image filenames
|
image_filenames (List[str]): List of image filenames
|
||||||
downscale_factor (int, optional): How much to downscale the image. Defaults to 1.
|
downscale_factor (int, optional): How much to downscale the image. Defaults to 1.
|
||||||
white_background (bool, optional): Sets transparent regions to white, otherwise black. Defaults to False.
|
alpha_color (TensorType[3], optional): Sets transparent regions to specified color, otherwise black.
|
||||||
"""
|
"""
|
||||||
super().__init__()
|
super().__init__()
|
||||||
assert isinstance(downscale_factor, int)
|
assert isinstance(downscale_factor, int)
|
||||||
self.image_filenames = image_filenames
|
self.image_filenames = image_filenames
|
||||||
self.downscale_factor = downscale_factor
|
self.downscale_factor = downscale_factor
|
||||||
self.white_background = white_background
|
self.alpha_color = alpha_color
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.image_filenames)
|
return len(self.image_filenames)
|
||||||
@@ -101,8 +104,10 @@ class ImageDataset(torch.utils.data.Dataset):
|
|||||||
# the image might be RGB or RGBA, so we separate it
|
# the image might be RGB or RGBA, so we separate it
|
||||||
original_image = torch.from_numpy(self.get_image(image_idx).astype("float32") / 255.0)
|
original_image = torch.from_numpy(self.get_image(image_idx).astype("float32") / 255.0)
|
||||||
|
|
||||||
if self.white_background:
|
if self.alpha_color is not None:
|
||||||
image = original_image[:, :, :3] * original_image[:, :, -1:] + (1.0 - original_image[:, :, -1:])
|
image = original_image[:, :, :3] * original_image[:, :, -1:] + self.alpha_color * (
|
||||||
|
1.0 - original_image[:, :, -1:]
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
image = original_image[:, :, :3]
|
image = original_image[:, :, :3]
|
||||||
num_channels = original_image.shape[2]
|
num_channels = original_image.shape[2]
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ For loading the blender dataset format.
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Dict, List, Tuple
|
from typing import Dict, List, Optional, Tuple, Union
|
||||||
|
|
||||||
import imageio
|
import imageio
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from omegaconf import ListConfig
|
||||||
import torch
|
import torch
|
||||||
|
from torchtyping import TensorType
|
||||||
|
from mattport.structures.colors import get_color
|
||||||
|
|
||||||
from mattport.utils.io import get_absolute_path, load_from_json
|
from mattport.utils.io import get_absolute_path, load_from_json
|
||||||
|
|
||||||
@@ -19,13 +22,13 @@ class DatasetInputs:
|
|||||||
|
|
||||||
image_filenames: List[str]
|
image_filenames: List[str]
|
||||||
downscale_factor: float = 1.0
|
downscale_factor: float = 1.0
|
||||||
white_background: bool = False
|
alpha_color: Optional[TensorType[3]] = None
|
||||||
intrinsics: torch.tensor = None
|
intrinsics: torch.tensor = None
|
||||||
camera_to_world: torch.tensor = None
|
camera_to_world: torch.tensor = None
|
||||||
|
|
||||||
|
|
||||||
def load_blender_data(
|
def load_blender_data(
|
||||||
basedir: str, downscale_factor: int = 1, white_background: bool = False, split: str = "train"
|
basedir: str, downscale_factor: int = 1, alpha_color: Optional[TensorType[3]] = None, split: str = "train"
|
||||||
) -> DatasetInputs:
|
) -> DatasetInputs:
|
||||||
"""Processes the a blender dataset directory.
|
"""Processes the a blender dataset directory.
|
||||||
Some of this code comes from https://github.com/yenchenlin/nerf-pytorch/blob/master/load_blender.py#L37.
|
Some of this code comes from https://github.com/yenchenlin/nerf-pytorch/blob/master/load_blender.py#L37.
|
||||||
@@ -33,7 +36,7 @@ def load_blender_data(
|
|||||||
Args:
|
Args:
|
||||||
data_directory (str): Location of data
|
data_directory (str): Location of data
|
||||||
downscale_factor (int, optional): How much to downscale images. Defaults to 1.0.
|
downscale_factor (int, optional): How much to downscale images. Defaults to 1.0.
|
||||||
white_background (bool, optional): Set pixel with zero opacity to white. Defaults to False.
|
alpha_color (TensorType[3], optional): Sets transparent regions to specified color, otherwise black.
|
||||||
split (str, optional): Which dataset split to generate.
|
split (str, optional): Which dataset split to generate.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -66,7 +69,7 @@ def load_blender_data(
|
|||||||
dataset_inputs = DatasetInputs(
|
dataset_inputs = DatasetInputs(
|
||||||
image_filenames=image_filenames,
|
image_filenames=image_filenames,
|
||||||
downscale_factor=downscale_factor,
|
downscale_factor=downscale_factor,
|
||||||
white_background=white_background,
|
alpha_color=alpha_color,
|
||||||
intrinsics=intrinsics * 1.0 / downscale_factor, # downscaling the intrinsics here
|
intrinsics=intrinsics * 1.0 / downscale_factor, # downscaling the intrinsics here
|
||||||
camera_to_world=camera_to_world,
|
camera_to_world=camera_to_world,
|
||||||
)
|
)
|
||||||
@@ -78,7 +81,7 @@ def get_dataset_inputs_dict(
|
|||||||
data_directory: str,
|
data_directory: str,
|
||||||
dataset_type: str,
|
dataset_type: str,
|
||||||
downscale_factor: int = 1,
|
downscale_factor: int = 1,
|
||||||
white_background: bool = False,
|
alpha_color: Optional[Union[str, list, ListConfig]] = None,
|
||||||
splits: Tuple[str] = ("train", "val"),
|
splits: Tuple[str] = ("train", "val"),
|
||||||
) -> Dict[str, DatasetInputs]:
|
) -> Dict[str, DatasetInputs]:
|
||||||
"""Returns the dataset inputs, which will be used with an ImageDataset and RayGenerator.
|
"""Returns the dataset inputs, which will be used with an ImageDataset and RayGenerator.
|
||||||
@@ -88,7 +91,7 @@ def get_dataset_inputs_dict(
|
|||||||
data_directory (str): Location of data
|
data_directory (str): Location of data
|
||||||
dataset_type (str): Name of dataset type
|
dataset_type (str): Name of dataset type
|
||||||
downscale_factor (int, optional): How much to downscale images. Defaults to 1.0.
|
downscale_factor (int, optional): How much to downscale images. Defaults to 1.0.
|
||||||
white_background (bool, optional): Set pixel with zero opacity to white. Defaults to False.
|
alpha_color (str, list, optional): Sets transparent regions to specified color, otherwise black.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, DatasetInputs]: The inputs needed for generating rays.
|
Dict[str, DatasetInputs]: The inputs needed for generating rays.
|
||||||
@@ -98,7 +101,9 @@ def get_dataset_inputs_dict(
|
|||||||
if dataset_type == "blender":
|
if dataset_type == "blender":
|
||||||
for split in splits:
|
for split in splits:
|
||||||
dataset_inputs = load_blender_data(
|
dataset_inputs = load_blender_data(
|
||||||
get_absolute_path(data_directory), downscale_factor=downscale_factor, white_background=white_background
|
get_absolute_path(data_directory),
|
||||||
|
downscale_factor=downscale_factor,
|
||||||
|
alpha_color=get_color(alpha_color),
|
||||||
)
|
)
|
||||||
dataset_inputs_dict[split] = dataset_inputs
|
dataset_inputs_dict[split] = dataset_inputs
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ from mattport.nerf.field_modules.ray_generator import RayGenerator
|
|||||||
from mattport.nerf.graph.base import Graph
|
from mattport.nerf.graph.base import Graph
|
||||||
from mattport.nerf.loss import MSELoss
|
from mattport.nerf.loss import MSELoss
|
||||||
from mattport.nerf.renderers import RGBRenderer
|
from mattport.nerf.renderers import RGBRenderer
|
||||||
from mattport.nerf.sampler import PDFSampler, UniformSampler # pylint: disable=unused-import
|
from mattport.nerf.sampler import PDFSampler, UniformSampler
|
||||||
|
from mattport.structures import colors
|
||||||
from mattport.structures.rays import RaySamples
|
from mattport.structures.rays import RaySamples
|
||||||
|
|
||||||
|
|
||||||
@@ -110,7 +111,7 @@ class NeRFGraph(Graph):
|
|||||||
self.field_fine = NeRFField()
|
self.field_fine = NeRFField()
|
||||||
|
|
||||||
# renderers
|
# renderers
|
||||||
self.renderer_rgb = RGBRenderer(white_background=True)
|
self.renderer_rgb = RGBRenderer(background_color=colors.WHITE)
|
||||||
|
|
||||||
# losses
|
# losses
|
||||||
self.rgb_loss = MSELoss()
|
self.rgb_loss = MSELoss()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
Collection of renderers
|
Collection of renderers
|
||||||
"""
|
"""
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from torch import nn
|
from torch import nn
|
||||||
@@ -19,13 +20,13 @@ class RendererOutputs:
|
|||||||
class RGBRenderer(nn.Module):
|
class RGBRenderer(nn.Module):
|
||||||
"""Standard volumetic rendering."""
|
"""Standard volumetic rendering."""
|
||||||
|
|
||||||
def __init__(self, white_background: bool = False) -> None:
|
def __init__(self, background_color: Optional[TensorType[3]] = None) -> None:
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
white_background (bool, optional): Composite onto white. Defaults to True.
|
background_color (TensorType[3], optional): Background color as RGB. Defaults to black.
|
||||||
"""
|
"""
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.white_background = white_background
|
self.background_color = background_color
|
||||||
|
|
||||||
def forward(
|
def forward(
|
||||||
self,
|
self,
|
||||||
@@ -44,8 +45,13 @@ class RGBRenderer(nn.Module):
|
|||||||
|
|
||||||
rgb = torch.sum(weights[..., None] * rgb, dim=-2)
|
rgb = torch.sum(weights[..., None] * rgb, dim=-2)
|
||||||
|
|
||||||
if self.white_background:
|
if self.background_color is not None:
|
||||||
rgb = rgb + (1.0 - torch.sum(weights, dim=-1))[..., None]
|
rgb = rgb + self.background_color.to(weights.device)[None, ...] * (
|
||||||
|
1.0 - torch.sum(weights, dim=-1)[..., None]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert torch.max(rgb) <= 1.0
|
||||||
|
assert torch.min(rgb) >= 0.0
|
||||||
|
|
||||||
renderer_outputs = RendererOutputs(rgb=rgb)
|
renderer_outputs = RendererOutputs(rgb=rgb)
|
||||||
return renderer_outputs
|
return renderer_outputs
|
||||||
@@ -78,7 +78,7 @@ class Trainer:
|
|||||||
self.train_image_dataset = ImageDataset(
|
self.train_image_dataset = ImageDataset(
|
||||||
image_filenames=dataset_inputs_dict["train"].image_filenames,
|
image_filenames=dataset_inputs_dict["train"].image_filenames,
|
||||||
downscale_factor=dataset_inputs_dict["train"].downscale_factor,
|
downscale_factor=dataset_inputs_dict["train"].downscale_factor,
|
||||||
white_background=dataset_inputs_dict["train"].white_background,
|
alpha_color=dataset_inputs_dict["train"].alpha_color,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.train_dataset = CollateIterDataset(
|
self.train_dataset = CollateIterDataset(
|
||||||
@@ -97,7 +97,7 @@ class Trainer:
|
|||||||
self.val_image_dataset = ImageDataset(
|
self.val_image_dataset = ImageDataset(
|
||||||
image_filenames=dataset_inputs_dict["val"].image_filenames,
|
image_filenames=dataset_inputs_dict["val"].image_filenames,
|
||||||
downscale_factor=dataset_inputs_dict["val"].downscale_factor,
|
downscale_factor=dataset_inputs_dict["val"].downscale_factor,
|
||||||
white_background=dataset_inputs_dict["val"].white_background,
|
alpha_color=dataset_inputs_dict["val"].alpha_color,
|
||||||
)
|
)
|
||||||
|
|
||||||
@profiler.time_function
|
@profiler.time_function
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""Common Colors"""
|
||||||
|
from typing import Union
|
||||||
|
from omegaconf import ListConfig
|
||||||
|
import torch
|
||||||
|
from torchtyping import TensorType
|
||||||
|
|
||||||
|
WHITE = torch.tensor([1.0, 1.0, 1.0])
|
||||||
|
BLACK = torch.tensor([0.0, 0.0, 0.0])
|
||||||
|
RED = torch.tensor([1.0, 0.0, 0.0])
|
||||||
|
GREEN = torch.tensor([0.0, 1.0, 0.0])
|
||||||
|
BLUE = torch.tensor([0.0, 0.0, 1.0])
|
||||||
|
|
||||||
|
COLORS_DICT = {
|
||||||
|
"white": WHITE,
|
||||||
|
"black": BLACK,
|
||||||
|
"red": RED,
|
||||||
|
"green": GREEN,
|
||||||
|
"blue": BLUE,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_color(color: Union[str, list, ListConfig]) -> TensorType[3]:
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
color (Union[str, list, ListConfig]): Color as a string or a rgb list
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
TensorType[3]: Parsed color
|
||||||
|
"""
|
||||||
|
if isinstance(color, str):
|
||||||
|
color = color.lower()
|
||||||
|
if color not in COLORS_DICT:
|
||||||
|
raise ValueError(f"{color} is not a valid preset color")
|
||||||
|
return COLORS_DICT[color]
|
||||||
|
elif isinstance(color, list) or isinstance(color, ListConfig):
|
||||||
|
if len(color) != 3:
|
||||||
|
raise ValueError(f"Color should be 3 values (RGB) instead got {color}")
|
||||||
|
return torch.tensor(color)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Color should be an RGB list or string, instead got {type(color)}")
|
||||||
Reference in new issue
Block a user