class_docstr: Head of CornerNet: Detecting Objects as Paired Keypoints.
  
  Code is modified from the `official github repo
  <https://github.com/princeton-vl/CornerNet/blob/master/models/py_utils/
  kp.py#L73>`_ .
  
  More details can be found in the `paper
  <https://arxiv.org/abs/1808.01244>`_ .
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      num_feat_levels (int): Levels of feature from the previous module.
          2 for HourglassNet-104 and 1 for HourglassNet-52. Because
          HourglassNet-104 outputs the final feature and intermediate
          supervision feature and HourglassNet-52 only outputs the final
          feature. Defaults to 2.
      corner_emb_channels (int): Channel of embedding vector. Defaults to 1.
      train_cfg (:obj:`ConfigDict` or dict, optional): Training config.
          Useless in CornerHead, but we keep this variable for
          SingleStageDetector.
      test_cfg (:obj:`ConfigDict` or dict, optional): Testing config of
          CornerHead.
      loss_heatmap (:obj:`ConfigDict` or dict): Config of corner heatmap
          loss. Defaults to GaussianFocalLoss.
      loss_embedding (:obj:`ConfigDict` or dict): Config of corner embedding
          loss. Defaults to AssociativeEmbeddingLoss.
      loss_offset (:obj:`ConfigDict` or dict): Config of corner offset loss.
          Defaults to SmoothL1Loss.
      init_cfg (:obj:`ConfigDict` or dict, optional): the config to control
          the initialization.
class_docstr_tok: ["Head","of","CornerNet",":","Detecting","Objects","as","Paired","Keypoints",".","Code","is","modified","from","the","`","official","github","repo","<","https",":","\/\/github.com\/princeton-vl\/CornerNet\/blob\/master\/models\/py_utils\/","kp.py","#","L73",">","`","_",".","More","details","can","be","found","in","the","`","paper","<","https",":","\/\/arxiv.org\/abs\/1808.01244",">","`","_",".","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","num_feat_levels","(","int",")",":","Levels","of","feature","from","the","previous","module",".","2","for","HourglassNet-104","and","1","for","HourglassNet-52",".","Because","HourglassNet-104","outputs","the","final","feature","and","intermediate","supervision","feature","and","HourglassNet-52","only","outputs","the","final","feature",".","Defaults","to","2.","corner_emb_channels","(","int",")",":","Channel","of","embedding","vector",".","Defaults","to","1.","train_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Training","config",".","Useless","in","CornerHead",",","but","we","keep","this","variable","for","SingleStageDetector",".","test_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Testing","config","of","CornerHead",".","loss_heatmap","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","corner","heatmap","loss",".","Defaults","to","GaussianFocalLoss",".","loss_embedding","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","corner","embedding","loss",".","Defaults","to","AssociativeEmbeddingLoss",".","loss_offset","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","corner","offset","loss",".","Defaults","to","SmoothL1Loss",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","the","config","to","control","the","initialization","."]
docstr: Calculate the loss of a single scale level based on the features
  extracted by the detection head.
  
  Args:
      tl_hmp (Tensor): Top-left corner heatmap for current level with
          shape (N, num_classes, H, W).
      br_hmp (Tensor): Bottom-right corner heatmap for current level with
          shape (N, num_classes, H, W).
      tl_emb (Tensor, optional): Top-left corner embedding for current
          level with shape (N, corner_emb_channels, H, W).
      br_emb (Tensor, optional): Bottom-right corner embedding for
          current level with shape (N, corner_emb_channels, H, W).
      tl_off (Tensor): Top-left corner offset for current level with
          shape (N, corner_offset_channels, H, W).
      br_off (Tensor): Bottom-right corner offset for current level with
          shape (N, corner_offset_channels, H, W).
      targets (dict): Corner target generated by `get_targets`.
  
  Returns:
      tuple[torch.Tensor]: Losses of the head's different branches
      containing the following losses:
  
          - det_loss (Tensor): Corner keypoint loss.
          - pull_loss (Tensor): Part one of AssociativeEmbedding loss.
          - push_loss (Tensor): Part two of AssociativeEmbedding loss.
          - off_loss (Tensor): Corner offset loss.
docstr_tok: ["Calculate","the","loss","of","a","single","scale","level","based","on","the","features","extracted","by","the","detection","head",".","Args",":","tl_hmp","(","Tensor",")",":","Top-left","corner","heatmap","for","current","level","with","shape","(","N",",","num_classes",",","H",",","W",")",".","br_hmp","(","Tensor",")",":","Bottom-right","corner","heatmap","for","current","level","with","shape","(","N",",","num_classes",",","H",",","W",")",".","tl_emb","(","Tensor",",","optional",")",":","Top-left","corner","embedding","for","current","level","with","shape","(","N",",","corner_emb_channels",",","H",",","W",")",".","br_emb","(","Tensor",",","optional",")",":","Bottom-right","corner","embedding","for","current","level","with","shape","(","N",",","corner_emb_channels",",","H",",","W",")",".","tl_off","(","Tensor",")",":","Top-left","corner","offset","for","current","level","with","shape","(","N",",","corner_offset_channels",",","H",",","W",")",".","br_off","(","Tensor",")",":","Bottom-right","corner","offset","for","current","level","with","shape","(","N",",","corner_offset_channels",",","H",",","W",")",".","targets","(","dict",")",":","Corner","target","generated","by","`","get_targets","`",".","Returns",":","tuple","[","torch.Tensor","]",":","Losses","of","the","head","'s","different","branches","containing","the","following","losses",":","-","det_loss","(","Tensor",")",":","Corner","keypoint","loss",".","-","pull_loss","(","Tensor",")",":","Part","one","of","AssociativeEmbedding","loss",".","-","push_loss","(","Tensor",")",":","Part","two","of","AssociativeEmbedding","loss",".","-","off_loss","(","Tensor",")",":","Corner","offset","loss","."]
code: def loss_by_feat_single(
      self,
      tl_hmp: Tensor,
      br_hmp: Tensor,
      tl_emb: Optional[Tensor],
      br_emb: Optional[Tensor],
      tl_off: Tensor,
      br_off: Tensor,
      targets: dict,
  ) -> Tuple[Tensor, ...]:
      """Calculate the loss of a single scale level based on the features
      extracted by the detection head.
  
      Args:
          tl_hmp (Tensor): Top-left corner heatmap for current level with
              shape (N, num_classes, H, W).
          br_hmp (Tensor): Bottom-right corner heatmap for current level with
              shape (N, num_classes, H, W).
          tl_emb (Tensor, optional): Top-left corner embedding for current
              level with shape (N, corner_emb_channels, H, W).
          br_emb (Tensor, optional): Bottom-right corner embedding for
              current level with shape (N, corner_emb_channels, H, W).
          tl_off (Tensor): Top-left corner offset for current level with
              shape (N, corner_offset_channels, H, W).
          br_off (Tensor): Bottom-right corner offset for current level with
              shape (N, corner_offset_channels, H, W).
          targets (dict): Corner target generated by `get_targets`.
  
      Returns:
          tuple[torch.Tensor]: Losses of the head's different branches
          containing the following losses:
  
              - det_loss (Tensor): Corner keypoint loss.
              - pull_loss (Tensor): Part one of AssociativeEmbedding loss.
              - push_loss (Tensor): Part two of AssociativeEmbedding loss.
              - off_loss (Tensor): Corner offset loss.
      """
      gt_tl_hmp = targets["topleft_heatmap"]
      gt_br_hmp = targets["bottomright_heatmap"]
      gt_tl_off = targets["topleft_offset"]
      gt_br_off = targets["bottomright_offset"]
      gt_embedding = targets["corner_embedding"]
  
      # Detection loss
      tl_det_loss = self.loss_heatmap(
          tl_hmp.sigmoid(),
          gt_tl_hmp,
          avg_factor=max(1, gt_tl_hmp.eq(1).sum()),
      )
      br_det_loss = self.loss_heatmap(
          br_hmp.sigmoid(),
          gt_br_hmp,
          avg_factor=max(1, gt_br_hmp.eq(1).sum()),
      )
      det_loss = (tl_det_loss + br_det_loss) / 2.0
  
      # AssociativeEmbedding loss
      if self.with_corner_emb and self.loss_embedding is not None:
          pull_loss, push_loss = self.loss_embedding(
              tl_emb, br_emb, gt_embedding
          )
      else:
          pull_loss, push_loss = None, None
  
      # Offset loss
      # We only compute the offset loss at the real corner position.
      # The value of real corner would be 1 in heatmap ground truth.
      # The mask is computed in class agnostic mode and its shape is
      # batch * 1 * width * height.
      tl_off_mask = (
          gt_tl_hmp.eq(1).sum(1).gt(0).unsqueeze(1).type_as(gt_tl_hmp)
      )
      br_off_mask = (
          gt_br_hmp.eq(1).sum(1).gt(0).unsqueeze(1).type_as(gt_br_hmp)
      )
      tl_off_loss = self.loss_offset(
          tl_off,
          gt_tl_off,
          tl_off_mask,
          avg_factor=max(1, tl_off_mask.sum()),
      )
      br_off_loss = self.loss_offset(
          br_off,
          gt_br_off,
          br_off_mask,
          avg_factor=max(1, br_off_mask.sum()),
      )
  
      off_loss = (tl_off_loss + br_off_loss) / 2.0
  
      return det_loss, pull_loss, push_loss, off_loss
  
imports: from logging import warning
  from math import ceil, log
  from typing import List, Optional, Sequence, Tuple
  import torch
  import torch.nn
  from mmcv.cnn import ConvModule
  from mmcv.ops import CornerPool, batched_nms
  from mmengine.config import ConfigDict
  from mmengine.model import BaseModule, bias_init_with_prob
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.utils import ConfigType, InstanceList, OptConfigType, OptInstanceList, OptMultiConfig
  from ..utils import gather_feat, gaussian_radius, gen_gaussian_target, get_local_maximum, get_topk_from_heatmap, multi_apply, transpose_and_gather_feat
  from .base_dense_head import BaseDenseHead
label_desc: Use image node_id 11 for calling the CornerHead obj's underlying member method code with example usage: obj.loss_by_feat_single(tl_hmp, br_hmp, tl_emb, br_emb, tl_off, br_off, targets) and returns: det_loss,  pull_loss,  push_loss,  off_loss