我有两个列表,分别包含真实图像和预测图像。这两个列表都包含二值图像。我需要获取这两个列表之间的准确率、F1分数、召回率和精确率报告。
可以使用sklearn.metrics.classification_report来获取预测值和真实值之间的分类报告,但它只接受一维数组。
http://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html
如何修改它以获取包含二值图像的两个图像列表之间的分类报告?或者是否有更好的方法来执行此操作?我的代码如下:
import osimport cv2import numpy as np from sklearn.metrics import confusion_matrix from sklearn.metrics import accuracy_score from sklearn.metrics import classification_reportpath_pred = "absolute_path/pred"pred_list = next(os.walk(path_pred))[2]true_list_new=[]pred_list_new=[]for img in pred_list: pred_img=cv2.imread("absolute_path/pred/%s" % img) true_img=cv2.imread("absolute_path/true/%s" % img) true_list_new.append(true_img) pred_list_new.append(pred_img)print("Confusion Matrix: ", confusion_matrix(true_list_new, pred_list_new)) print ("Accuracy : ", accuracy_score(true_list_new,pred_list_new)*100) print("Report : ", classification_report(true_list_new, pred_list_new))
p.s 解决方案
回答:
最简单的解决方案是读取图像,然后将它们重塑为单行向量。如果你不需要显示它们,你可以使用image.reshape(-1):通过这一行,由cv2加载的具有多列的numpy数组将被转换为具有单一维度的numpy数组。
图示如下:
[[1, 2, 3],[4, 5, 6], ==> [1, 2, 3, 4, 5, 6, 7, 8, 9][7, 8, 9]]