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: Calculate the loss based on the features extracted by the mask head.
  
  Args:
      mask_preds (list[Tensor]): List of predicted masks, each has
          shape (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","masks",",","each","has","shape","(","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],
      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 masks, each has
              shape (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 `BoxInstMaskHead`"
      losses = dict()
  
      loss_mask_project = 0.0
      loss_mask_pairwise = 0.0
      num_imgs = len(mask_preds)
      total_pos = 0.0
      avg_fatcor = 0.0
  
      for idx in range(num_imgs):
          (
              mask_pred,
              pos_mask_targets,
              pos_pairwise_masks,
              num_pos,
          ) = self._get_targets_single(
              mask_preds[idx],
              batch_gt_instances[idx],
              positive_infos[idx],
          )
          # mask loss
          total_pos += num_pos
          if num_pos == 0 or pos_mask_targets is None:
              loss_project = mask_pred.new_zeros(1).mean()
              loss_pairwise = mask_pred.new_zeros(1).mean()
              avg_fatcor += 0.0
          else:
              # compute the project term
              loss_project_x = self.loss_mask(
                  mask_pred.max(dim=1, keepdim=True)[0],
                  pos_mask_targets.max(dim=1, keepdim=True)[0],
                  reduction_override="none",
              ).sum()
              loss_project_y = self.loss_mask(
                  mask_pred.max(dim=2, keepdim=True)[0],
                  pos_mask_targets.max(dim=2, keepdim=True)[0],
                  reduction_override="none",
              ).sum()
              loss_project = loss_project_x + loss_project_y
              # compute the pairwise term
              pairwise_affinity = self.get_pairwise_affinity(mask_pred)
              avg_fatcor += pos_pairwise_masks.sum().clamp(min=1.0)
              loss_pairwise = (
                  pairwise_affinity * pos_pairwise_masks
              ).sum()
  
          loss_mask_project += loss_project
          loss_mask_pairwise += loss_pairwise
  
      if total_pos == 0:
          total_pos += 1  # avoid nan
      if avg_fatcor == 0:
          avg_fatcor += 1  # avoid nan
      loss_mask_project = loss_mask_project / total_pos
      loss_mask_pairwise = loss_mask_pairwise / avg_fatcor
      message_hub = MessageHub.get_current_instance()
      iter = message_hub.get_info("iter")
      warmup_factor = min(iter / float(self.warmup_iters), 1.0)
      loss_mask_pairwise *= warmup_factor
  
      losses.update(
          loss_mask_project=loss_mask_project,
          loss_mask_pairwise=loss_mask_pairwise,
      )
      return losses
  
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 3 for calling the BoxInstMaskHead obj's underlying member method code with example usage: obj.loss_by_feat(mask_preds, batch_gt_instances, batch_img_metas, positive_infos) and returns: losses