聊聊图像分割的DICE和IOU指标
创始人
2024-05-31 04:17:53
0

目录

1. 介绍

2. dice 和 iou 的联系

3. 代码实现

3.1 dice

3.2 iou

3.3 test

3.4 dice 和 iou 的关系曲线

4. 代码


1. 介绍

dice 和 iou 都是衡量两个集合之间相似性的度量

dice计算公式:

iou计算公式:

iou的集合理解:

 

iou 其实就是两个区域的 overlap 部分和 union 部分的比值,也就是两个集合的交集 / 并集

dice 的分母不是并集,因为dice的分母是两个区域的和,A+B = A + B - A∩B,所以dice的分母其实是少减去了一个 A∩B,所以就让分子的 A∩B(交集) 扩大2倍

2. dice 和 iou 的联系

如果将两个集合间的关系划分的更细一点,即这种形式:

那么 A∩B = TP , A∪B = FN + TP + FP ,A+B = FN + TP +TP + FP 

dice : 

 

iou : 

 

那么根据变形,可以得出:

 

3. 代码实现

|A ∩ B| = A * B 的 和 = 两个区域乘积的和

|A| + |B|  = A + B 的和 = 两个区域相加的总和

|A∪B| = |A| + |B| - |A ∩ B| = 两个区域相交的总和 - 两个区域相乘的和

3.1 dice

dice 的实现

# Dice
def Dice(pred,true):intersection = pred * true          # 计算交集  pred ∩ truetemp = pred + true                  # pred + truesmooth = 1e-8                       # 防止分母为 0dice_score = 2*intersection.sum() / (temp.sum() + smooth)return dice_score

intersection 为两个区域的交集,即两个区域的乘积

temp 为两个区域的和,(注:这里不是并集,因为没有减去相交的部分)

3.2 iou

iou 的实现

# Iou
def Iou(pred,true):intersection = pred * true          # 计算交集  pred ∩ truetemp = pred + true                  # pred + trueunion = temp - intersection         # 计算并集:A ∪ B = A + B - A ∩ Bsmooth = 1e-8                       # 防止分母为 0iou_score = intersection.sum() / (union.sum() + smooth)return iou_score

intersection 为两个区域的交集,即两个区域的乘积

temp 为两个区域的和,(注:这里不是并集,因为没有减去相交的部分)

union 为两个区域的并集

3.3 test

预测:

# prediction
predict = torch.tensor([0.01,0.03,0.02,0.02,0.05,0.12,0.09,0.07,0.89,0.85,0.88,0.91,0.99,0.97,0.95,0.97]).reshape(1,1,4,4)
'''
tensor([[[[0.0100, 0.0300, 0.0200, 0.0200],[0.0500, 0.1200, 0.0900, 0.0700],[0.8900, 0.8500, 0.8800, 0.9100],[0.9900, 0.9700, 0.9500, 0.9700]]]])
'''

label:

# label
label = torch.tensor([0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1]).reshape(1,1,4,4)
'''
tensor([[[[0, 0, 0, 0],[0, 0, 0, 0],[1, 1, 1, 1],[1, 1, 1, 1]]]])
'''

计算结果:

 

公式可知,dice和iou的关系为:

验证可知:

注:有些细微的差异是smooth所导致

3.4 dice 和 iou 的关系曲线

有公式可知,dice 和 iou 的关系公式如下:

关系曲线如图:

 

4. 代码

import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'import torch
import numpy as np
import matplotlib.pyplot as plt# prediction
predict = torch.tensor([0.01,0.03,0.02,0.02,0.05,0.12,0.09,0.07,0.89,0.85,0.88,0.91,0.99,0.97,0.95,0.97]).reshape(1,1,4,4)
'''
tensor([[[[0.0100, 0.0300, 0.0200, 0.0200],[0.0500, 0.1200, 0.0900, 0.0700],[0.8900, 0.8500, 0.8800, 0.9100],[0.9900, 0.9700, 0.9500, 0.9700]]]])
'''# label
label = torch.tensor([0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1]).reshape(1,1,4,4)
'''
tensor([[[[0, 0, 0, 0],[0, 0, 0, 0],[1, 1, 1, 1],[1, 1, 1, 1]]]])
'''# Dice
def Dice(pred,true):intersection = pred * true          # 计算交集  pred ∩ truetemp = pred + true                  # pred + truesmooth = 1e-8                       # 防止分母为 0dice_score = 2*intersection.sum() / (temp.sum() + smooth)return dice_score# Iou
def Iou(pred,true):intersection = pred * true          # 计算交集  pred ∩ truetemp = pred + true                  # pred + trueunion = temp - intersection         # 计算并集:A ∪ B = A + B - A ∩ Bsmooth = 1e-8                       # 防止分母为 0iou_score = intersection.sum() / (union.sum() + smooth)return iou_score# dice 和 iou 的换算
def dice_and_iou(x):y = x / (2 - x)return ydice = np.arange(0,1,0.001)
iou = dice_and_iou(dice)plt.plot(dice,iou)
plt.xlabel('dice')
plt.ylabel('iou')
plt.show()

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...