计算多类语义切分的mIoU

2024-06-28 10:47:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想计算多类语义切分的mIoU。我已经用mIoU度量训练了u-net,我想用不同的数据集测试这个模型,但是在这个阶段我不得不忽略一些像素值,但是我不能用我的mIoU代码运行它,我发现如下所示。但是我不明白它的输入/输出

对于pred,我给出(1,NumberofClass,w,h) 对于labes,我给出(1,w,h) 对于C,我给出了类的编号,并将ignore定义为一个列表(ignore=[0])

def iou(preds, labels, C, EMPTY=1., ignore=None, per_image=False):
"""
Array of IoU for each (non ignored) class
"""
if not per_image:
    preds, labels = (preds,), (labels,)
ious = []
for pred, label in zip(preds, labels):
    iou = []
    for i in range(C):
        if i != ignore:
            intersection = ((label == i) & (pred == i)).sum()
            union = ((label == i) | ((pred == i) & (label != ignore))).sum()
            if not union:
                iou.append(EMPTY)
            else:
                iou.append(float(intersection) / float(union))
    ious.append(iou)
ious = [mean(iou) for iou in zip(*ious)]  # mean across images if per_image
return 100 * np.array(ious)



def mean(l, ignore_nan=False, empty=0):
"""
nanmean compatible with generators.
"""
l = iter(l)
if ignore_nan:
    l = ifilterfalse(isnan, l)
try:
    n = 1
    acc = next(l)
except StopIteration:
    if empty == 'raise':
        raise ValueError('Empty mean')
    return empty
for n, v in enumerate(l, 2):
    acc += v
if n == 1:
    return acc
return acc / n

当我运行代码时,我的输出就是这样。我还注意到我的交点始终为0。 谁能找到更好的解决办法。这将非常有帮助

loss = [100. 0. 0. 100. 0. 0. 0. 0. 0. 100. 0. 0. 100. 0. 0.]


Tags: inforlabelsreturnifmeanlabelacc