class_docstr: Implementation of `Grid Head <https://arxiv.org/abs/1811.12030>`_
  
  Args:
      grid_points (int): The number of grid points. Defaults to 9.
      num_convs (int): The number of convolution layers. Defaults to 8.
      roi_feat_size (int): RoI feature size. Default to 14.
      in_channels (int): The channel number of inputs features.
          Defaults to 256.
      conv_kernel_size (int): The kernel size of convolution layers.
          Defaults to 3.
      point_feat_channels (int): The number of channels of each point
          features. Defaults to 64.
      class_agnostic (bool): Whether use class agnostic classification.
          If so, the output channels of logits will be 1. Defaults to False.
      loss_grid (:obj:`ConfigDict` or dict): Config of grid loss.
      conv_cfg (:obj:`ConfigDict` or dict, optional) dictionary to
          construct and config conv layer.
      norm_cfg (:obj:`ConfigDict` or dict): dictionary to construct and
          config norm layer.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or             dict]): Initialization config dict.
docstr: Calculate the ground truth for all samples in a batch according to
  the sampling_results.".
  
  Args:
      sampling_results (List[:obj:`SamplingResult`]): Assign results of
          all images in a batch after sampling.
      rcnn_train_cfg (:obj:`ConfigDict`): `train_cfg` of RCNN.
  
  Returns:
      Tensor: Grid heatmap targets.
docstr_tok: ["Calculate","the","ground","truth","for","all","samples","in","a","batch","according","to","the","sampling_results.","''",".","Args",":","sampling_results","(","List","[",":","obj",":","`","SamplingResult","`","]",")",":","Assign","results","of","all","images","in","a","batch","after","sampling",".","rcnn_train_cfg","(",":","obj",":","`","ConfigDict","`",")",":","`","train_cfg","`","of","RCNN",".","Returns",":","Tensor",":","Grid","heatmap","targets","."]
code: def get_targets(
      self,
      sampling_results: List[SamplingResult],
      rcnn_train_cfg: ConfigDict,
  ) -> Tensor:
      """Calculate the ground truth for all samples in a batch according to
      the sampling_results.".
  
      Args:
          sampling_results (List[:obj:`SamplingResult`]): Assign results of
              all images in a batch after sampling.
          rcnn_train_cfg (:obj:`ConfigDict`): `train_cfg` of RCNN.
  
      Returns:
          Tensor: Grid heatmap targets.
      """
      # mix all samples (across images) together.
      pos_bboxes = torch.cat(
          [res.pos_bboxes for res in sampling_results], dim=0
      ).cpu()
      pos_gt_bboxes = torch.cat(
          [res.pos_gt_bboxes for res in sampling_results], dim=0
      ).cpu()
      assert pos_bboxes.shape == pos_gt_bboxes.shape
  
      # expand pos_bboxes to 2x of original size
      x1 = pos_bboxes[:, 0] - (pos_bboxes[:, 2] - pos_bboxes[:, 0]) / 2
      y1 = pos_bboxes[:, 1] - (pos_bboxes[:, 3] - pos_bboxes[:, 1]) / 2
      x2 = pos_bboxes[:, 2] + (pos_bboxes[:, 2] - pos_bboxes[:, 0]) / 2
      y2 = pos_bboxes[:, 3] + (pos_bboxes[:, 3] - pos_bboxes[:, 1]) / 2
      pos_bboxes = torch.stack([x1, y1, x2, y2], dim=-1)
      pos_bbox_ws = (pos_bboxes[:, 2] - pos_bboxes[:, 0]).unsqueeze(-1)
      pos_bbox_hs = (pos_bboxes[:, 3] - pos_bboxes[:, 1]).unsqueeze(-1)
  
      num_rois = pos_bboxes.shape[0]
      map_size = self.whole_map_size
      # this is not the final target shape
      targets = torch.zeros(
          (num_rois, self.grid_points, map_size, map_size),
          dtype=torch.float,
      )
  
      # pre-compute interpolation factors for all grid points.
      # the first item is the factor of x-dim, and the second is y-dim.
      # for a 9-point grid, factors are like (1, 0), (0.5, 0.5), (0, 1)
      factors = []
      for j in range(self.grid_points):
          x_idx = j // self.grid_size
          y_idx = j % self.grid_size
          factors.append(
              (
                  1 - x_idx / (self.grid_size - 1),
                  1 - y_idx / (self.grid_size - 1),
              )
          )
  
      radius = rcnn_train_cfg.pos_radius
      radius2 = radius**2
      for i in range(num_rois):
          # ignore small bboxes
          if (
              pos_bbox_ws[i] <= self.grid_size
              or pos_bbox_hs[i] <= self.grid_size
          ):
              continue
          # for each grid point, mark a small circle as positive
          for j in range(self.grid_points):
              factor_x, factor_y = factors[j]
              gridpoint_x = (
                  factor_x * pos_gt_bboxes[i, 0]
                  + (1 - factor_x) * pos_gt_bboxes[i, 2]
              )
              gridpoint_y = (
                  factor_y * pos_gt_bboxes[i, 1]
                  + (1 - factor_y) * pos_gt_bboxes[i, 3]
              )
  
              cx = int(
                  (gridpoint_x - pos_bboxes[i, 0])
                  / pos_bbox_ws[i]
                  * map_size
              )
              cy = int(
                  (gridpoint_y - pos_bboxes[i, 1])
                  / pos_bbox_hs[i]
                  * map_size
              )
  
              for x in range(cx - radius, cx + radius + 1):
                  for y in range(cy - radius, cy + radius + 1):
                      if (
                          x >= 0
                          and x < map_size
                          and y >= 0
                          and y < map_size
                      ):
                          if (x - cx) ** 2 + (y - cy) ** 2 <= radius2:
                              targets[i, j, y, x] = 1
      # reduce the target heatmap size by a half
      # proposed in Grid R-CNN Plus (https://arxiv.org/abs/1906.05688).
      sub_targets = []
      for i in range(self.grid_points):
          sub_x1, sub_y1, sub_x2, sub_y2 = self.sub_regions[i]
          sub_targets.append(
              targets[:, [i], sub_y1:sub_y2, sub_x1:sub_x2]
          )
      sub_targets = torch.cat(sub_targets, dim=1)
      sub_targets = sub_targets.to(
          sampling_results[0].pos_bboxes.device
      )
      return sub_targets
  
imports: from typing import Dict, List, Tuple
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule
  from mmengine.config import ConfigDict
  from mmengine.model import BaseModule
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.models.task_modules.samplers import SamplingResult
  from mmdet.registry import MODELS
  from mmdet.utils import ConfigType, InstanceList, MultiConfig, OptConfigType
label_desc: Use image node_id 4 for calling the GridHead obj's underlying member method code with example usage: obj.get_targets(sampling_results, rcnn_train_cfg) and returns: sub_targets