def calculate_confusion_matrix_from_arrays(prediction, ground_truth, nr_labels):
"""
calculate the confusion matrix for one image pair.
prediction and ground_truth have to have the same shape.
"""
# a long 2xn array with each column being a pixel pair
replace_indices = np.vstack((
ground_truth.flatten(),
prediction.flatten())
).T
# add up confusion matrix
confusion_matrix, _ = np.histogramdd(
replace_indices,
bins=(nr_labels, nr_labels),
range=[(0, nr_labels), (0, nr_labels)]
)
confusion_matrix = confusion_matrix.astype(np.uint32)
return confusion_matrix
评论列表
文章目录