class_docstr: Base model for object detection with matching.
class_docstr_tok: ["Base","model","for","object","detection","with","matching","."]
docstr: Bounding box losses: L1 regression loss and GIoU loss.
  
  Args:
    outputs: dict; Model predictions. For the purpose of this loss, outputs
      must have key 'pred_boxes'. outputs['pred_boxes'] is a nd-array of the
      predicted box coordinates in (cx, cy, w, h) format. This nd-array has
      shape [batch-size, num-boxes, 4].
    batch: dict; that has 'inputs', 'batch_mask' and, 'label' (ground truth).
      batch['label'] is a dict. For the purpose of this loss, batch['label']
      must have key 'boxes', which the value has the same format as
      outputs['pred_boxes']. Additionally in batch['label'], key 'labels' is
      required that should match the specs defined in the member function
      `labels_losses_and_metrics`. This is to decide which boxes are invalid
      and need to be ignored. Invalid boxes have class label 0. If
      batch['batch_mask'] is provided it is used to weight the loss for
      different images in the current batch of examples.
    indices: list[tuple[nd-array, nd-array]]; Matcher output which conveys
      source to target pairing of objects.
  
  Returns:
    loss: dict with keys 'loss_bbox', 'loss_giou'. These are
      losses averaged over the batch. Therefore they have shape [].
    metrics: dict with keys 'loss_bbox' and 'loss_giou`.
      These are metrics psumed over the batch. Therefore they have shape [].
