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 of the bbox head and predict detection
  results on the features of the upstream network.
  
  Args:
      x(tuple[Tensor]): Feature maps of all scale level.
      batch_img_metas (list[dict]): List of image information.
      rpn_results_list (list[:obj:`InstanceData`]): List of region
          proposals.
      rcnn_test_cfg (obj:`ConfigDict`): `test_cfg` of R-CNN.
      rescale (bool): If True, return boxes in original image space.
          Defaults to False.
  
  Returns:
      list[:obj:`InstanceData`]: Detection results of each image
      after the post process.
      Each item usually contains following keys.
  
      - scores (Tensor): Classification scores, has a shape
        (num_instance, )
      - labels (Tensor): Labels of bboxes, has a shape
        (num_instances, ).
      - bboxes (Tensor): Has a shape (num_instances, 4),
        the last dimension 4 arrange as (x1, y1, x2, y2).
docstr_tok: ["Perform","forward","propagation","of","the","bbox","head","and","predict","detection","results","on","the","features","of","the","upstream","network",".","Args",":","x","(","tuple","[","Tensor","]",")",":","Feature","maps","of","all","scale","level",".","batch_img_metas","(","list","[","dict","]",")",":","List","of","image","information",".","rpn_results_list","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","List","of","region","proposals",".","rcnn_test_cfg","(","obj",":","`","ConfigDict","`",")",":","`","test_cfg","`","of","R-CNN",".","rescale","(","bool",")",":","If","True",",","return","boxes","in","original","image","space",".","Defaults","to","False",".","Returns",":","list","[",":","obj",":","`","InstanceData","`","]",":","Detection","results","of","each","image","after","the","post","process",".","Each","item","usually","contains","following","keys",".","-","scores","(","Tensor",")",":","Classification","scores",",","has","a","shape","(","num_instance",",",")","-","labels","(","Tensor",")",":","Labels","of","bboxes",",","has","a","shape","(","num_instances",",",")",".","-","bboxes","(","Tensor",")",":","Has","a","shape","(","num_instances",",","4",")",",","the","last","dimension","4","arrange","as","(","x1",",","y1",",","x2",",","y2",")","."]
code: def predict_bbox(
      self,
      x: Tuple[Tensor],
      batch_img_metas: List[dict],
      rpn_results_list: InstanceList,
      rcnn_test_cfg: ConfigType,
      rescale: bool = False,
  ) -> InstanceList:
      """Perform forward propagation of the bbox head and predict detection
      results on the features of the upstream network.
  
      Args:
          x(tuple[Tensor]): Feature maps of all scale level.
          batch_img_metas (list[dict]): List of image information.
          rpn_results_list (list[:obj:`InstanceData`]): List of region
              proposals.
          rcnn_test_cfg (obj:`ConfigDict`): `test_cfg` of R-CNN.
          rescale (bool): If True, return boxes in original image space.
              Defaults to False.
  
      Returns:
          list[:obj:`InstanceData`]: Detection results of each image
          after the post process.
          Each item usually contains following keys.
  
          - scores (Tensor): Classification scores, has a shape
            (num_instance, )
          - labels (Tensor): Labels of bboxes, has a shape
            (num_instances, ).
          - bboxes (Tensor): Has a shape (num_instances, 4),
            the last dimension 4 arrange as (x1, y1, x2, y2).
      """
      proposal_list = [res.bboxes for res in rpn_results_list]
      object_feats = torch.cat(
          [res.pop("features")[None, ...] for res in rpn_results_list]
      )
      if all([proposal.shape[0] == 0 for proposal in proposal_list]):
          # There is no proposal in the whole batch
          return empty_instances(
              batch_img_metas, x[0].device, task_type="bbox"
          )
  
      for stage in range(self.num_stages):
          rois = bbox2roi(proposal_list)
          bbox_results = self._bbox_forward(
              stage, x, rois, object_feats, batch_img_metas
          )
          object_feats = bbox_results["object_feats"]
          cls_score = bbox_results["cls_score"]
          proposal_list = bbox_results["detached_proposals"]
  
      num_classes = self.bbox_head[-1].num_classes
  
      if self.bbox_head[-1].loss_cls.use_sigmoid:
          cls_score = cls_score.sigmoid()
      else:
          cls_score = cls_score.softmax(-1)[..., :-1]
  
      topk_inds_list = []
      results_list = []
      for img_id in range(len(batch_img_metas)):
          cls_score_per_img = cls_score[img_id]
          scores_per_img, topk_inds = cls_score_per_img.flatten(
              0, 1
          ).topk(self.test_cfg.max_per_img, sorted=False)
          labels_per_img = topk_inds % num_classes
          bboxes_per_img = proposal_list[img_id][
              topk_inds // num_classes
          ]
          topk_inds_list.append(topk_inds)
          if rescale and bboxes_per_img.size(0) > 0:
              assert (
                  batch_img_metas[img_id].get("scale_factor")
                  is not None
              )
              scale_factor = bboxes_per_img.new_tensor(
                  batch_img_metas[img_id]["scale_factor"]
              ).repeat((1, 2))
              bboxes_per_img = (
                  bboxes_per_img.view(bboxes_per_img.size(0), -1, 4)
                  / scale_factor
              ).view(bboxes_per_img.size()[0], -1)
  
          results = InstanceData()
          results.bboxes = bboxes_per_img
          results.scores = scores_per_img
          results.labels = labels_per_img
          results_list.append(results)
      if self.with_mask:
          for img_id in range(len(batch_img_metas)):
              # add positive information in InstanceData to predict
              # mask results in `mask_head`.
              proposals = bbox_results["detached_proposals"][img_id]
              topk_inds = topk_inds_list[img_id]
              attn_feats = bbox_results["attn_feats"][img_id]
  
              results_list[img_id].proposals = proposals
              results_list[img_id].topk_inds = topk_inds
              results_list[img_id].attn_feats = attn_feats
      return results_list
  
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 7 for calling the SparseRoIHead obj's underlying member method code with example usage: obj.predict_bbox(x, batch_img_metas, rpn_results_list, rcnn_test_cfg, rescale) and returns: results_list, empty_instances