class_docstr: Evaluate semantic segmentation
class_docstr_tok: ["Evaluate","semantic","segmentation"]
code: def update(self, pred, gt):
      # get the labels and counts for the pred and gt.
      gt_labels, gt_labels_counts = np.unique(gt, return_counts=True)
      pred_labels, pred_labels_counts = np.unique(
          pred, return_counts=True
      )
      gt_segms = defaultdict(dict)
      pred_segms = defaultdict(dict)
      for label, label_count in zip(gt_labels, gt_labels_counts):
          category_id = (
              label // self.label_divisor
              if label > self.label_divisor
              else label
          )
          gt_segms[label]["area"] = label_count
          gt_segms[label]["category_id"] = category_id
          gt_segms[label]["iscrowd"] = (
              1 if label in self.thing_list else 0
          )
      for label, label_count in zip(pred_labels, pred_labels_counts):
          category_id = (
              label // self.label_divisor
              if label > self.label_divisor
              else label
          )
          pred_segms[label]["area"] = label_count
          pred_segms[label]["category_id"] = category_id
  
      # confusion matrix calculation
      pan_gt_pred = gt.astype(np.uint64) * OFFSET + pred.astype(
          np.uint64
      )
      gt_pred_map = {}
      labels, labels_cnt = np.unique(pan_gt_pred, return_counts=True)
      for label, intersection in zip(labels, labels_cnt):
          gt_id = label // OFFSET
          pred_id = label % OFFSET
          gt_pred_map[(gt_id, pred_id)] = intersection
  
      # count all matched pairs
      gt_matched = set()
      pred_matched = set()
      for label_tuple, intersection in gt_pred_map.items():
          gt_label, pred_label = label_tuple
          if (
              gt_label == self.ignore_index
              or pred_label == self.ignore_index
          ):
              continue
          if gt_segms[gt_label]["iscrowd"] == 1:
              continue
          if (
              gt_segms[gt_label]["category_id"]
              != pred_segms[pred_label]["category_id"]
          ):
              continue
          union = (
              pred_segms[pred_label]["area"]
              + gt_segms[gt_label]["area"]
              - intersection
              - gt_pred_map.get((self.ignore_index, pred_label), 0)
          )
          iou = intersection / union
          if iou > 0.5:
              self.pq_stat[gt_segms[gt_label]["category_id"]].tp += 1
              self.pq_stat[gt_segms[gt_label]["category_id"]].iou += iou
              gt_matched.add(gt_label)
              pred_matched.add(pred_label)
  
      # count false negtive
      crowd_labels_dict = {}
      for gt_label, gt_info in gt_segms.items():
          if gt_label in gt_matched:
              continue
          if gt_label == self.ignore_index:
              continue
          # ignore crowd
          if gt_info["iscrowd"] == 1:
              crowd_labels_dict[gt_info["category_id"]] = gt_label
              continue
          self.pq_stat[gt_info["category_id"]].fn += 1
  
      # count false positive
      for pred_label, pred_info in pred_segms.items():
          if pred_label in pred_matched:
              continue
          if pred_label == self.ignore_index:
              continue
          # intersection of the segment with self.ignore_index
          intersection = gt_pred_map.get(
              (self.ignore_index, pred_label), 0
          )
          if pred_info["category_id"] in crowd_labels_dict:
              intersection += gt_pred_map.get(
                  (
                      crowd_labels_dict[pred_info["category_id"]],
                      pred_label,
                  ),
                  0,
              )
          # predicted segment is ignored if more than half of the segment correspond to self.ignore_index regions
          if intersection / pred_info["area"] > 0.5:
              continue
          self.pq_stat[pred_info["category_id"]].fp += 1
  
file_path: PaddleSeg/contrib/PanopticDeepLab/utils/evaluation/panoptic.py
imports: from collections import defaultdict, OrderedDict
  import numpy
label_desc: Use image node_id 2 for calling the PanopticEvaluator obj's underlying member method code with example usage: obj.update(pred, gt) without return types