class_docstr: YOLACT box head used in https://arxiv.org/abs/1904.02689.
  
  Note that YOLACT head is a light version of RetinaNet head.
  Four differences are described as follows:
  
  1. YOLACT box head has three-times fewer anchors.
  2. YOLACT box head shares the convs for box and cls branches.
  3. YOLACT box head uses OHEM instead of Focal loss.
  4. YOLACT box head predicts a set of mask coefficients for each box.
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      anchor_generator (:obj:`ConfigDict` or dict): Config dict for
          anchor generator
      loss_cls (:obj:`ConfigDict` or dict): Config of classification loss.
      loss_bbox (:obj:`ConfigDict` or dict): Config of localization loss.
      num_head_convs (int): Number of the conv layers shared by
          box and cls branches.
      num_protos (int): Number of the mask coefficients.
      use_ohem (bool): If true, ``loss_single_OHEM`` will be used for
          cls loss calculation. If false, ``loss_single`` will be used.
      conv_cfg (:obj:`ConfigDict` or dict, optional): Dictionary to
          construct and config conv layer.
      norm_cfg (:obj:`ConfigDict` or dict, optional): Dictionary to
          construct and config norm layer.
      init_cfg (:obj:`ConfigDict` or list[:obj:`ConfigDict`] or dict or
          list[dict], optional): Initialization config dict.
