class_docstr: Detection Head of `FoveaBox: Beyond Anchor-based Object Detector.
  
  <https://arxiv.org/abs/1904.03797>`_.
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      base_edge_list (list[int]): List of edges.
      scale_ranges (list[tuple]): Range of scales.
      sigma (float): Super parameter of ``FoveaHead``.
      with_deform (bool):  Whether use deform conv.
      deform_groups (int): Deformable conv group size.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or             dict], optional): Initialization config dict.
class_docstr_tok: ["Detection","Head","of","`","FoveaBox",":","Beyond","Anchor-based","Object","Detector",".","<","https",":","\/\/arxiv.org\/abs\/1904.03797",">","`","_",".","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","base_edge_list","(","list","[","int","]",")",":","List","of","edges",".","scale_ranges","(","list","[","tuple","]",")",":","Range","of","scales",".","sigma","(","float",")",":","Super","parameter","of","``","FoveaHead","``",".","with_deform","(","bool",")",":","Whether","use","deform","conv",".","deform_groups","(","int",")",":","Deformable","conv","group","size",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","dict","or","list","[",":","obj",":","`","ConfigDict","`","or","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,
          each is a 4D-tensor, the channel number is
          num_priors * num_classes.
      bbox_preds (list[Tensor]): Box energies / deltas for each scale
          level, each is a 4D-tensor, the channel number is
          num_priors * 4.
      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[str, Tensor]: 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",",","each","is","a","4D-tensor",",","the","channel","number","is","num_priors","*","num_classes",".","bbox_preds","(","list","[","Tensor","]",")",":","Box","energies","\/","deltas","for","each","scale","level",",","each","is","a","4D-tensor",",","the","channel","number","is","num_priors","*","4.","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","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_by_feat(
      self,
      cls_scores: List[Tensor],
      bbox_preds: List[Tensor],
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      batch_gt_instances_ignore: OptInstanceList = None,
  ) -> Dict[str, Tensor]:
      """Calculate the loss based on the features extracted by the detection
      head.
  
      Args:
          cls_scores (list[Tensor]): Box scores for each scale level,
              each is a 4D-tensor, the channel number is
              num_priors * num_classes.
          bbox_preds (list[Tensor]): Box energies / deltas for each scale
              level, each is a 4D-tensor, the channel number is
              num_priors * 4.
          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[str, Tensor]: A dictionary of loss components.
      """
      assert len(cls_scores) == len(bbox_preds)
  
      featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
      priors = self.prior_generator.grid_priors(
          featmap_sizes,
          dtype=bbox_preds[0].dtype,
          device=bbox_preds[0].device,
      )
      num_imgs = cls_scores[0].size(0)
      flatten_cls_scores = [
          cls_score.permute(0, 2, 3, 1).reshape(
              -1, self.cls_out_channels
          )
          for cls_score in cls_scores
      ]
      flatten_bbox_preds = [
          bbox_pred.permute(0, 2, 3, 1).reshape(-1, 4)
          for bbox_pred in bbox_preds
      ]
      flatten_cls_scores = torch.cat(flatten_cls_scores)
      flatten_bbox_preds = torch.cat(flatten_bbox_preds)
      flatten_labels, flatten_bbox_targets = self.get_targets(
          batch_gt_instances, featmap_sizes, priors
      )
  
      # FG cat_id: [0, num_classes -1], BG cat_id: num_classes
      pos_inds = (
          ((flatten_labels >= 0) & (flatten_labels < self.num_classes))
          .nonzero()
          .view(-1)
      )
      num_pos = len(pos_inds)
  
      loss_cls = self.loss_cls(
          flatten_cls_scores,
          flatten_labels,
          avg_factor=num_pos + num_imgs,
      )
      if num_pos > 0:
          pos_bbox_preds = flatten_bbox_preds[pos_inds]
          pos_bbox_targets = flatten_bbox_targets[pos_inds]
          pos_weights = pos_bbox_targets.new_ones(
              pos_bbox_targets.size()
          )
          loss_bbox = self.loss_bbox(
              pos_bbox_preds,
              pos_bbox_targets,
              pos_weights,
              avg_factor=num_pos,
          )
      else:
          loss_bbox = torch.tensor(
              0,
              dtype=flatten_bbox_preds.dtype,
              device=flatten_bbox_preds.device,
          )
      return dict(loss_cls=loss_cls, loss_bbox=loss_bbox)
  
imports: from typing import Dict, List, Optional, Tuple
  import torch
  import torch.nn
  from mmcv.cnn import ConvModule
  from mmcv.ops import DeformConv2d
  from mmengine.config import ConfigDict
  from mmengine.model import BaseModule
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.utils import InstanceList, OptInstanceList, OptMultiConfig
  from ..utils import filter_scores_and_topk, multi_apply
  from .anchor_free_head import AnchorFreeHead
label_desc: Use image node_id 4 for calling the FoveaHead obj's underlying member method code with example usage: obj.loss_by_feat(cls_scores, bbox_preds, batch_gt_instances, batch_img_metas, batch_gt_instances_ignore) and returns: dict