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: Generate corner targets.
  
  Including corner heatmap, corner offset.
  
  Optional: corner embedding, corner guiding shift, centripetal shift.
  
  For CornerNet, we generate corner heatmap, corner offset and corner
  embedding from this function.
  
  For CentripetalNet, we generate corner heatmap, corner offset, guiding
  shift and centripetal shift from this function.
  
  Args:
      gt_bboxes (list[Tensor]): Ground truth bboxes of each image, each
          has shape (num_gt, 4).
      gt_labels (list[Tensor]): Ground truth labels of each box, each has
          shape (num_gt, ).
      feat_shape (Sequence[int]): Shape of output feature,
          [batch, channel, height, width].
      img_shape (Sequence[int]): Shape of input image,
          [height, width, channel].
      with_corner_emb (bool): Generate corner embedding target or not.
          Defaults to False.
      with_guiding_shift (bool): Generate guiding shift target or not.
          Defaults to False.
      with_centripetal_shift (bool): Generate centripetal shift target or
          not. Defaults to False.
  
  Returns:
      dict: Ground truth of corner heatmap, corner offset, corner
      embedding, guiding shift and centripetal shift. Containing the
      following keys:
  
          - topleft_heatmap (Tensor): Ground truth top-left corner
            heatmap.
          - bottomright_heatmap (Tensor): Ground truth bottom-right
            corner heatmap.
          - topleft_offset (Tensor): Ground truth top-left corner offset.
          - bottomright_offset (Tensor): Ground truth bottom-right corner
            offset.
          - corner_embedding (list[list[list[int]]]): Ground truth corner
            embedding. Not must have.
          - topleft_guiding_shift (Tensor): Ground truth top-left corner
            guiding shift. Not must have.
          - bottomright_guiding_shift (Tensor): Ground truth bottom-right
            corner guiding shift. Not must have.
          - topleft_centripetal_shift (Tensor): Ground truth top-left
            corner centripetal shift. Not must have.
          - bottomright_centripetal_shift (Tensor): Ground truth
            bottom-right corner centripetal shift. Not must have.
