class_docstr: Decoupled SOLO mask head used in `SOLO: Segmenting Objects by Locations.
  
  <https://arxiv.org/abs/1912.04488>`_
  
  Args:
      init_cfg (dict or list[dict], optional): Initialization config dict.
docstr: Calculate the loss based on the features extracted by the mask head.
  
  Args:
      mlvl_mask_preds_x (list[Tensor]): Multi-level mask prediction
          from x branch. Each element in the list has shape
          (batch_size, num_grids ,h ,w).
      mlvl_mask_preds_y (list[Tensor]): Multi-level mask prediction
          from y branch. Each element in the list has shape
          (batch_size, num_grids ,h ,w).
      mlvl_cls_preds (list[Tensor]): Multi-level scores. Each element
          in the list has shape
          (batch_size, num_classes, num_grids ,num_grids).
      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.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
code: def loss_by_feat(
      self,
      mlvl_mask_preds_x: List[Tensor],
      mlvl_mask_preds_y: List[Tensor],
      mlvl_cls_preds: List[Tensor],
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      **kwargs,
  ) -> dict:
      """Calculate the loss based on the features extracted by the mask head.
  
      Args:
          mlvl_mask_preds_x (list[Tensor]): Multi-level mask prediction
              from x branch. Each element in the list has shape
              (batch_size, num_grids ,h ,w).
          mlvl_mask_preds_y (list[Tensor]): Multi-level mask prediction
              from y branch. Each element in the list has shape
              (batch_size, num_grids ,h ,w).
          mlvl_cls_preds (list[Tensor]): Multi-level scores. Each element
              in the list has shape
              (batch_size, num_classes, num_grids ,num_grids).
          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.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      num_levels = self.num_levels
      num_imgs = len(batch_img_metas)
      featmap_sizes = [
          featmap.size()[-2:] for featmap in mlvl_mask_preds_x
      ]
  
      pos_mask_targets, labels, xy_pos_indexes = multi_apply(
          self._get_targets_single,
          batch_gt_instances,
          featmap_sizes=featmap_sizes,
      )
  
      # change from the outside list meaning multi images
      # to the outside list meaning multi levels
      mlvl_pos_mask_targets = [[] for _ in range(num_levels)]
      mlvl_pos_mask_preds_x = [[] for _ in range(num_levels)]
      mlvl_pos_mask_preds_y = [[] for _ in range(num_levels)]
      mlvl_labels = [[] for _ in range(num_levels)]
      for img_id in range(num_imgs):
          for lvl in range(num_levels):
              mlvl_pos_mask_targets[lvl].append(
                  pos_mask_targets[img_id][lvl]
              )
              mlvl_pos_mask_preds_x[lvl].append(
                  mlvl_mask_preds_x[lvl][
                      img_id, xy_pos_indexes[img_id][lvl][:, 1]
                  ]
              )
              mlvl_pos_mask_preds_y[lvl].append(
                  mlvl_mask_preds_y[lvl][
                      img_id, xy_pos_indexes[img_id][lvl][:, 0]
                  ]
              )
              mlvl_labels[lvl].append(labels[img_id][lvl].flatten())
  
      # cat multiple image
      temp_mlvl_cls_preds = []
      for lvl in range(num_levels):
          mlvl_pos_mask_targets[lvl] = torch.cat(
              mlvl_pos_mask_targets[lvl], dim=0
          )
          mlvl_pos_mask_preds_x[lvl] = torch.cat(
              mlvl_pos_mask_preds_x[lvl], dim=0
          )
          mlvl_pos_mask_preds_y[lvl] = torch.cat(
              mlvl_pos_mask_preds_y[lvl], dim=0
          )
          mlvl_labels[lvl] = torch.cat(mlvl_labels[lvl], dim=0)
          temp_mlvl_cls_preds.append(
              mlvl_cls_preds[lvl]
              .permute(0, 2, 3, 1)
              .reshape(-1, self.cls_out_channels)
          )
  
      num_pos = 0.0
      # dice loss
      loss_mask = []
      for pred_x, pred_y, target in zip(
          mlvl_pos_mask_preds_x,
          mlvl_pos_mask_preds_y,
          mlvl_pos_mask_targets,
      ):
          num_masks = pred_x.size(0)
          if num_masks == 0:
              # make sure can get grad
              loss_mask.append(
                  (pred_x.sum() + pred_y.sum()).unsqueeze(0)
              )
              continue
          num_pos += num_masks
          pred_mask = pred_y.sigmoid() * pred_x.sigmoid()
          loss_mask.append(
              self.loss_mask(
                  pred_mask, target, reduction_override="none"
              )
          )
      if num_pos > 0:
          loss_mask = torch.cat(loss_mask).sum() / num_pos
      else:
          loss_mask = torch.cat(loss_mask).mean()
  
      # cate
      flatten_labels = torch.cat(mlvl_labels)
      flatten_cls_preds = torch.cat(temp_mlvl_cls_preds)
  
      loss_cls = self.loss_cls(
          flatten_cls_preds, flatten_labels, avg_factor=num_pos + 1
      )
      return dict(loss_mask=loss_mask, loss_cls=loss_cls)
  
imports: from typing import List, Optional, Tuple
  import mmcv
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.models.utils.misc import floordiv
  from mmdet.registry import MODELS
  from mmdet.utils import ConfigType, InstanceList, MultiConfig, OptConfigType
  from ..layers import mask_matrix_nms
  from ..utils import center_of_mass, generate_coordinate, multi_apply
  from .base_mask_head import BaseMaskHead
label_desc: Use image node_id 4 for calling the DecoupledSOLOHead obj's underlying member method code with example usage: obj.loss_by_feat(mlvl_mask_preds_x, mlvl_mask_preds_y, mlvl_cls_preds, batch_gt_instances, batch_img_metas) and returns: dict