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 io
  import pathlib
  from collections import defaultdict
  from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Tuple, Union
  import numpy
  from ...feature_extraction_utils import BatchFeature
  from ...image_processing_utils import BaseImageProcessor, get_size_dict
  from ...image_transforms import PaddingMode, center_to_corners_format, corners_to_center_format, id_to_rgb, pad, rescale, resize, rgb_to_id, to_channel_dimension_format
  from ...image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, ImageInput, PILImageResampling, get_image_size, infer_channel_dimension_format, is_scaled_image, make_list_of_images, to_numpy_array, valid_images, validate_annotations
  from ...utils import TensorType, is_flax_available, is_jax_tensor, is_scipy_available, is_tf_available, is_tf_tensor, is_torch_available, is_torch_tensor, is_vision_available, logging
label_desc: Use image node_id 22 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