mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-06 15:59:16 +08:00
Cleaning Model() function signatures (#433)
Removing different model.forward() functions, eliminating overloading, making it easier to understand
This commit is contained in:
1 parent
acd126c1ef
commit
96d4974d73
10 files changed
+26
-38
No files matched your search
@@ -20,7 +20,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from typing import Any, Dict, List, Optional, Tuple, Union, overload
|
from typing import Any, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from torch import nn
|
from torch import nn
|
||||||
@@ -115,21 +115,9 @@ class Model(nn.Module):
|
|||||||
Outputs of model. (ie. rendered colors)
|
Outputs of model. (ie. rendered colors)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@overload
|
|
||||||
def forward(self, ray_bundle: RayBundle, batch: None = None) -> Dict[str, torch.Tensor]:
|
|
||||||
...
|
|
||||||
|
|
||||||
@overload
|
|
||||||
def forward(
|
|
||||||
self, ray_bundle: RayBundle, batch: Dict[str, torch.Tensor]
|
|
||||||
) -> Tuple[Dict[str, torch.Tensor], Dict[str, torch.Tensor], Dict[str, torch.Tensor]]:
|
|
||||||
...
|
|
||||||
|
|
||||||
def forward(
|
def forward(
|
||||||
self, ray_bundle: RayBundle, batch: Optional[Dict[str, torch.Tensor]] = None
|
self, ray_bundle: RayBundle, batch: Optional[Dict[str, torch.Tensor]] = None
|
||||||
) -> Union[
|
) -> Dict[str, torch.Tensor]:
|
||||||
Dict[str, torch.Tensor], Tuple[Dict[str, torch.Tensor], Dict[str, torch.Tensor], Dict[str, torch.Tensor]]
|
|
||||||
]:
|
|
||||||
"""Run forward starting with a ray bundle. This outputs different things depending on the configuration
|
"""Run forward starting with a ray bundle. This outputs different things depending on the configuration
|
||||||
of the model and whether or not the batch is provided (whether or not we are training basically)
|
of the model and whether or not the batch is provided (whether or not we are training basically)
|
||||||
|
|
||||||
@@ -157,11 +145,7 @@ class Model(nn.Module):
|
|||||||
batch = get_masked_dict(batch, valid_mask) # NOTE(ethan): this is really slow if on CPU!
|
batch = get_masked_dict(batch, valid_mask) # NOTE(ethan): this is really slow if on CPU!
|
||||||
|
|
||||||
outputs = self.get_outputs(intersected_ray_bundle)
|
outputs = self.get_outputs(intersected_ray_bundle)
|
||||||
metrics_dict = self.get_metrics_dict(outputs=outputs, batch=batch)
|
return outputs
|
||||||
loss_dict = self.get_loss_dict(
|
|
||||||
outputs=outputs, batch=batch, metrics_dict=metrics_dict, loss_coefficients=self.config.loss_coefficients
|
|
||||||
)
|
|
||||||
return outputs, loss_dict, metrics_dict
|
|
||||||
|
|
||||||
def get_metrics_dict(self, outputs, batch) -> Dict[str, torch.Tensor]:
|
def get_metrics_dict(self, outputs, batch) -> Dict[str, torch.Tensor]:
|
||||||
"""Compute and returns metrics.
|
"""Compute and returns metrics.
|
||||||
@@ -175,14 +159,13 @@ class Model(nn.Module):
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients) -> Dict[str, torch.Tensor]:
|
def get_loss_dict(self, outputs, batch, metrics_dict=None) -> Dict[str, torch.Tensor]:
|
||||||
"""Computes and returns the losses dict.
|
"""Computes and returns the losses dict.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
outputs: the output to compute loss dict to
|
outputs: the output to compute loss dict to
|
||||||
batch: ground truth batch corresponding to outputs
|
batch: ground truth batch corresponding to outputs
|
||||||
metrics_dict: collection of metrics to compute
|
metrics_dict: dictionary of metrics, some of which we can use for loss
|
||||||
loss_coefficients: list of loss coefficients/weightings to apply
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@torch.no_grad()
|
@torch.no_grad()
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ class CompoundModel(Model):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
def get_loss_dict(self, outputs, batch, metrics_dict=None):
|
||||||
image = batch["image"].to(self.device)
|
image = batch["image"].to(self.device)
|
||||||
mask = outputs["alive_ray_mask"]
|
mask = outputs["alive_ray_mask"]
|
||||||
rgb_loss = self.rgb_loss(image[mask], outputs["rgb"][mask])
|
rgb_loss = self.rgb_loss(image[mask], outputs["rgb"][mask])
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ class NGPModel(Model):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
def get_loss_dict(self, outputs, batch, metrics_dict=None):
|
||||||
image = batch["image"].to(self.device)
|
image = batch["image"].to(self.device)
|
||||||
mask = outputs["alive_ray_mask"]
|
mask = outputs["alive_ray_mask"]
|
||||||
rgb_loss = self.rgb_loss(image[mask], outputs["rgb"][mask])
|
rgb_loss = self.rgb_loss(image[mask], outputs["rgb"][mask])
|
||||||
|
|||||||
@@ -137,12 +137,12 @@ class MipNerfModel(Model):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
def get_loss_dict(self, outputs, batch, metrics_dict=None):
|
||||||
image = batch["image"]
|
image = batch["image"]
|
||||||
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
||||||
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
||||||
loss_dict = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
loss_dict = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
||||||
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def get_image_metrics_and_images(
|
def get_image_metrics_and_images(
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ class MipNerf360Model(Model):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients) -> Dict[str, torch.Tensor]:
|
def get_loss_dict(self, outputs, batch, metrics_dict=None) -> Dict[str, torch.Tensor]:
|
||||||
image = batch["image"]
|
image = batch["image"]
|
||||||
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
||||||
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
||||||
@@ -157,7 +157,7 @@ class MipNerf360Model(Model):
|
|||||||
"ray_loss_coarse": torch.mean(outputs["ray_loss_coarse"]),
|
"ray_loss_coarse": torch.mean(outputs["ray_loss_coarse"]),
|
||||||
"ray_loss_fine": torch.mean(outputs["ray_loss_fine"]),
|
"ray_loss_fine": torch.mean(outputs["ray_loss_fine"]),
|
||||||
}
|
}
|
||||||
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def get_image_metrics_and_images(
|
def get_image_metrics_and_images(
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ class NerfWModel(Model):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
def get_loss_dict(self, outputs, batch, metrics_dict=None):
|
||||||
device = outputs["rgb_coarse"].device
|
device = outputs["rgb_coarse"].device
|
||||||
image = batch["image"].to(device)
|
image = batch["image"].to(device)
|
||||||
rgb_coarse = outputs["rgb_coarse"]
|
rgb_coarse = outputs["rgb_coarse"]
|
||||||
@@ -213,7 +213,7 @@ class NerfWModel(Model):
|
|||||||
"uncertainty_loss": uncertainty_loss,
|
"uncertainty_loss": uncertainty_loss,
|
||||||
"density_loss": density_loss,
|
"density_loss": density_loss,
|
||||||
}
|
}
|
||||||
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def get_image_metrics_and_images(
|
def get_image_metrics_and_images(
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ class SemanticNerfModel(NeRFModel):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
def get_loss_dict(self, outputs, batch, metrics_dict=None):
|
||||||
image = batch["image"].to(outputs["rgb_coarse"].device)
|
image = batch["image"].to(outputs["rgb_coarse"].device)
|
||||||
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
||||||
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
||||||
@@ -127,7 +127,7 @@ class SemanticNerfModel(NeRFModel):
|
|||||||
"rgb_loss_fine": rgb_loss_fine,
|
"rgb_loss_fine": rgb_loss_fine,
|
||||||
"semantic_loss_fine": semantic_loss_fine,
|
"semantic_loss_fine": semantic_loss_fine,
|
||||||
}
|
}
|
||||||
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def get_image_metrics_and_images(
|
def get_image_metrics_and_images(
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ class TensoRFModel(Model):
|
|||||||
outputs = {"rgb": rgb, "accumulation": accumulation, "depth": depth}
|
outputs = {"rgb": rgb, "accumulation": accumulation, "depth": depth}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients) -> Dict[str, torch.Tensor]:
|
def get_loss_dict(self, outputs, batch, metrics_dict=None) -> Dict[str, torch.Tensor]:
|
||||||
# Scaling metrics by coefficients to create the losses.
|
# Scaling metrics by coefficients to create the losses.
|
||||||
device = outputs["rgb"].device
|
device = outputs["rgb"].device
|
||||||
image = batch["image"].to(device)
|
image = batch["image"].to(device)
|
||||||
@@ -192,7 +192,7 @@ class TensoRFModel(Model):
|
|||||||
line_feature_loss = self.feature_loss(line_coef, torch.zeros_like(line_coef))
|
line_feature_loss = self.feature_loss(line_coef, torch.zeros_like(line_coef))
|
||||||
|
|
||||||
loss_dict = {"rgb_loss": rgb_loss, "feature_loss": plane_feature_loss + line_feature_loss}
|
loss_dict = {"rgb_loss": rgb_loss, "feature_loss": plane_feature_loss + line_feature_loss}
|
||||||
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def get_image_metrics_and_images(
|
def get_image_metrics_and_images(
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ class NeRFModel(Model):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients) -> Dict[str, torch.Tensor]:
|
def get_loss_dict(self, outputs, batch, metrics_dict=None) -> Dict[str, torch.Tensor]:
|
||||||
# Scaling metrics by coefficients to create the losses.
|
# Scaling metrics by coefficients to create the losses.
|
||||||
device = outputs["rgb_coarse"].device
|
device = outputs["rgb_coarse"].device
|
||||||
image = batch["image"].to(device)
|
image = batch["image"].to(device)
|
||||||
@@ -175,7 +175,7 @@ class NeRFModel(Model):
|
|||||||
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
||||||
|
|
||||||
loss_dict = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
loss_dict = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
||||||
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def get_image_metrics_and_images(
|
def get_image_metrics_and_images(
|
||||||
|
|||||||
@@ -129,7 +129,10 @@ class Pipeline(nn.Module):
|
|||||||
assert self.datamanager.train_sampler is not None
|
assert self.datamanager.train_sampler is not None
|
||||||
self.datamanager.train_sampler.set_epoch(step)
|
self.datamanager.train_sampler.set_epoch(step)
|
||||||
ray_bundle, batch = self.datamanager.next_train(step)
|
ray_bundle, batch = self.datamanager.next_train(step)
|
||||||
model_outputs, loss_dict, metrics_dict = self.model(ray_bundle, batch)
|
model_outputs = self.model(ray_bundle, batch)
|
||||||
|
metrics_dict = self.model.get_metrics_dict(model_outputs, batch)
|
||||||
|
loss_dict = self.model.get_loss_dict(model_outputs, batch, metrics_dict)
|
||||||
|
|
||||||
return model_outputs, loss_dict, metrics_dict
|
return model_outputs, loss_dict, metrics_dict
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
@@ -146,7 +149,9 @@ class Pipeline(nn.Module):
|
|||||||
assert self.datamanager.eval_sampler is not None
|
assert self.datamanager.eval_sampler is not None
|
||||||
self.datamanager.eval_sampler.set_epoch(step)
|
self.datamanager.eval_sampler.set_epoch(step)
|
||||||
ray_bundle, batch = self.datamanager.next_eval(step)
|
ray_bundle, batch = self.datamanager.next_eval(step)
|
||||||
model_outputs, loss_dict, metrics_dict = self.model(ray_bundle, batch)
|
model_outputs = self.model(ray_bundle, batch)
|
||||||
|
loss_dict = self.model.get_loss_dict(model_outputs, batch)
|
||||||
|
metrics_dict = self.model.get_metrics_dict(model_outputs, batch)
|
||||||
self.train()
|
self.train()
|
||||||
return model_outputs, loss_dict, metrics_dict
|
return model_outputs, loss_dict, metrics_dict
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user