IOU metric in sematic segmentation

Hi all
I want to ask about the IOU metric for multiclass semantic segmantation
can I use this code from the semantic segmentation PyTorch model to calculate the IOU

def iou(pr, gt, eps=1e-7, threshold=None, ignore_channels=None):
“”“Calculate Intersection over Union between ground truth and prediction
pr (torch.Tensor): predicted tensor
gt (torch.Tensor): ground truth tensor
eps (float): epsilon to avoid zero division
threshold: threshold for outputs binarization
float: IoU (Jaccard) score

pr = _threshold(pr, threshold=threshold)
pr, gt = _take_channels(pr, gt, ignore_channels=ignore_channels)

intersection = torch.sum(gt * pr)
union = torch.sum(gt) + torch.sum(pr) - intersection + eps
return (intersection + eps) / union

my target mask is: 8x512x512
my predicted mask is: 8x5x512x512
where is 5: No of class
8: batch size

Hi Sarmad!

I haven’t looked at your code (After all, who knows what _threshold()
might do?), but if your intersection-over-union function does use a
“threshold for outputs binarization,” it will not be (usefully) differentiable,
and therefore can’t be used as a loss function for backpropagation and
training. (CrossEntropyLoss is a good loss function to start with for
semantic segmentation.) IoU (even with thresholding) is, of course, a
perfectly reasonable evaluation metric.


K. Frank

1 Like

thank you
the threshold is to binarize the predicated output I don’t now why they threshold out.
i have 5 class in output so the size is
and the gt (ground truth) is 8x512x512
how can i calculate the semantic segmentation metric (IOU and F1 ) ?
any suggestion !