IoU is used to estimate how well a predicted mask or bounding box matches the ground truth data.
The IoU is calculated by dividing the overlap between the prediction and ground truth label by the union of these.
The output is a percentage indicating the overlap between the two labels.
import numpy as npSMOOTH = 1e-6def iou_numpy(outputs: np.array, labels: np.array):outputs = outputs.squeeze(1)intersection = (outputs & labels).sum((1, 2))union = (outputs | labels).sum((1, 2))iou = (intersection + SMOOTH) / (union + SMOOTH)thresholded = np.ceil(np.clip(20 * (iou - 0.5), 0, 10)) / 10return thresholded # Or thresholded.mean()
import torchSMOOTH = 1e-6def iou_pytorch(outputs: torch.Tensor, labels: torch.Tensor):# You can comment out this line if you are passing tensors of equal shape# But if you are passing output from UNet or something it will most probably# be with the BATCH x 1 x H x W shapeoutputs = outputs.squeeze(1) # BATCH x 1 x H x W => BATCH x H x Wintersection = (outputs & labels).float().sum((1, 2)) # Will be zero if Truth=0 or Prediction=0union = (outputs | labels).float().sum((1, 2)) # Will be zzero if both are 0iou = (intersection + SMOOTH) / (union + SMOOTH) # We smooth our devision to avoid 0/0thresholded = torch.clamp(20 * (iou - 0.5), 0, 10).ceil() / 10 # This is equal to comparing with thresoldsreturn thresholded # Or thresholded.mean() if you are interested in average across the batch