code: def get_targets(self, sampling_results, rcnn_train_cfg):
      # 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
  
file_path: insightface/detection/scrfd/mmdet/models/roi_heads/mask_heads/grid_head.py
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule, kaiming_init, normal_init
  from mmdet.models.builder import HEADS, build_loss
label_desc: Use image node_id 5 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