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
IoU is calculated by dividing the overlap between the predicted and ground truth annotation by the union of these.
IoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union between the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation, the mean IoU of the image is calculated by taking the IoU of each class and averaging them.
The Intersection over Union (IoU) metric, also referred to as the Jaccard index, is essentially a method to quantify the percent overlap between the target mask and our prediction output. This metric is closely related to the Dice coefficient which is often used as a loss function during training.
Coding a function for IOU in python: The function IOU takes in 2 boxes, box1 and box2 as input. The data in each box is a list containing[x1, y1, x2, y2], which is the top left, and bottom right coordinates. We find the area of the intersection, followed by the area of the union, as described earlier.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With