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: Perform forward propagation and loss calculation of the bbox head on
  the features of the upstream network.
  
  Args:
      stage (int): The current stage in iterative process.
      x (tuple[Tensor]): List of multi-level img features.
      results_list (List[:obj:`InstanceData`]) : List of region
          proposals.
      object_feats (Tensor): The object feature extracted from
          the previous stage.
      batch_img_metas (list[dict]): Meta information of each image.
      batch_gt_instances (list[:obj:`InstanceData`]): Batch of
          gt_instance. It usually includes ``bboxes``, ``labels``, and
          ``masks`` attributes.
  
  Returns:
      dict[str, Tensor]: Usually returns a dictionary with keys:
  
      - `cls_score` (Tensor): Classification scores.
      - `bbox_pred` (Tensor): Box energies / deltas.
      - `bbox_feats` (Tensor): Extract bbox RoI features.
      - `loss_bbox` (dict): A dictionary of bbox loss components.
docstr_tok: ["Perform","forward","propagation","and","loss","calculation","of","the","bbox","head","on","the","features","of","the","upstream","network",".","Args",":","stage","(","int",")",":","The","current","stage","in","iterative","process",".","x","(","tuple","[","Tensor","]",")",":","List","of","multi-level","img","features",".","results_list","(","List","[",":","obj",":","`","InstanceData","`","]",")",":","List","of","region","proposals",".","object_feats","(","Tensor",")",":","The","object","feature","extracted","from","the","previous","stage",".","batch_img_metas","(","list","[","dict","]",")",":","Meta","information","of","each","image",".","batch_gt_instances","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Batch","of","gt_instance",".","It","usually","includes","``","bboxes","``",",","``","labels","``",",","and","``","masks","``","attributes",".","Returns",":","dict","[","str",",","Tensor","]",":","Usually","returns","a","dictionary","with","keys",":","-","`","cls_score","`","(","Tensor",")",":","Classification","scores",".","-","`","bbox_pred","`","(","Tensor",")",":","Box","energies","\/","deltas",".","-","`","bbox_feats","`","(","Tensor",")",":","Extract","bbox","RoI","features",".","-","`","loss_bbox","`","(","dict",")",":","A","dictionary","of","bbox","loss","components","."]
code: def bbox_loss(
      self,
      stage: int,
      x: Tuple[Tensor],
      results_list: InstanceList,
      object_feats: Tensor,
      batch_img_metas: List[dict],
      batch_gt_instances: InstanceList,
  ) -> dict:
      """Perform forward propagation and loss calculation of the bbox head on
      the features of the upstream network.
  
      Args:
          stage (int): The current stage in iterative process.
          x (tuple[Tensor]): List of multi-level img features.
          results_list (List[:obj:`InstanceData`]) : List of region
              proposals.
          object_feats (Tensor): The object feature extracted from
              the previous stage.
          batch_img_metas (list[dict]): Meta information of each image.
          batch_gt_instances (list[:obj:`InstanceData`]): Batch of
              gt_instance. It usually includes ``bboxes``, ``labels``, and
              ``masks`` attributes.
  
      Returns:
          dict[str, Tensor]: Usually returns a dictionary with keys:
  
          - `cls_score` (Tensor): Classification scores.
          - `bbox_pred` (Tensor): Box energies / deltas.
          - `bbox_feats` (Tensor): Extract bbox RoI features.
          - `loss_bbox` (dict): A dictionary of bbox loss components.
      """
      proposal_list = [res.bboxes for res in results_list]
      rois = bbox2roi(proposal_list)
      bbox_results = self._bbox_forward(
          stage, x, rois, object_feats, batch_img_metas
      )
      imgs_whwh = torch.cat(
          [res.imgs_whwh[None, ...] for res in results_list]
      )
      cls_pred_list = bbox_results["detached_cls_scores"]
      proposal_list = bbox_results["detached_proposals"]
  
      sampling_results = []
      bbox_head = self.bbox_head[stage]
      for i in range(len(batch_img_metas)):
          pred_instances = InstanceData()
          # TODO: Enhance the logic
          pred_instances.bboxes = proposal_list[i]  # for assinger
          pred_instances.scores = cls_pred_list[i]
          pred_instances.priors = proposal_list[i]  # for sampler
  
          assign_result = self.bbox_assigner[stage].assign(
              pred_instances=pred_instances,
              gt_instances=batch_gt_instances[i],
              gt_instances_ignore=None,
              img_meta=batch_img_metas[i],
          )
  
          sampling_result = self.bbox_sampler[stage].sample(
              assign_result, pred_instances, batch_gt_instances[i]
          )
          sampling_results.append(sampling_result)
  
      bbox_results.update(sampling_results=sampling_results)
  
      cls_score = bbox_results["cls_score"]
      decoded_bboxes = bbox_results["decoded_bboxes"]
      cls_score = cls_score.view(-1, cls_score.size(-1))
      decoded_bboxes = decoded_bboxes.view(-1, 4)
      bbox_loss_and_target = bbox_head.loss_and_target(
          cls_score,
          decoded_bboxes,
          sampling_results,
          self.train_cfg[stage],
          imgs_whwh=imgs_whwh,
          concat=True,
      )
      bbox_results.update(bbox_loss_and_target)
  
      # propose for the new proposal_list
      proposal_list = []
      for idx in range(len(batch_img_metas)):
          results = InstanceData()
          results.imgs_whwh = results_list[idx].imgs_whwh
          results.bboxes = bbox_results["detached_proposals"][idx]
          proposal_list.append(results)
      bbox_results.update(results_list=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 2 for calling the SparseRoIHead obj's underlying member method code with example usage: obj.bbox_loss(stage, x, results_list, object_feats, batch_img_metas, batch_gt_instances) and returns: bbox_results