zl程序教程

您现在的位置是:首页 > 

当前栏目

语义分割性能指标_语义分割评价指标

分割 指标 语义 评价 性能指标
2023-06-13 09:11:04 时间

大家好,又见面了,我是你们的朋友全栈君。

文章目录

语义分割的评价指标

在整理评价指标之前,先补充一下预备知识。 我们在进行语义分割结果评价的时候,常常将预测出来的结果分为四个部分:true positive,false positive,true negative,false negative,其中negative就是指非物体标签的部分(可以直接理解为背景),那么显而易见的,positive就是指有标签的部分。下图显示了四个部分的区别:

在图上可以清晰的看到,prediction图被分成四个部分,其中大块的白色斜线标记的是true negative(TN,预测中真实的背景部分),红色线部分标记是false negative(FN,预测中被预测为背景,但实际上并不是背景的部分),蓝色的斜线是false positive(FP,预测中分割为某标签的部分,但是实际上并不是该标签所属的部分),中间荧光黄色块就是true positive(TP,预测的某标签部分,符合真值)。 在评价的时候常用的指标有:IOU(交并比,也有叫做IU的),像素准确率(pixel-accuracy),有的时候还有平均准确率(mean-accuracy)。

IoU or IU(intersection over union)

IoU指标就是大家常说的交并比,在语义分割中作为标准度量一直被人使用。交并比不仅仅在语义分割中使用,在目标检测等方向也是常用的指标之一。 计算公式为: I o U = t a r g e t ⋀ p r e d i c t i o n t a r g e t ⋃ p r e d i c t i o n IoU =target\bigwedge prediction \over target\bigcup prediction target⋃predictionIoU=target⋀prediction​ 如图所示:

IoU一般都是基于类进行计算的,也有基于图片计算的。一定要看清数据集的评价标准,这里我吃过大亏,特意标注提醒。 基于类进行计算的IoU就是将每一类的IoU计算之后累加,再进行平均,得到的就是基于全局的评价,所以我们求的IoU其实是取了均值的IoU,也就是均交并比(mean IoU)

实现代码也很简单:intersection = np.logical_and(target, prediction) union = np.logical_or(target, prediction) iou_score = np.sum(intersection) / np.sum(union) 更具体的一些的如下所示:

import torch 
import pandas as pd  # For filelist reading
from torch.utils.data import Dataset
import myPytorchDatasetClass  # Custom dataset class, inherited from torch.utils.data.dataset


def iou(pred, target, n_classes = 37):
#n_classes :the number of classes in your dataset
  ious = []
  pred = pred.view(-1)
  target = target.view(-1)

  # Ignore IoU for background class ("0")
  for cls in xrange(1, n_classes):  # This goes from 1:n_classes-1 -> class "0" is ignored
    pred_inds = pred == cls
    target_inds = target == cls
    intersection = (pred_inds[target_inds]).long().sum().data.cpu()[0]  # Cast to long to prevent overflows
    union = pred_inds.long().sum().data.cpu()[0] + target_inds.long().sum().data.cpu()[0] - intersection
    if union == 0:
      ious.append(float('nan'))  # If there is no ground truth, do not include in evaluation
    else:
      ious.append(float(intersection) / float(max(union, 1)))
  return np.array(ious)


def evaluate_performance(net):
    Dataloader for test data
    batch_size = 1  
    filelist_name_test = '/path/to/my/test/filelist.txt'
    data_root_test = '/path/to/my/data/'
    dset_test = myPytorchDatasetClass.CustomDataset(filelist_name_test, data_root_test)
    test_loader = torch.utils.data.DataLoader(dataset=dset_test,  
                                              batch_size=batch_size,
                                              shuffle=False,
                                              pin_memory=True)

    data_info = pd.read_csv(filelist_name_test, header=None)
    num_test_files = data_info.shape[0]  #reture data.info's hangshu that means dots in dataset 
    sample_size = num_test_files


    # Containers for results
    preds = torch.FloatTensor(sample_size, opt.imageSizeH, opt.imageSizeW)
    preds = Variable(seg,volatile=True)
    gts = torch.FloatTensor(sample_size, 1, opt.imageSizeH, opt.imageSizeW)
    gts = Variable(gts,volatile=True)

    dataiter = iter(test_loader) 
    for i in xrange(sample_size):
        images, labels, filename = dataiter.next()
        images = Variable(images).cuda()
        labels = Variable(labels)
        gts[i:i+batch_size, :, :, :] = labels
        outputs = net(images)
        outputs = outputs.permute(0, 2, 3, 4, 1).contiguous()
        val, pred = torch.max(outputs, 4)
        preds[i:i+batch_size, :, :, :] = pred.cpu()
    acc = iou(preds, gts)
    return acc

pixcal-accuracy (PA,像素精度)

基于像素的精度计算是评估指标中最为基本也最为简单的指标,从字面上理解就可以知道,PA是指预测正确的像素占总像素的比例,计算公式就不赘述了,大家都懂的。

本次主要想写的是IoU,在进行复现工作的时候,因为这个评估指标整整折腾了两个星期。以此纪念我辛酸的调bug历史。 另外,在查询相关资料的时候发现了一个好东西:

《A Review on Deep Learning Techniques Applied to Semantic Segmentation》 下载PDF请点击这里

我觉得这个应该作为语义分割的基本知识普及资料之一,这里盗一张图来:

参考资料

A Review on Deep Learning Techniques Applied to Semantic Segmentation 个人主页:https://www.jeremyjordan.me/evaluating-image-segmentation-models/ 图片来源:https://blog.csdn.net/majinlei121/article/details/78965435

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/171649.html原文链接:https://javaforall.cn