class_docstr: Stage of CascadeRPNHead.
  
  Args:
      in_channels (int): Number of channels in the input feature map.
      anchor_generator (:obj:`ConfigDict` or dict): anchor generator config.
      adapt_cfg (:obj:`ConfigDict` or dict): adaptation config.
      bridged_feature (bool): whether update rpn feature. Defaults to False.
      with_cls (bool): whether use classification branch. Defaults to True.
      init_cfg :obj:`ConfigDict` or list[:obj:`ConfigDict`] or dict or
          list[dict], optional): Initialization config dict.
          Defaults to None.
class_docstr_tok: ["Stage","of","CascadeRPNHead",".","Args",":","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","anchor_generator","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","anchor","generator","config",".","adapt_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","adaptation","config",".","bridged_feature","(","bool",")",":","whether","update","rpn","feature",".","Defaults","to","False",".","with_cls","(","bool",")",":","whether","use","classification","branch",".","Defaults","to","True",".","init_cfg",":","obj",":","`","ConfigDict","`","or","list","[",":","obj",":","`","ConfigDict","`","]","or","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict",".","Defaults","to","None","."]
docstr: Get anchor targets based on region for single level.
  
  Args:
      flat_anchors (Tensor): Multi-level anchors of the image, which are
          concatenated into a single tensor of shape (num_anchors, 4)
      valid_flags (Tensor): Multi level valid flags of the image,
          which are concatenated into a single tensor of
              shape (num_anchors, ).
      gt_instances (:obj:`InstanceData`): Ground truth of instance
          annotations. It should includes ``bboxes`` and ``labels``
          attributes.
      img_meta (dict): Meta information for current image.
      gt_instances_ignore (:obj:`InstanceData`, optional): Instances
          to be ignored during training. It includes ``bboxes`` attribute
          data that is ignored during training and testing.
          Defaults to None.
      featmap_sizes (list[Tuple[int, int]]): Feature map size each level.
      num_level_anchors (list[int]): The number of anchors in each level.
  
  Returns:
      tuple:
  
          - labels (Tensor): Labels of each level.
          - label_weights (Tensor): Label weights of each level.
          - bbox_targets (Tensor): BBox targets of each level.
          - bbox_weights (Tensor): BBox weights of each level.
          - pos_inds (Tensor): positive samples indexes.
          - neg_inds (Tensor): negative samples indexes.
          - sampling_result (:obj:`SamplingResult`): Sampling results.
