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: Box head forward function used in both training and testing.
  
  Args:
      stage (int): The current stage in Cascade RoI Head.
      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.
      semantic_feat (Tensor): Semantic feature. Defaults to None.
      glbctx_feat (Tensor): Global context feature. Defaults to None.
  
  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.
docstr_tok: ["Box","head","forward","function","used","in","both","training","and","testing",".","Args",":","stage","(","int",")",":","The","current","stage","in","Cascade","RoI","Head",".","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",".","semantic_feat","(","Tensor",")",":","Semantic","feature",".","Defaults","to","None",".","glbctx_feat","(","Tensor",")",":","Global","context","feature",".","Defaults","to","None",".","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","."]
code: def _bbox_forward(
      self,
      stage: int,
      x: Tuple[Tensor],
      rois: Tensor,
      semantic_feat: Optional[Tensor] = None,
      glbctx_feat: Optional[Tensor] = None,
  ) -> dict:
      """Box head forward function used in both training and testing.
  
      Args:
          stage (int): The current stage in Cascade RoI Head.
          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.
          semantic_feat (Tensor): Semantic feature. Defaults to None.
          glbctx_feat (Tensor): Global context feature. Defaults to None.
  
      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.
      """
      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
      )
      if self.with_semantic and semantic_feat is not None:
          bbox_semantic_feat = self.semantic_roi_extractor(
              [semantic_feat], rois
          )
          if bbox_semantic_feat.shape[-2:] != bbox_feats.shape[-2:]:
              bbox_semantic_feat = adaptive_avg_pool2d(
                  bbox_semantic_feat, bbox_feats.shape[-2:]
              )
          bbox_feats += bbox_semantic_feat
      if self.with_glbctx and glbctx_feat is not None:
          bbox_feats = self._fuse_glbctx(bbox_feats, glbctx_feat, rois)
      cls_score, bbox_pred, relayed_feat = bbox_head(
          bbox_feats, return_shared_feat=True
      )
  
      bbox_results = dict(
          cls_score=cls_score,
          bbox_pred=bbox_pred,
          relayed_feat=relayed_feat,
      )
      return bbox_results
  
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 8 for calling the SCNetRoIHead obj's underlying member method code with example usage: obj._bbox_forward(stage, x, rois, semantic_feat, glbctx_feat) and returns: bbox_results