class_docstr: YOLACT mask head used in https://arxiv.org/abs/1904.02689.
  
  This head outputs the mask prototypes for YOLACT.
  
  Args:
      in_channels (int): Number of channels in the input feature map.
      proto_channels (tuple[int]): Output channels of protonet convs.
      proto_kernel_sizes (tuple[int]): Kernel sizes of protonet convs.
      include_last_relu (bool): If keep the last relu of protonet.
      num_protos (int): Number of prototypes.
      num_classes (int): Number of categories excluding the background
          category.
      loss_mask_weight (float): Reweight the mask loss by this factor.
      max_masks_to_train (int): Maximum number of masks to train for
          each image.
      with_seg_branch (bool): Whether to apply a semantic segmentation
          branch and calculate loss during training to increase
          performance with no speed penalty. Defaults to True.
      loss_segm (:obj:`ConfigDict` or dict, optional): Config of
          semantic segmentation loss.
      train_cfg (:obj:`ConfigDict` or dict, optional): Training config
          of head.
      test_cfg (:obj:`ConfigDict` or dict, optional): Testing config of
          head.
      init_cfg (:obj:`ConfigDict` or list[:obj:`ConfigDict`] or dict or
          list[dict], optional): Initialization config dict.
class_docstr_tok: ["YOLACT","mask","head","used","in","https",":","\/\/arxiv.org\/abs\/1904.02689",".","This","head","outputs","the","mask","prototypes","for","YOLACT",".","Args",":","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","proto_channels","(","tuple","[","int","]",")",":","Output","channels","of","protonet","convs",".","proto_kernel_sizes","(","tuple","[","int","]",")",":","Kernel","sizes","of","protonet","convs",".","include_last_relu","(","bool",")",":","If","keep","the","last","relu","of","protonet",".","num_protos","(","int",")",":","Number","of","prototypes",".","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","loss_mask_weight","(","float",")",":","Reweight","the","mask","loss","by","this","factor",".","max_masks_to_train","(","int",")",":","Maximum","number","of","masks","to","train","for","each","image",".","with_seg_branch","(","bool",")",":","Whether","to","apply","a","semantic","segmentation","branch","and","calculate","loss","during","training","to","increase","performance","with","no","speed","penalty",".","Defaults","to","True",".","loss_segm","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Config","of","semantic","segmentation","loss",".","train_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Training","config","of","head",".","test_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Testing","config","of","head",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","list","[",":","obj",":","`","ConfigDict","`","]","or","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict","."]
docstr: Compute targets for predictions of single image.
  
  Args:
      mask_preds (Tensor): Predicted prototypes with shape
          (num_classes, H, W).
      segm_pred (Tensor): Predicted semantic segmentation map
          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, ).
              - coeffs (Tensor): Positive mask coefficients
                with shape (num_pos, num_protos).
              - bboxes (Tensor): Positive bboxes with shape
                (num_pos, 4)
  
  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).
      - segm_targets (Tensor): Semantic segmentation targets with shape
        (num_classes, segm_h, segm_w).
      - num_pos (int): Positive numbers.
      - gt_bboxes_for_reweight (Tensor): GT bboxes that match to the
        positive priors has shape (num_pos, 4).
docstr_tok: ["Compute","targets","for","predictions","of","single","image",".","Args",":","mask_preds","(","Tensor",")",":","Predicted","prototypes","with","shape","(","num_classes",",","H",",","W",")",".","segm_pred","(","Tensor",")",":","Predicted","semantic","segmentation","map","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",",",")",".","-","coeffs","(","Tensor",")",":","Positive","mask","coefficients","with","shape","(","num_pos",",","num_protos",")",".","-","bboxes","(","Tensor",")",":","Positive","bboxes","with","shape","(","num_pos",",","4",")","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",")",".","-","segm_targets","(","Tensor",")",":","Semantic","segmentation","targets","with","shape","(","num_classes",",","segm_h",",","segm_w",")",".","-","num_pos","(","int",")",":","Positive","numbers",".","-","gt_bboxes_for_reweight","(","Tensor",")",":","GT","bboxes","that","match","to","the","positive","priors","has","shape","(","num_pos",",","4",")","."]
code: def _get_targets_single(
      self,
      mask_preds: Tensor,
      segm_pred: 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).
          segm_pred (Tensor): Predicted semantic segmentation map
              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, ).
                  - coeffs (Tensor): Positive mask coefficients
                    with shape (num_pos, num_protos).
                  - bboxes (Tensor): Positive bboxes with shape
                    (num_pos, 4)
  
      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).
          - segm_targets (Tensor): Semantic segmentation targets with shape
            (num_classes, segm_h, segm_w).
          - num_pos (int): Positive numbers.
          - gt_bboxes_for_reweight (Tensor): GT bboxes that match to the
            positive priors has shape (num_pos, 4).
      """
      gt_bboxes = gt_instances.bboxes
      gt_labels = gt_instances.labels
      device = gt_bboxes.device
      gt_masks = gt_instances.masks.to_tensor(
          dtype=torch.bool, device=device
      ).float()
      if gt_masks.size(0) == 0:
          return mask_preds, None, None, 0, None
  
      # process with semantic segmentation targets
      if segm_pred is not None:
          num_classes, segm_h, segm_w = segm_pred.size()
          with torch.no_grad():
              downsampled_masks = F.interpolate(
                  gt_masks.unsqueeze(0),
                  (segm_h, segm_w),
                  mode="bilinear",
                  align_corners=False,
              ).squeeze(0)
              downsampled_masks = downsampled_masks.gt(0.5).float()
              segm_targets = torch.zeros_like(
                  segm_pred, requires_grad=False
              )
              for obj_idx in range(downsampled_masks.size(0)):
                  segm_targets[gt_labels[obj_idx] - 1] = torch.max(
                      segm_targets[gt_labels[obj_idx] - 1],
                      downsampled_masks[obj_idx],
                  )
      else:
          segm_targets = None
      # process with mask targets
      pos_assigned_gt_inds = positive_info.pos_assigned_gt_inds
      num_pos = pos_assigned_gt_inds.size(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 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
  
      gt_bboxes_for_reweight = gt_bboxes[pos_assigned_gt_inds]
  
      mask_h, mask_w = mask_preds.shape[-2:]
      gt_masks = F.interpolate(
          gt_masks.unsqueeze(0),
          (mask_h, mask_w),
          mode="bilinear",
          align_corners=False,
      ).squeeze(0)
      gt_masks = gt_masks.gt(0.5).float()
      pos_mask_targets = gt_masks[pos_assigned_gt_inds]
  
      return (
          mask_preds,
          pos_mask_targets,
          segm_targets,
          num_pos,
          gt_bboxes_for_reweight,
      )
  
imports: import copy
  from typing import List, Optional
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule
  from mmengine.model import BaseModule, ModuleList
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.utils import ConfigType, InstanceList, OptConfigType, OptInstanceList, OptMultiConfig
  from ..layers import fast_nms
  from ..utils import images_to_levels, multi_apply, select_single_mlvl
  from ..utils.misc import empty_instances
  from .anchor_head import AnchorHead
  from .base_mask_head import BaseMaskHead
label_desc: Use image node_id 5 for calling the YOLACTProtonet obj's underlying member method code with example usage: obj._get_targets_single(mask_preds, segm_pred, gt_instances, positive_info) and returns: mask_preds,  pos_mask_targets,  segm_targets,  num_pos,  gt_bboxes_for_reweight, mask_preds,  None,  None,  int,  None