Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sklearn.metrics Jaccard Index with images?

I am trying to do some image comparisons, starting first by finding the Jaccard Index. I'm using the sklearn.metrics implementation of Jaccard Index Using the example below with just a small array of numbers, it works like expected.

import numpy as np
from sklearn.metrics import jaccard_similarity_score

#The y_pred represents the values that the program has found 
y_pred = [0,0,1,0,0,0,1,1,1,1,0,1,0,1,0,0,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,0,1,1,1,0,0,0,0,1,1,0,0,1,1,0,1,1,1]
#The y_true represents the values that are actually correct 
y_true = [1,0,0,1,0,1,1,0,1,1,1,0,1,0,1,1,0,1,1,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,1,0,0,0,0,1,1,1,0,1,0,1,1,1]

iou = jaccard_similarity_score(y_true, y_pred)

Though it is giving an error of...

ValueError: unknown is not supported

When I feed it the two images such as....

iou = jaccard_similarity_score(img_true, img_pred)

I'm unsure what to do, I tried converting the images to grayscale using OpenCV and making both the images astype(float) with no luck in either case.

like image 694
Carson Avatar asked Sep 12 '25 07:09

Carson


2 Answers

Posting as answer so question can be closed: flattening img_true and img_pred solved by doing img_true.flatten() and img_pred.flatten()

like image 142
Jason Stein Avatar answered Sep 14 '25 19:09

Jason Stein


You can use ravel() for converting it into 1-D:

img_true=np.array(img_true).ravel()
img_pred=np.array(img_pred).ravel()
iou = jaccard_similarity_score(img_true, img_pred)
like image 23
Sriram Sitharaman Avatar answered Sep 14 '25 19:09

Sriram Sitharaman