当前位置: 首页 > news >正文

聊聊图像分割的DICE和IOU指标

目录

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()

http://www.lryc.cn/news/36026.html

相关文章:

  • 软件设计师教程(十)计算机系统知识-结构化开发
  • 链表OJ之 快慢指针法总结
  • C++STL详解(五)——list的介绍与使用
  • 进程和进程的调度
  • TypeScript 深度剖析:TypeScript 的理解?与 JavaScript 的区别?
  • 美颜SDK关键技术讲解——人脸识别与人脸美化
  • Linux下C/C++ 网络扫描(主机扫描技术)
  • 无法将“vue-cli-service”项识别为 cmdlet、函数、脚本文件或不是内部命令的原因和解决方案
  • 逆流程 场景下 处理状态机变化的方案
  • 【剧前爆米花--爪哇岛寻宝】Java实现无头单向非循环链表和无头双向链表与相关题目
  • 学习MvvmLight工具
  • 基于BiLSTM+CRF医学病例命名实体识别项目
  • 05 C语言数据类型
  • C++11:右值引用和移动语义
  • tcpdump网络抓包工具
  • MaxCompute SQL中的所有保留字与关键字如下
  • Kafka 压缩算法
  • 关于React Hook(18)
  • 计算机网络:BGP协议
  • 91. 解码方法 ——【Leetcode每日刷题】
  • 人体存在传感器成品方案,精准感知静止存在,实时智能化感控技术
  • mysql连接池的实现
  • 哪种类型蓝牙耳机佩戴最舒服?舒适度最好的蓝牙耳机推荐
  • 2020蓝桥杯真题洁净数 C语言/C++
  • 【随笔二】useReducer详解及其应用场景
  • 打怪升级之istringstream介绍
  • 系统重装漏洞
  • C++面向对象编程之五:友元(friend)
  • [手写OS]动手实现一个OS 之X86实模式下的汇编开发
  • 【Linux内核二】常用的网络丢包错包debug工具介绍