class_docstr: The RoIHead for `Sparse R-CNN: End-to-End Object Detection with
  Learnable Proposals <https://arxiv.org/abs/2011.12450>`_
  and `Instances as Queries <http://arxiv.org/abs/2105.01928>`_
  
  Args:
      num_stages (int): Number of stage whole iterative process.
          Defaults to 6.
      stage_loss_weights (Tuple[float]): The loss
          weight of each stage. By default all stages have
          the same weight 1.
      bbox_roi_extractor (:obj:`ConfigDict` or dict): Config of box
          roi extractor.
      mask_roi_extractor (:obj:`ConfigDict` or dict): Config of mask
          roi extractor.
      bbox_head (:obj:`ConfigDict` or dict): Config of box head.
      mask_head (:obj:`ConfigDict` or dict): Config of mask head.
      train_cfg (:obj:`ConfigDict` or dict, Optional): Configuration
          information in train stage. Defaults to None.
      test_cfg (:obj:`ConfigDict` or dict, Optional): Configuration
          information in test stage. Defaults to None.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or \
          dict]): Initialization config dict. Defaults to None.
docstr: Box head forward function used in both training and testing. Returns
  all regression, classification results and a intermediate feature.
  
  Args:
      stage (int): The current stage in iterative process.
      x (tuple[Tensor]): List of multi-level img features.
      rois (Tensor): RoIs with the shape (n, 5) where the first
          column indicates batch id of each RoI.
          Each dimension means (img_index, x1, y1, x2, y2).
      object_feats (Tensor): The object feature extracted from
          the previous stage.
      batch_img_metas (list[dict]): Meta information of each image.
  
  Returns:
      dict[str, Tensor]: a dictionary of bbox head outputs,
      Containing the following results:
  
      - cls_score (Tensor): The score of each class, has
        shape (batch_size, num_proposals, num_classes)
        when use focal loss or
        (batch_size, num_proposals, num_classes+1)
        otherwise.
      - decoded_bboxes (Tensor): The regression results
        with shape (batch_size, num_proposal, 4).
        The last dimension 4 represents
        [tl_x, tl_y, br_x, br_y].
      - object_feats (Tensor): The object feature extracted
        from current stage
      - detached_cls_scores (list[Tensor]): The detached
        classification results, length is batch_size, and
        each tensor has shape (num_proposal, num_classes).
      - detached_proposals (list[tensor]): The detached
        regression results, length is batch_size, and each
        tensor has shape (num_proposal, 4). The last
        dimension 4 represents [tl_x, tl_y, br_x, br_y].