class_docstr_tok: ["YOLACT","box","head","used","in","https",":","\/\/arxiv.org\/abs\/1904.02689",".","Note","that","YOLACT","head","is","a","light","version","of","RetinaNet","head",".","Four","differences","are","described","as","follows",":","1",".","YOLACT","box","head","has","three-times","fewer","anchors",".","2",".","YOLACT","box","head","shares","the","convs","for","box","and","cls","branches",".","3",".","YOLACT","box","head","uses","OHEM","instead","of","Focal","loss",".","4",".","YOLACT","box","head","predicts","a","set","of","mask","coefficients","for","each","box",".","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","anchor_generator","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","anchor","generator","loss_cls","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","classification","loss",".","loss_bbox","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","localization","loss",".","num_head_convs","(","int",")",":","Number","of","the","conv","layers","shared","by","box","and","cls","branches",".","num_protos","(","int",")",":","Number","of","the","mask","coefficients",".","use_ohem","(","bool",")",":","If","true",",","``","loss_single_OHEM","``","will","be","used","for","cls","loss","calculation",".","If","false",",","``","loss_single","``","will","be","used",".","conv_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Dictionary","to","construct","and","config","conv","layer",".","norm_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Dictionary","to","construct","and","config","norm","layer",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","list","[",":","obj",":","`","ConfigDict","`","]","or","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict","."]
docstr: Compute loss of a single image. Similar to
  func:``SSDHead.loss_by_feat_single``
  
  Args:
      cls_score (Tensor): Box scores for eachimage
          Has shape (num_total_anchors, num_classes).
      bbox_pred (Tensor): Box energies / deltas for each image
          level with shape (num_total_anchors, 4).
      anchors (Tensor): Box reference for each scale level with shape
          (num_total_anchors, 4).
      labels (Tensor): Labels of each anchors with shape
          (num_total_anchors,).
      label_weights (Tensor): Label weights of each anchor with shape
          (num_total_anchors,)
      bbox_targets (Tensor): BBox regression targets of each anchor with
          shape (num_total_anchors, 4).
      bbox_weights (Tensor): BBox regression loss weights of each anchor
          with shape (num_total_anchors, 4).
      avg_factor (int): Average factor that is used to average
          the loss. When using sampling method, avg_factor is usually
          the sum of positive and negative priors. When using
          `PseudoSampler`, `avg_factor` is usually equal to the number
          of positive priors.
  
  Returns:
      Tuple[Tensor, Tensor]: A tuple of cls loss and bbox loss of one
      feature map.
docstr_tok: ["Compute","loss","of","a","single","image",".","Similar","to","func",":","``","SSDHead.loss_by_feat_single","``","Args",":","cls_score","(","Tensor",")",":","Box","scores","for","eachimage","Has","shape","(","num_total_anchors",",","num_classes",")",".","bbox_pred","(","Tensor",")",":","Box","energies","\/","deltas","for","each","image","level","with","shape","(","num_total_anchors",",","4",")",".","anchors","(","Tensor",")",":","Box","reference","for","each","scale","level","with","shape","(","num_total_anchors",",","4",")",".","labels","(","Tensor",")",":","Labels","of","each","anchors","with","shape","(","num_total_anchors",",",")",".","label_weights","(","Tensor",")",":","Label","weights","of","each","anchor","with","shape","(","num_total_anchors",",",")","bbox_targets","(","Tensor",")",":","BBox","regression","targets","of","each","anchor","with","shape","(","num_total_anchors",",","4",")",".","bbox_weights","(","Tensor",")",":","BBox","regression","loss","weights","of","each","anchor","with","shape","(","num_total_anchors",",","4",")",".","avg_factor","(","int",")",":","Average","factor","that","is","used","to","average","the","loss",".","When","using","sampling","method",",","avg_factor","is","usually","the","sum","of","positive","and","negative","priors",".","When","using","`","PseudoSampler","`",",","`","avg_factor","`","is","usually","equal","to","the","number","of","positive","priors",".","Returns",":","Tuple","[","Tensor",",","Tensor","]",":","A","tuple","of","cls","loss","and","bbox","loss","of","one","feature","map","."]
code: def OHEMloss_by_feat_single(
      self,
      cls_score: Tensor,
      bbox_pred: Tensor,
      anchors: Tensor,
      labels: Tensor,
      label_weights: Tensor,
      bbox_targets: Tensor,
      bbox_weights: Tensor,
      avg_factor: int,
  ) -> tuple:
      """Compute loss of a single image. Similar to
      func:``SSDHead.loss_by_feat_single``
  
      Args:
          cls_score (Tensor): Box scores for eachimage
              Has shape (num_total_anchors, num_classes).
          bbox_pred (Tensor): Box energies / deltas for each image
              level with shape (num_total_anchors, 4).
          anchors (Tensor): Box reference for each scale level with shape
              (num_total_anchors, 4).
          labels (Tensor): Labels of each anchors with shape
              (num_total_anchors,).
          label_weights (Tensor): Label weights of each anchor with shape
              (num_total_anchors,)
          bbox_targets (Tensor): BBox regression targets of each anchor with
              shape (num_total_anchors, 4).
          bbox_weights (Tensor): BBox regression loss weights of each anchor
              with shape (num_total_anchors, 4).
          avg_factor (int): Average factor that is used to average
              the loss. When using sampling method, avg_factor is usually
              the sum of positive and negative priors. When using
              `PseudoSampler`, `avg_factor` is usually equal to the number
              of positive priors.
  
      Returns:
          Tuple[Tensor, Tensor]: A tuple of cls loss and bbox loss of one
          feature map.
      """
  
      loss_cls_all = self.loss_cls(cls_score, labels, label_weights)
  
      # FG cat_id: [0, num_classes -1], BG cat_id: num_classes
      pos_inds = (
          ((labels >= 0) & (labels < self.num_classes))
          .nonzero(as_tuple=False)
          .reshape(-1)
      )
      neg_inds = (
          (labels == self.num_classes).nonzero(as_tuple=False).view(-1)
      )
  
      num_pos_samples = pos_inds.size(0)
      if num_pos_samples == 0:
          num_neg_samples = neg_inds.size(0)
      else:
          num_neg_samples = (
              self.train_cfg["neg_pos_ratio"] * num_pos_samples
          )
          if num_neg_samples > neg_inds.size(0):
              num_neg_samples = neg_inds.size(0)
      topk_loss_cls_neg, _ = loss_cls_all[neg_inds].topk(
          num_neg_samples
      )
      loss_cls_pos = loss_cls_all[pos_inds].sum()
      loss_cls_neg = topk_loss_cls_neg.sum()
      loss_cls = (loss_cls_pos + loss_cls_neg) / avg_factor
      if self.reg_decoded_bbox:
          # When the regression loss (e.g. `IouLoss`, `GIouLoss`)
          # is applied directly on the decoded bounding boxes, it
          # decodes the already encoded coordinates to absolute format.
          bbox_pred = self.bbox_coder.decode(anchors, bbox_pred)
      loss_bbox = self.loss_bbox(
          bbox_pred, bbox_targets, bbox_weights, avg_factor=avg_factor
      )
      return loss_cls[None], loss_bbox
  
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 YOLACTHead obj's underlying member method code with example usage: obj.OHEMloss_by_feat_single(cls_score, bbox_pred, anchors, labels, label_weights, bbox_targets, bbox_weights, avg_factor) and returns: loss_cls,  loss_bbox