如何为语义(图像)分割计算精确召回曲线?

时间:2019-07-14 10:06:01

标签: python numpy matplotlib image-segmentation precision-recall

我正在对图像执行语义分割(以材料为类),并希望计算出我的准确度的精确召回曲线。目前,我分别通过求和每个类别的真实性和预测与该类别相符的像素的真正值,虚假阳性和虚假负值,该像素的真相和预测与该类别的真相,预测和真相分别对应。然后,我计算精度并相应地调用:

pixel_probs = np.array(pixel_probs) # shape (num_pixels), the classification certainty for each pixel
pixel_labels_pred, pixel_labels_gt = np.array(pixel_labels_pred).astype(bool), np.array(pixel_labels_gt).astype(bool) # shape (num_pixels, num_classes), one hot labels for each pixel
precision_mat, recall_mat = np.array([]).reshape(num_labels, 0), np.array([]).reshape(num_labels, 0) # stores the precision-recall pairs for each certainty threshold
prev_num_pixels = sum(pixel_probs > 0.0)
for threshold in sorted(thresholds):
    pixel_mask = pixel_probs > threshold
    if sum(pixel_mask) == prev_num_pixels: continue
    prev_num_pixels == sum(pixel_mask)
    pixel_labels_pred_msk = pixel_labels_pred[pixel_mask]
    pixel_labels_gt_msk = pixel_labels_gt[pixel_mask]
    tps = np.sum(np.logical_and(pixel_labels_gt_msk, pixel_labels_pred_msk), axis=0)
    fps = np.sum(np.logical_and(np.logical_not(pixel_labels_gt_msk), pixel_labels_pred_msk), axis=0)
    fns = np.sum(np.logical_and(pixel_labels_gt_msk, np.logical_not(pixel_labels_pred_msk)), axis=0)
    precisions = tps / (tps + fps)
    recalls = tps / (tps + fns)
    precision_mat = np.concatenate([precision_mat, np.expand_dims(precisions, axis=-1)], axis=-1)
    recall_mat = np.concatenate([recall_mat, np.expand_dims(recalls, axis=-1)], axis=-1)

fig = plt.figure()
fig.set_size_inches(12, 5)
for label_index in range(precision_mat.shape[0]):
    r = recall_mat[label_index]
    p = precision_mat[label_index]
    sort_order = np.argsort(r)
    r = r[sort_order]
    p = p[sort_order]
    plt.plot(r, p, '-o', markersize=2, label=labels[label_index])
plt.title("Precision-recall curve")
plt.legend(loc='upper left', fontsize=8.5, ncol=1, bbox_to_anchor=(1, 1))
plt.xlabel('recall', fontsize=12)
plt.ylabel('precision', fontsize=12)
plt.savefig(dir + "test/pr_curves.png")

但是,这会产生一些非常奇怪的图形:

enter image description here

的确,我的细分器的表现相当糟糕,但是我至少希望曲线会或多或少地遵循向下的斜率。

我可以正确计算PR曲线吗?我应该考虑其他替代方法来计算此类曲线吗?我的绘图代码中可能有错误吗?

0 个答案:

没有答案