Loss Metric Refactoring (#181)

* metrics also returned on forward pass, reduction moved to right before backprop
This commit is contained in:
Alexander Kristoffersen authored and GitHub committed 2022-07-22 18:05:03 -07:00
1 parent a78394c49d
commit 960ac94e06
3 files changed
+27 -19

No files matched your search

+13 -12
View File
@@ -163,9 +163,14 @@ class Graph(AbstractGraph):
masked_intersected_ray_bundle = intersected_ray_bundle[valid_mask]
masked_batch = get_masked_dict(batch, valid_mask) # NOTE(ethan): this is really slow if on CPU!
outputs = self.get_outputs(masked_intersected_ray_bundle)
metrics_dict = self.get_metrics_dict(outputs=outputs, batch=masked_batch)
loss_dict = self.get_loss_dict(outputs=outputs, batch=masked_batch)
aggregated_loss_dict = self.get_aggregated_loss_dict(loss_dict)
return outputs, aggregated_loss_dict
# scaling losses by coefficients.
for loss_name in loss_dict.keys():
if loss_name in self.loss_coefficients:
loss_dict[loss_name] *= self.loss_coefficients[loss_name]
return outputs, loss_dict, metrics_dict
def forward(self, ray_indices: TensorType["num_rays", 3], batch: Union[str, Dict[str, torch.tensor]] = None):
"""Run the forward starting with ray indices."""
@@ -174,17 +179,13 @@ class Graph(AbstractGraph):
@abstractmethod
def get_loss_dict(self, outputs, batch) -> Dict[str, torch.tensor]:
"""Computes and returns the losses."""
"""Computes and returns the losses dict."""
def get_aggregated_loss_dict(self, loss_dict):
"""Computes the aggregated loss from the loss_dict and the coefficients specified."""
aggregated_loss_dict = {}
for loss_name, loss_value in loss_dict.items():
assert loss_name in self.loss_coefficients, f"{loss_name} no in self.loss_coefficients"
loss_coefficient = self.loss_coefficients[loss_name]
aggregated_loss_dict[loss_name] = loss_coefficient * loss_value
aggregated_loss_dict["aggregated_loss"] = sum(loss_dict.values())
return aggregated_loss_dict
def get_metrics_dict(self, outputs, batch) -> Dict[str, torch.tensor]:
"""Compute and obtain metrics and coefficients."""
# pylint: disable=unused-argument
# pylint: disable=no-self-use
return {}
@torch.no_grad()
def get_outputs_for_camera_ray_bundle(self, camera_ray_bundle: RayBundle):