class_docstr: Detection Head of `YOLOF <https://arxiv.org/abs/2103.09460>`_
  
  Args:
      num_classes (int): The number of object classes (w/o background)
      in_channels (list[int]): The number of input channels per scale.
      cls_num_convs (int): The number of convolutions of cls branch.
         Defaults to 2.
      reg_num_convs (int): The number of convolutions of reg branch.
         Defaults to 4.
      norm_cfg (:obj:`ConfigDict` or dict): Config dict for normalization
          layer. Defaults to ``dict(type='BN', requires_grad=True)``.
class_docstr_tok: ["Detection","Head","of","`","YOLOF","<","https",":","\/\/arxiv.org\/abs\/2103.09460",">","`","_","Args",":","num_classes","(","int",")",":","The","number","of","object","classes","(","w\/o","background",")","in_channels","(","list","[","int","]",")",":","The","number","of","input","channels","per","scale",".","cls_num_convs","(","int",")",":","The","number","of","convolutions","of","cls","branch",".","Defaults","to","2.","reg_num_convs","(","int",")",":","The","number","of","convolutions","of","reg","branch",".","Defaults","to","4.","norm_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","normalization","layer",".","Defaults","to","``","dict","(","type='BN","'",",","requires_grad=True",")","``","."]
docstr: Compute regression and classification targets for anchors in a
  single image.
  
  Args:
      bbox_preds (Tensor): Bbox prediction of the image, which
          shape is (h * w ,4)
      flat_anchors (Tensor): Anchors of the image, which shape is
          (h * w * num_anchors ,4)
      valid_flags (Tensor): Valid flags of the image, which shape is
          (h * w * 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.
      unmap_outputs (bool): Whether to map outputs back to the original
          set of anchors.
  
  Returns:
      tuple:
          labels (Tensor): Labels of image, which shape is
              (h * w * num_anchors, ).
          label_weights (Tensor): Label weights of image, which shape is
              (h * w * num_anchors, ).
          pos_inds (Tensor): Pos index of image.
          neg_inds (Tensor): Neg index of image.
          sampling_result (obj:`SamplingResult`): Sampling result.
          pos_bbox_weights (Tensor): The Weight of using to calculate
              the bbox branch loss, which shape is (num, ).
          pos_predicted_boxes (Tensor): boxes predicted value of
              using to calculate the bbox branch loss, which shape is
              (num, 4).
          pos_target_boxes (Tensor): boxes target value of
              using to calculate the bbox branch loss, which shape is
              (num, 4).
docstr_tok: ["Compute","regression","and","classification","targets","for","anchors","in","a","single","image",".","Args",":","bbox_preds","(","Tensor",")",":","Bbox","prediction","of","the","image",",","which","shape","is","(","h","*","w",",4",")","flat_anchors","(","Tensor",")",":","Anchors","of","the","image",",","which","shape","is","(","h","*","w","*","num_anchors",",4",")","valid_flags","(","Tensor",")",":","Valid","flags","of","the","image",",","which","shape","is","(","h","*","w","*","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",".","unmap_outputs","(","bool",")",":","Whether","to","map","outputs","back","to","the","original","set","of","anchors",".","Returns",":","tuple",":","labels","(","Tensor",")",":","Labels","of","image",",","which","shape","is","(","h","*","w","*","num_anchors",",",")",".","label_weights","(","Tensor",")",":","Label","weights","of","image",",","which","shape","is","(","h","*","w","*","num_anchors",",",")",".","pos_inds","(","Tensor",")",":","Pos","index","of","image",".","neg_inds","(","Tensor",")",":","Neg","index","of","image",".","sampling_result","(","obj",":","`","SamplingResult","`",")",":","Sampling","result",".","pos_bbox_weights","(","Tensor",")",":","The","Weight","of","using","to","calculate","the","bbox","branch","loss",",","which","shape","is","(","num",",",")",".","pos_predicted_boxes","(","Tensor",")",":","boxes","predicted","value","of","using","to","calculate","the","bbox","branch","loss",",","which","shape","is","(","num",",","4",")",".","pos_target_boxes","(","Tensor",")",":","boxes","target","value","of","using","to","calculate","the","bbox","branch","loss",",","which","shape","is","(","num",",","4",")","."]
code: def _get_targets_single(
      self,
      bbox_preds: Tensor,
      flat_anchors: Tensor,
      valid_flags: 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:
          bbox_preds (Tensor): Bbox prediction of the image, which
              shape is (h * w ,4)
          flat_anchors (Tensor): Anchors of the image, which shape is
              (h * w * num_anchors ,4)
          valid_flags (Tensor): Valid flags of the image, which shape is
              (h * w * 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.
          unmap_outputs (bool): Whether to map outputs back to the original
              set of anchors.
  
      Returns:
          tuple:
              labels (Tensor): Labels of image, which shape is
                  (h * w * num_anchors, ).
              label_weights (Tensor): Label weights of image, which shape is
                  (h * w * num_anchors, ).
              pos_inds (Tensor): Pos index of image.
              neg_inds (Tensor): Neg index of image.
              sampling_result (obj:`SamplingResult`): Sampling result.
              pos_bbox_weights (Tensor): The Weight of using to calculate
                  the bbox branch loss, which shape is (num, ).
              pos_predicted_boxes (Tensor): boxes predicted value of
                  using to calculate the bbox branch loss, which shape is
                  (num, 4).
              pos_target_boxes (Tensor): boxes target value of
                  using to calculate the bbox branch loss, which shape is
                  (num, 4).
      """
      inside_flags = anchor_inside_flags(
          flat_anchors,
          valid_flags,
          img_meta["img_shape"][:2],
          self.train_cfg["allowed_border"],
      )
      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
      anchors = flat_anchors[inside_flags, :]
      bbox_preds = bbox_preds.reshape(-1, 4)
      bbox_preds = bbox_preds[inside_flags, :]
  
      # decoded bbox
      decoder_bbox_preds = self.bbox_coder.decode(anchors, bbox_preds)
      pred_instances = InstanceData(
          priors=anchors, decoder_priors=decoder_bbox_preds
      )
      assign_result = self.assigner.assign(
          pred_instances, gt_instances, gt_instances_ignore
      )
  
      pos_bbox_weights = assign_result.get_extra_property("pos_idx")
      pos_predicted_boxes = assign_result.get_extra_property(
          "pos_predicted_boxes"
      )
      pos_target_boxes = assign_result.get_extra_property(
          "target_boxes"
      )
  
      sampling_result = self.sampler.sample(
          assign_result, pred_instances, gt_instances
      )
      num_valid_anchors = anchors.shape[0]
      labels = anchors.new_full(
          (num_valid_anchors,), self.num_classes, dtype=torch.long
      )
      label_weights = anchors.new_zeros(
          num_valid_anchors, dtype=torch.float
      )
  
      pos_inds = sampling_result.pos_inds
      neg_inds = sampling_result.neg_inds
      if len(pos_inds) > 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
  
      # map up to original set of anchors
      if unmap_outputs:
          num_total_anchors = flat_anchors.size(0)
          labels = unmap(
              labels,
              num_total_anchors,
              inside_flags,
              fill=self.num_classes,
          )  # fill bg label
          label_weights = unmap(
              label_weights, num_total_anchors, inside_flags
          )
  
      return (
          labels,
          label_weights,
          pos_inds,
          neg_inds,
          sampling_result,
          pos_bbox_weights,
          pos_predicted_boxes,
          pos_target_boxes,
      )
  
imports: from typing import List, Optional, Tuple
  import torch
  import torch.nn
  from mmcv.cnn import ConvModule, is_norm
  from mmengine.model import bias_init_with_prob, constant_init, normal_init
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.utils import ConfigType, InstanceList, OptInstanceList, reduce_mean
  from ..task_modules.prior_generators import anchor_inside_flags
  from ..utils import levels_to_images, multi_apply, unmap
  from .anchor_head import AnchorHead
label_desc: Use image node_id 7 for calling the YOLOFHead obj's underlying member method code with example usage: obj._get_targets_single(bbox_preds, flat_anchors, valid_flags, gt_instances, img_meta, gt_instances_ignore, unmap_outputs) and returns: labels,  label_weights,  pos_inds,  neg_inds,  sampling_result,  pos_bbox_weights,  pos_predicted_boxes,  pos_target_boxes