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: Calculate the loss based on the features extracted by the mask head.
  
  Args:
      mask_preds (list[Tensor]): List of predicted prototypes, each has
          shape (num_classes, H, W).
      segm_preds (Tensor):  Predicted semantic segmentation map with
          shape (N, num_classes, H, W)
      batch_gt_instances (list[:obj:`InstanceData`]): Batch of
          gt_instance. It usually includes ``bboxes``, ``masks``,
          and ``labels`` attributes.
      batch_img_metas (list[dict]): Meta information of multiple images.
      positive_infos (List[:obj:``InstanceData``]): Information of
          positive samples of each image that are assigned in detection
          head.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Calculate","the","loss","based","on","the","features","extracted","by","the","mask","head",".","Args",":","mask_preds","(","list","[","Tensor","]",")",":","List","of","predicted","prototypes",",","each","has","shape","(","num_classes",",","H",",","W",")",".","segm_preds","(","Tensor",")",":","Predicted","semantic","segmentation","map","with","shape","(","N",",","num_classes",",","H",",","W",")","batch_gt_instances","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Batch","of","gt_instance",".","It","usually","includes","``","bboxes","``",",","``","masks","``",",","and","``","labels","``","attributes",".","batch_img_metas","(","list","[","dict","]",")",":","Meta","information","of","multiple","images",".","positive_infos","(","List","[",":","obj",":","``","InstanceData","``","]",")",":","Information","of","positive","samples","of","each","image","that","are","assigned","in","detection","head",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_by_feat(
      self,
      mask_preds: List[Tensor],
      segm_preds: List[Tensor],
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      positive_infos: InstanceList,
      **kwargs,
  ) -> dict:
      """Calculate the loss based on the features extracted by the mask head.
  
      Args:
          mask_preds (list[Tensor]): List of predicted prototypes, each has
              shape (num_classes, H, W).
          segm_preds (Tensor):  Predicted semantic segmentation map with
              shape (N, num_classes, H, W)
          batch_gt_instances (list[:obj:`InstanceData`]): Batch of
              gt_instance. It usually includes ``bboxes``, ``masks``,
              and ``labels`` attributes.
          batch_img_metas (list[dict]): Meta information of multiple images.
          positive_infos (List[:obj:``InstanceData``]): Information of
              positive samples of each image that are assigned in detection
              head.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      assert (
          positive_infos is not None
      ), "positive_infos should not be None in `YOLACTProtonet`"
      losses = dict()
  
      # crop
      croped_mask_pred = self.crop_mask_preds(
          mask_preds, batch_img_metas, positive_infos
      )
  
      loss_mask = []
      loss_segm = []
      num_imgs, _, mask_h, mask_w = segm_preds.size()
      assert num_imgs == len(croped_mask_pred)
      segm_avg_factor = num_imgs * mask_h * mask_w
      total_pos = 0
  
      if self.segm_branch is not None:
          assert segm_preds is not None
  
      for idx in range(num_imgs):
          img_meta = batch_img_metas[idx]
  
          (
              mask_preds,
              pos_mask_targets,
              segm_targets,
              num_pos,
              gt_bboxes_for_reweight,
          ) = self._get_targets_single(
              croped_mask_pred[idx],
              segm_preds[idx],
              batch_gt_instances[idx],
              positive_infos[idx],
          )
  
          # segmentation loss
          if self.with_seg_branch:
              if segm_targets is None:
                  loss = segm_preds[idx].sum() * 0.0
              else:
                  loss = self.loss_segm(
                      segm_preds[idx],
                      segm_targets,
                      avg_factor=segm_avg_factor,
                  )
              loss_segm.append(loss)
          # mask loss
          total_pos += num_pos
          if num_pos == 0 or pos_mask_targets is None:
              loss = mask_preds.sum() * 0.0
          else:
              mask_preds = torch.clamp(mask_preds, 0, 1)
              loss = (
                  F.binary_cross_entropy(
                      mask_preds, pos_mask_targets, reduction="none"
                  )
                  * self.loss_mask_weight
              )
  
              h, w = img_meta["img_shape"][:2]
              gt_bboxes_width = (
                  gt_bboxes_for_reweight[:, 2]
                  - gt_bboxes_for_reweight[:, 0]
              ) / w
              gt_bboxes_height = (
                  gt_bboxes_for_reweight[:, 3]
                  - gt_bboxes_for_reweight[:, 1]
              ) / h
              loss = (
                  loss.mean(dim=(1, 2))
                  / gt_bboxes_width
                  / gt_bboxes_height
              )
              loss = torch.sum(loss)
          loss_mask.append(loss)
  
      if total_pos == 0:
          total_pos += 1  # avoid nan
      loss_mask = [x / total_pos for x in loss_mask]
  
      losses.update(loss_mask=loss_mask)
      if self.with_seg_branch:
          losses.update(loss_segm=loss_segm)
  
      return losses
  
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 4 for calling the YOLACTProtonet obj's underlying member method code with example usage: obj.loss_by_feat(mask_preds, segm_preds, batch_gt_instances, batch_img_metas, positive_infos) and returns: losses