class_docstr: Dynamic Instance Interactive Head for `Sparse R-CNN: End-to-End Object
  Detection with Learnable Proposals <https://arxiv.org/abs/2011.12450>`_
  
  Args:
      num_classes (int): Number of class in dataset.
          Defaults to 80.
      num_ffn_fcs (int): The number of fully-connected
          layers in FFNs. Defaults to 2.
      num_heads (int): The hidden dimension of FFNs.
          Defaults to 8.
      num_cls_fcs (int): The number of fully-connected
          layers in classification subnet. Defaults to 1.
      num_reg_fcs (int): The number of fully-connected
          layers in regression subnet. Defaults to 3.
      feedforward_channels (int): The hidden dimension
          of FFNs. Defaults to 2048
      in_channels (int): Hidden_channels of MultiheadAttention.
          Defaults to 256.
      dropout (float): Probability of drop the channel.
          Defaults to 0.0
      ffn_act_cfg (:obj:`ConfigDict` or dict): The activation config
          for FFNs.
      dynamic_conv_cfg (:obj:`ConfigDict` or dict): The convolution
          config for DynamicConv.
      loss_iou (:obj:`ConfigDict` or dict): The config for iou or
          giou loss.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or \
          dict]): Initialization config dict. Defaults to None.
docstr: Calculate the loss based on the features extracted by the DIIHead.
  
  Args:
      cls_score (Tensor): Classification prediction
          results of all class, has shape
          (batch_size * num_proposals_single_image, num_classes)
      bbox_pred (Tensor): Regression prediction results, has shape
          (batch_size * num_proposals_single_image, 4), the last
          dimension 4 represents [tl_x, tl_y, br_x, br_y].
      sampling_results (List[obj:SamplingResult]): Assign results of
          all images in a batch after sampling.
      rcnn_train_cfg (obj:ConfigDict): `train_cfg` of RCNN.
      imgs_whwh (Tensor): imgs_whwh (Tensor): Tensor with                shape (batch_size, num_proposals, 4), the last
          dimension means
          [img_width,img_height, img_width, img_height].
      concat (bool): Whether to concatenate the results of all
          the images in a single batch. Defaults to True.
      reduction_override (str, optional): The reduction
          method used to override the original reduction
          method of the loss. Options are "none",
          "mean" and "sum". Defaults to None.
  
  Returns:
      dict: A dictionary of loss and targets components.
      The targets are only used for cascade rcnn.
