The standard score for semantic segmentation is mean intersection over union. For each class, take the pixels predicted as that class and the pixels that truly belong to it; IoU is the size of their overlap divided by the size of their union. Averaging over classes gives mIoU. Everything is read off a confusion matrix whose entry C[i, j] counts pixels with true class i predicted as class j: the diagonal holds correct pixels, a row sum is the true size of a class and a column sum is its predicted size. A class that never occurs and is never predicted has an undefined IoU (0/0) and is excluded from the mean.
Implement confusion_matrix and mean_iou.
confusion_matrix(pred, target, n_classes) takes integer arrays of equal shape with values in 0..n_classes-1 and returns an (n_classes, n_classes) integer matrix with C[true, pred] counts.mean_iou(conf) computes per-class IoU_c = C[c, c] / (row_sum_c + col_sum_c - C[c, c]) and returns (miou, ious), where classes with a zero denominator get nan in ious and are ignored by the mean (np.nanmean).A single np.bincount(target * n + pred, minlength=n * n) builds the matrix without a loop.
Check the denominator before dividing, so no divide-by-zero warning appears.
No results yet
Run your code to check it against the examples, or submit to run every test.