class_docstr: Side-Aware Boundary Localization (SABL) for RetinaNet.
  
  The anchor generation, assigning and sampling in SABLRetinaHead
  are the same as GuidedAnchorHead for guided anchoring.
  
  Please refer to https://arxiv.org/abs/1912.04260 for more details.
  
  Args:
      num_classes (int): Number of classes.
      in_channels (int): Number of channels in the input feature map.
      stacked_convs (int): Number of Convs for classification and
          regression branches. Defaults to 4.
      feat_channels (int): Number of hidden channels. Defaults to 256.
      approx_anchor_generator (:obj:`ConfigType` or dict): Config dict for
          approx generator.
      square_anchor_generator (:obj:`ConfigDict` or dict): Config dict for
          square generator.
      conv_cfg (:obj:`ConfigDict` or dict, optional): Config dict for
          ConvModule. Defaults to None.
      norm_cfg (:obj:`ConfigDict` or dict, optional): Config dict for
          Norm Layer. Defaults to None.
      bbox_coder (:obj:`ConfigDict` or dict): Config dict for bbox coder.
      reg_decoded_bbox (bool): If true, the regression loss would be
          applied directly on decoded bounding boxes, converting both
          the predicted boxes and regression targets to absolute
          coordinates format. Default False. It should be ``True`` when
          using ``IoULoss``, ``GIoULoss``, or ``DIoULoss`` in the bbox head.
      train_cfg (:obj:`ConfigDict` or dict, optional): Training config of
          SABLRetinaHead.
      test_cfg (:obj:`ConfigDict` or dict, optional): Testing config of
          SABLRetinaHead.
      loss_cls (:obj:`ConfigDict` or dict): Config of classification loss.
      loss_bbox_cls (:obj:`ConfigDict` or dict): Config of classification
          loss for bbox branch.
      loss_bbox_reg (:obj:`ConfigDict` or dict): Config of regression loss
          for bbox branch.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or             dict], optional): Initialization config dict.
class_docstr_tok: ["Side-Aware","Boundary","Localization","(","SABL",")","for","RetinaNet",".","The","anchor","generation",",","assigning","and","sampling","in","SABLRetinaHead","are","the","same","as","GuidedAnchorHead","for","guided","anchoring",".","Please","refer","to","https",":","\/\/arxiv.org\/abs\/1912.04260","for","more","details",".","Args",":","num_classes","(","int",")",":","Number","of","classes",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","stacked_convs","(","int",")",":","Number","of","Convs","for","classification","and","regression","branches",".","Defaults","to","4.","feat_channels","(","int",")",":","Number","of","hidden","channels",".","Defaults","to","256.","approx_anchor_generator","(",":","obj",":","`","ConfigType","`","or","dict",")",":","Config","dict","for","approx","generator",".","square_anchor_generator","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","square","generator",".","conv_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Config","dict","for","ConvModule",".","Defaults","to","None",".","norm_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Config","dict","for","Norm","Layer",".","Defaults","to","None",".","bbox_coder","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","bbox","coder",".","reg_decoded_bbox","(","bool",")",":","If","true",",","the","regression","loss","would","be","applied","directly","on","decoded","bounding","boxes",",","converting","both","the","predicted","boxes","and","regression","targets","to","absolute","coordinates","format",".","Default","False",".","It","should","be","``","True","``","when","using","``","IoULoss","``",",","``","GIoULoss","``",",","or","``","DIoULoss","``","in","the","bbox","head",".","train_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Training","config","of","SABLRetinaHead",".","test_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Testing","config","of","SABLRetinaHead",".","loss_cls","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","classification","loss",".","loss_bbox_cls","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","classification","loss","for","bbox","branch",".","loss_bbox_reg","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","regression","loss","for","bbox","branch",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","dict","or","list","[",":","obj",":","`","ConfigDict","`","or","dict","]",",","optional",")",":","Initialization","config","dict","."]
docstr: Compute regression and classification targets for anchors in a
  single image.
  
  Args:
      flat_approxs (Tensor): flat approxs of a single image,
          shape (n, 4)
      inside_flags (Tensor): inside flags of a single image,
          shape (n, ).
      flat_squares (Tensor): flat squares of a single image,
          shape (approxs_per_octave * n, 4)
      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.
      unmap_outputs (bool): Whether to map outputs back to the original
          set of anchors.  Defaults to True.
  
  Returns:
      tuple:
  
      - labels_list (Tensor): Labels in a single image.
      - label_weights (Tensor): Label weights in a single image.
      - bbox_cls_targets (Tensor): BBox cls targets in a single image.
      - bbox_cls_weights (Tensor): BBox cls weights in a single image.
      - bbox_reg_targets (Tensor): BBox reg targets in a single image.
      - bbox_reg_weights (Tensor): BBox reg weights in a single image.
      - num_total_pos (int): Number of positive samples in a single             image.
      - num_total_neg (int): Number of negative samples in a single             image.
      - sampling_result (:obj:`SamplingResult`): Sampling result object.
