class_docstr: SOLOv2 mask head used in `SOLOv2: Dynamic and Fast Instance
  Segmentation. <https://arxiv.org/pdf/2003.10152>`_
  
  Args:
      mask_feature_head (dict): Config of SOLOv2MaskFeatHead.
      dynamic_conv_size (int): Dynamic Conv kernel size. Defaults to 1.
      dcn_cfg (dict): Dcn conv configurations in kernel_convs and cls_conv.
          Defaults to None.
      dcn_apply_to_all_conv (bool): Whether to use dcn in every layer of
          kernel_convs and cls_convs, or only the last layer. It shall be set
          `True` for the normal version of SOLOv2 and `False` for the
          light-weight version. Defaults to True.
      init_cfg (dict or list[dict], optional): Initialization config dict.
class_docstr_tok: ["SOLOv2","mask","head","used","in","`","SOLOv2",":","Dynamic","and","Fast","Instance","Segmentation",".","<","https",":","\/\/arxiv.org\/pdf\/2003.10152",">","`","_","Args",":","mask_feature_head","(","dict",")",":","Config","of","SOLOv2MaskFeatHead",".","dynamic_conv_size","(","int",")",":","Dynamic","Conv","kernel","size",".","Defaults","to","1.","dcn_cfg","(","dict",")",":","Dcn","conv","configurations","in","kernel_convs","and","cls_conv",".","Defaults","to","None",".","dcn_apply_to_all_conv","(","bool",")",":","Whether","to","use","dcn","in","every","layer","of","kernel_convs","and","cls_convs",",","or","only","the","last","layer",".","It","shall","be","set","`","True","`","for","the","normal","version","of","SOLOv2","and","`","False","`","for","the","light-weight","version",".","Defaults","to","True",".","init_cfg","(","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict","."]
docstr: Transform a batch of output features extracted from the head into
  mask results.
  
  Args:
      mlvl_kernel_preds (list[Tensor]): Multi-level dynamic kernel
          prediction. The kernel is used to generate instance
          segmentation masks by dynamic convolution. Each element in the
          list has shape
          (batch_size, kernel_out_channels, num_grids, num_grids).
      mlvl_cls_scores (list[Tensor]): Multi-level scores. Each element
          in the list has shape
          (batch_size, num_classes, num_grids, num_grids).
      mask_feats (Tensor): Unified mask feature map used to generate
          instance segmentation masks by dynamic convolution. Has shape
          (batch_size, mask_out_channels, h, w).
      batch_img_metas (list[dict]): Meta information of all images.
  
  Returns:
      list[:obj:`InstanceData`]: Processed results of multiple
      images.Each :obj:`InstanceData` usually contains
      following keys.
  
          - scores (Tensor): Classification scores, has shape
            (num_instance,).
          - labels (Tensor): Has shape (num_instances,).
          - masks (Tensor): Processed mask results, has
            shape (num_instances, h, w).
docstr_tok: ["Transform","a","batch","of","output","features","extracted","from","the","head","into","mask","results",".","Args",":","mlvl_kernel_preds","(","list","[","Tensor","]",")",":","Multi-level","dynamic","kernel","prediction",".","The","kernel","is","used","to","generate","instance","segmentation","masks","by","dynamic","convolution",".","Each","element","in","the","list","has","shape","(","batch_size",",","kernel_out_channels",",","num_grids",",","num_grids",")",".","mlvl_cls_scores","(","list","[","Tensor","]",")",":","Multi-level","scores",".","Each","element","in","the","list","has","shape","(","batch_size",",","num_classes",",","num_grids",",","num_grids",")",".","mask_feats","(","Tensor",")",":","Unified","mask","feature","map","used","to","generate","instance","segmentation","masks","by","dynamic","convolution",".","Has","shape","(","batch_size",",","mask_out_channels",",","h",",","w",")",".","batch_img_metas","(","list","[","dict","]",")",":","Meta","information","of","all","images",".","Returns",":","list","[",":","obj",":","`","InstanceData","`","]",":","Processed","results","of","multiple","images.Each",":","obj",":","`","InstanceData","`","usually","contains","following","keys",".","-","scores","(","Tensor",")",":","Classification","scores",",","has","shape","(","num_instance",",",")",".","-","labels","(","Tensor",")",":","Has","shape","(","num_instances",",",")",".","-","masks","(","Tensor",")",":","Processed","mask","results",",","has","shape","(","num_instances",",","h",",","w",")","."]
code: def predict_by_feat(
      self,
      mlvl_kernel_preds: List[Tensor],
      mlvl_cls_scores: List[Tensor],
      mask_feats: Tensor,
      batch_img_metas: List[dict],
      **kwargs,
  ) -> InstanceList:
      """Transform a batch of output features extracted from the head into
      mask results.
  
      Args:
          mlvl_kernel_preds (list[Tensor]): Multi-level dynamic kernel
              prediction. The kernel is used to generate instance
              segmentation masks by dynamic convolution. Each element in the
              list has shape
              (batch_size, kernel_out_channels, num_grids, num_grids).
          mlvl_cls_scores (list[Tensor]): Multi-level scores. Each element
              in the list has shape
              (batch_size, num_classes, num_grids, num_grids).
          mask_feats (Tensor): Unified mask feature map used to generate
              instance segmentation masks by dynamic convolution. Has shape
              (batch_size, mask_out_channels, h, w).
          batch_img_metas (list[dict]): Meta information of all images.
  
      Returns:
          list[:obj:`InstanceData`]: Processed results of multiple
          images.Each :obj:`InstanceData` usually contains
          following keys.
  
              - scores (Tensor): Classification scores, has shape
                (num_instance,).
              - labels (Tensor): Has shape (num_instances,).
              - masks (Tensor): Processed mask results, has
                shape (num_instances, h, w).
      """
      num_levels = len(mlvl_cls_scores)
      assert len(mlvl_kernel_preds) == len(mlvl_cls_scores)
  
      for lvl in range(num_levels):
          cls_scores = mlvl_cls_scores[lvl]
          cls_scores = cls_scores.sigmoid()
          local_max = F.max_pool2d(cls_scores, 2, stride=1, padding=1)
          keep_mask = local_max[:, :, :-1, :-1] == cls_scores
          cls_scores = cls_scores * keep_mask
          mlvl_cls_scores[lvl] = cls_scores.permute(0, 2, 3, 1)
  
      result_list = []
      for img_id in range(len(batch_img_metas)):
          img_cls_pred = [
              mlvl_cls_scores[lvl][img_id].view(
                  -1, self.cls_out_channels
              )
              for lvl in range(num_levels)
          ]
          img_mask_feats = mask_feats[[img_id]]
          img_kernel_pred = [
              mlvl_kernel_preds[lvl][img_id]
              .permute(1, 2, 0)
              .view(-1, self.kernel_out_channels)
              for lvl in range(num_levels)
          ]
          img_cls_pred = torch.cat(img_cls_pred, dim=0)
          img_kernel_pred = torch.cat(img_kernel_pred, dim=0)
          result = self._predict_by_feat_single(
              img_kernel_pred,
              img_cls_pred,
              img_mask_feats,
              img_meta=batch_img_metas[img_id],
          )
          result_list.append(result)
      return result_list
  
imports: import warnings
  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.model import BaseModule
  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 .solo_head import SOLOHead
label_desc: Use image node_id 6 for calling the SOLOV2Head obj's underlying member method code with example usage: obj.predict_by_feat(mlvl_kernel_preds, mlvl_cls_scores, mask_feats, batch_img_metas) and returns: result_list