语义分割准确率计算

目录

pytorch版

pytorch准确率,miou:

sklearn版


pytorch版

"""
reference from: https://github.com/LeeJunHyun/Image_Segmentation/blob/master/evaluation.py
"""
 
import torch
 
# SR : Segmentation Result
# GT : Ground Truth
 
def get_accuracy(SR,GT,threshold=0.5):
    SR = SR > threshold
    GT = GT == torch.max(GT)
    corr = torch.sum(SR==GT)
    tensor_size = SR.size(0)*SR.size(1)*SR.size(2)*SR.size(3)
    acc = float(corr)/float(tensor_size)
 
    return acc
 
def get_sensitivity(SR,GT,threshold=0.5):
    # Sensitivity == Recall
    SR = SR > threshold
    GT = GT == torch.max(GT)
 
    # TP : True Positive
    # FN : False Negative
    TP = ((SR==1)+(GT==1))==2
    FN = ((SR==0)+(GT==1))==2
 
    SE = float(torch.sum(TP))/(float(torch.sum(TP+FN)) + 1e-6)     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
语义分割准确率可以通过计算像素级别的交并比(Intersection over Union,IoU)来实现。而二值图的IoU计算方式比较简单,只需要将预测结果和真实标注二者转化为二值图,再进行像素级别的比对即可。 以下是一个简单的Python代码示例,假设预测结果和真实标注分别存储在两个文件夹中,且文件名相同: ```python import os import cv2 # 定义计算IoU的函数 def iou(pred, target): intersection = (pred & target).sum() union = (pred | target).sum() return intersection / union # 遍历文件夹中的所有图片,计算平均IoU iou_sum = 0 total_num = 0 pred_path = 'path/to/prediction/folder' target_path = 'path/to/target/folder' for filename in os.listdir(pred_path): if filename.endswith('.png'): pred_img = cv2.imread(os.path.join(pred_path, filename), cv2.IMREAD_GRAYSCALE) target_img = cv2.imread(os.path.join(target_path, filename), cv2.IMREAD_GRAYSCALE) pred_img = (pred_img > 0).astype('uint8') # 转化为二值图 target_img = (target_img > 0).astype('uint8') # 转化为二值图 iou_sum += iou(pred_img, target_img) total_num += 1 mean_iou = iou_sum / total_num print('Mean IoU:', mean_iou) ``` 需要注意的是,这个代码示例假设了预测结果和真实标注的文件名相同。如果不是这种情况,需要在遍历时进行文件名匹配。同时,这个代码示例也只适用于二值图的情况,对于多分类语义分割,需要进行更复杂的计算

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值