class_docstr: Base model for object detection with matching.
class_docstr_tok: ["Base","model","for","object","detection","with","matching","."]
docstr: Loss and metrics function.
docstr_tok: ["Loss","and","metrics","function","."]
code: def loss_function(  # pytype: disable=signature-mismatch  # overriding-parameter-count-checks
      self,
      outputs: ArrayDict,
      batch: ArrayDict,
      task: str,
      model_params: Optional[jnp.ndarray] = None,
  ) -> Tuple[jnp.ndarray, MetricsDict]:
      """Loss and metrics function."""
      if task == "layout_denoise":
          total_loss, metrics = self.label_losses_and_metrics(
              outputs=outputs, batch=batch
          )
  
          aux_outputs = outputs.get("aux_outputs", [])
          for i, aux_outputs in enumerate(aux_outputs):
              aux_loss_ce, aux_metrics = self.label_losses_and_metrics(
                  outputs=aux_outputs, batch=batch
              )
              # add metrics for aux outputs
              metrics.update(
                  {k + f"_aux_{i}": v for k, v in aux_metrics.items()}
              )
              total_loss += aux_loss_ce
  
          if self.config.get("l2_decay_factor") is not None:
              l2_loss = model_utils.l2_regularization(model_params)
              metrics["l2_loss"] = (l2_loss, 1)
              total_loss = (
                  total_loss
                  + 0.5 * self.config.l2_decay_factor * l2_loss
              )
  
          # Process metrics dictionary to generate final unnormalized metrics
          metrics["minibatch_object_detection_loss"] = (total_loss, 1)
          return (
              total_loss,
              metrics,
          )  # pytype: disable=bad-return-type  # jax-ndarray
      else:
          raise ValueError("Unsupported task %s" % task)
  
file_path: scenic/scenic/projects/layout_denoise/base_model.py
imports: from typing import Any, Dict, Optional, Tuple
  from absl import logging
  import jax
  import jax.numpy
  import ml_collections
  from scenic.model_lib.base_models import base_model
  from scenic.model_lib.base_models import model_utils
label_desc: Use image node_id 3 for calling the LayoutDenoiseBaseModel obj's underlying member method code with example usage: obj.loss_function(outputs, batch, task, model_params) and returns: total_loss,  metrics