医学图像语义分割多分类任务计算mIoU和dice的函数

29 篇文章 1 订阅
14 篇文章 0 订阅

多分类分割网络输出为的结果为[channel, height, width],channel对应分类,每个channel的pixel的值为对应当前分类的置信度。
需要先对输出结果执行一次np.argmax操作,将其转化为二维Tensor,在不同channel取置信度最高的channel(class)的值(如:0、1、2等)作为该像素的值

print(arr) # (8, 512, 512)
print(np.argmax(arr, axis=0)) # (1, 512, 512)
函数输入:
变量简介
seg神经网络输出的预测结果,类型:ndarray,shape:[channel=1, height, width]
gt对应样本的ground truth,类型:ndarray,shape:[channel=1, height, width]
classes分类数量,0到classes的数值-1
background_id背景在预测结果的Tensor中对应分类的值(用于过滤背景)
# 使用numba加速两个函数的计算速度
# pip install numpy
# pip install numba

import numpy as np
from numba import jit
# jit编译器编译执行python代码,不使用python解析器,编译执行加速计算速度
@jit(nopython=True)
def cal_mIoU(seg, gt, classes=2, background_id=-1):
    channel_iou = []
    for i in range(classes):
        if i == background_id:
            continue
        cond = i ** 2
        # 计算相交部分
        inter = len(np.where(seg * gt == cond)[0])
        union = len(np.where(seg == i)[0]) + len(np.where(gt == i)[0]) - inter
        if union == 0:
            iou = 0
        else:
            iou = inter / union
        channel_iou.append(iou)
    res = np.array(channel_iou).mean()
    return res

@jit(nopython=True)
def cal_dice(seg, gt, classes=2, background_id=-1):
    channel_dice = []
    for i in range(classes):
        if i == background_id:
            continue
        cond = i ** 2
        # 计算相交部分
        inter = len(np.where(seg * gt == cond)[0])
        total_pix = len(np.where(seg == i)[0]) + len(np.where(gt == i)[0])
        if total_pix == 0:
            dice = 0
        else:
            dice = (2 * inter) / total_pix
        channel_dice.append(dice)
    res = np.array(channel_dice).mean()
    return res
  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Alex-Leung

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

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

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

打赏作者

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

抵扣说明:

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

余额充值