code: def intersectionAndUnion(imPred, imLab, numClass):
      """
      This function takes the prediction and label of a single image,
      returns intersection and union areas for each class
      To compute over many images do:
      for i in range(Nimages):
          (area_intersection[:,i], area_union[:,i]) = intersectionAndUnion(imPred[i], imLab[i])
      IoU = 1.0 * np.sum(area_intersection, axis=1) / np.sum(np.spacing(1)+area_union, axis=1)
      """
      # Remove classes from unlabeled pixels in gt image.
      # We should not penalize detections in unlabeled portions of the image.
      imPred = imPred * (imLab > 0)
  
      # Compute area intersection:
      intersection = imPred * (imPred == imLab)
      (area_intersection, _) = np.histogram(
          intersection, bins=numClass, range=(1, numClass)
      )
  
      # Compute area union:
      (area_pred, _) = np.histogram(
          imPred, bins=numClass, range=(1, numClass)
      )
      (area_lab, _) = np.histogram(
          imLab, bins=numClass, range=(1, numClass)
      )
      area_union = area_pred + area_lab - area_intersection
      return (area_intersection, area_union)
  
file_path: gluon-cv/gluoncv/utils/metrics/segmentation.py
imports: import threading
  import numpy
  import mxnet
label_desc: Use image node_id 4 for calling a global function with example usage: intersectionAndUnion(imPred, imLab, numClass) and returns: area_intersection,  area_union