mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-05 15:28:49 +08:00
nerf-w and semantic-nerf initial implementations (#73)
* initial nerfw code * nerfw should be implmented correctly now * semantic nerf in progress. data loader is too slow and not clean right now * added implementation of semantic nerf * mend
This commit is contained in:
1 parent
97c9fe2541
commit
1c1a58c4e5
22 files changed
+725
-92
No files matched your search
@@ -111,13 +111,15 @@ class Graph(nn.Module):
|
||||
return aggregated_loss
|
||||
|
||||
@torch.no_grad()
|
||||
def get_outputs_for_camera(self, intrinsics, camera_to_world, chunk_size=1024):
|
||||
def get_outputs_for_camera(self, intrinsics, camera_to_world, chunk_size=1024, training_camera_index=0):
|
||||
"""Takes in camera parameters and computes the output of the graph."""
|
||||
assert len(intrinsics.shape) == 1
|
||||
num_intrinsics_params = len(intrinsics)
|
||||
camera_class = get_camera_model(num_intrinsics_params)
|
||||
camera = camera_class(*intrinsics.tolist(), camera_to_world=camera_to_world)
|
||||
camera_ray_bundle = camera.generate_camera_rays()
|
||||
# TODO(ethan): decide how to properly handle the image indices for validation images
|
||||
camera_ray_bundle.set_camera_indices(camera_index=training_camera_index)
|
||||
image_height, image_width = camera_ray_bundle.origins.shape[:2]
|
||||
|
||||
device = self.get_device()
|
||||
|
||||
@@ -2,28 +2,26 @@
|
||||
Implementation of Instant NGP.
|
||||
"""
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from mattport.nerf.field_modules.encoding import HashEncoding, NeRFEncoding
|
||||
from mattport.nerf.field_modules.field_heads import DensityFieldHead, RGBFieldHead
|
||||
from mattport.nerf.field_modules.mlp import MLP
|
||||
from mattport.nerf.graph.vanilla_nerf import NeRFGraph
|
||||
from mattport.structures.rays import RaySamples
|
||||
from mattport.nerf.graph.vanilla_nerf import NeRFField, NeRFGraph
|
||||
|
||||
|
||||
class NGPField(nn.Module):
|
||||
class NGPField(NeRFField):
|
||||
"""NeRF module"""
|
||||
|
||||
def __init__(self, num_layers=3, layer_width=64) -> None:
|
||||
super().__init__()
|
||||
self.num_layers = num_layers
|
||||
self.layer_width = layer_width
|
||||
super().__init__(num_layers=num_layers, layer_width=layer_width)
|
||||
|
||||
def build_encodings(self):
|
||||
self.encoding_xyz = HashEncoding()
|
||||
self.encoding_dir = NeRFEncoding(
|
||||
in_dim=3, num_frequencies=4, min_freq_exp=0.0, max_freq_exp=4.0, include_input=True
|
||||
)
|
||||
|
||||
def build_mlp_base(self):
|
||||
self.mlp_base = MLP(
|
||||
in_dim=self.encoding_xyz.get_out_dim(),
|
||||
out_dim=self.layer_width,
|
||||
@@ -31,36 +29,6 @@ class NGPField(nn.Module):
|
||||
layer_width=self.layer_width,
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
self.mlp_rgb = MLP(
|
||||
in_dim=self.mlp_base.get_out_dim() + self.encoding_dir.get_out_dim(),
|
||||
out_dim=self.layer_width // 2,
|
||||
num_layers=1,
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
self.field_output_rgb = RGBFieldHead(in_dim=self.mlp_rgb.get_out_dim())
|
||||
self.field_output_density = DensityFieldHead(in_dim=self.mlp_base.get_out_dim())
|
||||
|
||||
def forward(self, ray_samples: RaySamples):
|
||||
"""Evaluates the field at points along the ray
|
||||
Args:
|
||||
xyz: ()
|
||||
# TODO(ethan): change the input to be something more abstracted
|
||||
e.g., a FieldInput structure
|
||||
"""
|
||||
positions = ray_samples.positions
|
||||
directions = ray_samples.directions
|
||||
encoded_xyz = self.encoding_xyz(positions)
|
||||
encoded_dir = self.encoding_dir(directions)
|
||||
base_mlp_out = self.mlp_base(encoded_xyz)
|
||||
rgb_mlp_out = self.mlp_rgb(torch.cat([encoded_dir, base_mlp_out], dim=-1))
|
||||
|
||||
field_rgb_output = self.field_output_rgb(rgb_mlp_out)
|
||||
field_density_out = self.field_output_density(base_mlp_out)
|
||||
|
||||
field_outputs = {}
|
||||
field_outputs.update(field_rgb_output)
|
||||
field_outputs.update(field_density_out)
|
||||
return field_outputs
|
||||
|
||||
|
||||
class NGPGraph(NeRFGraph):
|
||||
|
||||
@@ -3,18 +3,215 @@ NeRF-W (NeRF in the wild) implementation.
|
||||
TODO:
|
||||
"""
|
||||
|
||||
from mattport.nerf.graph.vanilla_nerf import NeRFGraph
|
||||
from mattport.structures.rays import RayBundle
|
||||
from typing import Tuple
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from mattport.nerf.field_modules.embedding import Embedding
|
||||
from mattport.nerf.field_modules.field_heads import (
|
||||
FieldHeadNames,
|
||||
TransientDensityHead,
|
||||
TransientRGBHead,
|
||||
UncertaintyFieldHead,
|
||||
)
|
||||
from mattport.nerf.field_modules.mlp import MLP
|
||||
from mattport.nerf.graph.vanilla_nerf import NeRFField, NeRFGraph
|
||||
from mattport.nerf.renderers import UncertaintyRenderer
|
||||
from mattport.structures.rays import RayBundle, RaySamples
|
||||
from mattport.utils import visualization, writer
|
||||
|
||||
|
||||
class NerfWField(NeRFField):
|
||||
"""The NeRF-W field which has appearance and transient conditioning."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
num_layers=8,
|
||||
layer_width=256,
|
||||
skip_connections: Tuple = (4,),
|
||||
num_images: int = 0,
|
||||
appearance_embedding_dim: int = 48,
|
||||
transient_embedding_dim: int = 16,
|
||||
) -> None:
|
||||
assert num_images > 0
|
||||
self.num_images = num_images
|
||||
self.appearance_embedding_dim = appearance_embedding_dim
|
||||
self.transient_embedding_dim = transient_embedding_dim
|
||||
super().__init__(num_layers, layer_width, skip_connections)
|
||||
|
||||
def build_mlp_base(self):
|
||||
self.embedding_appearance = Embedding(self.num_images, self.appearance_embedding_dim)
|
||||
self.embedding_transient = Embedding(self.num_images, self.transient_embedding_dim)
|
||||
super().build_mlp_base()
|
||||
self.mlp_transient = MLP(
|
||||
in_dim=self.mlp_base.get_out_dim() + self.embedding_transient.get_out_dim(),
|
||||
out_dim=self.layer_width // 2,
|
||||
num_layers=4,
|
||||
layer_width=self.layer_width,
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
|
||||
def build_mlp_rgb(self):
|
||||
self.mlp_rgb = MLP(
|
||||
in_dim=self.mlp_base.get_out_dim()
|
||||
+ self.encoding_dir.get_out_dim()
|
||||
+ self.embedding_appearance.get_out_dim(),
|
||||
out_dim=self.layer_width // 2,
|
||||
num_layers=1,
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
|
||||
def build_heads(self):
|
||||
super().build_heads()
|
||||
self.field_output_transient_uncertainty = UncertaintyFieldHead(in_dim=self.mlp_transient.get_out_dim())
|
||||
self.field_output_transient_rgb = TransientRGBHead(in_dim=self.mlp_transient.get_out_dim())
|
||||
self.field_output_transient_density = TransientDensityHead(in_dim=self.mlp_transient.get_out_dim())
|
||||
|
||||
def forward(self, ray_samples: RaySamples):
|
||||
positions = ray_samples.positions
|
||||
directions = ray_samples.directions
|
||||
camera_indices = ray_samples.get_camera_indices()
|
||||
encoded_xyz = self.encoding_xyz(positions)
|
||||
encoded_dir = self.encoding_dir(directions)
|
||||
embedded_appearance = self.embedding_appearance(camera_indices)
|
||||
embedded_transient = self.embedding_transient(camera_indices)
|
||||
|
||||
base_mlp_out = self.mlp_base(encoded_xyz)
|
||||
rgb_mlp_out = self.mlp_rgb(torch.cat([base_mlp_out, encoded_dir, embedded_appearance], dim=-1))
|
||||
transient_mlp_out = self.mlp_transient(torch.cat([base_mlp_out, embedded_transient], dim=-1))
|
||||
|
||||
field_rgb_output = self.field_output_rgb(rgb_mlp_out)
|
||||
field_density_out = self.field_output_density(base_mlp_out)
|
||||
field_transient_uncertainty_out = self.field_output_transient_uncertainty(transient_mlp_out)
|
||||
field_transient_rgb_out = self.field_output_transient_rgb(transient_mlp_out)
|
||||
field_transient_density_out = self.field_output_transient_density(transient_mlp_out)
|
||||
|
||||
field_outputs = {}
|
||||
field_outputs.update(field_rgb_output)
|
||||
field_outputs.update(field_density_out)
|
||||
field_outputs.update(field_transient_uncertainty_out)
|
||||
field_outputs.update(field_transient_rgb_out)
|
||||
field_outputs.update(field_transient_density_out)
|
||||
return field_outputs
|
||||
|
||||
|
||||
class NerfWGraph(NeRFGraph):
|
||||
"""NeRF-W graph"""
|
||||
|
||||
def __init__(self, intrinsics=None, camera_to_world=None, **kwargs) -> None:
|
||||
self.num_images = len(intrinsics)
|
||||
self.appearance_embedding_dim = 48
|
||||
self.transient_embedding_dim = 16
|
||||
super().__init__(intrinsics=intrinsics, camera_to_world=camera_to_world, **kwargs)
|
||||
self.renderer_uncertainty = UncertaintyRenderer()
|
||||
|
||||
def populate_fields(self):
|
||||
"""Set the fields."""
|
||||
self.field_coarse = NeRFField()
|
||||
self.field_fine = NerfWField(
|
||||
num_images=self.num_images,
|
||||
appearance_embedding_dim=self.appearance_embedding_dim,
|
||||
transient_embedding_dim=self.transient_embedding_dim,
|
||||
)
|
||||
|
||||
def get_outputs(self, ray_bundle: RayBundle):
|
||||
raise NotImplementedError
|
||||
# coarse network
|
||||
uniform_ray_samples = self.sampler_uniform(ray_bundle)
|
||||
coarse_field_outputs = self.field_coarse(uniform_ray_samples)
|
||||
coarse_weights = uniform_ray_samples.get_weights(coarse_field_outputs[FieldHeadNames.DENSITY])
|
||||
rgb_coarse = self.renderer_rgb(
|
||||
rgb=coarse_field_outputs[FieldHeadNames.RGB],
|
||||
weights=coarse_weights,
|
||||
).rgb
|
||||
depth_coarse = self.renderer_depth(coarse_weights, uniform_ray_samples.ts).depth
|
||||
|
||||
# fine network
|
||||
pdf_ray_samples = self.sampler_pdf(uniform_ray_samples, coarse_weights)
|
||||
fine_field_outputs = self.field_fine(pdf_ray_samples)
|
||||
fine_weights = pdf_ray_samples.get_weights(
|
||||
fine_field_outputs[FieldHeadNames.DENSITY] + fine_field_outputs[FieldHeadNames.TRANSIENT_DENSITY]
|
||||
)
|
||||
fine_weights_static = pdf_ray_samples.get_weights(fine_field_outputs[FieldHeadNames.DENSITY])
|
||||
fine_weights_transient = pdf_ray_samples.get_weights(fine_field_outputs[FieldHeadNames.TRANSIENT_DENSITY])
|
||||
|
||||
rgb_fine_static_component = self.renderer_rgb(
|
||||
rgb=fine_field_outputs[FieldHeadNames.RGB],
|
||||
weights=fine_weights,
|
||||
).rgb
|
||||
rgb_fine_transient_component = self.renderer_rgb(
|
||||
rgb=fine_field_outputs[FieldHeadNames.TRANSIENT_RGB],
|
||||
weights=fine_weights,
|
||||
).rgb
|
||||
rgb_fine = rgb_fine_static_component + rgb_fine_transient_component
|
||||
|
||||
rgb_fine_static = self.renderer_rgb(
|
||||
rgb=fine_field_outputs[FieldHeadNames.RGB],
|
||||
weights=fine_weights_static,
|
||||
).rgb
|
||||
|
||||
# fine_renderer_accumulation_static = self.renderer_accumulation(fine_weights_static)
|
||||
depth_fine = self.renderer_depth(fine_weights, pdf_ray_samples.ts).depth
|
||||
depth_fine_static = self.renderer_depth(fine_weights_static, pdf_ray_samples.ts).depth
|
||||
uncertainty = self.renderer_uncertainty(
|
||||
fine_field_outputs[FieldHeadNames.UNCERTAINTY], fine_weights_transient
|
||||
).uncertainty
|
||||
|
||||
density_transient = fine_field_outputs[FieldHeadNames.TRANSIENT_DENSITY]
|
||||
|
||||
# outputs:
|
||||
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):
|
||||
raise NotImplementedError
|
||||
device = outputs["rgb_coarse"].device
|
||||
pixels = batch["pixels"].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 * ((pixels - rgb_coarse) ** 2).sum(-1).mean()
|
||||
rgb_loss_fine = 0.5 * (((pixels - 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["aggregated_loss"] = self.get_aggregated_loss_from_loss_dict(loss_dict)
|
||||
return loss_dict
|
||||
|
||||
def log_test_image_outputs(self, image_idx, step, image, outputs):
|
||||
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 = visualization.apply_depth_colormap(depth_coarse)
|
||||
depth_fine = visualization.apply_depth_colormap(depth_fine)
|
||||
depth_fine_static = visualization.apply_depth_colormap(depth_fine_static)
|
||||
uncertainty = visualization.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)
|
||||
|
||||
writer.write_event(
|
||||
{"name": f"image_idx_{image_idx}-nerfw", "x": combined_image, "step": step, "group": "val_img"}
|
||||
)
|
||||
@@ -1,4 +1,160 @@
|
||||
"""
|
||||
Semantic NeRF implementation.
|
||||
TODO:
|
||||
"""
|
||||
|
||||
from typing import Tuple
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from mattport.nerf.field_modules.field_heads import FieldHeadNames, SemanticStuffHead
|
||||
from mattport.nerf.field_modules.mlp import MLP
|
||||
from mattport.nerf.graph.vanilla_nerf import NeRFField, NeRFGraph
|
||||
from mattport.nerf.renderers import SemanticRenderer
|
||||
from mattport.structures.rays import RayBundle, RaySamples
|
||||
from mattport.utils import visualization, writer
|
||||
|
||||
|
||||
class SemanticNerfField(NeRFField):
|
||||
"""Semantic-NeRF field"""
|
||||
|
||||
def __init__(
|
||||
self, num_layers=8, layer_width=256, skip_connections: Tuple = (4,), num_stuff_classes: int = None
|
||||
) -> None:
|
||||
assert num_stuff_classes is not None
|
||||
self.num_stuff_classes = num_stuff_classes
|
||||
super().__init__(num_layers=num_layers, layer_width=layer_width, skip_connections=skip_connections)
|
||||
self.semantic_mlp = MLP(
|
||||
in_dim=self.mlp_base.get_out_dim(),
|
||||
out_dim=self.layer_width // 2,
|
||||
num_layers=1,
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
self.field_output_semantic = SemanticStuffHead(
|
||||
in_dim=self.semantic_mlp.get_out_dim(), num_classes=num_stuff_classes
|
||||
)
|
||||
self.semantic_sequential = torch.nn.Sequential(self.semantic_mlp, self.field_output_semantic)
|
||||
|
||||
def forward(self, ray_samples: RaySamples):
|
||||
"""Evaluates the field at points along the ray."""
|
||||
positions = ray_samples.positions
|
||||
directions = ray_samples.directions
|
||||
encoded_xyz = self.encoding_xyz(positions)
|
||||
encoded_dir = self.encoding_dir(directions)
|
||||
base_mlp_out = self.mlp_base(encoded_xyz)
|
||||
rgb_mlp_out = self.mlp_rgb(torch.cat([encoded_dir, base_mlp_out], dim=-1))
|
||||
|
||||
field_rgb_output = self.field_output_rgb(rgb_mlp_out)
|
||||
field_density_out = self.field_output_density(base_mlp_out)
|
||||
field_semantic_output = self.semantic_sequential(base_mlp_out)
|
||||
|
||||
field_outputs = {}
|
||||
field_outputs.update(field_rgb_output)
|
||||
field_outputs.update(field_density_out)
|
||||
field_outputs.update(field_semantic_output)
|
||||
return field_outputs
|
||||
|
||||
|
||||
class SemanticNerfGraph(NeRFGraph):
|
||||
"""Semantic-NeRF graph"""
|
||||
|
||||
def __init__(self, intrinsics=None, camera_to_world=None, stuff_classes: int = None, **kwargs) -> None:
|
||||
assert stuff_classes is not None
|
||||
self.stuff_classes = stuff_classes
|
||||
super().__init__(intrinsics=intrinsics, camera_to_world=camera_to_world, **kwargs)
|
||||
self.renderer_semantic = SemanticRenderer()
|
||||
self.cross_entropy_loss = nn.CrossEntropyLoss(reduction="mean")
|
||||
|
||||
def populate_fields(self):
|
||||
"""Set the fields."""
|
||||
self.field_coarse = NeRFField()
|
||||
self.field_fine = SemanticNerfField(num_stuff_classes=len(self.stuff_classes))
|
||||
|
||||
def get_outputs(self, ray_bundle: RayBundle):
|
||||
# coarse network:
|
||||
uniform_ray_samples = self.sampler_uniform(ray_bundle) # RaySamples
|
||||
|
||||
coarse_field_outputs = self.field_coarse(uniform_ray_samples) # FieldOutputs
|
||||
|
||||
coarse_weights = uniform_ray_samples.get_weights(coarse_field_outputs[FieldHeadNames.DENSITY])
|
||||
|
||||
coarse_renderer_outputs = self.renderer_rgb(
|
||||
rgb=coarse_field_outputs[FieldHeadNames.RGB],
|
||||
weights=coarse_weights,
|
||||
) # RendererOutputs
|
||||
coarse_renderer_accumulation = self.renderer_accumulation(coarse_weights) # RendererOutputs
|
||||
coarse_renderer_depth = self.renderer_depth(coarse_weights, uniform_ray_samples.ts)
|
||||
|
||||
# fine network:
|
||||
pdf_ray_samples = self.sampler_pdf(uniform_ray_samples, coarse_weights) # RaySamples
|
||||
fine_field_outputs = self.field_fine(pdf_ray_samples) # FieldOutputs
|
||||
|
||||
fine_weights = pdf_ray_samples.get_weights(fine_field_outputs[FieldHeadNames.DENSITY])
|
||||
|
||||
fine_renderer_outputs = self.renderer_rgb(
|
||||
rgb=fine_field_outputs[FieldHeadNames.RGB],
|
||||
weights=fine_weights,
|
||||
) # RendererOutputs
|
||||
fine_renderer_accumulation = self.renderer_accumulation(fine_weights) # RendererOutputs
|
||||
fine_renderer_depth = self.renderer_depth(fine_weights, pdf_ray_samples.ts)
|
||||
|
||||
# TODO refactor this into "vis" section. Doesn't need to be run during training.
|
||||
coarse_renderer_depth = visualization.apply_depth_colormap(
|
||||
coarse_renderer_depth.depth,
|
||||
accumulation=coarse_renderer_accumulation.accumulation,
|
||||
near_plane=self.near_plane,
|
||||
far_plane=self.far_plane,
|
||||
)
|
||||
fine_renderer_depth = visualization.apply_depth_colormap(
|
||||
fine_renderer_depth.depth,
|
||||
accumulation=fine_renderer_accumulation.accumulation,
|
||||
near_plane=self.near_plane,
|
||||
far_plane=self.far_plane,
|
||||
)
|
||||
coarse_renderer_accumulation = visualization.apply_colormap(coarse_renderer_accumulation.accumulation)
|
||||
fine_renderer_accumulation = visualization.apply_colormap(fine_renderer_accumulation.accumulation)
|
||||
|
||||
semantic_logits = self.renderer_semantic(
|
||||
fine_field_outputs[FieldHeadNames.SEMANTICS_STUFF], weights=fine_weights
|
||||
).semantics
|
||||
|
||||
# outputs:
|
||||
outputs = {
|
||||
"rgb_coarse": coarse_renderer_outputs.rgb,
|
||||
"rgb_fine": fine_renderer_outputs.rgb,
|
||||
"accumulation_coarse": coarse_renderer_accumulation,
|
||||
"accumulation_fine": fine_renderer_accumulation,
|
||||
"depth_coarse": coarse_renderer_depth,
|
||||
"depth_fine": fine_renderer_depth,
|
||||
"semantic_logits": semantic_logits,
|
||||
}
|
||||
return outputs
|
||||
|
||||
def get_loss_dict(self, outputs, batch):
|
||||
# TODO(ethan): batch has "stuff_image". use it
|
||||
device = outputs["rgb_coarse"].device
|
||||
pixels = batch["pixels"].to(device)
|
||||
rgb_loss_coarse = self.rgb_loss(pixels, outputs["rgb_coarse"])
|
||||
rgb_loss_fine = self.rgb_loss(pixels, outputs["rgb_fine"])
|
||||
|
||||
semantic_logits = outputs["semantic_logits"]
|
||||
semantic_classes = batch["stuff_image"][..., 0].to(device).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["aggregated_loss"] = self.get_aggregated_loss_from_loss_dict(loss_dict)
|
||||
return loss_dict
|
||||
|
||||
def log_test_image_outputs(self, image_idx, step, image, outputs):
|
||||
super().log_test_image_outputs(image_idx, step, image, outputs)
|
||||
semantic_labels = torch.argmax(torch.nn.functional.softmax(outputs["semantic_logits"], dim=-1), dim=-1) / len(
|
||||
self.stuff_classes
|
||||
)
|
||||
semantic_labels_image = visualization.apply_colormap(semantic_labels[..., None])
|
||||
writer.write_event(
|
||||
{"name": f"semantic_image_idx_{image_idx}", "x": semantic_labels_image, "step": step, "group": "val_sem"}
|
||||
)
|
||||
@@ -32,12 +32,22 @@ class NeRFField(nn.Module):
|
||||
self.layer_width = layer_width
|
||||
self.skip_connections = skip_connections
|
||||
|
||||
self.build_encodings()
|
||||
self.build_mlp_base()
|
||||
self.build_mlp_rgb()
|
||||
self.build_heads()
|
||||
|
||||
def build_encodings(self):
|
||||
"""Build the encodings."""
|
||||
self.encoding_xyz = NeRFEncoding(
|
||||
in_dim=3, num_frequencies=10, min_freq_exp=0.0, max_freq_exp=8.0, include_input=True
|
||||
)
|
||||
self.encoding_dir = NeRFEncoding(
|
||||
in_dim=3, num_frequencies=4, min_freq_exp=0.0, max_freq_exp=4.0, include_input=True
|
||||
)
|
||||
|
||||
def build_mlp_base(self):
|
||||
"""Build the MLP base."""
|
||||
self.mlp_base = MLP(
|
||||
in_dim=self.encoding_xyz.get_out_dim(),
|
||||
out_dim=self.layer_width,
|
||||
@@ -46,6 +56,9 @@ class NeRFField(nn.Module):
|
||||
skip_connections=self.skip_connections,
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
|
||||
def build_mlp_rgb(self):
|
||||
"""Build the MLP for RGB."""
|
||||
self.mlp_rgb = MLP(
|
||||
in_dim=self.mlp_base.get_out_dim() + self.encoding_dir.get_out_dim(),
|
||||
out_dim=self.layer_width // 2,
|
||||
@@ -53,16 +66,14 @@ class NeRFField(nn.Module):
|
||||
layer_width=self.layer_width // 2,
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
|
||||
def build_heads(self):
|
||||
"""Build the heads."""
|
||||
self.field_output_rgb = RGBFieldHead(in_dim=self.mlp_rgb.get_out_dim())
|
||||
self.field_output_density = DensityFieldHead(in_dim=self.mlp_base.get_out_dim())
|
||||
|
||||
def forward(self, ray_samples: RaySamples):
|
||||
"""Evaluates the field at points along the ray
|
||||
Args:
|
||||
xyz: ()
|
||||
# TODO(ethan): change the input to be something more abstracted
|
||||
e.g., a FieldInput structure
|
||||
"""
|
||||
"""Evaluates the field at points along the ray."""
|
||||
positions = ray_samples.positions
|
||||
directions = ray_samples.directions
|
||||
encoded_xyz = self.encoding_xyz(positions)
|
||||
@@ -144,9 +155,7 @@ class NeRFGraph(Graph):
|
||||
# coarse network:
|
||||
uniform_ray_samples = self.sampler_uniform(ray_bundle) # RaySamples
|
||||
|
||||
# time coarse network # xyz -> asd
|
||||
coarse_field_outputs = self.field_coarse(uniform_ray_samples) # FieldOutputs
|
||||
# time end coarse network
|
||||
|
||||
coarse_weights = uniform_ray_samples.get_weights(coarse_field_outputs[FieldHeadNames.DENSITY])
|
||||
|
||||
|
||||
Reference in new issue
Block a user