class_docstr: Guided-Anchor-based head (GA-RPN, GA-RetinaNet, etc.).
  
  This GuidedAnchorHead will predict high-quality feature guided
  anchors and locations where anchors will be kept in inference.
  There are mainly 3 categories of bounding-boxes.
  
  - Sampled 9 pairs for target assignment. (approxes)
  - The square boxes where the predicted anchors are based on. (squares)
  - Guided anchors.
  
  Please refer to https://arxiv.org/abs/1901.03278 for more details.
  
  Args:
      num_classes (int): Number of classes.
      in_channels (int): Number of channels in the input feature map.
      feat_channels (int): Number of hidden channels. Defaults to 256.
      approx_anchor_generator (:obj:`ConfigDict` or dict): Config dict
          for approx generator
      square_anchor_generator (:obj:`ConfigDict` or dict): Config dict
          for square generator
      anchor_coder (:obj:`ConfigDict` or dict): Config dict for anchor coder
      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. Defaults to False. It should be `True` when
          using `IoULoss`, `GIoULoss`, or `DIoULoss` in the bbox head.
      deform_groups: (int): Group number of DCN in FeatureAdaption module.
          Defaults to 4.
      loc_filter_thr (float): Threshold to filter out unconcerned regions.
          Defaults to 0.01.
      loss_loc (:obj:`ConfigDict` or dict): Config of location loss.
      loss_shape (:obj:`ConfigDict` or dict): Config of anchor shape loss.
      loss_cls (:obj:`ConfigDict` or dict): Config of classification loss.
      loss_bbox (:obj:`ConfigDict` or dict): Config of bbox regression loss.
      init_cfg (:obj:`ConfigDict` or list[:obj:`ConfigDict`] or dict or             list[dict], optional): Initialization config dict.
