多类语义分割模型评估

时间:2020-06-19 00:16:53

标签: python-3.x pytorch image-segmentation multiclass-classification semantic-segmentation

我正在做一个关于多类语义分割的项目。我制定了一个模型,通过减少损失值来输出漂亮的下降分割图像。但是,我无法使用均值或Dice系数等指标来评估模型性能。 在二进制语义分割的情况下,很容易将阈值设置为0.5,将输出分类为对象或背景,但是在多类语义分割的情况下则不起作用。您能否告诉我如何在上述指标上获得模型性能?任何帮助将不胜感激!

顺便说一句,我正在使用PyTorch框架和CamVid数据集。

1 个答案:

答案 0 :(得分:2)

下面给出的是在PyTorch中实现均值IoU(联合交叉)。

def mIOU(label, pred, num_classes=19):
    pred = F.softmax(pred, dim=1)              
    pred = torch.argmax(pred, dim=1).squeeze(1)
    iou_list = list()
    present_iou_list = list()

    pred = pred.view(-1)
    label = label.view(-1)
    # Note: Following for loop goes from 0 to (num_classes-1)
    # and ignore_index is num_classes, thus ignore_index is
    # not considered in computation of IoU.
    for sem_class in range(num_classes):
        pred_inds = (pred == sem_class)
        target_inds = (label == sem_class)
        if target_inds.long().sum().item() == 0:
            iou_now = float('nan')
        else: 
            intersection_now = (pred_inds[target_inds]).long().sum().item()
            union_now = pred_inds.long().sum().item() + target_inds.long().sum().item() - intersection_now
            iou_now = float(intersection_now) / float(union_now)
            present_iou_list.append(iou_now)
        iou_list.append(iou_now)
    return np.mean(present_iou_list)

模型的预测将采用单幅形式,因此首先采用softmax(如果尚未建立模型),然后采用argmax,以获取每个像素处概率最高的索引。然后,我们为每个类计算IoU(并在最后取平均值)。

我们可以将预测和标签重塑为一维向量(我读到它使计算速度更快)。对于每个类,我们首先使用pred_inds = (pred == sem_class)target_inds = (label == sem_class)来标识该类的索引。生成的pred_indstarget_inds在标记为该特定类的像素处将有1个像素,而对于任何其他类则为0。

然后,目标就有可能根本不包含该特定类。这将使该类的IoU计算无效,因为它不在目标中。因此,您可以为此类分配一个NaN IoU(以便稍后识别),而不必将它们参与均值的计算。

如果目标中存在特定类别,则pred_inds[target_inds]将给出1和0的向量,其中具有1的索引是预测和目标相等的索引,否则为零。取所有这些元素的总和就可以得出交集。

如果我们添加pred_indstarget_inds的所有元素,我们将获得该特定类的像素的并集+交集。因此,我们减去已经计算出的交集即可得到并集。然后,我们可以分割交集和并集以获取该特定类的IoU,并将其添加到有效IoU列表中。

最后,您取整个列表的平均值以获得mIoU。如果您想要骰子系数,可以用类似的方式进行计算。