code: def compute_mean_ioU(
      preds,
      scales,
      centers,
      num_classes,
      datadir,
      input_size=[473, 473],
      dataset="val",
      reverse=False,
  ):
      file_list_name = os.path.join(datadir, dataset + "_list.txt")
      val_id = [
          line.split()[0][7:-4]
          for line in open(file_list_name).readlines()
      ]
  
      confusion_matrix = np.zeros((num_classes, num_classes))
  
      label_names_file = os.path.join(datadir, "label_names.txt")
      gt_label_names = pred_label_names = _read_names(label_names_file)
  
      assert gt_label_names[0] == pred_label_names[0] == "bg"
  
      hists = []
      for i, im_name in enumerate(val_id):
          gt_path = os.path.join(
              datadir, dataset, "labels", im_name + ".png"
          )
          gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE)
          h, w = gt.shape
          pred_out = preds[i]
          if scales is not None:
              s = scales[i]
              c = centers[i]
          else:
              s = None
              c = None
          pred_old = transform_parsing(pred_out, c, s, w, h, input_size)
          gt = np.asarray(gt, dtype=np.int32)
          pred = np.asarray(pred_old, dtype=np.int32)
          ignore_index = gt != 255
  
          gt = gt[ignore_index]
          pred = pred[ignore_index]
  
          hist = fast_histogram(
              gt, pred, len(gt_label_names), len(pred_label_names)
          )
          hists.append(hist)
  
          confusion_matrix += get_confusion_matrix(
              gt, pred, num_classes
          )
  
      hist_sum = np.sum(np.stack(hists, axis=0), axis=0)
  
      eval_names = dict()
      for label_name in gt_label_names:
          gt_ind = gt_label_names.index(label_name)
          pred_ind = pred_label_names.index(label_name)
          eval_names[label_name] = ([gt_ind], [pred_ind])
  
      if "le" in eval_names and "re" in eval_names:
          eval_names["eyes"] = _merge(
              eval_names["le"], eval_names["re"]
          )
      if "lb" in eval_names and "rb" in eval_names:
          eval_names["brows"] = _merge(
              eval_names["lb"], eval_names["rb"]
          )
      if (
          "ulip" in eval_names
          and "imouth" in eval_names
          and "llip" in eval_names
      ):
          eval_names["mouth"] = _merge(
              eval_names["ulip"],
              eval_names["imouth"],
              eval_names["llip"],
          )
  
      # Helen
      if (
          "eyes" in eval_names
          and "brows" in eval_names
          and "nose" in eval_names
          and "mouth" in eval_names
      ):
          eval_names["overall"] = _merge(
              eval_names["eyes"],
              eval_names["brows"],
              eval_names["nose"],
              eval_names["mouth"],
          )
  
      pos = confusion_matrix.sum(1)
      res = confusion_matrix.sum(0)
      tp = np.diag(confusion_matrix)
  
      pixel_accuracy = (tp.sum() / pos.sum()) * 100
      mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100
      IoU_array = tp / np.maximum(1.0, pos + res - tp)
      IoU_array = IoU_array * 100
      mean_IoU = IoU_array.mean()
      print("Pixel accuracy: %f \n" % pixel_accuracy)
      print("Mean accuracy: %f \n" % mean_accuracy)
      print("Mean IU: %f \n" % mean_IoU)
      mIoU_value = []
      f1_value = []
      mf1_value = []
  
      for i, (label, iou) in enumerate(zip(LABELS, IoU_array)):
          mIoU_value.append((label, iou))
  
      mIoU_value.append(("Pixel accuracy", pixel_accuracy))
      mIoU_value.append(("Mean accuracy", mean_accuracy))
      mIoU_value.append(("Mean IU", mean_IoU))
      mIoU_value = OrderedDict(mIoU_value)
  
      for eval_name, (gt_inds, pred_inds) in eval_names.items():
          A = hist_sum[gt_inds, :].sum()
          B = hist_sum[:, pred_inds].sum()
          intersected = hist_sum[gt_inds, :][:, pred_inds].sum()
          f1 = 2 * intersected / (A + B)
  
          if eval_name in gt_label_names[1:]:
              mf1_value.append(f1)
          f1_value.append((eval_name, f1))
  
      f1_value.append(("Mean_F1", np.array(mf1_value).mean()))
      f1_value = OrderedDict(f1_value)
  
      return mIoU_value, f1_value
  
file_path: insightface/parsing/dml_csr/utils/miou.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import argparse
  import cv2
  import json
  import numpy
  import os
  from collections import OrderedDict
  from PIL import Image
  from utils.transforms import transform_parsing
label_desc: Use image node_id 6 for calling a global function with example usage: compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size, dataset, reverse) and returns: mIoU_value,  f1_value