docstr_tok: ["Get","anchor","targets","based","on","region","for","single","level",".","Args",":","flat_anchors","(","Tensor",")",":","Multi-level","anchors","of","the","image",",","which","are","concatenated","into","a","single","tensor","of","shape","(","num_anchors",",","4",")","valid_flags","(","Tensor",")",":","Multi","level","valid","flags","of","the","image",",","which","are","concatenated","into","a","single","tensor","of","shape","(","num_anchors",",",")",".","gt_instances","(",":","obj",":","`","InstanceData","`",")",":","Ground","truth","of","instance","annotations",".","It","should","includes","``","bboxes","``","and","``","labels","``","attributes",".","img_meta","(","dict",")",":","Meta","information","for","current","image",".","gt_instances_ignore","(",":","obj",":","`","InstanceData","`",",","optional",")",":","Instances","to","be","ignored","during","training",".","It","includes","``","bboxes","``","attribute","data","that","is","ignored","during","training","and","testing",".","Defaults","to","None",".","featmap_sizes","(","list","[","Tuple","[","int",",","int","]","]",")",":","Feature","map","size","each","level",".","num_level_anchors","(","list","[","int","]",")",":","The","number","of","anchors","in","each","level",".","Returns",":","tuple",":","-","labels","(","Tensor",")",":","Labels","of","each","level",".","-","label_weights","(","Tensor",")",":","Label","weights","of","each","level",".","-","bbox_targets","(","Tensor",")",":","BBox","targets","of","each","level",".","-","bbox_weights","(","Tensor",")",":","BBox","weights","of","each","level",".","-","pos_inds","(","Tensor",")",":","positive","samples","indexes",".","-","neg_inds","(","Tensor",")",":","negative","samples","indexes",".","-","sampling_result","(",":","obj",":","`","SamplingResult","`",")",":","Sampling","results","."]
code: def _region_targets_single(
      self,
      flat_anchors: Tensor,
      valid_flags: Tensor,
      gt_instances: InstanceData,
      img_meta: dict,
      gt_instances_ignore: InstanceData,
      featmap_sizes: List[Tuple[int, int]],
      num_level_anchors: List[int],
  ) -> tuple:
      """Get anchor targets based on region for single level.
  
      Args:
          flat_anchors (Tensor): Multi-level anchors of the image, which are
              concatenated into a single tensor of shape (num_anchors, 4)
          valid_flags (Tensor): Multi level valid flags of the image,
              which are concatenated into a single tensor of
                  shape (num_anchors, ).
          gt_instances (:obj:`InstanceData`): Ground truth of instance
              annotations. It should includes ``bboxes`` and ``labels``
              attributes.
          img_meta (dict): Meta information for current image.
          gt_instances_ignore (:obj:`InstanceData`, optional): Instances
              to be ignored during training. It includes ``bboxes`` attribute
              data that is ignored during training and testing.
              Defaults to None.
          featmap_sizes (list[Tuple[int, int]]): Feature map size each level.
          num_level_anchors (list[int]): The number of anchors in each level.
  
      Returns:
          tuple:
  
              - labels (Tensor): Labels of each level.
              - label_weights (Tensor): Label weights of each level.
              - bbox_targets (Tensor): BBox targets of each level.
              - bbox_weights (Tensor): BBox weights of each level.
              - pos_inds (Tensor): positive samples indexes.
              - neg_inds (Tensor): negative samples indexes.
              - sampling_result (:obj:`SamplingResult`): Sampling results.
      """
      pred_instances = InstanceData()
      pred_instances.priors = flat_anchors
      pred_instances.valid_flags = valid_flags
  
      assign_result = self.assigner.assign(
          pred_instances,
          gt_instances,
          img_meta,
          featmap_sizes,
          num_level_anchors,
          self.anchor_scales[0],
          self.anchor_strides,
          gt_instances_ignore=gt_instances_ignore,
          allowed_border=self.train_cfg["allowed_border"],
      )
      sampling_result = self.sampler.sample(
          assign_result, pred_instances, gt_instances
      )
  
      num_anchors = flat_anchors.shape[0]
      bbox_targets = torch.zeros_like(flat_anchors)
      bbox_weights = torch.zeros_like(flat_anchors)
      labels = flat_anchors.new_zeros(num_anchors, dtype=torch.long)
      label_weights = flat_anchors.new_zeros(
          num_anchors, dtype=torch.float
      )
  
      pos_inds = sampling_result.pos_inds
      neg_inds = sampling_result.neg_inds
      if len(pos_inds) > 0:
          if not self.reg_decoded_bbox:
              pos_bbox_targets = self.bbox_coder.encode(
                  sampling_result.pos_bboxes,
                  sampling_result.pos_gt_bboxes,
              )
          else:
              pos_bbox_targets = sampling_result.pos_gt_bboxes
          bbox_targets[pos_inds, :] = pos_bbox_targets
          bbox_weights[pos_inds, :] = 1.0
          labels[pos_inds] = sampling_result.pos_gt_labels
          if self.train_cfg["pos_weight"] <= 0:
              label_weights[pos_inds] = 1.0
          else:
              label_weights[pos_inds] = self.train_cfg["pos_weight"]
      if len(neg_inds) > 0:
          label_weights[neg_inds] = 1.0
  
      return (
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          pos_inds,
          neg_inds,
          sampling_result,
      )
  
imports: from __future__ import division
  import copy
  from typing import Dict, List, Optional, Tuple, Union
  import torch
  import torch.nn
  from mmcv.ops import DeformConv2d
  from mmengine.config import ConfigDict
  from mmengine.model import BaseModule, ModuleList
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures import SampleList
  from mmdet.utils import ConfigType, InstanceList, MultiConfig, OptInstanceList, OptMultiConfig
  from ..task_modules.assigners import RegionAssigner
  from ..task_modules.samplers import PseudoSampler
  from ..utils import images_to_levels, multi_apply, select_single_mlvl, unpack_gt_instances
  from .base_dense_head import BaseDenseHead
  from .rpn_head import RPNHead
label_desc: Use image node_id 5 for calling the StageCascadeRPNHead obj's underlying member method code with example usage: obj._region_targets_single(flat_anchors, valid_flags, gt_instances, img_meta, gt_instances_ignore, featmap_sizes, num_level_anchors) and returns: labels,  label_weights,  bbox_targets,  bbox_weights,  pos_inds,  neg_inds,  sampling_result