docstr_tok: ["Compute","regression","and","classification","targets","for","anchors","in","a","single","image",".","Args",":","flat_approxs","(","Tensor",")",":","flat","approxs","of","a","single","image",",","shape","(","n",",","4",")","inside_flags","(","Tensor",")",":","inside","flags","of","a","single","image",",","shape","(","n",",",")",".","flat_squares","(","Tensor",")",":","flat","squares","of","a","single","image",",","shape","(","approxs_per_octave","*","n",",","4",")","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",".","unmap_outputs","(","bool",")",":","Whether","to","map","outputs","back","to","the","original","set","of","anchors",".","Defaults","to","True",".","Returns",":","tuple",":","-","labels_list","(","Tensor",")",":","Labels","in","a","single","image",".","-","label_weights","(","Tensor",")",":","Label","weights","in","a","single","image",".","-","bbox_cls_targets","(","Tensor",")",":","BBox","cls","targets","in","a","single","image",".","-","bbox_cls_weights","(","Tensor",")",":","BBox","cls","weights","in","a","single","image",".","-","bbox_reg_targets","(","Tensor",")",":","BBox","reg","targets","in","a","single","image",".","-","bbox_reg_weights","(","Tensor",")",":","BBox","reg","weights","in","a","single","image",".","-","num_total_pos","(","int",")",":","Number","of","positive","samples","in","a","single","image",".","-","num_total_neg","(","int",")",":","Number","of","negative","samples","in","a","single","image",".","-","sampling_result","(",":","obj",":","`","SamplingResult","`",")",":","Sampling","result","object","."]
code: def _get_targets_single(
      self,
      flat_approxs: Tensor,
      inside_flags: Tensor,
      flat_squares: Tensor,
      gt_instances: InstanceData,
      img_meta: dict,
      gt_instances_ignore: Optional[InstanceData] = None,
      unmap_outputs: bool = True,
  ) -> tuple:
      """Compute regression and classification targets for anchors in a
          single image.
  
          Args:
              flat_approxs (Tensor): flat approxs of a single image,
                  shape (n, 4)
              inside_flags (Tensor): inside flags of a single image,
                  shape (n, ).
              flat_squares (Tensor): flat squares of a single image,
                  shape (approxs_per_octave * n, 4)
              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.
              unmap_outputs (bool): Whether to map outputs back to the original
                  set of anchors.  Defaults to True.
  
          Returns:
              tuple:
  
              - labels_list (Tensor): Labels in a single image.
              - label_weights (Tensor): Label weights in a single image.
              - bbox_cls_targets (Tensor): BBox cls targets in a single image.
              - bbox_cls_weights (Tensor): BBox cls weights in a single image.
              - bbox_reg_targets (Tensor): BBox reg targets in a single image.
              - bbox_reg_weights (Tensor): BBox reg weights in a single image.
              - num_total_pos (int): Number of positive samples in a single \
              image.
              - num_total_neg (int): Number of negative samples in a single \
              image.
              - sampling_result (:obj:`SamplingResult`): Sampling result object.
          """
      if not inside_flags.any():
          raise ValueError(
              "There is no valid anchor inside the image boundary. Please "
              "check the image size and anchor sizes, or set "
              "``allowed_border`` to -1 to skip the condition."
          )
      # assign gt and sample anchors
      num_square = flat_squares.size(0)
      approxs = flat_approxs.view(
          num_square, self.approxs_per_octave, 4
      )
      approxs = approxs[inside_flags, ...]
      squares = flat_squares[inside_flags, :]
  
      pred_instances = InstanceData()
      pred_instances.priors = squares
      pred_instances.approxs = approxs
      assign_result = self.assigner.assign(
          pred_instances, gt_instances, gt_instances_ignore
      )
      sampling_result = self.sampler.sample(
          assign_result, pred_instances, gt_instances
      )
  
      num_valid_squares = squares.shape[0]
      bbox_cls_targets = squares.new_zeros(
          (num_valid_squares, self.side_num * 4)
      )
      bbox_cls_weights = squares.new_zeros(
          (num_valid_squares, self.side_num * 4)
      )
      bbox_reg_targets = squares.new_zeros(
          (num_valid_squares, self.side_num * 4)
      )
      bbox_reg_weights = squares.new_zeros(
          (num_valid_squares, self.side_num * 4)
      )
      labels = squares.new_full(
          (num_valid_squares,), self.num_classes, dtype=torch.long
      )
      label_weights = squares.new_zeros(
          num_valid_squares, dtype=torch.float
      )
  
      pos_inds = sampling_result.pos_inds
      neg_inds = sampling_result.neg_inds
      if len(pos_inds) > 0:
          (
              pos_bbox_reg_targets,
              pos_bbox_reg_weights,
              pos_bbox_cls_targets,
              pos_bbox_cls_weights,
          ) = self.bbox_coder.encode(
              sampling_result.pos_bboxes, sampling_result.pos_gt_bboxes
          )
  
          bbox_cls_targets[pos_inds, :] = pos_bbox_cls_targets
          bbox_reg_targets[pos_inds, :] = pos_bbox_reg_targets
          bbox_cls_weights[pos_inds, :] = pos_bbox_cls_weights
          bbox_reg_weights[pos_inds, :] = pos_bbox_reg_weights
          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
  
      # map up to original set of anchors
      if unmap_outputs:
          num_total_anchors = flat_squares.size(0)
          labels = unmap(
              labels,
              num_total_anchors,
              inside_flags,
              fill=self.num_classes,
          )
          label_weights = unmap(
              label_weights, num_total_anchors, inside_flags
          )
          bbox_cls_targets = unmap(
              bbox_cls_targets, num_total_anchors, inside_flags
          )
          bbox_cls_weights = unmap(
              bbox_cls_weights, num_total_anchors, inside_flags
          )
          bbox_reg_targets = unmap(
              bbox_reg_targets, num_total_anchors, inside_flags
          )
          bbox_reg_weights = unmap(
              bbox_reg_weights, num_total_anchors, inside_flags
          )
      return (
          labels,
          label_weights,
          bbox_cls_targets,
          bbox_cls_weights,
          bbox_reg_targets,
          bbox_reg_weights,
          pos_inds,
          neg_inds,
          sampling_result,
      )
  
imports: from typing import List, Optional, Tuple, Union
  import numpy
  import torch
  import torch.nn
  from mmcv.cnn import ConvModule
  from mmengine.config import ConfigDict
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.utils import ConfigType, InstanceList, MultiConfig, OptConfigType, OptInstanceList
  from ..task_modules.samplers import PseudoSampler
  from ..utils import filter_scores_and_topk, images_to_levels, multi_apply, unmap
  from .base_dense_head import BaseDenseHead
  from .guided_anchor_head import GuidedAnchorHead
label_desc: Use image node_id 7 for calling the SABLRetinaHead obj's underlying member method code with example usage: obj._get_targets_single(flat_approxs, inside_flags, flat_squares, gt_instances, img_meta, gt_instances_ignore, unmap_outputs) and returns: labels,  label_weights,  bbox_cls_targets,  bbox_cls_weights,  bbox_reg_targets,  bbox_reg_weights,  pos_inds,  neg_inds,  sampling_result