class_docstr: Base model for object detection with matching.
class_docstr_tok: ["Base","model","for","object","detection","with","matching","."]
docstr: Computes the unnormalized per-example classification loss and denom.
docstr_tok: ["Computes","the","unnormalized","per-example","classification","loss","and","denom","."]
code: def _compute_per_example_class_loss(
      self,
      *,
      tgt_labels: jnp.ndarray,
      src_logits: jnp.ndarray,
      batch_weights: Optional[jnp.ndarray],
  ) -> Tuple[jnp.ndarray, jnp.ndarray]:
      """Computes the unnormalized per-example classification loss and denom."""
      loss_kwargs = {
          "weights": batch_weights,
      }
      if self.config.focal_loss:
          loss_kwargs["gamma"] = self.config.focal_gamma
          loss_kwargs["alpha"] = self.config.focal_alpha
          loss_fn = model_utils.focal_sigmoid_cross_entropy
      else:
          loss_fn = (
              model_utils.weighted_unnormalized_sigmoid_cross_entropy
          )
  
      # Don't compute loss for the padding index.
      unnormalized_loss_class = loss_fn(
          src_logits[..., 1:], tgt_labels[..., 1:], **loss_kwargs
      )
      # Sum losses over all classes. The unnormalized_loss_class is of shape
      # [bs, 1 + max_num_boxes, num_classes], and after the next line, it becomes
      # [bs, 1 + max_num_boxes].
      unnormalized_loss_class = jnp.sum(
          unnormalized_loss_class, axis=-1
      )
      # Normalize by number of "true" labels after removing padding label.
      denom = tgt_labels[..., 1:].sum(
          axis=[1, 2]
      )  # pytype: disable=wrong-arg-types  # jax-ndarray
  
      if batch_weights is not None:
          denom *= batch_weights
  
      return unnormalized_loss_class, denom
  
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 6 for calling the BaseModelWithMatching obj's underlying member method code with example usage: obj._compute_per_example_class_loss() and returns: unnormalized_loss_class,  denom