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 mask 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.
      results_list (list[:obj:`InstanceData`]): Detection results of
          each image. 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 (Tensor): Bboxes predicted from bbox_head,
            has a shape (num_instances, 4).
          - topk_inds (Tensor): Topk indices of each image, has
            shape (num_instances, )
          - attn_feats (Tensor): Intermediate feature get from the last
            diihead, has shape (num_instances, feature_dimensions)
  
      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).
      - masks (Tensor): Has a shape (num_instances, H, W).
docstr_tok: ["Perform","forward","propagation","of","the","mask","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",".","results_list","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Detection","results","of","each","image",".","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","(","Tensor",")",":","Bboxes","predicted","from","bbox_head",",","has","a","shape","(","num_instances",",","4",")",".","-","topk_inds","(","Tensor",")",":","Topk","indices","of","each","image",",","has","shape","(","num_instances",",",")","-","attn_feats","(","Tensor",")",":","Intermediate","feature","get","from","the","last","diihead",",","has","shape","(","num_instances",",","feature_dimensions",")","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",")",".","-","masks","(","Tensor",")",":","Has","a","shape","(","num_instances",",","H",",","W",")","."]
code: def predict_mask(
      self,
      x: Tuple[Tensor],
      batch_img_metas: List[dict],
      results_list: InstanceList,
      rescale: bool = False,
  ) -> InstanceList:
      """Perform forward propagation of the mask 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.
          results_list (list[:obj:`InstanceData`]): Detection results of
              each image. 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 (Tensor): Bboxes predicted from bbox_head,
                has a shape (num_instances, 4).
              - topk_inds (Tensor): Topk indices of each image, has
                shape (num_instances, )
              - attn_feats (Tensor): Intermediate feature get from the last
                diihead, has shape (num_instances, feature_dimensions)
  
          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).
          - masks (Tensor): Has a shape (num_instances, H, W).
      """
      proposal_list = [res.pop("proposals") for res in results_list]
      topk_inds_list = [res.pop("topk_inds") for res in results_list]
      attn_feats = torch.cat(
          [res.pop("attn_feats")[None, ...] for res in results_list]
      )
  
      rois = bbox2roi(proposal_list)
  
      if rois.shape[0] == 0:
          results_list = empty_instances(
              batch_img_metas,
              rois.device,
              task_type="mask",
              instance_results=results_list,
              mask_thr_binary=self.test_cfg.mask_thr_binary,
          )
          return results_list
  
      last_stage = self.num_stages - 1
      mask_results = self._mask_forward(last_stage, x, rois, attn_feats)
  
      num_imgs = len(batch_img_metas)
      mask_results["mask_preds"] = mask_results["mask_preds"].reshape(
          num_imgs, -1, *mask_results["mask_preds"].size()[1:]
      )
      num_classes = self.bbox_head[-1].num_classes
  
      mask_preds = []
      for img_id in range(num_imgs):
          topk_inds = topk_inds_list[img_id]
          masks_per_img = mask_results["mask_preds"][img_id].flatten(
              0, 1
          )[topk_inds]
          masks_per_img = masks_per_img[:, None, ...].repeat(
              1, num_classes, 1, 1
          )
          mask_preds.append(masks_per_img)
      results_list = self.mask_head[-1].predict_by_feat(
          mask_preds,
          results_list,
          batch_img_metas,
          rcnn_test_cfg=self.test_cfg,
          rescale=rescale,
      )
  
      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 8 for calling the SparseRoIHead obj's underlying member method code with example usage: obj.predict_mask(x, batch_img_metas, results_list, rescale) and returns: results_list, results_list