100. Evaluating Segmentation Models with IOU

There are several ways to evaluate your semantic segmentation model, and one way is to calculate the IOU(Intersection Over Union). This is basically calculating how much area is intersecting between the region proposed by the model and the actual region.

Note that I am using a binary semantic segmentation for this example

  1. Import
    import torch
    import cv2
    import numpy as np
    
  2. Load Input and Mask Image
    #Load Image
    img = cv2.imread('/PATH/TO/.jpg')
    mask = cv2.imread('/PATH/TO/MASK/.jpg')
    img_height = img.shape[0]
    img_width = img.shape[1]
    img = img.transpose(2,0,1).reshape(1,3,img_height,img_width)
    
  3. Run inference with your semantic segmentation model and get results
    #Run inference
    with torch.no_grad():
            a = model(torch.from_numpy(img).type(torch.cuda.FloatTensor)/255)
    
  4. Unify dimensions to compare tensors: y_pred(Model Prediction) and y_true(Actual answer)
    y_pred = a['out'].cpu().detach().numpy()[0][0]>threshold
    y_true = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    
    #Confirm whether dimension is unified
    print(y_pred.shape) # OUTPUt: (240, 320)
    print(y_true.shape) # OUTPUt: (240, 320)
    
  5. Calculate IOU score
    intersection = np.logical_and( y_true, y_pred)
    union = np.logical_or(y_true,y_pred)
    iou_score = np.sum(intersection) / np.sum(union)