def test_contingency_table(): im_true = np.array([1, 2, 3, 4]) im_test = np.array([1, 1, 8, 8]) table1 = np.array([[0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0.25, 0., 0., 0., 0., 0., 0., 0.], [0., 0.25, 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0.25], [0., 0., 0., 0., 0., 0., 0., 0., 0.25]]) sparse_table2 = contingency_table(im_true, im_test, normalize=True) table2 = sparse_table2.toarray() assert_array_equal(table1, table2)
def _iou_matrix(gt, seg): # relabel gt and seg for smaller memory footprint of contingency table gt = _relabel(gt) seg = _relabel(seg) # get number of overlapping pixels between GT and SEG n_inter = contingency_table(gt, seg).A # number of pixels for GT instances n_gt = n_inter.sum(axis=1, keepdims=True) # number of pixels for SEG instances n_seg = n_inter.sum(axis=0, keepdims=True) # number of pixels in the union between GT and SEG instances n_union = n_gt + n_seg - n_inter iou_matrix = n_inter / n_union # make sure that the values are within [0,1] range assert 0 <= np.min(iou_matrix) <= np.max(iou_matrix) <= 1 return iou_matrix