mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-26 10:59:53 +08:00
annealing for proposal network (#560)
* merged * adding depth * added annealing * reverted launch.json * reverted hidden dim * changed loss * losses match master * comments * fix black
This commit is contained in:
1 parent
203d36c433
commit
1267045aed
8 files changed
+104
-30
No files matched your search
@@ -26,7 +26,7 @@ def distortion_loss(
|
||||
weights: TensorType["bs":..., "num_samples", 1] = None,
|
||||
scale_factor: float = 1.0,
|
||||
) -> TensorType["bs":..., 1]:
|
||||
"""Ray baserd distortion loss proposed in MipNeRF-360. Returns distortion Loss.
|
||||
"""Ray based distortion loss proposed in MipNeRF-360. Returns distortion Loss.
|
||||
|
||||
.. math::
|
||||
|
||||
|
||||
@@ -551,9 +551,16 @@ class ProposalNetworkSampler(Sampler):
|
||||
self.num_nerf_samples_per_ray = num_nerf_samples_per_ray
|
||||
self.num_proposal_network_iterations = num_proposal_network_iterations
|
||||
|
||||
# samplers
|
||||
self.initial_sampler = UniformLinDispPiecewiseSampler()
|
||||
self.pdf_sampler = PDFSampler(include_original=False)
|
||||
|
||||
self._anneal = 1.0
|
||||
|
||||
def set_anneal(self, anneal: float) -> None:
|
||||
"""Set the anneal value for the proposal network."""
|
||||
self._anneal = anneal
|
||||
|
||||
def generate_ray_samples(
|
||||
self,
|
||||
ray_bundle: Optional[RayBundle] = None,
|
||||
@@ -572,7 +579,9 @@ class ProposalNetworkSampler(Sampler):
|
||||
ray_samples = self.initial_sampler(ray_bundle, num_samples=num_samples)
|
||||
else:
|
||||
# PDF sampling based on the last samples and their weights
|
||||
ray_samples = self.pdf_sampler(ray_bundle, ray_samples, weights, num_samples=num_samples)
|
||||
# Perform annealing to the weights. This will be a no-op if self._anneal is 1.0.
|
||||
annealed_weights = torch.pow(weights, self._anneal)
|
||||
ray_samples = self.pdf_sampler(ray_bundle, ray_samples, annealed_weights, num_samples=num_samples)
|
||||
if is_prop:
|
||||
density = density_fn(ray_samples.frustums.get_positions())
|
||||
weights = ray_samples.get_weights(density)
|
||||
|
||||
@@ -21,6 +21,7 @@ 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
|
||||
@@ -44,6 +45,11 @@ from nerfactory.renderers.renderers import (
|
||||
RGBRenderer,
|
||||
)
|
||||
from nerfactory.utils import colors, visualization
|
||||
from nerfactory.utils.callbacks import (
|
||||
TrainingCallback,
|
||||
TrainingCallbackAttributes,
|
||||
TrainingCallbackLocation,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -69,6 +75,12 @@ class ProposalModelConfig(ModelConfig):
|
||||
"""Distortion loss multiplier."""
|
||||
use_appearance_conditioning: bool = True
|
||||
"""Whether to use appearance conditioning."""
|
||||
use_proposal_weight_anneal: bool = True
|
||||
"""Whether to use proposal weight annealing."""
|
||||
proposal_weights_anneal_slope: float = 10.0
|
||||
"""Slope of the annealing function for the proposal weights."""
|
||||
proposal_weights_anneal_max_num_iters: int = 1000
|
||||
"""Max num iterations for the annealing function."""
|
||||
|
||||
|
||||
class ProposalModel(Model):
|
||||
@@ -124,6 +136,30 @@ class ProposalModel(Model):
|
||||
param_groups["fields"] = list(self.proposal_network.parameters()) + 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_fn=self.proposal_network.density_fn
|
||||
@@ -140,6 +176,9 @@ class ProposalModel(Model):
|
||||
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])
|
||||
return outputs
|
||||
|
||||
def get_metrics_dict(self, outputs, batch):
|
||||
@@ -153,10 +192,7 @@ class ProposalModel(Model):
|
||||
image = batch["image"].to(self.device)
|
||||
loss_dict["rgb_loss"] = self.rgb_loss(image, outputs["rgb"])
|
||||
loss_dict["interlevel_loss"] = self.config.interlevel_loss_mult * interlevel_loss(
|
||||
outputs["weights_list"],
|
||||
outputs["ray_samples_list"],
|
||||
self.config.near_plane,
|
||||
self.config.far_plane,
|
||||
outputs["weights_list"], outputs["ray_samples_list"]
|
||||
)
|
||||
loss_dict["distortion_loss"] = self.config.distortion_loss_mult * torch.mean(
|
||||
distortion_loss(
|
||||
@@ -197,4 +233,12 @@ class ProposalModel(Model):
|
||||
|
||||
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 = visualization.apply_depth_colormap(
|
||||
outputs[key],
|
||||
accumulation=outputs["accumulation"],
|
||||
)
|
||||
images_dict[key] = prop_depth_i
|
||||
|
||||
return metrics_dict, images_dict
|
||||
Reference in new issue
Block a user