docstr_tok: ["Bounding","box","losses",":","L1","regression","loss","and","GIoU","loss",".","Args",":","outputs",":","dict",";","Model","predictions",".","For","the","purpose","of","this","loss",",","outputs","must","have","key","'pred_boxes","'",".","outputs","[","'pred_boxes","'","]","is","a","nd-array","of","the","predicted","box","coordinates","in","(","cx",",","cy",",","w",",","h",")","format",".","This","nd-array","has","shape","[","batch-size",",","num-boxes",",","4","]",".","batch",":","dict",";","that","has","'inputs","'",",","'batch_mask","'","and",",","'label","'","(","ground","truth",")",".","batch","[","'label","'","]","is","a","dict",".","For","the","purpose","of","this","loss",",","batch","[","'label","'","]","must","have","key","'boxes","'",",","which","the","value","has","the","same","format","as","outputs","[","'pred_boxes","'","]",".","Additionally","in","batch","[","'label","'","]",",","key","'labels","'","is","required","that","should","match","the","specs","defined","in","the","member","function","`","labels_losses_and_metrics","`",".","This","is","to","decide","which","boxes","are","invalid","and","need","to","be","ignored",".","Invalid","boxes","have","class","label","0",".","If","batch","[","'batch_mask","'","]","is","provided","it","is","used","to","weight","the","loss","for","different","images","in","the","current","batch","of","examples",".","indices",":","list","[","tuple","[","nd-array",",","nd-array","]","]",";","Matcher","output","which","conveys","source","to","target","pairing","of","objects",".","Returns",":","loss",":","dict","with","keys","'loss_bbox","'",",","'loss_giou","'",".","These","are","losses","averaged","over","the","batch",".","Therefore","they","have","shape","[","]",".","metrics",":","dict","with","keys","'loss_bbox","'","and","'loss_giou","`",".","These","are","metrics","psumed","over","the","batch",".","Therefore","they","have","shape","[","]","."]
code: def boxes_losses_and_metrics(
      self, outputs: ArrayDict, batch: ArrayDict, indices: jnp.ndarray
  ) -> Tuple[ArrayDict, MetricsDict]:
      """Bounding box losses: L1 regression loss and GIoU loss.
  
      Args:
        outputs: dict; Model predictions. For the purpose of this loss, outputs
          must have key 'pred_boxes'. outputs['pred_boxes'] is a nd-array of the
          predicted box coordinates in (cx, cy, w, h) format. This nd-array has
          shape [batch-size, num-boxes, 4].
        batch: dict; that has 'inputs', 'batch_mask' and, 'label' (ground truth).
          batch['label'] is a dict. For the purpose of this loss, batch['label']
          must have key 'boxes', which the value has the same format as
          outputs['pred_boxes']. Additionally in batch['label'], key 'labels' is
          required that should match the specs defined in the member function
          `labels_losses_and_metrics`. This is to decide which boxes are invalid
          and need to be ignored. Invalid boxes have class label 0. If
          batch['batch_mask'] is provided it is used to weight the loss for
          different images in the current batch of examples.
        indices: list[tuple[nd-array, nd-array]]; Matcher output which conveys
          source to target pairing of objects.
  
      Returns:
        loss: dict with keys 'loss_bbox', 'loss_giou'. These are
          losses averaged over the batch. Therefore they have shape [].
        metrics: dict with keys 'loss_bbox' and 'loss_giou`.
          These are metrics psumed over the batch. Therefore they have shape [].
      """
      assert "pred_boxes" in outputs
      assert "label" in batch
  
      targets = batch["label"]
      assert "boxes" in targets
      assert "labels" in targets
      losses, metrics = {}, {}
      batch_weights = batch.get("batch_mask")
  
      src_boxes = model_utils.simple_gather(
          outputs["pred_boxes"], indices[:, 0]
      )
      tgt_boxes = model_utils.simple_gather(
          targets["boxes"], indices[:, 1]
      )
      tgt_labels = targets["labels"]
  
      # Some of the boxes are padding. We want to discount them from the loss.
      n_labels_per_instance = jnp.sum(tgt_labels[..., 1:], axis=-1)
      tgt_not_padding = n_labels_per_instance > 0  # [B, M]
  
      # tgt_is_padding has shape [batch-size, num-boxes].
      # Align this with the model predictions using simple_gather.
      tgt_not_padding = model_utils.simple_gather(
          tgt_not_padding, indices[:, 1]
      )
  
      src_boxes_xyxy = box_utils.box_cxcywh_to_xyxy(src_boxes)
      tgt_boxes_xyxy = box_utils.box_cxcywh_to_xyxy(tgt_boxes)
      unnormalized_loss_giou = 1 - box_utils.generalized_box_iou(
          src_boxes_xyxy, tgt_boxes_xyxy, all_pairs=False
      )
  
      unnormalized_loss_bbox = model_utils.weighted_box_l1_loss(
          src_boxes_xyxy,
          tgt_boxes_xyxy,
          weights=batch_weights,
      ).sum(axis=2)
  
      denom = tgt_not_padding.sum(axis=1)
      if batch_weights is not None:
          denom *= batch_weights
          unnormalized_loss_giou = model_utils.apply_weights(
              unnormalized_loss_giou, batch_weights
          )
  
      unnormalized_loss_bbox *= tgt_not_padding
      unnormalized_loss_giou *= tgt_not_padding
  
      if self.config.normalization != "per_example":
          # Normalize by number of boxes in batch.
          denom = jnp.maximum(
              jax.lax.pmean(denom.sum(), axis_name="batch"), 1
          )
          normalized_loss_bbox = unnormalized_loss_bbox.sum() / denom
          normalized_loss_giou = unnormalized_loss_giou.sum() / denom
      else:  # Normalize by number of boxes in image.
          denom = jnp.maximum(denom, 1.0)
          normalized_loss_bbox = (
              unnormalized_loss_bbox.sum(axis=1) / denom
          ).mean()
          normalized_loss_giou = (
              unnormalized_loss_giou.sum(axis=1) / denom
          ).mean()
  
      losses["loss_bbox"] = normalized_loss_bbox
      metrics["loss_bbox"] = (normalized_loss_bbox, 1.0)
      losses["loss_giou"] = normalized_loss_giou
      metrics["loss_giou"] = (normalized_loss_giou, 1.0)
  
      # Sum metrics and normalizers over all replicas.
      for k, v in metrics.items():
          metrics[k] = model_utils.psum_metric_normalizer(
              v
          )  # pytype: disable=wrong-arg-types  # jax-ndarray
      return losses, metrics
  
imports: import abc
  import functools
  from typing import Any, Callable, Dict, Optional, Tuple
  import jax
  import jax.numpy
  import ml_collections
  from scenic.model_lib import matchers
  from scenic.model_lib.base_models import base_model
  from scenic.model_lib.base_models import box_utils
  from scenic.model_lib.base_models import model_utils
  from scenic.projects.owl_vit import losses
label_desc: Use image node_id 4 for calling the ObjectDetectionModel obj's underlying member method code with example usage: obj.boxes_losses_and_metrics(outputs, batch, indices) and returns: losses,  metrics