class_docstr: CondInst box head used in https://arxiv.org/abs/1904.02689.
  
  Note that CondInst Bbox Head is a extension of FCOS head.
  Two differences are described as follows:
  
  1. CondInst box head predicts a set of params for each instance.
  2. CondInst box head return the pos_gt_inds and pos_inds.
  
  Args:
      num_params (int): Number of params for instance segmentation.
class_docstr_tok: ["CondInst","box","head","used","in","https",":","\/\/arxiv.org\/abs\/1904.02689",".","Note","that","CondInst","Bbox","Head","is","a","extension","of","FCOS","head",".","Two","differences","are","described","as","follows",":","1",".","CondInst","box","head","predicts","a","set","of","params","for","each","instance",".","2",".","CondInst","box","head","return","the","pos_gt_inds","and","pos_inds",".","Args",":","num_params","(","int",")",":","Number","of","params","for","instance","segmentation","."]
docstr: Compute regression and classification targets for a single image.
docstr_tok: ["Compute","regression","and","classification","targets","for","a","single","image","."]
code: def _get_targets_single(
      self,
      gt_instances: InstanceData,
      points: Tensor,
      regress_ranges: Tensor,
      num_points_per_lvl: List[int],
  ) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
      """Compute regression and classification targets for a single image."""
      num_points = points.size(0)
      num_gts = len(gt_instances)
      gt_bboxes = gt_instances.bboxes
      gt_labels = gt_instances.labels
      gt_masks = gt_instances.get("masks", None)
  
      if num_gts == 0:
          return (
              gt_labels.new_full((num_points,), self.num_classes),
              gt_bboxes.new_zeros((num_points, 4)),
              gt_bboxes.new_zeros((0,), dtype=torch.int64),
              gt_bboxes.new_zeros((0,), dtype=torch.int64),
          )
  
      areas = (gt_bboxes[:, 2] - gt_bboxes[:, 0]) * (
          gt_bboxes[:, 3] - gt_bboxes[:, 1]
      )
      # TODO: figure out why these two are different
      # areas = areas[None].expand(num_points, num_gts)
      areas = areas[None].repeat(num_points, 1)
      regress_ranges = regress_ranges[:, None, :].expand(
          num_points, num_gts, 2
      )
      gt_bboxes = gt_bboxes[None].expand(num_points, num_gts, 4)
      xs, ys = points[:, 0], points[:, 1]
      xs = xs[:, None].expand(num_points, num_gts)
      ys = ys[:, None].expand(num_points, num_gts)
  
      left = xs - gt_bboxes[..., 0]
      right = gt_bboxes[..., 2] - xs
      top = ys - gt_bboxes[..., 1]
      bottom = gt_bboxes[..., 3] - ys
      bbox_targets = torch.stack((left, top, right, bottom), -1)
  
      if self.center_sampling:
          # condition1: inside a `center bbox`
          radius = self.center_sample_radius
          # if gt_mask not None, use gt mask's centroid to determine
          # the center region rather than gt_bbox center
          if gt_masks is None:
              center_xs = (gt_bboxes[..., 0] + gt_bboxes[..., 2]) / 2
              center_ys = (gt_bboxes[..., 1] + gt_bboxes[..., 3]) / 2
          else:
              h, w = gt_masks.height, gt_masks.width
              masks = gt_masks.to_tensor(
                  dtype=torch.bool, device=gt_bboxes.device
              )
              yys = torch.arange(
                  0, h, dtype=torch.float32, device=masks.device
              )
              xxs = torch.arange(
                  0, w, dtype=torch.float32, device=masks.device
              )
              # m00/m10/m01 represent the moments of a contour
              # centroid is computed by m00/m10 and m00/m01
              m00 = masks.sum(dim=-1).sum(dim=-1).clamp(min=1e-6)
              m10 = (masks * xxs).sum(dim=-1).sum(dim=-1)
              m01 = (masks * yys[:, None]).sum(dim=-1).sum(dim=-1)
              center_xs = m10 / m00
              center_ys = m01 / m00
  
              center_xs = center_xs[None].expand(num_points, num_gts)
              center_ys = center_ys[None].expand(num_points, num_gts)
          center_gts = torch.zeros_like(gt_bboxes)
          stride = center_xs.new_zeros(center_xs.shape)
  
          # project the points on current lvl back to the `original` sizes
          lvl_begin = 0
          for lvl_idx, num_points_lvl in enumerate(num_points_per_lvl):
              lvl_end = lvl_begin + num_points_lvl
              stride[lvl_begin:lvl_end] = self.strides[lvl_idx] * radius
              lvl_begin = lvl_end
  
          x_mins = center_xs - stride
          y_mins = center_ys - stride
          x_maxs = center_xs + stride
          y_maxs = center_ys + stride
          center_gts[..., 0] = torch.where(
              x_mins > gt_bboxes[..., 0], x_mins, gt_bboxes[..., 0]
          )
          center_gts[..., 1] = torch.where(
              y_mins > gt_bboxes[..., 1], y_mins, gt_bboxes[..., 1]
          )
          center_gts[..., 2] = torch.where(
              x_maxs > gt_bboxes[..., 2], gt_bboxes[..., 2], x_maxs
          )
          center_gts[..., 3] = torch.where(
              y_maxs > gt_bboxes[..., 3], gt_bboxes[..., 3], y_maxs
          )
  
          cb_dist_left = xs - center_gts[..., 0]
          cb_dist_right = center_gts[..., 2] - xs
          cb_dist_top = ys - center_gts[..., 1]
          cb_dist_bottom = center_gts[..., 3] - ys
          center_bbox = torch.stack(
              (
                  cb_dist_left,
                  cb_dist_top,
                  cb_dist_right,
                  cb_dist_bottom,
              ),
              -1,
          )
          inside_gt_bbox_mask = center_bbox.min(-1)[0] > 0
      else:
          # condition1: inside a gt bbox
          inside_gt_bbox_mask = bbox_targets.min(-1)[0] > 0
  
      # condition2: limit the regression range for each location
      max_regress_distance = bbox_targets.max(-1)[0]
      inside_regress_range = (
          max_regress_distance >= regress_ranges[..., 0]
      ) & (max_regress_distance <= regress_ranges[..., 1])
  
      # if there are still more than one objects for a location,
      # we choose the one with minimal area
      areas[inside_gt_bbox_mask == 0] = INF
      areas[inside_regress_range == 0] = INF
      min_area, min_area_inds = areas.min(dim=1)
  
      labels = gt_labels[min_area_inds]
      labels[min_area == INF] = self.num_classes  # set as BG
      bbox_targets = bbox_targets[range(num_points), min_area_inds]
  
      # return pos_inds & pos_gt_inds
      bg_class_ind = self.num_classes
      pos_inds = (
          ((labels >= 0) & (labels < bg_class_ind))
          .nonzero()
          .reshape(-1)
      )
      pos_gt_inds = min_area_inds[labels < self.num_classes]
      return labels, bbox_targets, pos_inds, pos_gt_inds
  
imports: import copy
  from typing import Dict, List, Optional, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule, Scale
  from mmengine.config import ConfigDict
  from mmengine.model import BaseModule, kaiming_init
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures.bbox import cat_boxes
  from mmdet.utils import ConfigType, InstanceList, MultiConfig, OptConfigType, OptInstanceList, reduce_mean
  from ..task_modules.prior_generators import MlvlPointGenerator
  from ..utils import aligned_bilinear, filter_scores_and_topk, multi_apply, relative_coordinate_maps, select_single_mlvl
  from ..utils.misc import empty_instances
  from .base_mask_head import BaseMaskHead
  from .fcos_head import FCOSHead
label_desc: Use image node_id 6 for calling the CondInstBboxHead obj's underlying member method code with example usage: obj._get_targets_single(gt_instances, points, regress_ranges, num_points_per_lvl) and returns: labels,  bbox_targets,  pos_inds,  pos_gt_inds, gt_labels,  gt_bboxes,  gt_bboxes,  gt_bboxes