8

I am doing an image segmentation task and I am using a dataset that only has ground truths but no bounding boxes or polygons.

I have 2 classes( ignoring 0 for background) and the outputs and ground truth labels are in an array like

Predicted--/---Labels

0|0|0|1|2 0|0|0|1|2 0|2|1|0|0 0|2|1|0|0 0|0|1|1|1 0|0|1|1|1 0|0|0|0|1 0|0|0|0|1

How do I calculate IoU from these ?

PS: I am using python3 with pytorch api

Farshid Rayhan
  • 1,134
  • 4
  • 17
  • 31

2 Answers2

8

So I just found out that jaccard_similarity_score is regarded as IoU.

So the solution is very simple,

from sklearn.metrics import jaccard_similarity_score jac = jaccard_similarity_score(predictions, label, Normalize = True/False)

Source link: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.jaccard_score.html#sklearn.metrics.jaccard_score

GPrathap
  • 7,336
  • 7
  • 65
  • 83
Farshid Rayhan
  • 1,134
  • 4
  • 17
  • 31
1

You can create binary maps for given class.

    def calculate_iou(self, gt_mask, pred_mask, class=1):
    if threshold:
        pred_mask = (pred_mask == class) * 1
        gt_mask = (gt_mask == class) * 1

    overlap = pred_mask * gt_mask  # Logical AND
    union = (pred_mask + gt_mask)>0  # Logical OR
    iou = overlap.sum() / float(union.sum())
    return iou

Note that this kind of representation, is created by creating binary probabilities for each class. So, models are creating 4 probability maps for 4 classes. Then, largest probability for each pixel is considered as the largest probability class.