code: def compute_segments(
      mask_probs,
      pred_scores,
      pred_labels,
      mask_threshold: float = 0.5,
      overlap_mask_area_threshold: float = 0.8,
      label_ids_to_fuse: Optional[Set[int]] = None,
      target_size: Tuple[int, int] = None,
  ):
      height = (
          mask_probs.shape[1] if target_size is None else target_size[0]
      )
      width = (
          mask_probs.shape[2] if target_size is None else target_size[1]
      )
  
      segmentation = torch.zeros(
          (height, width), dtype=torch.int32, device=mask_probs.device
      )
      segments: List[Dict] = []
  
      if target_size is not None:
          mask_probs = nn.functional.interpolate(
              mask_probs.unsqueeze(0),
              size=target_size,
              mode="bilinear",
              align_corners=False,
          )[0]
  
      current_segment_id = 0
  
      # Weigh each mask by its prediction score
      mask_probs *= pred_scores.view(-1, 1, 1)
      mask_labels = mask_probs.argmax(0)  # [height, width]
  
      # Keep track of instances of each class
      stuff_memory_list: Dict[str, int] = {}
      for k in range(pred_labels.shape[0]):
          pred_class = pred_labels[k].item()
          should_fuse = pred_class in label_ids_to_fuse
  
          # Check if mask exists and large enough to be a segment
          mask_exists, mask_k = check_segment_validity(
              mask_labels,
              mask_probs,
              k,
              mask_threshold,
              overlap_mask_area_threshold,
          )
  
          if mask_exists:
              if pred_class in stuff_memory_list:
                  current_segment_id = stuff_memory_list[pred_class]
              else:
                  current_segment_id += 1
  
              # Add current object segment to final segmentation map
              segmentation[mask_k] = current_segment_id
              segment_score = round(pred_scores[k].item(), 6)
              segments.append(
                  {
                      "id": current_segment_id,
                      "label_id": pred_class,
                      "was_fused": should_fuse,
                      "score": segment_score,
                  }
              )
              if should_fuse:
                  stuff_memory_list[pred_class] = current_segment_id
  
      return segmentation, segments
  
imports: import pathlib
  import warnings
  from typing import Dict, List, Optional, Set, Tuple, Union
  import numpy
  from PIL import Image
  from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin
  from ...image_utils import ImageFeatureExtractionMixin, is_torch_tensor
  from ...utils import TensorType, is_torch_available, logging
label_desc: Use image node_id 9 for calling a global function with example usage: compute_segments(mask_probs, pred_scores, pred_labels, mask_threshold, overlap_mask_area_threshold, label_ids_to_fuse, target_size) and returns: segmentation,  segments