docstr_tok: ["Calculate","the","loss","based","on","the","features","extracted","by","the","DIIHead",".","Args",":","cls_score","(","Tensor",")",":","Classification","prediction","results","of","all","class",",","has","shape","(","batch_size","*","num_proposals_single_image",",","num_classes",")","bbox_pred","(","Tensor",")",":","Regression","prediction","results",",","has","shape","(","batch_size","*","num_proposals_single_image",",","4",")",",","the","last","dimension","4","represents","[","tl_x",",","tl_y",",","br_x",",","br_y","]",".","sampling_results","(","List","[","obj",":","SamplingResult","]",")",":","Assign","results","of","all","images","in","a","batch","after","sampling",".","rcnn_train_cfg","(","obj",":","ConfigDict",")",":","`","train_cfg","`","of","RCNN",".","imgs_whwh","(","Tensor",")",":","imgs_whwh","(","Tensor",")",":","Tensor","with","shape","(","batch_size",",","num_proposals",",","4",")",",","the","last","dimension","means","[","img_width",",","img_height",",","img_width",",","img_height","]",".","concat","(","bool",")",":","Whether","to","concatenate","the","results","of","all","the","images","in","a","single","batch",".","Defaults","to","True",".","reduction_override","(","str",",","optional",")",":","The","reduction","method","used","to","override","the","original","reduction","method","of","the","loss",".","Options","are","``","none","''",",","``","mean","''","and","``","sum","''",".","Defaults","to","None",".","Returns",":","dict",":","A","dictionary","of","loss","and","targets","components",".","The","targets","are","only","used","for","cascade","rcnn","."]
code: def loss_and_target(
      self,
      cls_score: Tensor,
      bbox_pred: Tensor,
      sampling_results: List[SamplingResult],
      rcnn_train_cfg: ConfigType,
      imgs_whwh: Tensor,
      concat: bool = True,
      reduction_override: str = None,
  ) -> dict:
      """Calculate the loss based on the features extracted by the DIIHead.
  
          Args:
              cls_score (Tensor): Classification prediction
                  results of all class, has shape
                  (batch_size * num_proposals_single_image, num_classes)
              bbox_pred (Tensor): Regression prediction results, has shape
                  (batch_size * num_proposals_single_image, 4), the last
                  dimension 4 represents [tl_x, tl_y, br_x, br_y].
              sampling_results (List[obj:SamplingResult]): Assign results of
                  all images in a batch after sampling.
              rcnn_train_cfg (obj:ConfigDict): `train_cfg` of RCNN.
              imgs_whwh (Tensor): imgs_whwh (Tensor): Tensor with\
                  shape (batch_size, num_proposals, 4), the last
                  dimension means
                  [img_width,img_height, img_width, img_height].
              concat (bool): Whether to concatenate the results of all
                  the images in a single batch. Defaults to True.
              reduction_override (str, optional): The reduction
                  method used to override the original reduction
                  method of the loss. Options are "none",
                  "mean" and "sum". Defaults to None.
  
          Returns:
              dict: A dictionary of loss and targets components.
              The targets are only used for cascade rcnn.
          """
      cls_reg_targets = self.get_targets(
          sampling_results=sampling_results,
          rcnn_train_cfg=rcnn_train_cfg,
          concat=concat,
      )
      (
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
      ) = cls_reg_targets
  
      losses = dict()
      bg_class_ind = self.num_classes
      # note in spare rcnn num_gt == num_pos
      pos_inds = (labels >= 0) & (labels < bg_class_ind)
      num_pos = pos_inds.sum().float()
      avg_factor = reduce_mean(num_pos)
      if cls_score is not None:
          if cls_score.numel() > 0:
              losses["loss_cls"] = self.loss_cls(
                  cls_score,
                  labels,
                  label_weights,
                  avg_factor=avg_factor,
                  reduction_override=reduction_override,
              )
              losses["pos_acc"] = accuracy(
                  cls_score[pos_inds], labels[pos_inds]
              )
      if bbox_pred is not None:
          # 0~self.num_classes-1 are FG, self.num_classes is BG
          # do not perform bounding box regression for BG anymore.
          if pos_inds.any():
              pos_bbox_pred = bbox_pred.reshape(bbox_pred.size(0), 4)[
                  pos_inds.type(torch.bool)
              ]
              imgs_whwh = imgs_whwh.reshape(bbox_pred.size(0), 4)[
                  pos_inds.type(torch.bool)
              ]
              losses["loss_bbox"] = self.loss_bbox(
                  pos_bbox_pred / imgs_whwh,
                  bbox_targets[pos_inds.type(torch.bool)] / imgs_whwh,
                  bbox_weights[pos_inds.type(torch.bool)],
                  avg_factor=avg_factor,
              )
              losses["loss_iou"] = self.loss_iou(
                  pos_bbox_pred,
                  bbox_targets[pos_inds.type(torch.bool)],
                  bbox_weights[pos_inds.type(torch.bool)],
                  avg_factor=avg_factor,
              )
          else:
              losses["loss_bbox"] = bbox_pred.sum() * 0
              losses["loss_iou"] = bbox_pred.sum() * 0
      return dict(loss_bbox=losses, bbox_targets=cls_reg_targets)
  
imports: from typing import List
  import torch
  import torch.nn
  from mmcv.cnn import build_activation_layer, build_norm_layer
  from mmcv.cnn.bricks.transformer import FFN, MultiheadAttention
  from mmengine.config import ConfigDict
  from mmengine.model import bias_init_with_prob
  from torch import Tensor
  from mmdet.models.losses import accuracy
  from mmdet.models.task_modules import SamplingResult
  from mmdet.models.utils import multi_apply
  from mmdet.registry import MODELS
  from mmdet.utils import ConfigType, OptConfigType, reduce_mean
  from .bbox_head import BBoxHead
label_desc: Use image node_id 4 for calling the DIIHead obj's underlying member method code with example usage: obj.loss_and_target(cls_score, bbox_pred, sampling_results, rcnn_train_cfg, imgs_whwh, concat, reduction_override) and returns: dict