docstr_tok: ["Generate","corner","targets",".","Including","corner","heatmap",",","corner","offset",".","Optional",":","corner","embedding",",","corner","guiding","shift",",","centripetal","shift",".","For","CornerNet",",","we","generate","corner","heatmap",",","corner","offset","and","corner","embedding","from","this","function",".","For","CentripetalNet",",","we","generate","corner","heatmap",",","corner","offset",",","guiding","shift","and","centripetal","shift","from","this","function",".","Args",":","gt_bboxes","(","list","[","Tensor","]",")",":","Ground","truth","bboxes","of","each","image",",","each","has","shape","(","num_gt",",","4",")",".","gt_labels","(","list","[","Tensor","]",")",":","Ground","truth","labels","of","each","box",",","each","has","shape","(","num_gt",",",")",".","feat_shape","(","Sequence","[","int","]",")",":","Shape","of","output","feature",",","[","batch",",","channel",",","height",",","width","]",".","img_shape","(","Sequence","[","int","]",")",":","Shape","of","input","image",",","[","height",",","width",",","channel","]",".","with_corner_emb","(","bool",")",":","Generate","corner","embedding","target","or","not",".","Defaults","to","False",".","with_guiding_shift","(","bool",")",":","Generate","guiding","shift","target","or","not",".","Defaults","to","False",".","with_centripetal_shift","(","bool",")",":","Generate","centripetal","shift","target","or","not",".","Defaults","to","False",".","Returns",":","dict",":","Ground","truth","of","corner","heatmap",",","corner","offset",",","corner","embedding",",","guiding","shift","and","centripetal","shift",".","Containing","the","following","keys",":","-","topleft_heatmap","(","Tensor",")",":","Ground","truth","top-left","corner","heatmap",".","-","bottomright_heatmap","(","Tensor",")",":","Ground","truth","bottom-right","corner","heatmap",".","-","topleft_offset","(","Tensor",")",":","Ground","truth","top-left","corner","offset",".","-","bottomright_offset","(","Tensor",")",":","Ground","truth","bottom-right","corner","offset",".","-","corner_embedding","(","list","[","list","[","list","[","int","]","]","]",")",":","Ground","truth","corner","embedding",".","Not","must","have",".","-","topleft_guiding_shift","(","Tensor",")",":","Ground","truth","top-left","corner","guiding","shift",".","Not","must","have",".","-","bottomright_guiding_shift","(","Tensor",")",":","Ground","truth","bottom-right","corner","guiding","shift",".","Not","must","have",".","-","topleft_centripetal_shift","(","Tensor",")",":","Ground","truth","top-left","corner","centripetal","shift",".","Not","must","have",".","-","bottomright_centripetal_shift","(","Tensor",")",":","Ground","truth","bottom-right","corner","centripetal","shift",".","Not","must","have","."]
code: def get_targets(
      self,
      gt_bboxes: List[Tensor],
      gt_labels: List[Tensor],
      feat_shape: Sequence[int],
      img_shape: Sequence[int],
      with_corner_emb: bool = False,
      with_guiding_shift: bool = False,
      with_centripetal_shift: bool = False,
  ) -> dict:
      """Generate corner targets.
  
      Including corner heatmap, corner offset.
  
      Optional: corner embedding, corner guiding shift, centripetal shift.
  
      For CornerNet, we generate corner heatmap, corner offset and corner
      embedding from this function.
  
      For CentripetalNet, we generate corner heatmap, corner offset, guiding
      shift and centripetal shift from this function.
  
      Args:
          gt_bboxes (list[Tensor]): Ground truth bboxes of each image, each
              has shape (num_gt, 4).
          gt_labels (list[Tensor]): Ground truth labels of each box, each has
              shape (num_gt, ).
          feat_shape (Sequence[int]): Shape of output feature,
              [batch, channel, height, width].
          img_shape (Sequence[int]): Shape of input image,
              [height, width, channel].
          with_corner_emb (bool): Generate corner embedding target or not.
              Defaults to False.
          with_guiding_shift (bool): Generate guiding shift target or not.
              Defaults to False.
          with_centripetal_shift (bool): Generate centripetal shift target or
              not. Defaults to False.
  
      Returns:
          dict: Ground truth of corner heatmap, corner offset, corner
          embedding, guiding shift and centripetal shift. Containing the
          following keys:
  
              - topleft_heatmap (Tensor): Ground truth top-left corner
                heatmap.
              - bottomright_heatmap (Tensor): Ground truth bottom-right
                corner heatmap.
              - topleft_offset (Tensor): Ground truth top-left corner offset.
              - bottomright_offset (Tensor): Ground truth bottom-right corner
                offset.
              - corner_embedding (list[list[list[int]]]): Ground truth corner
                embedding. Not must have.
              - topleft_guiding_shift (Tensor): Ground truth top-left corner
                guiding shift. Not must have.
              - bottomright_guiding_shift (Tensor): Ground truth bottom-right
                corner guiding shift. Not must have.
              - topleft_centripetal_shift (Tensor): Ground truth top-left
                corner centripetal shift. Not must have.
              - bottomright_centripetal_shift (Tensor): Ground truth
                bottom-right corner centripetal shift. Not must have.
      """
      batch_size, _, height, width = feat_shape
      img_h, img_w = img_shape[:2]
  
      width_ratio = float(width / img_w)
      height_ratio = float(height / img_h)
  
      gt_tl_heatmap = gt_bboxes[-1].new_zeros(
          [batch_size, self.num_classes, height, width]
      )
      gt_br_heatmap = gt_bboxes[-1].new_zeros(
          [batch_size, self.num_classes, height, width]
      )
      gt_tl_offset = gt_bboxes[-1].new_zeros(
          [batch_size, 2, height, width]
      )
      gt_br_offset = gt_bboxes[-1].new_zeros(
          [batch_size, 2, height, width]
      )
  
      if with_corner_emb:
          match = []
  
      # Guiding shift is a kind of offset, from center to corner
      if with_guiding_shift:
          gt_tl_guiding_shift = gt_bboxes[-1].new_zeros(
              [batch_size, 2, height, width]
          )
          gt_br_guiding_shift = gt_bboxes[-1].new_zeros(
              [batch_size, 2, height, width]
          )
      # Centripetal shift is also a kind of offset, from center to corner
      # and normalized by log.
      if with_centripetal_shift:
          gt_tl_centripetal_shift = gt_bboxes[-1].new_zeros(
              [batch_size, 2, height, width]
          )
          gt_br_centripetal_shift = gt_bboxes[-1].new_zeros(
              [batch_size, 2, height, width]
          )
  
      for batch_id in range(batch_size):
          # Ground truth of corner embedding per image is a list of coord set
          corner_match = []
          for box_id in range(len(gt_labels[batch_id])):
              left, top, right, bottom = gt_bboxes[batch_id][box_id]
              center_x = (left + right) / 2.0
              center_y = (top + bottom) / 2.0
              label = gt_labels[batch_id][box_id]
  
              # Use coords in the feature level to generate ground truth
              scale_left = left * width_ratio
              scale_right = right * width_ratio
              scale_top = top * height_ratio
              scale_bottom = bottom * height_ratio
              scale_center_x = center_x * width_ratio
              scale_center_y = center_y * height_ratio
  
              # Int coords on feature map/ground truth tensor
              left_idx = int(min(scale_left, width - 1))
              right_idx = int(min(scale_right, width - 1))
              top_idx = int(min(scale_top, height - 1))
              bottom_idx = int(min(scale_bottom, height - 1))
  
              # Generate gaussian heatmap
              scale_box_width = ceil(scale_right - scale_left)
              scale_box_height = ceil(scale_bottom - scale_top)
              radius = gaussian_radius(
                  (scale_box_height, scale_box_width), min_overlap=0.3
              )
              radius = max(0, int(radius))
              gt_tl_heatmap[batch_id, label] = gen_gaussian_target(
                  gt_tl_heatmap[batch_id, label],
                  [left_idx, top_idx],
                  radius,
              )
              gt_br_heatmap[batch_id, label] = gen_gaussian_target(
                  gt_br_heatmap[batch_id, label],
                  [right_idx, bottom_idx],
                  radius,
              )
  
              # Generate corner offset
              left_offset = scale_left - left_idx
              top_offset = scale_top - top_idx
              right_offset = scale_right - right_idx
              bottom_offset = scale_bottom - bottom_idx
              gt_tl_offset[batch_id, 0, top_idx, left_idx] = left_offset
              gt_tl_offset[batch_id, 1, top_idx, left_idx] = top_offset
              gt_br_offset[
                  batch_id, 0, bottom_idx, right_idx
              ] = right_offset
              gt_br_offset[
                  batch_id, 1, bottom_idx, right_idx
              ] = bottom_offset
  
              # Generate corner embedding
              if with_corner_emb:
                  corner_match.append(
                      [[top_idx, left_idx], [bottom_idx, right_idx]]
                  )
              # Generate guiding shift
              if with_guiding_shift:
                  gt_tl_guiding_shift[
                      batch_id, 0, top_idx, left_idx
                  ] = (scale_center_x - left_idx)
                  gt_tl_guiding_shift[
                      batch_id, 1, top_idx, left_idx
                  ] = (scale_center_y - top_idx)
                  gt_br_guiding_shift[
                      batch_id, 0, bottom_idx, right_idx
                  ] = (right_idx - scale_center_x)
                  gt_br_guiding_shift[
                      batch_id, 1, bottom_idx, right_idx
                  ] = (bottom_idx - scale_center_y)
              # Generate centripetal shift
              if with_centripetal_shift:
                  gt_tl_centripetal_shift[
                      batch_id, 0, top_idx, left_idx
                  ] = log(scale_center_x - scale_left)
                  gt_tl_centripetal_shift[
                      batch_id, 1, top_idx, left_idx
                  ] = log(scale_center_y - scale_top)
                  gt_br_centripetal_shift[
                      batch_id, 0, bottom_idx, right_idx
                  ] = log(scale_right - scale_center_x)
                  gt_br_centripetal_shift[
                      batch_id, 1, bottom_idx, right_idx
                  ] = log(scale_bottom - scale_center_y)
  
          if with_corner_emb:
              match.append(corner_match)
  
      target_result = dict(
          topleft_heatmap=gt_tl_heatmap,
          topleft_offset=gt_tl_offset,
          bottomright_heatmap=gt_br_heatmap,
          bottomright_offset=gt_br_offset,
      )
  
      if with_corner_emb:
          target_result.update(corner_embedding=match)
      if with_guiding_shift:
          target_result.update(
              topleft_guiding_shift=gt_tl_guiding_shift,
              bottomright_guiding_shift=gt_br_guiding_shift,
          )
      if with_centripetal_shift:
          target_result.update(
              topleft_centripetal_shift=gt_tl_centripetal_shift,
              bottomright_centripetal_shift=gt_br_centripetal_shift,
          )
  
      return target_result
  
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 9 for calling the CornerHead obj's underlying member method code with example usage: obj.get_targets(gt_bboxes, gt_labels, feat_shape, img_shape, with_corner_emb, with_guiding_shift, with_centripetal_shift) and returns: target_result