U-Net代码复现--utils dice_score.py

本文记录自己的学习过程,内容包括:

代码解读:Pytorch-UNet

深度学习编程基础:Pytorch-深度学习(新手友好)

UNet论文解读:医学图像分割:U_Net 论文阅读

数据:https://hackernoon.com/hacking-gta-v-for-carvana-kaggle-challenge-6d0b7fb4c781

完整代码解读详见:U-Net代码复现--更新中

utils

dice_score.py

在train.py中:
loss = criterion(masks_pred.squeeze(1), true_masks.float())
loss += dice_loss(F.sigmoid(masks_pred.squeeze(1)), true_masks.float(), multiclass=False)

其中:

  • 交叉信息熵:criterion = nn.CrossEntropyLoss() if model.n_classes > 1 else nn.BCEWithLogitsLoss() .
  • Dice 系数(Dice coefficient) 损失函数:dice_loss(F.sigmoid(masks_pred.squeeze(1)), true_masks.float(), multiclass=False)

Dice coefficient也被称为F1-score,是一种用于衡量两个集合相似度的指标,通常用于图像分割任务中。在医学图像分割中,Dice coefficient常用于衡量模型预测的分割掩码与真实标签之间的相似度。Dice coefficient越接近1,表示预测结果与真实标签越相似。

dice coefficient 源于二分类,本质上是衡量两个样本的重叠部分。该指标范围从0到1,其中"1"表示完整的重叠。 其计算公式为:
D i c e = 2 ∣ A ∩ B ∣ ∣ A ∣ + ∣ B ∣ Dice=\frac{2|A∩B|}{|A|+|B|} Dice=∣A∣+∣B∣2∣A∩B∣

其中 ∣ A ∩ B ∣ |A∩B| ∣A∩B∣ 表示集合A、B 之间的共同元素,|A| 表示 A 中的元素的个数,B也用相似的表示方法。

关于inter = 2 * (input * target).sum(dim=sum_dim)sum的理解参考:Pytorch:torch.sum()函数用法

python 复制代码
def dice_coeff(input: Tensor, target: Tensor, reduce_batch_first: bool = False, epsilon: float = 1e-6):
    # Average of Dice coefficient for all batches, or for a single mask
    # 确保input和target数量一致
    assert input.size() == target.size()
    # 确保imput维度为3,或者reduce_batch_first为False(这里reduce_batch_first为True,所以input.dim() == 3)
    assert input.dim() == 3 or not reduce_batch_first

	# sum_dim = (-1, -2, -3)
    sum_dim = (-1, -2) if input.dim() == 2 or not reduce_batch_first else (-1, -2, -3)

	# input 和 target像素为[0,1],相乘则得到均为1的部分,即A∩B
    inter = 2 * (input * target).sum(dim=sum_dim) # 对所有维度求和
    # 计算|A|+|B|
    sets_sum = input.sum(dim=sum_dim) + target.sum(dim=sum_dim)
    sets_sum = torch.where(sets_sum == 0, inter, sets_sum) 

    dice = (inter + epsilon) / (sets_sum + epsilon)
    return dice.mean()
python 复制代码
def multiclass_dice_coeff(input: Tensor, target: Tensor, reduce_batch_first: bool = False, epsilon: float = 1e-6):
    # Average of Dice coefficient for all classes
    return dice_coeff(input.flatten(0, 1), target.flatten(0, 1), reduce_batch_first, epsilon)
python 复制代码
def dice_loss(input: Tensor, target: Tensor, multiclass: bool = False):
    # Dice loss (objective to minimize) between 0 and 1
    fn = multiclass_dice_coeff if multiclass else dice_coeff
    return 1 - fn(input, target, reduce_batch_first=True)
相关推荐
Qres8218 分钟前
Rabrg/artificial-life test
python·模拟
财经资讯数据_灵砚智能16 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月1日
大数据·人工智能·python·信息可视化·自然语言处理
ting945200016 分钟前
动手学深度学习(PyTorch版)深度详解(8):现代循环神经网络(实战 + 避坑)
pytorch·rnn·深度学习
葫三生26 分钟前
三生原理文章被AtomGit‌开源社区收录的意义探析?
人工智能·深度学习·神经网络·算法·搜索引擎·开源·transformer
好奇龙猫29 分钟前
[大学院ーpython-base learning3: python and recommendation system ]
开发语言·python
篮子里的玫瑰37 分钟前
Python与网络爬虫——字典与集合
开发语言·python
DogDaoDao39 分钟前
【GitHub】OpenClaw:开源个人AI助手的新标杆
人工智能·深度学习·开源·大模型·github·ai编程·opeclaw
skilllite作者1 小时前
Zed 1.0 编辑器深度评测与实战指南
开发语言·人工智能·windows·python·编辑器·agi
2401_882273721 小时前
pattern属性在旧版Android浏览器无效怎么办_手动验证补充【操作】
jvm·数据库·python
贾红平1 小时前
Python装饰器实战指南
python