code: def _instance_inference(self, mask_cls, mask_pred, text_prompts):
      num_class = len(text_prompts)
  
      if self.task in ["ref-seg", "caption"]:
          scores = F.softmax(mask_cls, dim=-1)
          scores_per_image = scores.max(dim=-1)[0]
          labels_per_image = torch.arange(num_class)
      else:
          scores = F.softmax(mask_cls, dim=-1)[:, :-1]
  
          labels = (
              torch.arange(num_class, device=scores.device)
              .unsqueeze(0)
              .repeat(scores.shape[0], 1)
              .flatten(0, 1)
          )
          scores_per_image, topk_indices = scores.flatten(0, 1).topk(
              self.test_cfg.get("max_per_img", 100), sorted=False
          )
  
          labels_per_image = labels[topk_indices]
          topk_indices = topk_indices // num_class
          mask_pred = mask_pred[topk_indices]
  
      result = InstanceData()
      mask_pred = mask_pred.sigmoid()
      result.masks = (mask_pred > self.test_cfg.mask_thr).float()
  
      # calculate average mask prob
      mask_scores_per_image = (
          mask_pred.flatten(1) * result.masks.flatten(1)
      ).sum(1) / (result.masks.flatten(1).sum(1) + 1e-6)
      result.scores = scores_per_image * mask_scores_per_image
      result.labels = labels_per_image
      result.label_names = [
          text_prompts[label] for label in labels_per_image
      ]
      result.bboxes = result.scores.new_zeros(len(result.scores), 4)
      return result
  
file_path: mmdetection/projects/XDecoder/xdecoder/unified_head.py
imports: import copy
  from typing import Sequence
  import torch
  from mmengine.structures import InstanceData, PixelData
  from torch import nn
  from torch.nn import functional
  from mmdet.evaluation.functional import INSTANCE_OFFSET
  from mmdet.registry import MODELS
  from .utils import is_lower_torch_version, retry_if_cuda_oom, sem_seg_postprocess
label_desc: Use image node_id 5 for calling the XDecoderUnifiedhead obj's underlying member method code with example usage: obj._instance_inference(mask_cls, mask_pred, text_prompts) and returns: result