docstr_tok: ["Box","head","forward","function","used","in","both","training","and","testing",".","Returns","all","regression",",","classification","results","and","a","intermediate","feature",".","Args",":","stage","(","int",")",":","The","current","stage","in","iterative","process",".","x","(","tuple","[","Tensor","]",")",":","List","of","multi-level","img","features",".","rois","(","Tensor",")",":","RoIs","with","the","shape","(","n",",","5",")","where","the","first","column","indicates","batch","id","of","each","RoI",".","Each","dimension","means","(","img_index",",","x1",",","y1",",","x2",",","y2",")",".","object_feats","(","Tensor",")",":","The","object","feature","extracted","from","the","previous","stage",".","batch_img_metas","(","list","[","dict","]",")",":","Meta","information","of","each","image",".","Returns",":","dict","[","str",",","Tensor","]",":","a","dictionary","of","bbox","head","outputs",",","Containing","the","following","results",":","-","cls_score","(","Tensor",")",":","The","score","of","each","class",",","has","shape","(","batch_size",",","num_proposals",",","num_classes",")","when","use","focal","loss","or","(","batch_size",",","num_proposals",",","num_classes+1",")","otherwise",".","-","decoded_bboxes","(","Tensor",")",":","The","regression","results","with","shape","(","batch_size",",","num_proposal",",","4",")",".","The","last","dimension","4","represents","[","tl_x",",","tl_y",",","br_x",",","br_y","]",".","-","object_feats","(","Tensor",")",":","The","object","feature","extracted","from","current","stage","-","detached_cls_scores","(","list","[","Tensor","]",")",":","The","detached","classification","results",",","length","is","batch_size",",","and","each","tensor","has","shape","(","num_proposal",",","num_classes",")",".","-","detached_proposals","(","list","[","tensor","]",")",":","The","detached","regression","results",",","length","is","batch_size",",","and","each","tensor","has","shape","(","num_proposal",",","4",")",".","The","last","dimension","4","represents","[","tl_x",",","tl_y",",","br_x",",","br_y","]","."]
code: def _bbox_forward(
      self,
      stage: int,
      x: Tuple[Tensor],
      rois: Tensor,
      object_feats: Tensor,
      batch_img_metas: List[dict],
  ) -> dict:
      """Box head forward function used in both training and testing. Returns
      all regression, classification results and a intermediate feature.
  
      Args:
          stage (int): The current stage in iterative process.
          x (tuple[Tensor]): List of multi-level img features.
          rois (Tensor): RoIs with the shape (n, 5) where the first
              column indicates batch id of each RoI.
              Each dimension means (img_index, x1, y1, x2, y2).
          object_feats (Tensor): The object feature extracted from
              the previous stage.
          batch_img_metas (list[dict]): Meta information of each image.
  
      Returns:
          dict[str, Tensor]: a dictionary of bbox head outputs,
          Containing the following results:
  
          - cls_score (Tensor): The score of each class, has
            shape (batch_size, num_proposals, num_classes)
            when use focal loss or
            (batch_size, num_proposals, num_classes+1)
            otherwise.
          - decoded_bboxes (Tensor): The regression results
            with shape (batch_size, num_proposal, 4).
            The last dimension 4 represents
            [tl_x, tl_y, br_x, br_y].
          - object_feats (Tensor): The object feature extracted
            from current stage
          - detached_cls_scores (list[Tensor]): The detached
            classification results, length is batch_size, and
            each tensor has shape (num_proposal, num_classes).
          - detached_proposals (list[tensor]): The detached
            regression results, length is batch_size, and each
            tensor has shape (num_proposal, 4). The last
            dimension 4 represents [tl_x, tl_y, br_x, br_y].
      """
      num_imgs = len(batch_img_metas)
      bbox_roi_extractor = self.bbox_roi_extractor[stage]
      bbox_head = self.bbox_head[stage]
      bbox_feats = bbox_roi_extractor(
          x[: bbox_roi_extractor.num_inputs], rois
      )
      cls_score, bbox_pred, object_feats, attn_feats = bbox_head(
          bbox_feats, object_feats
      )
  
      fake_bbox_results = dict(
          rois=rois,
          bbox_targets=(
              rois.new_zeros(len(rois), dtype=torch.long),
              None,
          ),
          bbox_pred=bbox_pred.view(-1, bbox_pred.size(-1)),
          cls_score=cls_score.view(-1, cls_score.size(-1)),
      )
      fake_sampling_results = [
          InstanceData(pos_is_gt=rois.new_zeros(object_feats.size(1)))
          for _ in range(len(batch_img_metas))
      ]
  
      results_list = bbox_head.refine_bboxes(
          sampling_results=fake_sampling_results,
          bbox_results=fake_bbox_results,
          batch_img_metas=batch_img_metas,
      )
      proposal_list = [res.bboxes for res in results_list]
      bbox_results = dict(
          cls_score=cls_score,
          decoded_bboxes=torch.cat(proposal_list),
          object_feats=object_feats,
          attn_feats=attn_feats,
          # detach then use it in label assign
          detached_cls_scores=[
              cls_score[i].detach() for i in range(num_imgs)
          ],
          detached_proposals=[item.detach() for item in proposal_list],
      )
  
      return bbox_results
  
imports: from typing import List, Tuple
  import torch
  from mmengine.config import ConfigDict
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.models.task_modules.samplers import PseudoSampler
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox2roi
  from mmdet.utils import ConfigType, InstanceList, OptConfigType
  from ..utils.misc import empty_instances, unpack_gt_instances
  from .cascade_roi_head import CascadeRoIHead
label_desc: Use image node_id 3 for calling the SparseRoIHead obj's underlying member method code with example usage: obj._bbox_forward(stage, x, rois, object_feats, batch_img_metas) and returns: bbox_results