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: Compute losses of the head.
  
  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.
      label_assignment_results (tuple, optional): As the outputs defined
          in the function `self.get_
          label_assignment`.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss gmm_assignment.
docstr_tok: ["Compute","losses","of","the","head",".","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",".","label_assignment_results","(","tuple",",","optional",")",":","As","the","outputs","defined","in","the","function","`","self.get_","label_assignment","`",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","gmm_assignment","."]
code: def loss_by_feat(
      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,
      label_assignment_results: Optional[tuple] = None,
  ) -> dict:
      """Compute losses of the head.
  
      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.
          label_assignment_results (tuple, optional): As the outputs defined
              in the function `self.get_
              label_assignment`.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss gmm_assignment.
      """
  
      (
          labels,
          labels_weight,
          bboxes_target,
          bboxes_weight,
          pos_inds_flatten,
          pos_anchors,
          num_pos,
      ) = label_assignment_results
  
      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]
      iou_preds = levels_to_images(iou_preds)
      iou_preds = [item.reshape(-1, 1) for item in iou_preds]
  
      # convert all tensor list to a flatten tensor
      cls_scores = torch.cat(cls_scores, 0).view(
          -1, cls_scores[0].size(-1)
      )
      bbox_preds = torch.cat(bbox_preds, 0).view(
          -1, bbox_preds[0].size(-1)
      )
      iou_preds = torch.cat(iou_preds, 0).view(
          -1, iou_preds[0].size(-1)
      )
  
      losses_cls = self.loss_cls(
          cls_scores,
          labels,
          labels_weight,
          avg_factor=max(num_pos, len(batch_img_metas)),
      )  # avoid num_pos=0
      if num_pos:
          pos_bbox_pred = self.bbox_coder.decode(
              pos_anchors, bbox_preds[pos_inds_flatten]
          )
          pos_bbox_target = bboxes_target[pos_inds_flatten]
          iou_target = bbox_overlaps(
              pos_bbox_pred.detach(), pos_bbox_target, is_aligned=True
          )
          losses_iou = self.loss_centerness(
              iou_preds[pos_inds_flatten],
              iou_target.unsqueeze(-1),
              avg_factor=num_pos,
          )
          losses_bbox = self.loss_bbox(
              pos_bbox_pred, pos_bbox_target, avg_factor=num_pos
          )
  
      else:
          losses_iou = iou_preds.sum() * 0
          losses_bbox = bbox_preds.sum() * 0
  
      return dict(
          loss_cls=losses_cls,
          loss_bbox=losses_bbox,
          loss_iou=losses_iou,
      )
  
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 3 for calling the LADHead obj's underlying member method code with example usage: obj.loss_by_feat(cls_scores, bbox_preds, iou_preds, batch_gt_instances, batch_img_metas, batch_gt_instances_ignore, label_assignment_results) and returns: dict