Files
nerfstudio/nerfstudio/data/datasets/semantic_dataset.py
T
Brent YiandEthan Weber ad8599aad0 Add missing __init__.py files (#935)
* Add missing __init__.py files

* fixing lint

Co-authored-by: Ethan Weber <eweb0124@gmail.com>
2022-11-10 16:56:13 -08:00

54 lines
2.0 KiB
Python

# Copyright 2022 The Nerfstudio Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Semantic dataset.
"""
from typing import Dict
import torch
from nerfstudio.data.dataparsers.base_dataparser import DataparserOutputs, Semantics
from nerfstudio.data.datasets.base_dataset import InputDataset
from nerfstudio.data.utils.data_utils import get_semantics_and_mask_tensors_from_path
class SemanticDataset(InputDataset):
"""Dataset that returns images and semantics and masks.
Args:
dataparser_outputs: description of where and how to read input images.
"""
def __init__(self, dataparser_outputs: DataparserOutputs):
super().__init__(dataparser_outputs)
assert "semantics" in dataparser_outputs.metadata.keys() and isinstance(
dataparser_outputs.metadata["semantics"], Semantics
)
self.semantics = dataparser_outputs.metadata["semantics"]
self.mask_indices = torch.tensor(
[self.semantics.classes.index(mask_class) for mask_class in self.semantics.mask_classes]
).view(1, 1, -1)
def get_metadata(self, data: Dict) -> Dict:
# handle mask
filepath = self.semantics.filenames[data["image_idx"]]
semantic_label, mask = get_semantics_and_mask_tensors_from_path(
filepath=filepath, mask_indices=self.mask_indices
)
if "mask" in data.keys():
mask = mask & data["mask"]
return {"mask": mask, "semantics": semantic_label}