class_docstr: Test ObjectDetectionWithMatchingModel.
class_docstr_tok: ["Test","ObjectDetectionWithMatchingModel","."]
docstr: Test loss_boxes by checking its output's dictionary format.
docstr_tok: ["Test","loss_boxes","by","checking","its","output","'s","dictionary","format","."]
code: def test_boxes_losses_and_metrics(self, num_boxes):
      """Test loss_boxes by checking its output's dictionary format."""
      model = MyObjectDetectionWithMatchingModel()
      outputs, batch, indices = fake_model_outputs_batch(num_boxes)
  
      # Test loss function in the pmapped setup:
      boxes_lm_pmapped = jax.pmap(
          lambda o, b: model.boxes_losses_and_metrics(o, b, indices),
          axis_name="batch",
      )
  
      outputs_replicate, batch_replicate = (
          jax_utils.replicate(outputs),
          jax_utils.replicate(batch),
      )
  
      losses, metrics = boxes_lm_pmapped(
          outputs_replicate, batch_replicate
      )
      losses = jax_utils.unreplicate(losses)
      metrics = jax_utils.unreplicate(metrics)
  
      self.assertSameElements(["loss_bbox", "loss_giou"], losses.keys())
      self.is_valid_loss(losses["loss_bbox"])
      self.is_valid_loss(losses["loss_giou"])
  
      self.assertSameElements(
          ["loss_bbox", "loss_giou"], metrics.keys()
      )
      for i in range(2):  # metric and its normalizer
          self.is_valid(metrics["loss_bbox"][i])
          self.is_valid(metrics["loss_giou"][i])
  
      # Check whether hard-wiring boxes to match and hard-wiring indices to align
      # gives loss_bbox = 0.0 and loss_giou = 1.0:
      outputs["pred_boxes"] = batch["label"]["boxes"]
      indices = jnp.stack([indices[:, 0, :], indices[:, 0, :]], axis=1)
      outputs_replicate, batch_replicate = (
          jax_utils.replicate(outputs),
          jax_utils.replicate(batch),
      )
      boxes_lm_pmapped = jax.pmap(
          lambda o, b: model.boxes_losses_and_metrics(o, b, indices),
          axis_name="batch",
      )
      losses, metrics = boxes_lm_pmapped(
          outputs_replicate, batch_replicate
      )
      losses = jax_utils.unreplicate(losses)
      metrics = jax_utils.unreplicate(metrics)
  
      self.assertAlmostEqual(losses["loss_bbox"], 0.0, places=4)
      self.assertAlmostEqual(losses["loss_giou"], 0.0, places=4)
  
      self.assertAlmostEqual(
          metrics["loss_bbox"][0] / metrics["loss_bbox"][1],
          0.0,
          places=5,
      )
      self.assertAlmostEqual(
          metrics["loss_giou"][0] / metrics["loss_giou"][1],
          0.0,
          places=4,
      )
  
imports: from absl.testing import absltest
  from absl.testing import parameterized
  from flax import jax_utils
  import jax
  import jax.numpy
  import ml_collections
  import numpy
  from scenic.projects.baselines.detr import detr_base_model
label_desc: Use image node_id 4 for calling the TestObjectDetectionWithMatchingModel obj's underlying member method code with example usage: obj.test_boxes_losses_and_metrics(num_boxes) without return types