class_docstr: CondInst mask head used in https://arxiv.org/abs/1904.02689.
  
  This head outputs the mask for CondInst.
  
  Args:
      mask_feature_head (dict): Config of CondInstMaskFeatHead.
      num_layers (int): Number of dynamic conv layers.
      feat_channels (int): Number of channels in the dynamic conv.
      mask_out_stride (int): The stride of the mask feat.
      size_of_interest (int): The size of the region used in rel coord.
      max_masks_to_train (int): Maximum number of masks to train for
          each image.
      loss_segm (:obj:`ConfigDict` or dict, optional): Config of
          segmentation loss.
      train_cfg (:obj:`ConfigDict` or dict, optional): Training config
          of head.
      test_cfg (:obj:`ConfigDict` or dict, optional): Testing config of
          head.
class_docstr_tok: ["CondInst","mask","head","used","in","https",":","\/\/arxiv.org\/abs\/1904.02689",".","This","head","outputs","the","mask","for","CondInst",".","Args",":","mask_feature_head","(","dict",")",":","Config","of","CondInstMaskFeatHead",".","num_layers","(","int",")",":","Number","of","dynamic","conv","layers",".","feat_channels","(","int",")",":","Number","of","channels","in","the","dynamic","conv",".","mask_out_stride","(","int",")",":","The","stride","of","the","mask","feat",".","size_of_interest","(","int",")",":","The","size","of","the","region","used","in","rel","coord",".","max_masks_to_train","(","int",")",":","Maximum","number","of","masks","to","train","for","each","image",".","loss_segm","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Config","of","segmentation","loss",".","train_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Training","config","of","head",".","test_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Testing","config","of","head","."]
docstr: Compute targets for predictions of single image.
  
  Args:
      mask_preds (Tensor): Predicted prototypes with shape
          (num_classes, H, W).
      gt_instances (:obj:`InstanceData`): Ground truth of instance
          annotations. It should includes ``bboxes``, ``labels``,
          and ``masks`` attributes.
      positive_info (:obj:`InstanceData`): Information of positive
          samples that are assigned in detection head. It usually
          contains following keys.
  
              - pos_assigned_gt_inds (Tensor): Assigner GT indexes of
                positive proposals, has shape (num_pos, )
              - pos_inds (Tensor): Positive index of image, has
                shape (num_pos, ).
              - param_pred (Tensor): Positive param preditions
                with shape (num_pos, num_params).
  
  Returns:
      tuple: Usually returns a tuple containing learning targets.
  
      - mask_preds (Tensor): Positive predicted mask with shape
        (num_pos, mask_h, mask_w).
      - pos_mask_targets (Tensor): Positive mask targets with shape
        (num_pos, mask_h, mask_w).
      - num_pos (int): Positive numbers.
docstr_tok: ["Compute","targets","for","predictions","of","single","image",".","Args",":","mask_preds","(","Tensor",")",":","Predicted","prototypes","with","shape","(","num_classes",",","H",",","W",")",".","gt_instances","(",":","obj",":","`","InstanceData","`",")",":","Ground","truth","of","instance","annotations",".","It","should","includes","``","bboxes","``",",","``","labels","``",",","and","``","masks","``","attributes",".","positive_info","(",":","obj",":","`","InstanceData","`",")",":","Information","of","positive","samples","that","are","assigned","in","detection","head",".","It","usually","contains","following","keys",".","-","pos_assigned_gt_inds","(","Tensor",")",":","Assigner","GT","indexes","of","positive","proposals",",","has","shape","(","num_pos",",",")","-","pos_inds","(","Tensor",")",":","Positive","index","of","image",",","has","shape","(","num_pos",",",")",".","-","param_pred","(","Tensor",")",":","Positive","param","preditions","with","shape","(","num_pos",",","num_params",")",".","Returns",":","tuple",":","Usually","returns","a","tuple","containing","learning","targets",".","-","mask_preds","(","Tensor",")",":","Positive","predicted","mask","with","shape","(","num_pos",",","mask_h",",","mask_w",")",".","-","pos_mask_targets","(","Tensor",")",":","Positive","mask","targets","with","shape","(","num_pos",",","mask_h",",","mask_w",")",".","-","num_pos","(","int",")",":","Positive","numbers","."]
code: def _get_targets_single(
      self,
      mask_preds: Tensor,
      gt_instances: InstanceData,
      positive_info: InstanceData,
  ):
      """Compute targets for predictions of single image.
  
      Args:
          mask_preds (Tensor): Predicted prototypes with shape
              (num_classes, H, W).
          gt_instances (:obj:`InstanceData`): Ground truth of instance
              annotations. It should includes ``bboxes``, ``labels``,
              and ``masks`` attributes.
          positive_info (:obj:`InstanceData`): Information of positive
              samples that are assigned in detection head. It usually
              contains following keys.
  
                  - pos_assigned_gt_inds (Tensor): Assigner GT indexes of
                    positive proposals, has shape (num_pos, )
                  - pos_inds (Tensor): Positive index of image, has
                    shape (num_pos, ).
                  - param_pred (Tensor): Positive param preditions
                    with shape (num_pos, num_params).
  
      Returns:
          tuple: Usually returns a tuple containing learning targets.
  
          - mask_preds (Tensor): Positive predicted mask with shape
            (num_pos, mask_h, mask_w).
          - pos_mask_targets (Tensor): Positive mask targets with shape
            (num_pos, mask_h, mask_w).
          - num_pos (int): Positive numbers.
      """
      gt_bboxes = gt_instances.bboxes
      device = gt_bboxes.device
      gt_masks = gt_instances.masks.to_tensor(
          dtype=torch.bool, device=device
      ).float()
  
      # process with mask targets
      pos_assigned_gt_inds = positive_info.get("pos_assigned_gt_inds")
      scores = positive_info.get("scores")
      centernesses = positive_info.get("centernesses")
      num_pos = pos_assigned_gt_inds.size(0)
  
      if gt_masks.size(0) == 0 or num_pos == 0:
          return mask_preds, None, 0
      # Since we're producing (near) full image masks,
      # it'd take too much vram to backprop on every single mask.
      # Thus we select only a subset.
      if (self.max_masks_to_train != -1) and (
          num_pos > self.max_masks_to_train
      ):
          perm = torch.randperm(num_pos)
          select = perm[: self.max_masks_to_train]
          mask_preds = mask_preds[select]
          pos_assigned_gt_inds = pos_assigned_gt_inds[select]
          num_pos = self.max_masks_to_train
      elif self.topk_masks_per_img != -1:
          unique_gt_inds = pos_assigned_gt_inds.unique()
          num_inst_per_gt = max(
              int(self.topk_masks_per_img / len(unique_gt_inds)), 1
          )
  
          keep_mask_preds = []
          keep_pos_assigned_gt_inds = []
          for gt_ind in unique_gt_inds:
              per_inst_pos_inds = pos_assigned_gt_inds == gt_ind
              mask_preds_per_inst = mask_preds[per_inst_pos_inds]
              gt_inds_per_inst = pos_assigned_gt_inds[per_inst_pos_inds]
              if sum(per_inst_pos_inds) > num_inst_per_gt:
                  per_inst_scores = (
                      scores[per_inst_pos_inds].sigmoid().max(dim=1)[0]
                  )
                  per_inst_centerness = (
                      centernesses[per_inst_pos_inds]
                      .sigmoid()
                      .reshape(
                          -1,
                      )
                  )
                  select = (per_inst_scores * per_inst_centerness).topk(
                      k=num_inst_per_gt, dim=0
                  )[1]
                  mask_preds_per_inst = mask_preds_per_inst[select]
                  gt_inds_per_inst = gt_inds_per_inst[select]
              keep_mask_preds.append(mask_preds_per_inst)
              keep_pos_assigned_gt_inds.append(gt_inds_per_inst)
          mask_preds = torch.cat(keep_mask_preds)
          pos_assigned_gt_inds = torch.cat(keep_pos_assigned_gt_inds)
          num_pos = pos_assigned_gt_inds.size(0)
  
      # Follow the origin implement
      start = int(self.mask_out_stride // 2)
      gt_masks = gt_masks[
          :,
          start :: self.mask_out_stride,
          start :: self.mask_out_stride,
      ]
      gt_masks = gt_masks.gt(0.5).float()
      pos_mask_targets = gt_masks[pos_assigned_gt_inds]
  
      return (mask_preds, pos_mask_targets, num_pos)
  
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 8 for calling the CondInstMaskHead obj's underlying member method code with example usage: obj._get_targets_single(mask_preds, gt_instances, positive_info) and returns: mask_preds,  pos_mask_targets,  num_pos, mask_preds,  None,  int