class_docstr_tok: ["Guided-Anchor-based","head","(","GA-RPN",",","GA-RetinaNet",",","etc.",")",".","This","GuidedAnchorHead","will","predict","high-quality","feature","guided","anchors","and","locations","where","anchors","will","be","kept","in","inference",".","There","are","mainly","3","categories","of","bounding-boxes",".","-","Sampled","9","pairs","for","target","assignment",".","(","approxes",")","-","The","square","boxes","where","the","predicted","anchors","are","based","on",".","(","squares",")","-","Guided","anchors",".","Please","refer","to","https",":","\/\/arxiv.org\/abs\/1901.03278","for","more","details",".","Args",":","num_classes","(","int",")",":","Number","of","classes",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","feat_channels","(","int",")",":","Number","of","hidden","channels",".","Defaults","to","256.","approx_anchor_generator","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","approx","generator","square_anchor_generator","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","square","generator","anchor_coder","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","anchor","coder","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",".","Defaults","to","False",".","It","should","be","`","True","`","when","using","`","IoULoss","`",",","`","GIoULoss","`",",","or","`","DIoULoss","`","in","the","bbox","head",".","deform_groups",":","(","int",")",":","Group","number","of","DCN","in","FeatureAdaption","module",".","Defaults","to","4.","loc_filter_thr","(","float",")",":","Threshold","to","filter","out","unconcerned","regions",".","Defaults","to","0.01.","loss_loc","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","location","loss",".","loss_shape","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","anchor","shape","loss",".","loss_cls","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","classification","loss",".","loss_bbox","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","bbox","regression","loss",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","list","[",":","obj",":","`","ConfigDict","`","]","or","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict","."]
docstr: Calculate the loss based on the features extracted by the detection
  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).
      shape_preds (list[Tensor]): shape predictions for each scale
          level with shape (N, 1, H, W).
      loc_preds (list[Tensor]): location predictions for each scale
          level with shape (N, num_anchors * 2, 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:
      dict: A dictionary of loss components.
docstr_tok: ["Calculate","the","loss","based","on","the","features","extracted","by","the","detection","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",")",".","shape_preds","(","list","[","Tensor","]",")",":","shape","predictions","for","each","scale","level","with","shape","(","N",",","1",",","H",",","W",")",".","loc_preds","(","list","[","Tensor","]",")",":","location","predictions","for","each","scale","level","with","shape","(","N",",","num_anchors","*","2",",","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",":","dict",":","A","dictionary","of","loss","components","."]
code: def loss_by_feat(
      self,
      cls_scores: List[Tensor],
      bbox_preds: List[Tensor],
      shape_preds: List[Tensor],
      loc_preds: List[Tensor],
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      batch_gt_instances_ignore: OptInstanceList = None,
  ) -> dict:
      """Calculate the loss based on the features extracted by the detection
      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).
          shape_preds (list[Tensor]): shape predictions for each scale
              level with shape (N, 1, H, W).
          loc_preds (list[Tensor]): location predictions for each scale
              level with shape (N, num_anchors * 2, 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:
          dict: A dictionary of loss components.
      """
  
      featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
      assert (
          len(featmap_sizes) == self.approx_anchor_generator.num_levels
      )
  
      device = cls_scores[0].device
  
      # get loc targets
      loc_targets, loc_weights, loc_avg_factor = self.ga_loc_targets(
          batch_gt_instances, featmap_sizes
      )
  
      # get sampled approxes
      approxs_list, inside_flag_list = self.get_sampled_approxs(
          featmap_sizes, batch_img_metas, device=device
      )
      # get squares and guided anchors
      squares_list, guided_anchors_list, _ = self.get_anchors(
          featmap_sizes,
          shape_preds,
          loc_preds,
          batch_img_metas,
          device=device,
      )
  
      # get shape targets
      shape_targets = self.ga_shape_targets(
          approxs_list,
          inside_flag_list,
          squares_list,
          batch_gt_instances,
          batch_img_metas,
      )
      (
          bbox_anchors_list,
          bbox_gts_list,
          anchor_weights_list,
          ga_avg_factor,
      ) = shape_targets
  
      # get anchor targets
      cls_reg_targets = self.get_targets(
          guided_anchors_list,
          inside_flag_list,
          batch_gt_instances,
          batch_img_metas,
          batch_gt_instances_ignore=batch_gt_instances_ignore,
      )
      (
          labels_list,
          label_weights_list,
          bbox_targets_list,
          bbox_weights_list,
          avg_factor,
      ) = cls_reg_targets
  
      # anchor number of multi levels
      num_level_anchors = [
          anchors.size(0) for anchors in guided_anchors_list[0]
      ]
      # concat all level anchors to a single tensor
      concat_anchor_list = []
      for i in range(len(guided_anchors_list)):
          concat_anchor_list.append(torch.cat(guided_anchors_list[i]))
      all_anchor_list = images_to_levels(
          concat_anchor_list, num_level_anchors
      )
  
      # get classification and bbox regression losses
      losses_cls, losses_bbox = multi_apply(
          self.loss_by_feat_single,
          cls_scores,
          bbox_preds,
          all_anchor_list,
          labels_list,
          label_weights_list,
          bbox_targets_list,
          bbox_weights_list,
          avg_factor=avg_factor,
      )
  
      # get anchor location loss
      losses_loc = []
      for i in range(len(loc_preds)):
          loss_loc = self.loss_loc_single(
              loc_preds[i],
              loc_targets[i],
              loc_weights[i],
              avg_factor=loc_avg_factor,
          )
          losses_loc.append(loss_loc)
  
      # get anchor shape loss
      losses_shape = []
      for i in range(len(shape_preds)):
          loss_shape = self.loss_shape_single(
              shape_preds[i],
              bbox_anchors_list[i],
              bbox_gts_list[i],
              anchor_weights_list[i],
              avg_factor=ga_avg_factor,
          )
          losses_shape.append(loss_shape)
  
      return dict(
          loss_cls=losses_cls,
          loss_bbox=losses_bbox,
          loss_shape=losses_shape,
          loss_loc=losses_loc,
      )
  
imports: from typing import List, Optional, Tuple
  import torch
  import torch.nn
  from mmcv.ops import DeformConv2d, MaskedConv2d
  from mmengine.model import BaseModule
  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 ..layers import multiclass_nms
  from ..task_modules.prior_generators import anchor_inside_flags, calc_region
  from ..task_modules.samplers import PseudoSampler
  from ..utils import images_to_levels, multi_apply, unmap
  from .anchor_head import AnchorHead
label_desc: Use image node_id 13 for calling the GuidedAnchorHead obj's underlying member method code with example usage: obj.loss_by_feat(cls_scores, bbox_preds, shape_preds, loc_preds, batch_gt_instances, batch_img_metas, batch_gt_instances_ignore) and returns: dict