class_docstr: BoxInst mask head used in https://arxiv.org/abs/2012.02310.
  
  This head outputs the mask for BoxInst.
  
  Args:
      pairwise_size (dict): The size of neighborhood for each pixel.
          Defaults to 3.
      pairwise_dilation (int): The dilation of neighborhood for each pixel.
          Defaults to 2.
      warmup_iters (int): Warmup iterations for pair-wise loss.
          Defaults to 10000.
class_docstr_tok: ["BoxInst","mask","head","used","in","https",":","\/\/arxiv.org\/abs\/2012.02310",".","This","head","outputs","the","mask","for","BoxInst",".","Args",":","pairwise_size","(","dict",")",":","The","size","of","neighborhood","for","each","pixel",".","Defaults","to","3.","pairwise_dilation","(","int",")",":","The","dilation","of","neighborhood","for","each","pixel",".","Defaults","to","2.","warmup_iters","(","int",")",":","Warmup","iterations","for","pair-wise","loss",".","Defaults","to","10000","."]
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).
      - pos_pairwise_masks (Tensor): Positive pairwise masks with
        shape: (num_pos, num_neighborhood, 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",")",".","-","pos_pairwise_masks","(","Tensor",")",":","Positive","pairwise","masks","with","shape",":","(","num_pos",",","num_neighborhood",",","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).
          - pos_pairwise_masks (Tensor): Positive pairwise masks with
            shape: (num_pos, num_neighborhood, mask_h, mask_w).
          - num_pos (int): Positive numbers.
      """
      gt_bboxes = gt_instances.bboxes
      device = gt_bboxes.device
      # Note that gt_masks are generated by full box
      # from BoxInstDataPreprocessor
      gt_masks = gt_instances.masks.to_tensor(
          dtype=torch.bool, device=device
      ).float()
      # Note that pairwise_masks are generated by image color similarity
      # from BoxInstDataPreprocessor
      pairwise_masks = gt_instances.pairwise_masks
      pairwise_masks = pairwise_masks.to(device=device)
  
      # 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, 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]
      pos_pairwise_masks = pairwise_masks[pos_assigned_gt_inds]
      pos_pairwise_masks = (
          pos_pairwise_masks * pos_mask_targets.unsqueeze(1)
      )
  
      return (mask_preds, pos_mask_targets, pos_pairwise_masks, num_pos)
  
file_path: mmdetection/mmdet/models/dense_heads/boxinst_head.py
imports: from typing import List
  import torch
  import torch.nn.functional
  from mmengine import MessageHub
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.utils import InstanceList
  from ..utils.misc import unfold_wo_center
  from .condinst_head import CondInstBboxHead, CondInstMaskHead
label_desc: Use image node_id 4 for calling the BoxInstMaskHead 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,  pos_pairwise_masks,  num_pos, mask_preds,  None,  None,  int