Add multiple color options

This commit is contained in:
Matt Tancik committed 2022-05-04 15:47:54 -07:00
1 parent cc6fd15927
commit fc282f8857
7 files changed
+81 -24

No files matched your search

+11 -6
View File
@@ -2,7 +2,7 @@
Some dataset code.
"""
from typing import List
from typing import List, Optional
import numpy as np
import numpy.typing as npt
@@ -10,6 +10,7 @@ import PIL
import torch
from PIL import Image
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):
@@ -50,19 +51,21 @@ def collate_batch(batch_list, num_rays_per_batch, keep_full_image: bool = False)
class ImageDataset(torch.utils.data.Dataset):
"""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_
Args:
image_filenames (List[str]): List of image filenames
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__()
assert isinstance(downscale_factor, int)
self.image_filenames = image_filenames
self.downscale_factor = downscale_factor
self.white_background = white_background
self.alpha_color = alpha_color
def __len__(self):
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
original_image = torch.from_numpy(self.get_image(image_idx).astype("float32") / 255.0)
if self.white_background:
image = original_image[:, :, :3] * original_image[:, :, -1:] + (1.0 - original_image[:, :, -1:])
if self.alpha_color is not None:
image = original_image[:, :, :3] * original_image[:, :, -1:] + self.alpha_color * (
1.0 - original_image[:, :, -1:]
)
else:
image = original_image[:, :, :3]
num_channels = original_image.shape[2]
+13 -8
View File
@@ -4,11 +4,14 @@ For loading the blender dataset format.
import os
from dataclasses import dataclass
from typing import Dict, List, Tuple
from typing import Dict, List, Optional, Tuple, Union
import imageio
import numpy as np
from omegaconf import ListConfig
import torch
from torchtyping import TensorType
from mattport.structures.colors import get_color
from mattport.utils.io import get_absolute_path, load_from_json
@@ -19,13 +22,13 @@ class DatasetInputs:
image_filenames: List[str]
downscale_factor: float = 1.0
white_background: bool = False
alpha_color: Optional[TensorType[3]] = None
intrinsics: torch.tensor = None
camera_to_world: torch.tensor = None
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:
"""Processes the a blender dataset directory.
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:
data_directory (str): Location of data
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.
Returns:
@@ -66,7 +69,7 @@ def load_blender_data(
dataset_inputs = DatasetInputs(
image_filenames=image_filenames,
downscale_factor=downscale_factor,
white_background=white_background,
alpha_color=alpha_color,
intrinsics=intrinsics * 1.0 / downscale_factor, # downscaling the intrinsics here
camera_to_world=camera_to_world,
)
@@ -78,7 +81,7 @@ def get_dataset_inputs_dict(
data_directory: str,
dataset_type: str,
downscale_factor: int = 1,
white_background: bool = False,
alpha_color: Optional[Union[str, list, ListConfig]] = None,
splits: Tuple[str] = ("train", "val"),
) -> Dict[str, DatasetInputs]:
"""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
dataset_type (str): Name of dataset type
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:
Dict[str, DatasetInputs]: The inputs needed for generating rays.
@@ -98,7 +101,9 @@ def get_dataset_inputs_dict(
if dataset_type == "blender":
for split in splits:
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
else: