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. Default: 2.
      corner_emb_channels (int): Channel of embedding vector. Default: 1.
      train_cfg (dict | None): Training config. Useless in CornerHead,
          but we keep this variable for SingleStageDetector. Default: None.
      test_cfg (dict | None): Testing config of CornerHead. Default: None.
      loss_heatmap (dict | None): Config of corner heatmap loss. Default:
          GaussianFocalLoss.
      loss_embedding (dict | None): Config of corner embedding loss. Default:
          AssociativeEmbeddingLoss.
      loss_offset (dict | None): Config of corner offset loss. Default:
          SmoothL1Loss.
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",".","Default",":","2.","corner_emb_channels","(","int",")",":","Channel","of","embedding","vector",".","Default",":","1.","train_cfg","(","dict","|","None",")",":","Training","config",".","Useless","in","CornerHead",",","but","we","keep","this","variable","for","SingleStageDetector",".","Default",":","None",".","test_cfg","(","dict","|","None",")",":","Testing","config","of","CornerHead",".","Default",":","None",".","loss_heatmap","(","dict","|","None",")",":","Config","of","corner","heatmap","loss",".","Default",":","GaussianFocalLoss",".","loss_embedding","(","dict","|","None",")",":","Config","of","corner","embedding","loss",".","Default",":","AssociativeEmbeddingLoss",".","loss_offset","(","dict","|","None",")",":","Config","of","corner","offset","loss",".","Default",":","SmoothL1Loss","."]
docstr: Compute losses for single level.
  
  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): Top-left corner embedding for current level with
          shape (N, corner_emb_channels, H, W).
      br_emb (Tensor): 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 differnet 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: ["Compute","losses","for","single","level",".","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",")",":","Top-left","corner","embedding","for","current","level","with","shape","(","N",",","corner_emb_channels",",","H",",","W",")",".","br_emb","(","Tensor",")",":","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","differnet","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_single(
      self, tl_hmp, br_hmp, tl_emb, br_emb, tl_off, br_off, targets
  ):
      """Compute losses for single level.
  
      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): Top-left corner embedding for current level with
              shape (N, corner_emb_channels, H, W).
          br_emb (Tensor): 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 differnet 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 math import ceil, log
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule, bias_init_with_prob
  from mmcv.ops import CornerPool, batched_nms
  from mmdet.core import multi_apply
  from ..builder import HEADS, build_loss
  from ..utils import gaussian_radius, gen_gaussian_target
  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_single(tl_hmp, br_hmp, tl_emb, br_emb, tl_off, br_off, targets) and returns: det_loss,  pull_loss,  push_loss,  off_loss