mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-02 05:48:57 +08:00
1 parent
a637888844
commit
05ba4b6b00
16 files changed
+507
-497
No files matched your search
@@ -1,277 +0,0 @@
|
||||
# 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.
|
||||
|
||||
"""
|
||||
NeRF-W (NeRF in the wild) implementation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, Tuple, Type
|
||||
|
||||
import torch
|
||||
from torchmetrics import PeakSignalNoiseRatio
|
||||
from torchmetrics.functional import structural_similarity_index_measure
|
||||
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
|
||||
|
||||
from nerfstudio.cameras.rays import RayBundle
|
||||
from nerfstudio.configs.config_utils import to_immutable_dict
|
||||
from nerfstudio.field_components.encodings import NeRFEncoding
|
||||
from nerfstudio.field_components.field_heads import FieldHeadNames
|
||||
from nerfstudio.fields.nerfw_field import VanillaNerfWField
|
||||
from nerfstudio.fields.vanilla_nerf_field import NeRFField
|
||||
from nerfstudio.model_components.losses import MSELoss
|
||||
from nerfstudio.model_components.ray_samplers import PDFSampler, UniformSampler
|
||||
from nerfstudio.model_components.renderers import (
|
||||
AccumulationRenderer,
|
||||
DepthRenderer,
|
||||
RGBRenderer,
|
||||
UncertaintyRenderer,
|
||||
)
|
||||
from nerfstudio.model_components.scene_colliders import AABBBoxCollider
|
||||
from nerfstudio.models.base_model import Model, VanillaModelConfig
|
||||
from nerfstudio.utils import colormaps, colors, misc
|
||||
|
||||
|
||||
@dataclass
|
||||
class NerfWModelConfig(VanillaModelConfig):
|
||||
"""NerfW model config"""
|
||||
|
||||
_target: Type = field(default_factory=lambda: NerfWModel)
|
||||
"""target class to instantiate"""
|
||||
loss_coefficients: Dict[str, float] = to_immutable_dict(
|
||||
{"rgb_loss_coarse": 1.0, "rgb_loss_fine": 1.0, "uncertainty_loss": 1.0, "density_loss": 0.01}
|
||||
)
|
||||
"""Loss specific weights."""
|
||||
num_coarse_samples: int = 64
|
||||
"""Number of samples in coarse field evaluation."""
|
||||
num_importance_samples: int = 64
|
||||
"""Number of samples in fine field evaluation."""
|
||||
uncertainty_min: float = 0.03
|
||||
"""This is added to the end of the uncertainty
|
||||
rendering operation. It's called 'beta_min' in other repos.
|
||||
This avoids calling torch.log() on a zero value, which would be undefined.
|
||||
"""
|
||||
num_images: int = 10000 # TODO: don't hardcode this
|
||||
"""How many images exist in the dataset."""
|
||||
appearance_embedding_dim: int = 48
|
||||
"""Dimension of appearance embedding."""
|
||||
transient_embedding_dim: int = 16
|
||||
"""Dimension of transient embedding."""
|
||||
|
||||
|
||||
class NerfWModel(Model):
|
||||
"""NeRF-W model
|
||||
|
||||
Args:
|
||||
config: NerfW configuration to instantiate model
|
||||
"""
|
||||
|
||||
config: NerfWModelConfig
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: NerfWModelConfig,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
"""A NeRF-W model.
|
||||
|
||||
Args:
|
||||
...
|
||||
uncertainty_min (float, optional): This is added to the end of the uncertainty
|
||||
rendering operation. It's called 'beta_min' in other repos.
|
||||
This avoids calling torch.log() on a zero value, which would be undefined.
|
||||
"""
|
||||
self.field_coarse = None
|
||||
self.field_fine = None
|
||||
super().__init__(config=config, **kwargs)
|
||||
|
||||
def populate_modules(self):
|
||||
"""Set the fields and modules"""
|
||||
super().populate_modules()
|
||||
|
||||
# setting up fields
|
||||
position_encoding = NeRFEncoding(
|
||||
in_dim=3, num_frequencies=10, min_freq_exp=0.0, max_freq_exp=8.0, include_input=True
|
||||
)
|
||||
direction_encoding = NeRFEncoding(
|
||||
in_dim=3, num_frequencies=4, min_freq_exp=0.0, max_freq_exp=4.0, include_input=True
|
||||
)
|
||||
|
||||
self.field_coarse = NeRFField(position_encoding=position_encoding, direction_encoding=direction_encoding)
|
||||
self.field_fine = VanillaNerfWField(
|
||||
num_images=self.config.num_images,
|
||||
position_encoding=position_encoding,
|
||||
direction_encoding=direction_encoding,
|
||||
appearance_embedding_dim=self.config.appearance_embedding_dim,
|
||||
transient_embedding_dim=self.config.transient_embedding_dim,
|
||||
)
|
||||
|
||||
# samplers
|
||||
self.sampler_uniform = UniformSampler(num_samples=self.config.num_coarse_samples)
|
||||
self.sampler_pdf = PDFSampler(num_samples=self.config.num_importance_samples)
|
||||
|
||||
# renderers
|
||||
self.renderer_rgb = RGBRenderer(background_color=colors.BLACK)
|
||||
self.renderer_accumulation = AccumulationRenderer()
|
||||
self.renderer_depth = DepthRenderer()
|
||||
self.renderer_uncertainty = UncertaintyRenderer()
|
||||
|
||||
# losses
|
||||
self.rgb_loss = MSELoss()
|
||||
|
||||
# metrics
|
||||
self.psnr = PeakSignalNoiseRatio(data_range=1.0)
|
||||
self.ssim = structural_similarity_index_measure
|
||||
self.lpips = LearnedPerceptualImagePatchSimilarity()
|
||||
|
||||
# colliders
|
||||
if self.config.enable_collider:
|
||||
self.collider = AABBBoxCollider(scene_box=self.scene_box)
|
||||
|
||||
def get_param_groups(self):
|
||||
param_groups = {}
|
||||
if self.field_coarse is None or self.field_fine is None:
|
||||
raise ValueError("populate_fields() must be called before get_param_groups")
|
||||
param_groups["fields"] = list(self.field_coarse.parameters()) + list(self.field_fine.parameters())
|
||||
return param_groups
|
||||
|
||||
def get_outputs(self, ray_bundle: RayBundle):
|
||||
|
||||
if ray_bundle.camera_indices is not None:
|
||||
# TODO(ethan): remove this check
|
||||
assert (
|
||||
torch.max(ray_bundle.camera_indices) < self.config.num_images
|
||||
), "num_images must be greater than the largest camera index"
|
||||
|
||||
if self.field_coarse is None or self.field_fine is None:
|
||||
raise ValueError("populate_fields() must be called before get_outputs")
|
||||
|
||||
# uniform sampling
|
||||
ray_samples_uniform = self.sampler_uniform(ray_bundle)
|
||||
|
||||
# coarse field
|
||||
field_outputs_coarse = self.field_coarse.forward(ray_samples_uniform)
|
||||
weights_coarse = ray_samples_uniform.get_weights(field_outputs_coarse[FieldHeadNames.DENSITY])
|
||||
rgb_coarse = self.renderer_rgb(
|
||||
rgb=field_outputs_coarse[FieldHeadNames.RGB],
|
||||
weights=weights_coarse,
|
||||
)
|
||||
depth_coarse = self.renderer_depth(weights_coarse, ray_samples_uniform)
|
||||
|
||||
# pdf sampling
|
||||
ray_samples_pdf = self.sampler_pdf(ray_bundle, ray_samples_uniform, weights_coarse)
|
||||
|
||||
# fine field
|
||||
field_outputs_fine = self.field_fine.forward(ray_samples_pdf)
|
||||
|
||||
# fine weights
|
||||
weights_fine = ray_samples_pdf.get_weights(
|
||||
field_outputs_fine[FieldHeadNames.DENSITY] + field_outputs_fine[FieldHeadNames.TRANSIENT_DENSITY]
|
||||
)
|
||||
weights_fine_static = ray_samples_pdf.get_weights(field_outputs_fine[FieldHeadNames.DENSITY])
|
||||
weights_fine_transient = ray_samples_pdf.get_weights(field_outputs_fine[FieldHeadNames.TRANSIENT_DENSITY])
|
||||
|
||||
# rgb
|
||||
rgb_fine_static_component = self.renderer_rgb(
|
||||
rgb=field_outputs_fine[FieldHeadNames.RGB],
|
||||
weights=weights_fine,
|
||||
)
|
||||
rgb_fine_transient_component = self.renderer_rgb(
|
||||
rgb=field_outputs_fine[FieldHeadNames.TRANSIENT_RGB],
|
||||
weights=weights_fine,
|
||||
)
|
||||
rgb_fine = rgb_fine_static_component + rgb_fine_transient_component
|
||||
rgb_fine_static = self.renderer_rgb(
|
||||
rgb=field_outputs_fine[FieldHeadNames.RGB],
|
||||
weights=weights_fine_static,
|
||||
)
|
||||
|
||||
# density
|
||||
density_transient = field_outputs_fine[FieldHeadNames.TRANSIENT_DENSITY]
|
||||
|
||||
# depth
|
||||
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf)
|
||||
depth_fine_static = self.renderer_depth(weights_fine_static, ray_samples_pdf)
|
||||
|
||||
# uncertainty
|
||||
uncertainty = self.renderer_uncertainty(field_outputs_fine[FieldHeadNames.UNCERTAINTY], weights_fine_transient)
|
||||
uncertainty += self.config.uncertainty_min
|
||||
|
||||
outputs = {
|
||||
"rgb_coarse": rgb_coarse, # (num_rays, 3)
|
||||
"rgb_fine": rgb_fine,
|
||||
"rgb_fine_static": rgb_fine_static,
|
||||
"depth_coarse": depth_coarse,
|
||||
"depth_fine": depth_fine,
|
||||
"depth_fine_static": depth_fine_static,
|
||||
"density_transient": density_transient, # (num_rays, num_samples, 1)
|
||||
"uncertainty": uncertainty, # (num_rays, 1)
|
||||
}
|
||||
return outputs
|
||||
|
||||
def get_loss_dict(self, outputs, batch, metrics_dict=None):
|
||||
device = outputs["rgb_coarse"].device
|
||||
image = batch["image"].to(device)
|
||||
rgb_coarse = outputs["rgb_coarse"]
|
||||
rgb_fine = outputs["rgb_fine"]
|
||||
density_transient = outputs["density_transient"]
|
||||
betas = outputs["uncertainty"]
|
||||
rgb_loss_coarse = 0.5 * ((image - rgb_coarse) ** 2).sum(-1).mean()
|
||||
rgb_loss_fine = 0.5 * (((image - rgb_fine) ** 2).sum(-1) / (betas[..., 0] ** 2)).mean()
|
||||
uncertainty_loss = 0.5 * (3 + torch.log(betas)).mean()
|
||||
density_loss = density_transient.mean()
|
||||
|
||||
loss_dict = {
|
||||
"rgb_loss_coarse": rgb_loss_coarse,
|
||||
"rgb_loss_fine": rgb_loss_fine,
|
||||
"uncertainty_loss": uncertainty_loss,
|
||||
"density_loss": density_loss,
|
||||
}
|
||||
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
||||
return loss_dict
|
||||
|
||||
def get_image_metrics_and_images(
|
||||
self, outputs: Dict[str, torch.Tensor], batch: Dict[str, torch.Tensor]
|
||||
) -> Tuple[Dict[str, float], Dict[str, torch.Tensor]]:
|
||||
image = batch["image"].to(outputs["rgb_coarse"].device)
|
||||
rgb_coarse = outputs["rgb_coarse"]
|
||||
rgb_fine = outputs["rgb_fine"]
|
||||
rgb_fine_static = outputs["rgb_fine_static"]
|
||||
depth_coarse = outputs["depth_coarse"]
|
||||
depth_fine = outputs["depth_fine"]
|
||||
depth_fine_static = outputs["depth_fine_static"]
|
||||
uncertainty = outputs["uncertainty"]
|
||||
|
||||
depth_coarse = colormaps.apply_depth_colormap(depth_coarse)
|
||||
depth_fine = colormaps.apply_depth_colormap(depth_fine)
|
||||
depth_fine_static = colormaps.apply_depth_colormap(depth_fine_static)
|
||||
uncertainty = colormaps.apply_depth_colormap(uncertainty)
|
||||
|
||||
row0 = torch.cat([image, uncertainty, torch.ones_like(rgb_fine)], dim=-2)
|
||||
row1 = torch.cat([rgb_fine, rgb_fine_static, rgb_coarse], dim=-2)
|
||||
row2 = torch.cat([depth_fine, depth_fine_static, depth_coarse], dim=-2)
|
||||
combined_image = torch.cat([row0, row1, row2], dim=-3)
|
||||
|
||||
# this doesn't really make sense but do it anyway
|
||||
fine_psnr = self.psnr(image, rgb_fine)
|
||||
metrics_dict = {
|
||||
"psnr": float(fine_psnr.item()),
|
||||
}
|
||||
images_dict = {"img": combined_image}
|
||||
if "mask" in batch:
|
||||
mask = batch["mask"].repeat(1, 1, 3)
|
||||
images_dict["mask"] = mask
|
||||
return metrics_dict, images_dict
|
||||
@@ -1,145 +0,0 @@
|
||||
# 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 NeRF implementation.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, Tuple
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from nerfstudio.cameras.rays import RayBundle
|
||||
from nerfstudio.data.dataparsers.base_dataparser import Semantics
|
||||
from nerfstudio.field_components.encodings import NeRFEncoding
|
||||
from nerfstudio.field_components.field_heads import FieldHeadNames
|
||||
from nerfstudio.fields.semantic_nerf_field import SemanticNerfField
|
||||
from nerfstudio.fields.vanilla_nerf_field import NeRFField
|
||||
from nerfstudio.model_components.renderers import SemanticRenderer
|
||||
from nerfstudio.model_components.scene_colliders import AABBBoxCollider
|
||||
from nerfstudio.models.base_model import ModelConfig
|
||||
from nerfstudio.models.vanilla_nerf import NeRFModel
|
||||
from nerfstudio.utils import misc
|
||||
|
||||
|
||||
class SemanticNerfModel(NeRFModel):
|
||||
"""Semantic-NeRF model
|
||||
|
||||
Args:
|
||||
config: SemanticNeRF configuration to instantiate model
|
||||
semantics: additional semantics data info
|
||||
"""
|
||||
|
||||
def __init__(self, config: ModelConfig, semantics: Semantics, **kwargs) -> None:
|
||||
self.stuff_classes = semantics.stuff_classes
|
||||
self.stuff_colors = semantics.stuff_colors
|
||||
super().__init__(config=config, **kwargs)
|
||||
|
||||
def populate_modules(self):
|
||||
"""Set the fields and modules"""
|
||||
super().populate_modules()
|
||||
|
||||
# setting up fields
|
||||
position_encoding = NeRFEncoding(
|
||||
in_dim=3, num_frequencies=10, min_freq_exp=0.0, max_freq_exp=8.0, include_input=True
|
||||
)
|
||||
direction_encoding = NeRFEncoding(
|
||||
in_dim=3, num_frequencies=4, min_freq_exp=0.0, max_freq_exp=4.0, include_input=True
|
||||
)
|
||||
self.field_coarse = NeRFField(position_encoding=position_encoding, direction_encoding=direction_encoding)
|
||||
|
||||
num_semantic_classes = len(self.stuff_classes)
|
||||
self.field_fine = SemanticNerfField(
|
||||
num_semantic_classes, position_encoding=position_encoding, direction_encoding=direction_encoding
|
||||
)
|
||||
|
||||
# renderers
|
||||
self.renderer_semantic = SemanticRenderer()
|
||||
|
||||
# losses
|
||||
self.cross_entropy_loss = nn.CrossEntropyLoss(reduction="mean")
|
||||
|
||||
# colliders
|
||||
if self.config.enable_collider:
|
||||
self.collider = AABBBoxCollider(scene_box=self.scene_box)
|
||||
|
||||
def get_outputs(self, ray_bundle: RayBundle):
|
||||
# uniform sampling
|
||||
ray_samples_uniform = self.sampler_uniform(ray_bundle)
|
||||
|
||||
# coarse field
|
||||
field_outputs_coarse = self.field_coarse.forward(ray_samples_uniform)
|
||||
weights_coarse = ray_samples_uniform.get_weights(field_outputs_coarse[FieldHeadNames.DENSITY])
|
||||
rgb_coarse = self.renderer_rgb(
|
||||
rgb=field_outputs_coarse[FieldHeadNames.RGB],
|
||||
weights=weights_coarse,
|
||||
)
|
||||
accumulation_coarse = self.renderer_accumulation(weights_coarse)
|
||||
depth_coarse = self.renderer_depth(weights_coarse, ray_samples_uniform)
|
||||
|
||||
# pdf sampling
|
||||
ray_samples_pdf = self.sampler_pdf(ray_bundle, ray_samples_uniform, weights_coarse)
|
||||
|
||||
# fine field
|
||||
field_outputs_fine = self.field_fine.forward(ray_samples_pdf)
|
||||
weights_fine = ray_samples_pdf.get_weights(field_outputs_fine[FieldHeadNames.DENSITY])
|
||||
rgb_fine = self.renderer_rgb(
|
||||
rgb=field_outputs_fine[FieldHeadNames.RGB],
|
||||
weights=weights_fine,
|
||||
)
|
||||
accumulation_fine = self.renderer_accumulation(weights_fine)
|
||||
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf)
|
||||
semantic_fine = self.renderer_semantic(field_outputs_fine[FieldHeadNames.SEMANTICS], weights=weights_fine)
|
||||
|
||||
outputs = {
|
||||
"rgb_coarse": rgb_coarse,
|
||||
"rgb_fine": rgb_fine,
|
||||
"accumulation_coarse": accumulation_coarse,
|
||||
"accumulation_fine": accumulation_fine,
|
||||
"depth_coarse": depth_coarse,
|
||||
"depth_fine": depth_fine,
|
||||
"semantic_fine": semantic_fine,
|
||||
}
|
||||
return outputs
|
||||
|
||||
def get_loss_dict(self, outputs, batch, metrics_dict=None):
|
||||
image = batch["image"].to(outputs["rgb_coarse"].device)
|
||||
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
||||
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
||||
semantic_logits = outputs["semantic_fine"]
|
||||
semantic_classes = batch["semantics"][..., 0].long()
|
||||
semantic_loss_fine = self.cross_entropy_loss(semantic_logits, semantic_classes)
|
||||
loss_dict = {
|
||||
"rgb_loss_coarse": rgb_loss_coarse,
|
||||
"rgb_loss_fine": rgb_loss_fine,
|
||||
"semantic_loss_fine": semantic_loss_fine,
|
||||
}
|
||||
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
||||
return loss_dict
|
||||
|
||||
def get_image_metrics_and_images(
|
||||
self, outputs: Dict[str, torch.Tensor], batch: Dict[str, torch.Tensor]
|
||||
) -> Tuple[Dict[str, float], Dict[str, torch.Tensor]]:
|
||||
metrics_dict, images_dict = super().get_image_metrics_and_images(outputs, batch)
|
||||
semantic_logits = outputs["semantic_fine"]
|
||||
semantic_labels = torch.argmax(torch.nn.functional.softmax(semantic_logits, dim=-1), dim=-1) # type: ignore
|
||||
semantic_labels_image = self.stuff_colors[semantic_labels]
|
||||
|
||||
mask = batch["mask"].repeat(1, 1, 3)
|
||||
|
||||
images_dict["semantics"] = semantic_labels_image
|
||||
images_dict["mask"] = mask
|
||||
return metrics_dict, images_dict
|
||||
@@ -0,0 +1,311 @@
|
||||
# Copyright 2022 The Plenoptix 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 NeRF-W implementation which should be fast enough to view in the viewer.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List, Tuple, Type
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from torch.nn import Parameter
|
||||
from torchmetrics import PeakSignalNoiseRatio
|
||||
from torchmetrics.functional import structural_similarity_index_measure
|
||||
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
|
||||
|
||||
from nerfstudio.cameras.rays import RayBundle
|
||||
from nerfstudio.data.dataparsers.base_dataparser import Semantics
|
||||
from nerfstudio.engine.callbacks import (
|
||||
TrainingCallback,
|
||||
TrainingCallbackAttributes,
|
||||
TrainingCallbackLocation,
|
||||
)
|
||||
from nerfstudio.field_components.field_heads import FieldHeadNames
|
||||
from nerfstudio.field_components.spatial_distortions import SceneContraction
|
||||
from nerfstudio.fields.density_fields import HashMLPDensityField
|
||||
from nerfstudio.fields.nerfacto_field import TCNNNerfactoField
|
||||
from nerfstudio.model_components.losses import MSELoss, distortion_loss, interlevel_loss
|
||||
from nerfstudio.model_components.ray_samplers import ProposalNetworkSampler
|
||||
from nerfstudio.model_components.renderers import (
|
||||
AccumulationRenderer,
|
||||
DepthRenderer,
|
||||
RGBRenderer,
|
||||
SemanticRenderer,
|
||||
UncertaintyRenderer,
|
||||
)
|
||||
from nerfstudio.model_components.scene_colliders import NearFarCollider
|
||||
from nerfstudio.models.base_model import Model
|
||||
from nerfstudio.models.nerfacto import NerfactoModelConfig
|
||||
from nerfstudio.utils import colormaps, colors
|
||||
|
||||
|
||||
@dataclass
|
||||
class SemanticNerfWModelConfig(NerfactoModelConfig):
|
||||
"""Nerfacto Model Config"""
|
||||
|
||||
_target: Type = field(default_factory=lambda: SemanticNerfWModel)
|
||||
use_transient_embedding: bool = False
|
||||
"""Whether to use transient embedding."""
|
||||
|
||||
|
||||
class SemanticNerfWModel(Model):
|
||||
"""Nerfacto model
|
||||
|
||||
Args:
|
||||
config: Nerfacto configuration to instantiate model
|
||||
"""
|
||||
|
||||
config: SemanticNerfWModelConfig
|
||||
|
||||
def __init__(self, config: SemanticNerfWModelConfig, semantics: Semantics, **kwargs) -> None:
|
||||
self.semantics = semantics
|
||||
super().__init__(config=config, **kwargs)
|
||||
|
||||
def populate_modules(self):
|
||||
"""Set the fields and modules."""
|
||||
super().populate_modules()
|
||||
|
||||
scene_contraction = SceneContraction(order=float("inf"))
|
||||
|
||||
if self.config.use_transient_embedding:
|
||||
raise ValueError("Transient embedding is not fully working for semantic nerf-w.")
|
||||
|
||||
# Fields
|
||||
self.field = TCNNNerfactoField(
|
||||
self.scene_box.aabb,
|
||||
spatial_distortion=scene_contraction,
|
||||
num_images=self.num_train_data,
|
||||
use_average_appearance_embedding=self.config.use_average_appearance_embedding,
|
||||
use_transient_embedding=self.config.use_transient_embedding,
|
||||
use_semantics=True,
|
||||
num_semantics_stuff_classes=len(self.semantics.stuff_classes),
|
||||
num_semantics_thing_classes=len(self.semantics.thing_classes),
|
||||
)
|
||||
|
||||
# Build the proposal network(s)
|
||||
self.proposal_networks = torch.nn.ModuleList()
|
||||
if self.config.use_same_proposal_network:
|
||||
network = HashMLPDensityField(self.scene_box.aabb, spatial_distortion=scene_contraction)
|
||||
self.proposal_networks.append(network)
|
||||
self.density_fns = [network.density_fn for _ in range(self.config.num_proposal_network_iterations)]
|
||||
else:
|
||||
for _ in range(self.config.num_proposal_network_iterations):
|
||||
network = HashMLPDensityField(self.scene_box.aabb, spatial_distortion=scene_contraction)
|
||||
self.proposal_networks.append(network)
|
||||
self.density_fns = [network.density_fn for network in self.proposal_networks]
|
||||
|
||||
# Collider
|
||||
self.collider = NearFarCollider(near_plane=self.config.near_plane, far_plane=self.config.far_plane)
|
||||
|
||||
# Samplers
|
||||
self.proposal_sampler = ProposalNetworkSampler(
|
||||
num_nerf_samples_per_ray=self.config.num_nerf_samples_per_ray,
|
||||
num_proposal_samples_per_ray=self.config.num_proposal_samples_per_ray,
|
||||
num_proposal_network_iterations=self.config.num_proposal_network_iterations,
|
||||
single_jitter=self.config.use_single_jitter,
|
||||
)
|
||||
|
||||
# renderers
|
||||
background_color = None if self.config.randomize_background else colors.WHITE
|
||||
self.renderer_rgb = RGBRenderer(background_color=background_color)
|
||||
self.renderer_accumulation = AccumulationRenderer()
|
||||
self.renderer_depth = DepthRenderer()
|
||||
self.renderer_uncertainty = UncertaintyRenderer()
|
||||
self.renderer_semantics = SemanticRenderer()
|
||||
|
||||
# losses
|
||||
self.rgb_loss = MSELoss()
|
||||
self.cross_entropy_loss = torch.nn.CrossEntropyLoss(reduction="mean")
|
||||
|
||||
# metrics
|
||||
self.psnr = PeakSignalNoiseRatio(data_range=1.0)
|
||||
self.ssim = structural_similarity_index_measure
|
||||
self.lpips = LearnedPerceptualImagePatchSimilarity()
|
||||
|
||||
def get_param_groups(self) -> Dict[str, List[Parameter]]:
|
||||
param_groups = {}
|
||||
param_groups["proposal_networks"] = list(self.proposal_networks.parameters())
|
||||
param_groups["fields"] = list(self.field.parameters())
|
||||
return param_groups
|
||||
|
||||
def get_training_callbacks(
|
||||
self, training_callback_attributes: TrainingCallbackAttributes
|
||||
) -> List[TrainingCallback]:
|
||||
callbacks = []
|
||||
if self.config.use_proposal_weight_anneal:
|
||||
# anneal the weights of the proposal network before doing PDF sampling
|
||||
N = self.config.proposal_weights_anneal_max_num_iters
|
||||
|
||||
def set_anneal(step):
|
||||
# https://arxiv.org/pdf/2111.12077.pdf eq. 18
|
||||
train_frac = np.clip(step / N, 0, 1)
|
||||
bias = lambda x, b: (b * x) / ((b - 1) * x + 1)
|
||||
anneal = bias(train_frac, self.config.proposal_weights_anneal_slope)
|
||||
self.proposal_sampler.set_anneal(anneal)
|
||||
|
||||
callbacks.append(
|
||||
TrainingCallback(
|
||||
where_to_run=[TrainingCallbackLocation.BEFORE_TRAIN_ITERATION],
|
||||
update_every_num_iters=1,
|
||||
func=set_anneal,
|
||||
)
|
||||
)
|
||||
return callbacks
|
||||
|
||||
def get_outputs(self, ray_bundle: RayBundle):
|
||||
ray_samples, weights_list, ray_samples_list = self.proposal_sampler(ray_bundle, density_fns=self.density_fns)
|
||||
field_outputs = self.field(ray_samples)
|
||||
|
||||
if self.training and self.config.use_transient_embedding:
|
||||
density = field_outputs[FieldHeadNames.DENSITY] + field_outputs[FieldHeadNames.TRANSIENT_DENSITY]
|
||||
weights = ray_samples.get_weights(density)
|
||||
weights_static = ray_samples.get_weights(field_outputs[FieldHeadNames.DENSITY])
|
||||
rgb_static_component = self.renderer_rgb(rgb=field_outputs[FieldHeadNames.RGB], weights=weights)
|
||||
rgb_transient_component = self.renderer_rgb(
|
||||
rgb=field_outputs[FieldHeadNames.TRANSIENT_RGB], weights=weights
|
||||
)
|
||||
rgb = rgb_static_component + rgb_transient_component
|
||||
else:
|
||||
weights_static = ray_samples.get_weights(field_outputs[FieldHeadNames.DENSITY])
|
||||
weights = weights_static
|
||||
rgb = self.renderer_rgb(rgb=field_outputs[FieldHeadNames.RGB], weights=weights)
|
||||
weights_list.append(weights_static)
|
||||
ray_samples_list.append(ray_samples)
|
||||
|
||||
depth = self.renderer_depth(weights=weights_static, ray_samples=ray_samples)
|
||||
accumulation = self.renderer_accumulation(weights=weights_static)
|
||||
|
||||
outputs = {"rgb": rgb, "accumulation": accumulation, "depth": depth}
|
||||
outputs["weights_list"] = weights_list
|
||||
outputs["ray_samples_list"] = ray_samples_list
|
||||
|
||||
for i in range(self.config.num_proposal_network_iterations):
|
||||
outputs[f"prop_depth_{i}"] = self.renderer_depth(weights=weights_list[i], ray_samples=ray_samples_list[i])
|
||||
|
||||
# transients
|
||||
if self.training and self.config.use_transient_embedding:
|
||||
weights_transient = ray_samples.get_weights(field_outputs[FieldHeadNames.TRANSIENT_DENSITY])
|
||||
uncertainty = self.renderer_uncertainty(field_outputs[FieldHeadNames.UNCERTAINTY], weights_transient)
|
||||
outputs["uncertainty"] = uncertainty + 0.03 # NOTE(ethan): this is the uncertainty min
|
||||
outputs["density_transient"] = field_outputs[FieldHeadNames.TRANSIENT_DENSITY]
|
||||
|
||||
# semantics
|
||||
outputs["semantics_thing"] = self.renderer_semantics(
|
||||
field_outputs[FieldHeadNames.SEMANTICS_THING], weights=weights_static.detach()
|
||||
)
|
||||
outputs["semantics_stuff"] = self.renderer_semantics(
|
||||
field_outputs[FieldHeadNames.SEMANTICS_STUFF], weights=weights_static.detach()
|
||||
)
|
||||
|
||||
# semantics colormaps
|
||||
semantic_labels = torch.argmax(torch.nn.functional.softmax(outputs["semantics_stuff"], dim=-1), dim=-1)
|
||||
outputs["semantics_colormap_stuff"] = self.semantics.stuff_colors[semantic_labels]
|
||||
semantic_labels = torch.argmax(torch.nn.functional.softmax(outputs["semantics_thing"], dim=-1), dim=-1)
|
||||
outputs["semantics_colormap_thing"] = self.semantics.thing_colors[semantic_labels]
|
||||
# TODO: adding these together is incorrect and produces weird results
|
||||
outputs["semantics_colormap"] = outputs["semantics_colormap_stuff"] + outputs["semantics_colormap_thing"]
|
||||
|
||||
return outputs
|
||||
|
||||
def get_metrics_dict(self, outputs, batch):
|
||||
metrics_dict = {}
|
||||
image = batch["image"].to(self.device)
|
||||
metrics_dict["psnr"] = self.psnr(outputs["rgb"], image)
|
||||
metrics_dict["distortion"] = distortion_loss(outputs["weights_list"], outputs["ray_samples_list"])
|
||||
return metrics_dict
|
||||
|
||||
def get_loss_dict(self, outputs, batch, metrics_dict=None):
|
||||
loss_dict = {}
|
||||
image = batch["image"].to(self.device)
|
||||
loss_dict["interlevel_loss"] = self.config.interlevel_loss_mult * interlevel_loss(
|
||||
outputs["weights_list"], outputs["ray_samples_list"]
|
||||
)
|
||||
assert metrics_dict is not None and "distortion" in metrics_dict
|
||||
loss_dict["distortion_loss"] = self.config.distortion_loss_mult * metrics_dict["distortion"]
|
||||
|
||||
# transient loss
|
||||
if self.training and self.config.use_transient_embedding:
|
||||
betas = outputs["uncertainty"]
|
||||
loss_dict["uncertainty_loss"] = 3 + torch.log(betas).mean()
|
||||
loss_dict["density_loss"] = 0.01 * outputs["density_transient"].mean()
|
||||
loss_dict["rgb_loss"] = (((image - outputs["rgb"]) ** 2).sum(-1) / (betas[..., 0] ** 2)).mean()
|
||||
else:
|
||||
loss_dict["rgb_loss"] = self.rgb_loss(image, outputs["rgb"])
|
||||
|
||||
# semantic loss
|
||||
loss_dict["semantics_stuff_loss"] = self.cross_entropy_loss(
|
||||
outputs["semantics_stuff"], batch["semantics_stuff"][..., 0].long()
|
||||
)
|
||||
loss_dict["semantics_thing_loss"] = self.cross_entropy_loss(
|
||||
outputs["semantics_thing"], batch["semantics_thing"][..., 0].long()
|
||||
)
|
||||
return loss_dict
|
||||
|
||||
def get_image_metrics_and_images(
|
||||
self, outputs: Dict[str, torch.Tensor], batch: Dict[str, torch.Tensor]
|
||||
) -> Tuple[Dict[str, float], Dict[str, torch.Tensor]]:
|
||||
|
||||
image = batch["image"].to(self.device)
|
||||
rgb = outputs["rgb"]
|
||||
rgb = torch.clamp(rgb, min=0, max=1)
|
||||
acc = colormaps.apply_colormap(outputs["accumulation"])
|
||||
depth = colormaps.apply_depth_colormap(
|
||||
outputs["depth"],
|
||||
accumulation=outputs["accumulation"],
|
||||
)
|
||||
|
||||
combined_rgb = torch.cat([image, rgb], dim=1)
|
||||
combined_acc = torch.cat([acc], dim=1)
|
||||
combined_depth = torch.cat([depth], dim=1)
|
||||
|
||||
# Switch images from [H, W, C] to [1, C, H, W] for metrics computations
|
||||
image = torch.moveaxis(image, -1, 0)[None, ...]
|
||||
rgb = torch.moveaxis(rgb, -1, 0)[None, ...]
|
||||
|
||||
psnr = self.psnr(image, rgb)
|
||||
ssim = self.ssim(image, rgb)
|
||||
lpips = self.lpips(image, rgb)
|
||||
|
||||
# all of these metrics will be logged as scalars
|
||||
metrics_dict = {"psnr": float(psnr.item()), "ssim": float(ssim)} # type: ignore
|
||||
metrics_dict["lpips"] = float(lpips)
|
||||
|
||||
images_dict = {"img": combined_rgb, "accumulation": combined_acc, "depth": combined_depth}
|
||||
|
||||
for i in range(self.config.num_proposal_network_iterations):
|
||||
key = f"prop_depth_{i}"
|
||||
prop_depth_i = colormaps.apply_depth_colormap(
|
||||
outputs[key],
|
||||
accumulation=outputs["accumulation"],
|
||||
)
|
||||
images_dict[key] = prop_depth_i
|
||||
|
||||
# semantics
|
||||
semantic_labels = torch.argmax(torch.nn.functional.softmax(outputs["semantics_stuff"], dim=-1), dim=-1)
|
||||
images_dict["semantics_colormap_stuff"] = self.semantics.stuff_colors[semantic_labels]
|
||||
semantic_labels = torch.argmax(torch.nn.functional.softmax(outputs["semantics_thing"], dim=-1), dim=-1)
|
||||
images_dict["semantics_colormap_thing"] = self.semantics.thing_colors[semantic_labels]
|
||||
# TODO: adding these together is incorrect and produces weird results
|
||||
images_dict["semantics_colormap"] = (
|
||||
images_dict["semantics_colormap_stuff"] + images_dict["semantics_colormap_thing"]
|
||||
)
|
||||
|
||||
# valid mask
|
||||
images_dict["mask"] = batch["mask"].repeat(1, 1, 3)
|
||||
|
||||
return metrics_dict, images_dict
|
||||
Reference in new issue
Block a user