class_docstr: Label Assignment Head from the paper: `Improving Object Detection by
  Label Assignment Distillation <https://arxiv.org/pdf/2108.10520.pdf>`_
class_docstr_tok: ["Label","Assignment","Head","from","the","paper",":","`","Improving","Object","Detection","by","Label","Assignment","Distillation","<","https",":","\/\/arxiv.org\/pdf\/2108.10520.pdf",">","`","_"]
docstr: Get label assignment (from teacher).
  
  Args:
      cls_scores (list[Tensor]): Box scores for each scale level
          Has shape (N, num_anchors * num_classes, H, W)
      bbox_preds (list[Tensor]): Box energies / deltas for each scale
          level with shape (N, num_anchors * 4, H, W)
      iou_preds (list[Tensor]): iou_preds for each scale
          level with shape (N, num_anchors * 1, H, W)
      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.
  
  Returns:
      tuple: Returns a tuple containing label assignment variables.
  
      - labels (Tensor): Labels of all anchors, each with
        shape (num_anchors,).
      - labels_weight (Tensor): Label weights of all anchor.
        each with shape (num_anchors,).
      - bboxes_target (Tensor): BBox targets of all anchors.
        each with shape (num_anchors, 4).
      - bboxes_weight (Tensor): BBox weights of all anchors.
        each with shape (num_anchors, 4).
      - pos_inds_flatten (Tensor): Contains all index of positive
        sample in all anchor.
      - pos_anchors (Tensor): Positive anchors.
      - num_pos (int): Number of positive anchors.
docstr_tok: ["Get","label","assignment","(","from","teacher",")",".","Args",":","cls_scores","(","list","[","Tensor","]",")",":","Box","scores","for","each","scale","level","Has","shape","(","N",",","num_anchors","*","num_classes",",","H",",","W",")","bbox_preds","(","list","[","Tensor","]",")",":","Box","energies","\/","deltas","for","each","scale","level","with","shape","(","N",",","num_anchors","*","4",",","H",",","W",")","iou_preds","(","list","[","Tensor","]",")",":","iou_preds","for","each","scale","level","with","shape","(","N",",","num_anchors","*","1",",","H",",","W",")","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",".","Returns",":","tuple",":","Returns","a","tuple","containing","label","assignment","variables",".","-","labels","(","Tensor",")",":","Labels","of","all","anchors",",","each","with","shape","(","num_anchors",",",")",".","-","labels_weight","(","Tensor",")",":","Label","weights","of","all","anchor",".","each","with","shape","(","num_anchors",",",")",".","-","bboxes_target","(","Tensor",")",":","BBox","targets","of","all","anchors",".","each","with","shape","(","num_anchors",",","4",")",".","-","bboxes_weight","(","Tensor",")",":","BBox","weights","of","all","anchors",".","each","with","shape","(","num_anchors",",","4",")",".","-","pos_inds_flatten","(","Tensor",")",":","Contains","all","index","of","positive","sample","in","all","anchor",".","-","pos_anchors","(","Tensor",")",":","Positive","anchors",".","-","num_pos","(","int",")",":","Number","of","positive","anchors","."]
code: def get_label_assignment(
      self,
      cls_scores: List[Tensor],
      bbox_preds: List[Tensor],
      iou_preds: List[Tensor],
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      batch_gt_instances_ignore: OptInstanceList = None,
  ) -> tuple:
      """Get label assignment (from teacher).
  
      Args:
          cls_scores (list[Tensor]): Box scores for each scale level
              Has shape (N, num_anchors * num_classes, H, W)
          bbox_preds (list[Tensor]): Box energies / deltas for each scale
              level with shape (N, num_anchors * 4, H, W)
          iou_preds (list[Tensor]): iou_preds for each scale
              level with shape (N, num_anchors * 1, H, W)
          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.
  
      Returns:
          tuple: Returns a tuple containing label assignment variables.
  
          - labels (Tensor): Labels of all anchors, each with
            shape (num_anchors,).
          - labels_weight (Tensor): Label weights of all anchor.
            each with shape (num_anchors,).
          - bboxes_target (Tensor): BBox targets of all anchors.
            each with shape (num_anchors, 4).
          - bboxes_weight (Tensor): BBox weights of all anchors.
            each with shape (num_anchors, 4).
          - pos_inds_flatten (Tensor): Contains all index of positive
            sample in all anchor.
          - pos_anchors (Tensor): Positive anchors.
          - num_pos (int): Number of positive anchors.
      """
  
      featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
      assert len(featmap_sizes) == self.prior_generator.num_levels
  
      device = cls_scores[0].device
      anchor_list, valid_flag_list = self.get_anchors(
          featmap_sizes, batch_img_metas, device=device
      )
      cls_reg_targets = self.get_targets(
          anchor_list,
          valid_flag_list,
          batch_gt_instances,
          batch_img_metas,
          batch_gt_instances_ignore=batch_gt_instances_ignore,
      )
      (
          labels,
          labels_weight,
          bboxes_target,
          bboxes_weight,
          pos_inds,
          pos_gt_index,
      ) = cls_reg_targets
      cls_scores = levels_to_images(cls_scores)
      cls_scores = [
          item.reshape(-1, self.cls_out_channels) for item in cls_scores
      ]
      bbox_preds = levels_to_images(bbox_preds)
      bbox_preds = [item.reshape(-1, 4) for item in bbox_preds]
      (pos_losses_list,) = multi_apply(
          self.get_pos_loss,
          anchor_list,
          cls_scores,
          bbox_preds,
          labels,
          labels_weight,
          bboxes_target,
          bboxes_weight,
          pos_inds,
      )
  
      with torch.no_grad():
          (
              reassign_labels,
              reassign_label_weight,
              reassign_bbox_weights,
              num_pos,
          ) = multi_apply(
              self.paa_reassign,
              pos_losses_list,
              labels,
              labels_weight,
              bboxes_weight,
              pos_inds,
              pos_gt_index,
              anchor_list,
          )
          num_pos = sum(num_pos)
      # convert all tensor list to a flatten tensor
      labels = torch.cat(reassign_labels, 0).view(-1)
      flatten_anchors = torch.cat(
          [torch.cat(item, 0) for item in anchor_list]
      )
      labels_weight = torch.cat(reassign_label_weight, 0).view(-1)
      bboxes_target = torch.cat(bboxes_target, 0).view(
          -1, bboxes_target[0].size(-1)
      )
  
      pos_inds_flatten = (
          ((labels >= 0) & (labels < self.num_classes))
          .nonzero()
          .reshape(-1)
      )
  
      if num_pos:
          pos_anchors = flatten_anchors[pos_inds_flatten]
      else:
          pos_anchors = None
  
      label_assignment_results = (
          labels,
          labels_weight,
          bboxes_target,
          bboxes_weight,
          pos_inds_flatten,
          pos_anchors,
          num_pos,
      )
      return label_assignment_results
  
file_path: mmdetection/mmdet/models/dense_heads/lad_head.py
imports: from typing import List, Optional
  import torch
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox_overlaps
  from mmdet.utils import InstanceList, OptInstanceList
  from ..utils import levels_to_images, multi_apply, unpack_gt_instances
  from .paa_head import PAAHead
label_desc: Use image node_id 1 for calling the LADHead obj's underlying member method code with example usage: obj.get_label_assignment(cls_scores, bbox_preds, iou_preds, batch_gt_instances, batch_img_metas, batch_gt_instances_ignore) and returns: label_assignment_results