Refactor Data Pipeline (#896)

* add none check

* black formatting

* cleanup

* cleanup

* fix tests

* remove new line

* init

* toy example working

* semantic nerf working

* simplify pixel samplers

* cleanup + fix tests

* comments

* fix tests

* fix tests
This commit is contained in:
Nikhil Mohan authored and GitHub committed 2022-11-06 07:40:43 -08:00
1 parent 761645de05
commit 1a24f3e58c
19 files changed
+229 -197

No files matched your search

+13 -22
View File
@@ -34,18 +34,14 @@ from nerfstudio.data.scene_box import SceneBox
class Semantics:
"""Dataclass for semantic labels."""
stuff_filenames: List[Path]
"""filenames to load "stuff"/background data"""
stuff_classes: List[str]
"""class labels for "stuff" data"""
stuff_colors: torch.Tensor
"""color mapping for "stuff" classes"""
thing_filenames: List[Path]
"""filenames to load "thing"/foreground data"""
thing_classes: List[str]
"""class labels for "thing" data"""
thing_colors: torch.Tensor
"""color mapping for "thing" classes"""
filenames: List[Path]
"""filenames to load semantic data"""
classes: List[str]
"""class labels for data"""
colors: torch.Tensor
"""color mapping for classes"""
mask_classes: List[str] = field(default_factory=lambda: [])
"""classes to mask out from training for all modalities"""
@dataclass
@@ -61,16 +57,11 @@ class DataparserOutputs:
"""Color of dataset background."""
scene_box: SceneBox = SceneBox()
"""Scene box of dataset. Used to bound the scene or provide the scene scale depending on model."""
semantics: Optional[Semantics] = None
"""Semantics information."""
times: Optional[TensorType[1]] = None
"""Time in range [0,1] for when each image was taken."""
additional_inputs: Dict[str, Any] = to_immutable_dict({})
"""Dictionary of additional dataset information (e.g. semantics/point clouds/masks).
{input_name:
... {"func": function to process additional dataparser outputs,
... "kwargs": dictionary of data to pass into "func"}
}
mask_filenames: Optional[List[Path]] = None
"""Filenames for any masks that are required"""
metadata: Dict[str, Any] = to_immutable_dict({})
"""Dictionary of any metadata that be required for the given experiment.
Will be processed by the InputDataset to create any additional tensors that may be required.
"""
def as_dict(self) -> dict:
@@ -19,9 +19,7 @@ from dataclasses import dataclass, field
from pathlib import Path
from typing import Type
import numpy as np
import torch
from PIL import Image
from rich.console import Console
from nerfstudio.cameras.cameras import Cameras, CameraType
@@ -37,31 +35,6 @@ from nerfstudio.utils.io import load_from_json
CONSOLE = Console()
def get_semantics_and_masks(image_idx: int, semantics: Semantics):
"""function to process additional semantics and mask information
Args:
image_idx: specific image index to work with
semantics: semantics data
"""
# handle mask
person_index = semantics.thing_classes.index("person")
thing_image_filename = semantics.thing_filenames[image_idx]
pil_image = Image.open(thing_image_filename)
thing_semantics = torch.from_numpy(np.array(pil_image, dtype="int32"))[..., None]
mask = (thing_semantics != person_index).to(torch.float32) # 1 where valid
# handle semantics
# stuff
stuff_image_filename = semantics.stuff_filenames[image_idx]
pil_image = Image.open(stuff_image_filename)
stuff_semantics = torch.from_numpy(np.array(pil_image, dtype="int32"))[..., None]
# thing
thing_image_filename = semantics.thing_filenames[image_idx]
pil_image = Image.open(thing_image_filename)
thing_semantics = torch.from_numpy(np.array(pil_image, dtype="int32"))[..., None]
return {"mask": mask, "semantics_stuff": stuff_semantics, "semantics_thing": thing_semantics}
@dataclass
class FriendsDataParserConfig(DataParserConfig):
"""Friends dataset parser config"""
@@ -142,9 +115,8 @@ class Friends(DataParser):
camera_to_worlds[..., 3] *= scale # cameras
# --- semantics ---
semantics = None
if self.config.include_semantics:
thing_filenames = [
filenames = [
Path(
str(image_filename)
.replace(f"/{images_folder}/", f"/{segmentations_folder}/thing/")
@@ -152,27 +124,10 @@ class Friends(DataParser):
)
for image_filename in image_filenames
]
stuff_filenames = [
Path(
str(image_filename)
.replace(f"/{images_folder}/", f"/{segmentations_folder}/stuff/")
.replace(".jpg", ".png")
)
for image_filename in image_filenames
]
panoptic_classes = load_from_json(self.config.data / "panoptic_classes.json")
stuff_classes = panoptic_classes["stuff"]
stuff_colors = torch.tensor(panoptic_classes["stuff_colors"], dtype=torch.float32) / 255.0
thing_classes = panoptic_classes["thing"]
thing_colors = torch.tensor(panoptic_classes["thing_colors"], dtype=torch.float32) / 255.0
semantics = Semantics(
stuff_classes=stuff_classes,
stuff_colors=stuff_colors,
stuff_filenames=stuff_filenames,
thing_classes=thing_classes,
thing_colors=thing_colors,
thing_filenames=thing_filenames,
)
classes = panoptic_classes["thing"]
colors = torch.tensor(panoptic_classes["thing_colors"], dtype=torch.float32) / 255.0
semantics = Semantics(filenames=filenames, classes=classes, colors=colors, mask_classes=["person"])
assert torch.all(cx[0] == cx), "Not all cameras have the same cx. Our Cameras class does not support this."
assert torch.all(cy[0] == cy), "Not all cameras have the same cy. Our Cameras class does not support this."
@@ -191,7 +146,6 @@ class Friends(DataParser):
image_filenames=image_filenames,
cameras=cameras,
scene_box=scene_box,
additional_inputs={"semantics": {"func": get_semantics_and_masks, "kwargs": {"semantics": semantics}}},
semantics=semantics,
metadata={"semantics": semantics} if self.config.include_semantics else {},
)
return dataparser_outputs