code: def seesaw_ce_loss(
      cls_score: Tensor,
      labels: Tensor,
      label_weights: Tensor,
      cum_samples: Tensor,
      num_classes: int,
      p: float,
      q: float,
      eps: float,
      reduction: str = "mean",
      avg_factor: Optional[int] = None,
  ) -> Tensor:
      """Calculate the Seesaw CrossEntropy loss.
  
      Args:
          cls_score (Tensor): The prediction with shape (N, C),
               C is the number of classes.
          labels (Tensor): The learning label of the prediction.
          label_weights (Tensor): Sample-wise loss weight.
          cum_samples (Tensor): Cumulative samples for each category.
          num_classes (int): The number of classes.
          p (float): The ``p`` in the mitigation factor.
          q (float): The ``q`` in the compenstation factor.
          eps (float): The minimal value of divisor to smooth
               the computation of compensation factor
          reduction (str, optional): The method used to reduce the loss.
          avg_factor (int, optional): Average factor that is used to average
              the loss. Defaults to None.
  
      Returns:
          Tensor: The calculated loss
      """
      assert cls_score.size(-1) == num_classes
      assert len(cum_samples) == num_classes
  
      onehot_labels = F.one_hot(labels, num_classes)
      seesaw_weights = cls_score.new_ones(onehot_labels.size())
  
      # mitigation factor
      if p > 0:
          sample_ratio_matrix = cum_samples[None, :].clamp(
              min=1
          ) / cum_samples[:, None].clamp(min=1)
          index = (sample_ratio_matrix < 1.0).float()
          sample_weights = sample_ratio_matrix.pow(p) * index + (
              1 - index
          )
          mitigation_factor = sample_weights[labels.long(), :]
          seesaw_weights = seesaw_weights * mitigation_factor
  
      # compensation factor
      if q > 0:
          scores = F.softmax(cls_score.detach(), dim=1)
          self_scores = scores[
              torch.arange(0, len(scores)).to(scores.device).long(),
              labels.long(),
          ]
          score_matrix = scores / self_scores[:, None].clamp(min=eps)
          index = (score_matrix > 1.0).float()
          compensation_factor = score_matrix.pow(q) * index + (
              1 - index
          )
          seesaw_weights = seesaw_weights * compensation_factor
  
      cls_score = cls_score + (
          seesaw_weights.log() * (1 - onehot_labels)
      )
  
      loss = F.cross_entropy(
          cls_score, labels, weight=None, reduction="none"
      )
  
      if label_weights is not None:
          label_weights = label_weights.float()
      loss = weight_reduce_loss(
          loss,
          weight=label_weights,
          reduction=reduction,
          avg_factor=avg_factor,
      )
      return loss
  
file_path: mmdetection/mmdet/models/losses/seesaw_loss.py
imports: from typing import Dict, Optional, Tuple, Union
  import torch
  import torch.nn
  import torch.nn.functional
  from torch import Tensor
  from mmdet.registry import MODELS
  from .accuracy import accuracy
  from .cross_entropy_loss import cross_entropy
  from .utils import weight_reduce_loss
label_desc: Use image node_id 1 for calling a global function with example usage: seesaw_ce_loss(cls_score, labels, label_weights, cum_samples, num_classes, p, q, eps, reduction, avg_factor) and returns: loss