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
  multiple images.
  
  Args:
      cls_scores_list (list[Tensor]): Classification scores of
          each image. each is a 4D-tensor, the shape is
          (h * w, num_anchors * num_classes).
      bbox_preds_list (list[Tensor]): Bbox preds of each image.
          each is a 4D-tensor, the shape is (h * w, num_anchors * 4).
      anchor_list (list[Tensor]): Anchors of each image. Each element of
          is a tensor of shape (h * w * num_anchors, 4).
      valid_flag_list (list[Tensor]): Valid flags of each image. Each
         element of is a tensor of shape (h * w * num_anchors, )
      batch_gt_instances (list[:obj:`InstanceData`]): Batch of
          gt_instance. It usually includes ``bboxes`` and ``labels``
          attributes.
      batch_img_metas (list[dict]): Meta information of each image, e.g.,
          image size, scaling factor, etc.
      batch_gt_instances_ignore (list[:obj:`InstanceData`], optional):
          Batch of gt_instances_ignore. 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: Usually returns a tuple containing learning targets.
  
          - batch_labels (Tensor): Label of all images. Each element                     of is a tensor of shape (batch, h * w * num_anchors)
          - batch_label_weights (Tensor): Label weights of all images                     of is a tensor of shape (batch, h * w * num_anchors)
          - num_total_pos (int): Number of positive samples in all                     images.
          - num_total_neg (int): Number of negative samples in all                     images.
      additional_returns: This function enables user-defined returns from
          `self._get_targets_single`. These returns are currently refined
          to properties at each feature map (i.e. having HxW dimension).
          The results will be concatenated after the end
docstr_tok: ["Compute","regression","and","classification","targets","for","anchors","in","multiple","images",".","Args",":","cls_scores_list","(","list","[","Tensor","]",")",":","Classification","scores","of","each","image",".","each","is","a","4D-tensor",",","the","shape","is","(","h","*","w",",","num_anchors","*","num_classes",")",".","bbox_preds_list","(","list","[","Tensor","]",")",":","Bbox","preds","of","each","image",".","each","is","a","4D-tensor",",","the","shape","is","(","h","*","w",",","num_anchors","*","4",")",".","anchor_list","(","list","[","Tensor","]",")",":","Anchors","of","each","image",".","Each","element","of","is","a","tensor","of","shape","(","h","*","w","*","num_anchors",",","4",")",".","valid_flag_list","(","list","[","Tensor","]",")",":","Valid","flags","of","each","image",".","Each","element","of","is","a","tensor","of","shape","(","h","*","w","*","num_anchors",",",")","batch_gt_instances","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Batch","of","gt_instance",".","It","usually","includes","``","bboxes","``","and","``","labels","``","attributes",".","batch_img_metas","(","list","[","dict","]",")",":","Meta","information","of","each","image",",","e.g.",",","image","size",",","scaling","factor",",","etc",".","batch_gt_instances_ignore","(","list","[",":","obj",":","`","InstanceData","`","]",",","optional",")",":","Batch","of","gt_instances_ignore",".","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",":","Usually","returns","a","tuple","containing","learning","targets",".","-","batch_labels","(","Tensor",")",":","Label","of","all","images",".","Each","element","of","is","a","tensor","of","shape","(","batch",",","h","*","w","*","num_anchors",")","-","batch_label_weights","(","Tensor",")",":","Label","weights","of","all","images","of","is","a","tensor","of","shape","(","batch",",","h","*","w","*","num_anchors",")","-","num_total_pos","(","int",")",":","Number","of","positive","samples","in","all","images",".","-","num_total_neg","(","int",")",":","Number","of","negative","samples","in","all","images",".","additional_returns",":","This","function","enables","user-defined","returns","from","`","self._get_targets_single","`",".","These","returns","are","currently","refined","to","properties","at","each","feature","map","(","i.e",".","having","HxW","dimension",")",".","The","results","will","be","concatenated","after","the","end"]
code: def get_targets(
      self,
      cls_scores_list: List[Tensor],
      bbox_preds_list: List[Tensor],
      anchor_list: List[Tensor],
      valid_flag_list: List[Tensor],
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      batch_gt_instances_ignore: OptInstanceList = None,
      unmap_outputs: bool = True,
  ):
      """Compute regression and classification targets for anchors in
          multiple images.
  
          Args:
              cls_scores_list (list[Tensor]): Classification scores of
                  each image. each is a 4D-tensor, the shape is
                  (h * w, num_anchors * num_classes).
              bbox_preds_list (list[Tensor]): Bbox preds of each image.
                  each is a 4D-tensor, the shape is (h * w, num_anchors * 4).
              anchor_list (list[Tensor]): Anchors of each image. Each element of
                  is a tensor of shape (h * w * num_anchors, 4).
              valid_flag_list (list[Tensor]): Valid flags of each image. Each
                 element of is a tensor of shape (h * w * num_anchors, )
              batch_gt_instances (list[:obj:`InstanceData`]): Batch of
                  gt_instance. It usually includes ``bboxes`` and ``labels``
                  attributes.
              batch_img_metas (list[dict]): Meta information of each image, e.g.,
                  image size, scaling factor, etc.
              batch_gt_instances_ignore (list[:obj:`InstanceData`], optional):
                  Batch of gt_instances_ignore. 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: Usually returns a tuple containing learning targets.
  
                  - batch_labels (Tensor): Label of all images. Each element \
                      of is a tensor of shape (batch, h * w * num_anchors)
                  - batch_label_weights (Tensor): Label weights of all images \
                      of is a tensor of shape (batch, h * w * num_anchors)
                  - num_total_pos (int): Number of positive samples in all \
                      images.
                  - num_total_neg (int): Number of negative samples in all \
                      images.
              additional_returns: This function enables user-defined returns from
                  `self._get_targets_single`. These returns are currently refined
                  to properties at each feature map (i.e. having HxW dimension).
                  The results will be concatenated after the end
          """
      num_imgs = len(batch_img_metas)
      assert len(anchor_list) == len(valid_flag_list) == num_imgs
  
      # compute targets for each image
      if batch_gt_instances_ignore is None:
          batch_gt_instances_ignore = [None] * num_imgs
      results = multi_apply(
          self._get_targets_single,
          bbox_preds_list,
          anchor_list,
          valid_flag_list,
          batch_gt_instances,
          batch_img_metas,
          batch_gt_instances_ignore,
          unmap_outputs=unmap_outputs,
      )
      (
          all_labels,
          all_label_weights,
          pos_inds,
          neg_inds,
          sampling_results_list,
      ) = results[:5]
      # Get `avg_factor` of all images, which calculate in `SamplingResult`.
      # When using sampling method, avg_factor is usually the sum of
      # positive and negative priors. When using `PseudoSampler`,
      # `avg_factor` is usually equal to the number of positive priors.
      avg_factor = sum(
          [results.avg_factor for results in sampling_results_list]
      )
      rest_results = list(results[5:])  # user-added return values
  
      batch_labels = torch.stack(all_labels, 0)
      batch_label_weights = torch.stack(all_label_weights, 0)
  
      res = (batch_labels, batch_label_weights, avg_factor)
      for i, rests in enumerate(
          rest_results
      ):  # user-added return values
          rest_results[i] = torch.cat(rests, 0)
  
      return res + tuple(rest_results)
  
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 6 for calling the YOLOFHead obj's underlying member method code with example usage: obj.get_targets(cls_scores_list, bbox_preds_list, anchor_list, valid_flag_list, batch_gt_instances, batch_img_metas, batch_gt_instances_ignore, unmap_outputs) and returns: unknown