code: def merge_semantic_and_instance(
      sem_seg,
      ins_seg,
      semantic_thing_seg,
      label_divisor,
      thing_ids,
      stuff_area,
      void_label,
  ):
      """
      Post-processing for panoptic segmentation, by merging semantic segmentation
          label and class agnostic instance segmentation label.
      Args:
          sem_seg: A Tensor of shape [1, H, W], predicted category id for each pixel.
          ins_seg: A Tensor of shape [1, H, W], predicted instance id for each pixel.
          semantic_thing_seg: A Tensor of shape [1, H, W], predicted foreground mask.
          label_divisor: An integer, used to convert panoptic id =
              semantic id * label_divisor + instance_id.
          thing_ids: Set, a set of ids from contiguous category ids belonging
              to thing categories.
          stuff_area: An integer, remove stuff whose area is less tan stuff_area.
          void_label: An integer, indicates the region has no confident prediction.
      Returns:
          A Tensor of shape [1, H, W].
      """
      # In case thing mask does not align with semantic prediction.
      pan_seg = torch.zeros_like(sem_seg) + void_label
      is_thing = (ins_seg > 0) & (semantic_thing_seg > 0)
  
      # Keep track of instance id for each class.
      class_id_tracker = Counter()
  
      # Paste thing by majority voting.
      instance_ids = torch.unique(ins_seg)
      for ins_id in instance_ids:
          if ins_id == 0:
              continue
          # Make sure only do majority voting within `semantic_thing_seg`.
          thing_mask = (ins_seg == ins_id) & is_thing
          if torch.nonzero(thing_mask).size(0) == 0:
              continue
          class_id, _ = torch.mode(sem_seg[thing_mask].view(-1))
          class_id_tracker[class_id.item()] += 1
          new_ins_id = class_id_tracker[class_id.item()]
          pan_seg[thing_mask] = class_id * label_divisor + new_ins_id
  
      # Paste stuff to unoccupied area.
      class_ids = torch.unique(sem_seg)
      for class_id in class_ids:
          if class_id.item() in thing_ids:
              # thing class
              continue
          # Calculate stuff area.
          stuff_mask = (sem_seg == class_id) & (ins_seg == 0)
          if stuff_mask.sum().item() >= stuff_area:
              pan_seg[stuff_mask] = class_id * label_divisor
  
      return pan_seg
  
file_path: detectron2/projects/Panoptic-DeepLab/panoptic_deeplab/post_processing.py
imports: from collections import Counter
  import torch
  import torch.nn.functional
label_desc: Use image node_id 4 for calling a global function with example usage: merge_semantic_and_instance(sem_seg, ins_seg, semantic_thing_seg, label_divisor, thing_ids, stuff_area, void_label) and returns: pan_seg