def calculate_confusion_matrix(dataset, results):
    """Calculate the confusion matrix.

    Args:
        dataset (Dataset): Test or val dataset.
        results (list[ndarray]): A list of segmentation results in each image.
    """
    n = len(dataset.METAINFO['classes'])
    confusion_matrix = np.zeros(shape=[n, n])
    assert len(dataset) == len(results)
    ignore_index = dataset.ignore_index
    reduce_zero_label = dataset.reduce_zero_label
    prog_bar = progressbar.ProgressBar(len(results))
    for idx, per_img_res in enumerate(results):
        res_segm = per_img_res
        gt_segm = dataset[idx]['data_samples'] \
            .gt_sem_seg.data.squeeze().numpy().astype(np.uint8)
        gt_segm, res_segm = gt_segm.flatten(), res_segm.flatten()
        if reduce_zero_label:
            gt_segm = gt_segm - 1
        to_ignore = gt_segm == ignore_index

        gt_segm, res_segm = gt_segm[~to_ignore], res_segm[~to_ignore]
        inds = n * gt_segm + res_segm
        mat = np.bincount(inds, minlength=n**2).reshape(n, n)
        confusion_matrix += mat
        prog_bar.update()
    return confusion_matrix
Calculate the confusion matrix.

    Args:
        dataset (Dataset): Test or val dataset.
        results (list[ndarray]): A list of segmentation results in each image.
    
segmentation/tools/analysis_tools/confusion_matrix.py