class_docstr: MaskFormer fusion head which postprocesses results for panoptic
  segmentation, instance segmentation and semantic segmentation.
class_docstr_tok: ["MaskFormer","fusion","head","which","postprocesses","results","for","panoptic","segmentation",",","instance","segmentation","and","semantic","segmentation","."]
docstr: Instance segmengation postprocess.
  
  Args:
      mask_cls (Tensor): Classfication outputs of shape
          (num_queries, cls_out_channels) for a image.
          Note `cls_out_channels` should includes
          background.
      mask_pred (Tensor): Mask outputs of shape
          (num_queries, h, w) for a image.
  
  Returns:
      :obj:`InstanceData`: Instance segmentation results.
  
          - scores (Tensor): Classification scores, has a shape
              (num_instance, )
          - labels (Tensor): Labels of bboxes, has a shape
              (num_instances, ).
          - bboxes (Tensor): Has a shape (num_instances, 4),
              the last dimension 4 arrange as (x1, y1, x2, y2).
          - masks (Tensor): Has a shape (num_instances, H, W).
docstr_tok: ["Instance","segmengation","postprocess",".","Args",":","mask_cls","(","Tensor",")",":","Classfication","outputs","of","shape","(","num_queries",",","cls_out_channels",")","for","a","image",".","Note","`","cls_out_channels","`","should","includes","background",".","mask_pred","(","Tensor",")",":","Mask","outputs","of","shape","(","num_queries",",","h",",","w",")","for","a","image",".","Returns",":",":","obj",":","`","InstanceData","`",":","Instance","segmentation","results",".","-","scores","(","Tensor",")",":","Classification","scores",",","has","a","shape","(","num_instance",",",")","-","labels","(","Tensor",")",":","Labels","of","bboxes",",","has","a","shape","(","num_instances",",",")",".","-","bboxes","(","Tensor",")",":","Has","a","shape","(","num_instances",",","4",")",",","the","last","dimension","4","arrange","as","(","x1",",","y1",",","x2",",","y2",")",".","-","masks","(","Tensor",")",":","Has","a","shape","(","num_instances",",","H",",","W",")","."]
code: def instance_postprocess(
      self, mask_cls: Tensor, mask_pred: Tensor
  ) -> InstanceData:
      """Instance segmengation postprocess.
  
      Args:
          mask_cls (Tensor): Classfication outputs of shape
              (num_queries, cls_out_channels) for a image.
              Note `cls_out_channels` should includes
              background.
          mask_pred (Tensor): Mask outputs of shape
              (num_queries, h, w) for a image.
  
      Returns:
          :obj:`InstanceData`: Instance segmentation results.
  
              - scores (Tensor): Classification scores, has a shape
                  (num_instance, )
              - labels (Tensor): Labels of bboxes, has a shape
                  (num_instances, ).
              - bboxes (Tensor): Has a shape (num_instances, 4),
                  the last dimension 4 arrange as (x1, y1, x2, y2).
              - masks (Tensor): Has a shape (num_instances, H, W).
      """
      max_per_image = self.test_cfg.get("max_per_image", 100)
      num_queries = mask_cls.shape[0]
      # shape (num_queries, num_class)
      scores = F.softmax(mask_cls, dim=-1)[:, :-1]
      # shape (num_queries * num_class, )
      labels = (
          torch.arange(self.num_classes, device=mask_cls.device)
          .unsqueeze(0)
          .repeat(num_queries, 1)
          .flatten(0, 1)
      )
      scores_per_image, top_indices = scores.flatten(0, 1).topk(
          max_per_image, sorted=False
      )
      labels_per_image = labels[top_indices]
  
      query_indices = top_indices // self.num_classes
      mask_pred = mask_pred[query_indices]
  
      # extract things
      is_thing = labels_per_image < self.num_things_classes
      scores_per_image = scores_per_image[is_thing]
      labels_per_image = labels_per_image[is_thing]
      mask_pred = mask_pred[is_thing]
  
      mask_pred_binary = (mask_pred > 0).float()
      mask_scores_per_image = (
          mask_pred.sigmoid() * mask_pred_binary
      ).flatten(1).sum(1) / (mask_pred_binary.flatten(1).sum(1) + 1e-6)
      det_scores = scores_per_image * mask_scores_per_image
      mask_pred_binary = mask_pred_binary.bool()
      bboxes = mask2bbox(mask_pred_binary)
  
      results = InstanceData()
      results.bboxes = bboxes
      results.labels = labels_per_image
      results.scores = det_scores
      results.masks = mask_pred_binary
      return results
  
file_path: mmdetection/mmdet/models/seg_heads/panoptic_fusion_heads/maskformer_fusion_head.py
imports: from typing import List
  import torch
  import torch.nn.functional
  from mmengine.structures import InstanceData, PixelData
  from torch import Tensor
  from mmdet.evaluation.functional import INSTANCE_OFFSET
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.mask import mask2bbox
  from mmdet.utils import OptConfigType, OptMultiConfig
  from .base_panoptic_fusion_head import BasePanopticFusionHead
label_desc: Use image node_id 5 for calling the MaskFormerFusionHead obj's underlying member method code with example usage: obj.instance_postprocess(mask_cls, mask_pred) and returns: results