class_docstr: RoIHead for `SCNet <https://arxiv.org/abs/2012.10150>`_.
  
  Args:
      num_stages (int): number of cascade stages.
      stage_loss_weights (list): loss weight of cascade stages.
      semantic_roi_extractor (dict): config to init semantic roi extractor.
      semantic_head (dict): config to init semantic head.
      feat_relay_head (dict): config to init feature_relay_head.
      glbctx_head (dict): config to init global context head.
docstr: Perform forward propagation and loss calculation of the detection
  roi on the features of the upstream network.
  
  Args:
      x (tuple[Tensor]): List of multi-level img features.
      rpn_results_list (list[:obj:`InstanceData`]): List of region
          proposals.
      batch_data_samples (list[:obj:`DetDataSample`]): The batch
          data samples. It usually includes information such
          as `gt_instance` or `gt_panoptic_seg` or `gt_sem_seg`.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components
docstr_tok: ["Perform","forward","propagation","and","loss","calculation","of","the","detection","roi","on","the","features","of","the","upstream","network",".","Args",":","x","(","tuple","[","Tensor","]",")",":","List","of","multi-level","img","features",".","rpn_results_list","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","List","of","region","proposals",".","batch_data_samples","(","list","[",":","obj",":","`","DetDataSample","`","]",")",":","The","batch","data","samples",".","It","usually","includes","information","such","as","`","gt_instance","`","or","`","gt_panoptic_seg","`","or","`","gt_sem_seg","`",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components"]
code: def loss(
      self,
      x: Tensor,
      rpn_results_list: InstanceList,
      batch_data_samples: SampleList,
  ) -> dict:
      """Perform forward propagation and loss calculation of the detection
      roi on the features of the upstream network.
  
      Args:
          x (tuple[Tensor]): List of multi-level img features.
          rpn_results_list (list[:obj:`InstanceData`]): List of region
              proposals.
          batch_data_samples (list[:obj:`DetDataSample`]): The batch
              data samples. It usually includes information such
              as `gt_instance` or `gt_panoptic_seg` or `gt_sem_seg`.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components
      """
      assert len(rpn_results_list) == len(batch_data_samples)
      outputs = unpack_gt_instances(batch_data_samples)
      (
          batch_gt_instances,
          batch_gt_instances_ignore,
          batch_img_metas,
      ) = outputs
  
      losses = dict()
  
      # semantic segmentation branch
      if self.with_semantic:
          semantic_results = self.semantic_loss(
              x=x, batch_data_samples=batch_data_samples
          )
          losses["loss_semantic_seg"] = semantic_results["loss_seg"]
          semantic_feat = semantic_results["semantic_feat"]
      else:
          semantic_feat = None
  
      # global context branch
      if self.with_glbctx:
          global_context_results = self.global_context_loss(
              x=x, batch_gt_instances=batch_gt_instances
          )
          losses["loss_glbctx"] = global_context_results["loss_glbctx"]
          glbctx_feat = global_context_results["glbctx_feat"]
      else:
          glbctx_feat = None
  
      results_list = rpn_results_list
      num_imgs = len(batch_img_metas)
      for stage in range(self.num_stages):
          stage_loss_weight = self.stage_loss_weights[stage]
  
          # assign gts and sample proposals
          sampling_results = []
          bbox_assigner = self.bbox_assigner[stage]
          bbox_sampler = self.bbox_sampler[stage]
          for i in range(num_imgs):
              results = results_list[i]
              # rename rpn_results.bboxes to rpn_results.priors
              results.priors = results.pop("bboxes")
  
              assign_result = bbox_assigner.assign(
                  results,
                  batch_gt_instances[i],
                  batch_gt_instances_ignore[i],
              )
              sampling_result = bbox_sampler.sample(
                  assign_result,
                  results,
                  batch_gt_instances[i],
                  feats=[lvl_feat[i][None] for lvl_feat in x],
              )
              sampling_results.append(sampling_result)
  
          # bbox head forward and loss
          bbox_results = self.bbox_loss(
              stage=stage,
              x=x,
              sampling_results=sampling_results,
              semantic_feat=semantic_feat,
              glbctx_feat=glbctx_feat,
          )
  
          for name, value in bbox_results["loss_bbox"].items():
              losses[f"s{stage}.{name}"] = (
                  value * stage_loss_weight if "loss" in name else value
              )
  
          # refine bboxes
          if stage < self.num_stages - 1:
              bbox_head = self.bbox_head[stage]
              with torch.no_grad():
                  results_list = bbox_head.refine_bboxes(
                      sampling_results=sampling_results,
                      bbox_results=bbox_results,
                      batch_img_metas=batch_img_metas,
                  )
  
      if self.with_feat_relay:
          relayed_feat = self._slice_pos_feats(
              bbox_results["relayed_feat"], sampling_results
          )
          relayed_feat = self.feat_relay_head(relayed_feat)
      else:
          relayed_feat = None
  
      # mask head forward and loss
      mask_results = self.mask_loss(
          x=x,
          sampling_results=sampling_results,
          batch_gt_instances=batch_gt_instances,
          semantic_feat=semantic_feat,
          glbctx_feat=glbctx_feat,
          relayed_feat=relayed_feat,
      )
      mask_stage_loss_weight = sum(self.stage_loss_weights)
      losses["loss_mask"] = (
          mask_stage_loss_weight
          * mask_results["loss_mask"]["loss_mask"]
      )
  
      return losses
  
imports: from typing import List, Optional, Tuple
  import torch
  import torch.nn.functional
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox2roi
  from mmdet.utils import ConfigType, InstanceList, OptConfigType
  from ..layers import adaptive_avg_pool2d
  from ..task_modules.samplers import SamplingResult
  from ..utils import empty_instances, unpack_gt_instances
  from .cascade_roi_head import CascadeRoIHead
label_desc: Use image node_id 14 for calling the SCNetRoIHead obj's underlying member method code with example usage: obj.loss(x, rpn_results_list, batch_data_samples) and returns: losses