损失函数#
分割损失#
DiceLoss#
- class monai.losses.DiceLoss(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, squared_pred=False, jaccard=False, reduction=mean, smooth_nr=1e-05, smooth_dr=1e-05, batch=False, weight=None)[source]#
计算两个张量之间的平均 Dice 损失。它支持多类别和多标签任务。将数据 input(BNHW[D],其中 N 是类别数)与真实值 target(BNHW[D])进行比较。
注意,input 的 N 轴应是每个类别的 logits 或概率,如果输入是 logits,则必须设置 sigmoid=True 或 softmax=True,或者指定 other_act。target 的同一轴可以是 1 或 N(one-hot 格式)。
smooth_nr 和 smooth_dr 参数分别是添加到交集和并集计算中的值,用于平滑结果,这些值应该很小。
原始论文:Milletari, F. et. al. (2016) V-Net: Fully Convolutional Neural Networks forVolumetric Medical Image Segmentation, 3DV, 2016。
- __init__(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, squared_pred=False, jaccard=False, reduction=mean, smooth_nr=1e-05, smooth_dr=1e-05, batch=False, weight=None)[source]#
- 参数:
include_background – 如果为 False,则计算中排除通道索引 0(背景类别)。如果非背景分割区域相对于总图像大小很小,它们可能会被背景信号淹没,因此在这种情况下排除背景有助于收敛。
to_onehot_y – 是否将
target
转换为 one-hot 格式,使用从 input (input.shape[1]
) 推断的类别数。默认为 False。sigmoid – 如果为 True,则对预测结果应用 sigmoid 函数。
softmax – 如果为 True,则对预测结果应用 softmax 函数。
other_act – 用于执行其他激活层的可调用函数,默认为
None
。例如:other_act = torch.tanh
。squared_pred – 是否在分母中使用 target 和 prediction 的平方形式。
jaccard – 是否计算 Jaccard Index (soft IoU) 而不是 Dice。
reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
smooth_nr – 添加到分子中的一个很小的常数,以避免出现零。
smooth_dr – 添加到分母中的一个很小的常数,以避免出现 nan。
batch – 是否在除法之前,跨 batch 维度对交集和并集面积求和。默认为 False,即在应用任何 reduction 之前,对 batch 中的每个项独立计算 Dice 损失值。
weight – 应用于每个类别的 voxel 的权重。如果为 None,则不应用权重。输入可以是一个单一值(所有类别使用相同的权重),或者一个序列值(序列长度应与类别数相同。如果未设置
include_background
,则类别数不应包含背景类别 0)。该值/这些值应不小于 0。默认为 None。
- 抛出:
TypeError – 当
other_act
不是Optional[Callable]
时。ValueError – 当 [
sigmoid=True
,softmax=True
,other_act is not None
] 中多于一个为 True 时。不兼容的值。
- forward(input, target)[source]#
- 参数:
input (
Tensor
) – 形状应为 BNH[WD],其中 N 是类别数。target (
Tensor
) – 形状应为 BNH[WD] 或 B1H[WD],其中 N 是类别数。
- 抛出:
AssertionError – 当 input 和 target(如果设置了 one hot 转换后)形状不同时。
ValueError – 当
self.reduction
不是 [“mean”, “sum”, “none”] 中的一个时。
示例
>>> from monai.losses.dice import * # NOQA >>> import torch >>> from monai.losses.dice import DiceLoss >>> B, C, H, W = 7, 5, 3, 2 >>> input = torch.rand(B, C, H, W) >>> target_idx = torch.randint(low=0, high=C - 1, size=(B, H, W)).long() >>> target = one_hot(target_idx[:, None, ...], num_classes=C) >>> self = DiceLoss(reduction='none') >>> loss = self(input, target) >>> assert np.broadcast_shapes(loss.shape, input.shape) == input.shape
- 返回值类型:
Tensor
MaskedDiceLoss#
- class monai.losses.MaskedDiceLoss(*args, **kwargs)[source]#
在 DiceLoss 之前添加额外的 masking 过程,接受一个指示区域的二进制 mask ([0, 1])。input 和 target 将按区域进行掩码:mask 为 1 的区域保留原始值,mask 为 0 的区域转换为 0。然后将掩码后的 input 和 target 输入到正常的 DiceLoss 计算中。这样可以确保只有掩码区域对损失计算以及随后的梯度计算做出贡献。
- __init__(*args, **kwargs)[source]#
参数遵循
monai.losses.DiceLoss
。
GeneralizedDiceLoss#
- class monai.losses.GeneralizedDiceLoss(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, w_type=square, reduction=mean, smooth_nr=1e-05, smooth_dr=1e-05, batch=False)[source]#
计算在以下论文中定义的广义 Dice 损失:
Sudre, C. et. al. (2017) Generalised Dice overlap as a deep learning loss function for highly unbalanced segmentations. DLMIA 2017.
- __init__(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, w_type=square, reduction=mean, smooth_nr=1e-05, smooth_dr=1e-05, batch=False)[source]#
- 参数:
include_background – 如果为 False,则计算中排除通道索引 0(背景类别)。
to_onehot_y – 是否将
target
转换为 one-hot 格式,使用从 input (input.shape[1]
) 推断的类别数。默认为 False。sigmoid – 如果为 True,则对预测结果应用 sigmoid 函数。
softmax – 如果为 True,则对预测结果应用 softmax 函数。
other_act – 用于执行其他激活层的可调用函数,默认为
None
。例如:other_act = torch.tanh
。w_type – {
"square"
,"simple"
,"uniform"
} 将真实值体素转换为权重因子的函数类型。默认为"square"
。reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
smooth_nr – 添加到分子中的一个很小的常数,以避免出现零。
smooth_dr – 添加到分母中的一个很小的常数,以避免出现 nan。
batch – 是否在除法之前,跨 batch 维度对交集和并集面积求和。默认为 False,即对 batch 中的每个项计算 intersection over union。如果为 True,则首先跨 batch 对类别加权的交集和并集面积求和。
- 抛出:
TypeError – 当
other_act
不是Optional[Callable]
时。ValueError – 当 [
sigmoid=True
,softmax=True
,other_act is not None
] 中多于一个为 True 时。不兼容的值。
GeneralizedWassersteinDiceLoss#
- class monai.losses.GeneralizedWassersteinDiceLoss(dist_matrix, weighting_mode='default', reduction=mean, smooth_nr=1e-05, smooth_dr=1e-05)[source]#
计算在以下论文中定义的广义 Wasserstein Dice 损失:
Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class Segmentation using Holistic Convolutional Networks. BrainLes 2017.
或其变体(使用 weighting_mode=”GDL” 选项),定义在以下论文的附录中:
Tilborghs, S. et al. (2020) Comparative study of deep learning methods for the automatic segmentation of lung, lesion and lesion type in CT scans of COVID-19 patients. arXiv preprint arXiv:2007.15546
- __init__(dist_matrix, weighting_mode='default', reduction=mean, smooth_nr=1e-05, smooth_dr=1e-05)[source]#
- 参数:
dist_matrix – 2D tensor 或 2D numpy 数组;类别之间的距离矩阵。
classes. (其维度必须为 C x C,其中 C 是类别数。)
weighting_mode –
{
"default"
,"GDL"
} 指定如何对类别特定的误差总和进行加权。默认为"default"
。"default"
:(推荐)使用原始加权方法,如Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class Segmentation using Holistic Convolutional Networks. BrainLes 2017.
"GDL"
: 使用类似于 GDL 的加权方法,如附录中的Tilborghs, S. et al. (2020) Comparative study of deep learning methods for the automatic segmentation of lung, lesion and lesion type in CT scans of COVID-19 patients. arXiv preprint arXiv:2007.15546
reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
smooth_nr – 添加到分子中的一个很小的常数,以避免出现零。
smooth_dr – 添加到分母中的一个很小的常数,以避免出现 nan。
- 抛出:
ValueError – 当
dist_matrix
不是方阵时。
示例
import torch import numpy as np from monai.losses import GeneralizedWassersteinDiceLoss # Example with 3 classes (including the background: label 0). # The distance between the background class (label 0) and the other classes is the maximum, equal to 1. # The distance between class 1 and class 2 is 0.5. dist_mat = np.array([[0.0, 1.0, 1.0], [1.0, 0.0, 0.5], [1.0, 0.5, 0.0]], dtype=np.float32) wass_loss = GeneralizedWassersteinDiceLoss(dist_matrix=dist_mat) pred_score = torch.tensor([[1000, 0, 0], [0, 1000, 0], [0, 0, 1000]], dtype=torch.float32) grnd = torch.tensor([0, 1, 2], dtype=torch.int64) wass_loss(pred_score, grnd) # 0
- forward(input, target)[source]#
- 参数:
input (
Tensor
) – 形状应为 BNH[WD]。target (
Tensor
) – 形状应为 BNH[WD]。
- 返回值类型:
Tensor
- wasserstein_distance_map(flat_proba, flat_target)[source]#
计算在标签空间 M 上的距离矩阵下,扁平化预测与扁平化标签(真实值)之间的体素级 Wasserstein 距离。这对应于以下论文中的公式 6:
Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class Segmentation using Holistic Convolutional Networks. BrainLes 2017.
- 参数:
flat_proba (
Tensor
) – 输入(预测)张量的概率。flat_target (
Tensor
) – 目标张量。
- 返回值类型:
Tensor
DiceCELoss#
- class monai.losses.DiceCELoss(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, squared_pred=False, jaccard=False, reduction='mean', smooth_nr=1e-05, smooth_dr=1e-05, batch=False, weight=None, lambda_dice=1.0, lambda_ce=1.0, label_smoothing=0.0)[source]#
计算 Dice 损失和交叉熵损失,并返回这两种损失的加权和。Dice 损失的详细信息请参阅
monai.losses.DiceLoss
。交叉熵损失的详细信息请参阅torch.nn.CrossEntropyLoss
和torch.nn.BCEWithLogitsLoss()
。此实现不支持两个已弃用的参数size_average
和reduce
,以及参数ignore_index
。- __init__(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, squared_pred=False, jaccard=False, reduction='mean', smooth_nr=1e-05, smooth_dr=1e-05, batch=False, weight=None, lambda_dice=1.0, lambda_ce=1.0, label_smoothing=0.0)[source]#
- 参数:
损失。 (reduction 和 weight 用于两种损失,其他参数仅用于 Dice)
损失。
include_background – 如果为 False,则计算中排除通道索引 0(背景类别)。
to_onehot_y – 是否将
target
转换为 one-hot 格式,使用从 input (input.shape[1]
) 推断的类别数。默认为 False。sigmoid – 如果为 True,则对预测结果应用 sigmoid 函数,仅用于 DiceLoss,CrossEntropyLoss 和 BCEWithLogitsLoss 不需要指定激活函数。
softmax – 如果为 True,则对预测结果应用 softmax 函数,仅用于 DiceLoss,CrossEntropyLoss 和 BCEWithLogitsLoss 不需要指定激活函数。
other_act – 用于执行其他激活层的可调用函数,默认为
None
。例如:other_act = torch.tanh
。仅用于 DiceLoss,不用于 CrossEntropyLoss 和 BCEWithLogitsLoss。squared_pred – 是否在分母中使用 target 和 prediction 的平方形式。
jaccard – 是否计算 Jaccard Index (soft IoU) 而不是 Dice。
reduction –
{
"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。Dice 损失至少应 reduction 空间维度,这与交叉熵损失不同,因此此处不能使用none
选项。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
smooth_nr – 添加到分子中的一个很小的常数,以避免出现零。
smooth_dr – 添加到分母中的一个很小的常数,以避免出现 nan。
batch – 是否在除法之前,跨 batch 维度对交集和并集面积求和。默认为 False,即在应用任何 reduction 之前,对 batch 中的每个项独立计算 Dice 损失值。
weight – 用于 CrossEntropyLoss 的交叉熵损失中赋予每个类别的重新缩放权重。或用于 BCEWithLogitsLoss 中与 target 一起广播的正例权重 pos_weight。更多信息请参阅
torch.nn.CrossEntropyLoss()
或torch.nn.BCEWithLogitsLoss()
。此 weight 也用于 DiceLoss。lambda_dice – Dice 损失的折衷权重值。该值应不小于 0.0。默认为 1.0。
lambda_ce – 交叉熵损失的折衷权重值。该值应不小于 0.0。默认为 1.0。
label_smoothing – [0, 1] 范围内的值。如果 > 0,标签将按给定因子进行平滑处理,以减少过拟合。默认为 0.0。
- ce(input, target)[source]#
计算输入的 logits 和 target 的交叉熵损失。将根据 PyTorch CrossEntropyLoss 移除通道维度:https://pytorch.ac.cn/docs/stable/generated/torch.nn.CrossEntropyLoss.html?#torch.nn.CrossEntropyLoss。
- 返回值类型:
Tensor
DiceFocalLoss#
- class monai.losses.DiceFocalLoss(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, squared_pred=False, jaccard=False, reduction='mean', smooth_nr=1e-05, smooth_dr=1e-05, batch=False, gamma=2.0, weight=None, lambda_dice=1.0, lambda_focal=1.0, alpha=None)[source]#
计算 Dice 损失和 Focal 损失,并返回这两种损失的加权和。Dice 损失的详细信息请参阅
monai.losses.DiceLoss
。Focal 损失的详细信息请参阅monai.losses.FocalLoss
。gamma
和lambda_focal
仅用于 Focal 损失。include_background
,weight
,reduction
和alpha
用于两种损失,其他参数仅用于 Dice 损失。- __init__(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, squared_pred=False, jaccard=False, reduction='mean', smooth_nr=1e-05, smooth_dr=1e-05, batch=False, gamma=2.0, weight=None, lambda_dice=1.0, lambda_focal=1.0, alpha=None)[source]#
- 参数:
include_background – 如果为 False,则计算中排除通道索引 0(背景类别)。
to_onehot_y – 是否将
target
转换为 one-hot 格式,使用从 input (input.shape[1]
) 推断的类别数。默认为 False。sigmoid – 如果为 True,则对预测结果应用 sigmoid 函数,仅用于 DiceLoss,FocalLoss 不需要指定激活函数。
softmax – 如果为 True,则对预测结果应用 softmax 函数,仅用于 DiceLoss,FocalLoss 不需要指定激活函数。
other_act – 用于执行其他激活层的可调用函数,默认为
None
。例如:other_act = torch.tanh。仅用于 DiceLoss,不用于 FocalLoss。squared_pred – 是否在分母中使用 target 和 prediction 的平方形式。
jaccard – 是否计算 Jaccard Index (soft IoU) 而不是 Dice。
reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
smooth_nr – 添加到分子中的一个很小的常数,以避免出现零。
smooth_dr – 添加到分母中的一个很小的常数,以避免出现 nan。
batch – 是否在除法之前,跨 batch 维度对交集和并集面积求和。默认为 False,即在应用任何 reduction 之前,对 batch 中的每个项独立计算 Dice 损失值。
gamma – Focal 损失定义中的指数 gamma 值。
weight – 应用于每个类别 voxel 的权重。如果为 None,则不应用权重。输入可以是一个单一值(所有类别使用相同的权重),或者一个序列值(序列长度应与类别数相同)。
lambda_dice – Dice 损失的折衷权重值。该值应不小于 0.0。默认为 1.0。
lambda_focal – Focal 损失的折衷权重值。该值应不小于 0.0。默认为 1.0。
alpha – alpha-balanced Focal 损失定义中的 alpha 值。该值应在 [0, 1] 范围内。默认为 None。
GeneralizedDiceFocalLoss#
- class monai.losses.GeneralizedDiceFocalLoss(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, w_type=square, reduction=mean, smooth_nr=1e-05, smooth_dr=1e-05, batch=False, gamma=2.0, weight=None, lambda_gdl=1.0, lambda_focal=1.0)[source]#
计算广义 Dice 损失和 Focal 损失,并返回它们的加权平均值。广义 Dice 损失和 Focal 损失的详细信息可在
monai.losses.GeneralizedDiceLoss
和monai.losses.FocalLoss
中找到。- 参数:
include_background (bool, 可选) – 如果为 False,则计算中排除通道索引 0(背景类别)。默认为 True。
to_onehot_y – 是否将
target
转换为 one-hot 格式,使用从 input (input.shape[1]
) 推断的类别数。默认为 False。sigmoid (bool, 可选) – 如果为 True,则对预测结果应用 sigmoid 函数。默认为 False。
softmax (bool, 可选) – 如果为 True,则对预测结果应用 softmax 函数。默认为 False。
other_act (Optional[Callable], 可选) – 用于执行其他激活层的可调用函数,默认为
None
。例如:other_act = torch.tanh。仅用于 GeneralizedDiceLoss,不用于 FocalLoss。w_type (Union[Weight, str], 可选) – {
"square"
,"simple"
,"uniform"
}。将真实值体素转换为权重因子的函数类型。默认为"square"
。reduction (Union[LossReduction, str], 可选) – {
"none"
,"mean"
,"sum"
}。指定要应用于输出的 reduction 方式。默认为"mean"
。 -"none"
: 不应用 reduction。 -"mean"
: 输出的总和将除以输出中的元素数量。 -"sum"
: 输出将求和。smooth_nr (float, 可选) – 添加到分子中的一个很小的常数,以避免出现零。默认为 1e-5。
smooth_dr (float, 可选) – 添加到分母中的一个很小的常数,以避免出现 nan。默认为 1e-5。
batch (bool, 可选) – 是否在除法之前,跨 batch 维度对交集和并集面积求和。默认为 False,即对 batch 中的每个项计算面积。
gamma (float, 可选) – Focal 损失定义中的指数 gamma 值。默认为 2.0。
weight (Optional[Union[Sequence[float], float, int, torch.Tensor]], 可选) – 应用于每个类别 voxel 的权重。如果为 None,则不应用权重。输入可以是一个单一值(所有类别使用相同的权重),或者一个序列值(序列长度应与类别数相同)。默认为 None。
lambda_gdl (float, 可选) – 广义 Dice 损失的折衷权重值。该值应不小于 0.0。默认为 1.0。
lambda_focal (float, 可选) – Focal 损失的折衷权重值。该值应不小于 0.0。默认为 1.0。
- 抛出:
ValueError – 如果 lambda_gdl 或 lambda_focal 小于 0。
FocalLoss#
- class monai.losses.FocalLoss(include_background=True, to_onehot_y=False, gamma=2.0, alpha=None, weight=None, reduction=mean, use_softmax=False)[source]#
FocalLoss 是 BCEWithLogitsLoss 的扩展,它对高置信度正确预测的损失进行降权。
重新实现了以下论文中描述的 Focal Loss:
[“Focal Loss for Dense Object Detection”](https://arxiv.org/abs/1708.02002),T. Lin et al.,ICCV 2017
“AnatomyNet: Deep learning for fast and fully automated whole-volume segmentation of head and neck anatomy”,Zhu et al.,Medical Physics 2018
示例
>>> import torch >>> from monai.losses import FocalLoss >>> from torch.nn import BCEWithLogitsLoss >>> shape = B, N, *DIMS = 2, 3, 5, 7, 11 >>> input = torch.rand(*shape) >>> target = torch.rand(*shape) >>> # Demonstrate equivalence to BCE when gamma=0 >>> fl_g0_criterion = FocalLoss(reduction='none', gamma=0) >>> fl_g0_loss = fl_g0_criterion(input, target) >>> bce_criterion = BCEWithLogitsLoss(reduction='none') >>> bce_loss = bce_criterion(input, target) >>> assert torch.allclose(fl_g0_loss, bce_loss) >>> # Demonstrate "focus" by setting gamma > 0. >>> fl_g2_criterion = FocalLoss(reduction='none', gamma=2) >>> fl_g2_loss = fl_g2_criterion(input, target) >>> # Mark easy and hard cases >>> is_easy = (target > 0.7) & (input > 0.7) >>> is_hard = (target > 0.7) & (input < 0.3) >>> easy_loss_g0 = fl_g0_loss[is_easy].mean() >>> hard_loss_g0 = fl_g0_loss[is_hard].mean() >>> easy_loss_g2 = fl_g2_loss[is_easy].mean() >>> hard_loss_g2 = fl_g2_loss[is_hard].mean() >>> # Gamma > 0 causes the loss function to "focus" on the hard >>> # cases. IE, easy cases are downweighted, so hard cases >>> # receive a higher proportion of the loss. >>> hard_to_easy_ratio_g2 = hard_loss_g2 / easy_loss_g2 >>> hard_to_easy_ratio_g0 = hard_loss_g0 / easy_loss_g0 >>> assert hard_to_easy_ratio_g2 > hard_to_easy_ratio_g0
- __init__(include_background=True, to_onehot_y=False, gamma=2.0, alpha=None, weight=None, reduction=mean, use_softmax=False)[source]#
- 参数:
include_background – 如果为 False,则损失计算中排除通道索引 0(背景类别)。如果为 False,则在使用 softmax 时 alpha 无效。
to_onehot_y – 是否将标签 y 转换为 one-hot 格式。默认为 False。
gamma – Focal 损失定义中的指数 gamma 值。默认为 2。
alpha – alpha-balanced Focal 损失定义中的 alpha 值。该值应在 [0, 1] 范围内。默认为 None。
weight – 应用于每个类别的 voxel 的权重。如果为 None,则不应用权重。输入可以是一个单一值(所有类别使用相同的权重),或者一个序列值(序列长度应与类别数相同。如果未设置
include_background
,则类别数不应包含背景类别 0)。该值/这些值应不小于 0。默认为 None。reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
use_softmax – 是否使用 softmax 将原始 logits 转换为概率。如果为 True,则使用 softmax。如果为 False,则使用 sigmoid。默认为 False。
示例
>>> import torch >>> from monai.losses import FocalLoss >>> pred = torch.tensor([[1, 0], [0, 1], [1, 0]], dtype=torch.float32) >>> grnd = torch.tensor([[0], [1], [0]], dtype=torch.int64) >>> fl = FocalLoss(to_onehot_y=True) >>> fl(pred, grnd)
- forward(input, target)[source]#
- 参数:
input (
Tensor
) – 形状应为 BNH[WD],其中 N 是类别数。输入应为原始的 logits,因为它将在 forward 函数中通过 sigmoid/softmax 进行转换。target (
Tensor
) – 形状应为 BNH[WD] 或 B1H[WD],其中 N 是类别数。
- 抛出:
ValueError – 当 input 和 target(如果设置了 one hot 转换后)形状不同时。
ValueError – 当
self.reduction
不是 [“mean”, “sum”, “none”] 中的一个时。ValueError – 当
self.weight
是一个序列且长度与类别数不相等时。ValueError – 当
self.weight
是/包含一个小于 0 的值时。
- 返回值类型:
Tensor
TverskyLoss#
- class monai.losses.TverskyLoss(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, alpha=0.5, beta=0.5, reduction=mean, smooth_nr=1e-05, smooth_dr=1e-05, batch=False)[source]#
计算在以下论文中定义的 Tversky 损失:
Sadegh et al. (2017) Tversky loss function for image segmentation using 3D fully convolutional deep networks. (https://arxiv.org/abs/1706.05721)
- __init__(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, alpha=0.5, beta=0.5, reduction=mean, smooth_nr=1e-05, smooth_dr=1e-05, batch=False)[source]#
- 参数:
include_background – 如果为 False,则计算中排除通道索引 0(背景类别)。
to_onehot_y – 是否将 y 转换为 one-hot 格式。默认为 False。
sigmoid – 如果为 True,则对预测结果应用 sigmoid 函数。
softmax – 如果为 True,则对预测结果应用 softmax 函数。
other_act – 如果不想使用 sigmoid 或 softmax,则使用其他可调用函数来执行其他激活层,默认为
None
。例如:other_act = torch.tanh。alpha – 假阳性权重
beta – 假阴性权重
reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
smooth_nr – 添加到分子中的一个很小的常数,以避免出现零。
smooth_dr – 添加到分母中的一个很小的常数,以避免出现 nan。
batch – 是否在除法之前,跨 batch 维度对交集和并集面积求和。默认为 False,即在应用任何 reduction 之前,对 batch 中的每个项独立计算 Dice 损失值。
- 抛出:
TypeError – 当
other_act
不是Optional[Callable]
时。ValueError – 当 [
sigmoid=True
,softmax=True
,other_act is not None
] 中多于一个为 True 时。不兼容的值。
ContrastiveLoss#
- class monai.losses.ContrastiveLoss(temperature=0.5, batch_size=-1)[source]#
计算在以下论文中定义的 Contrastive 损失:
Chen, Ting, et al. “A simple framework for contrastive learning of visual representations.” International conference on machine learning. PMLR, 2020. (http://proceedings.mlr.press/v119/chen20j.html)
BarlowTwinsLoss#
- class monai.losses.BarlowTwinsLoss(lambd=0.005)[source]#
Barlow Twins 成本函数获取神经网络从两个失真视图中提取的表示,并试图使这两个表示的互相关矩阵趋向于单位矩阵。这鼓励神经网络学习相似且冗余度最低的表示。此成本函数特别适用于多模态学习,处理来自两种模态的表示。最常见的用例是非监督学习,其中使用数据增强来生成同一样本的两个失真视图,以强制编码器提取对下游任务有用的特征。
Zbontar, Jure, 等人。“Barlow Twins: Self-Supervised Learning via Redundancy Reduction” 国际机器学习大会。PMLR, 2020。 (http://proceedings.mlr.press/v139/zbontar21a/zbontar21a.pdf)
HausdorffDTLoss#
- class monai.losses.HausdorffDTLoss(alpha=2.0, include_background=False, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, reduction=mean, batch=False)[source]#
根据距离变换计算通道级二元 Hausdorff 损失。它可以支持多类别和多标签任务。输入数据 input (BNHW[D],其中 N 是类别数) 与真实值 target (BNHW[D]) 进行比较。
注意,input 的 N 轴应是每个类别的 logits 或概率,如果输入是 logits,则必须设置 sigmoid=True 或 softmax=True,或者指定 other_act。target 的同一轴可以是 1 或 N(one-hot 格式)。
原始论文: Karimi, D. et. al. (2019) Reducing the Hausdorff Distance in Medical Image Segmentation with Convolutional Neural Networks, IEEE Transactions on medical imaging, 39(2), 499-513
- __init__(alpha=2.0, include_background=False, to_onehot_y=False, sigmoid=False, softmax=False, other_act=None, reduction=mean, batch=False)[source]#
- 参数:
include_background – 如果为 False,则计算中排除通道索引 0(背景类别)。如果非背景分割区域相对于总图像大小很小,它们可能会被背景信号淹没,因此在这种情况下排除背景有助于收敛。
to_onehot_y – 是否将
target
转换为 one-hot 格式,使用从 input (input.shape[1]
) 推断的类别数。默认为 False。sigmoid – 如果为 True,则对预测结果应用 sigmoid 函数。
softmax – 如果为 True,则对预测结果应用 softmax 函数。
other_act – 用于执行其他激活层的可调用函数,默认为
None
。例如:other_act = torch.tanh
。reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
batch – 在除以总数之前,是否对批量维度上的交集和并集区域求和。默认为 False,在应用任何 reduction 之前,批量中的每个项目都独立计算损失值。
- 抛出:
TypeError – 当
other_act
不是Optional[Callable]
时。ValueError – 当 [
sigmoid=True
,softmax=True
,other_act is not None
] 中多于一个为 True 时。不兼容的值。
- distance_field(img)#
生成距离变换。
- 参数:
img (np.ndarray) – 输入掩码,形状为 NCHWD 或 NCHW。
- 返回:
距离场。
- 返回值类型:
np.ndarray
- forward(input, target)[source]#
- 参数:
input (
Tensor
) – 形状应为 BNHW[D],其中 N 是类别数。target (
Tensor
) – 形状应为 BNHW[D] 或 B1HW[D],其中 N 是类别数。
- 抛出:
ValueError – 如果输入不是 2D (NCHW) 或 3D (NCHWD)。
AssertionError – 当 input 和 target(如果设置了 one hot 转换后)形状不同时。
ValueError – 当
self.reduction
不是 [“mean”, “sum”, “none”] 中的一个时。
示例
>>> import torch >>> from monai.losses.hausdorff_loss import HausdorffDTLoss >>> from monai.networks.utils import one_hot >>> B, C, H, W = 7, 5, 3, 2 >>> input = torch.rand(B, C, H, W) >>> target_idx = torch.randint(low=0, high=C - 1, size=(B, H, W)).long() >>> target = one_hot(target_idx[:, None, ...], num_classes=C) >>> self = HausdorffDTLoss(reduction='none') >>> loss = self(input, target) >>> assert np.broadcast_shapes(loss.shape, input.shape) == input.shape
- 返回值类型:
Tensor
SoftclDiceLoss#
- class monai.losses.SoftclDiceLoss(iter_=3, smooth=1.0)[source]#
计算 Shit 等人定义的 Soft clDice 损失。
Shit et al. (2021) clDice – A Novel Topology-Preserving Loss Function for Tubular Structure Segmentation. (https://arxiv.org/abs/2003.07311)
SoftDiceclDiceLoss#
- class monai.losses.SoftDiceclDiceLoss(iter_=3, alpha=0.5, smooth=1.0)[source]#
计算 Shit 等人定义的 Soft clDice 损失。
Shit et al. (2021) clDice – A Novel Topology-Preserving Loss Function for Tubular Structure Segmentation. (https://arxiv.org/abs/2003.07311)
NACLLoss#
- class monai.losses.NACLLoss(classes, dim, kernel_size=3, kernel_ops='mean', distance_type='l1', alpha=0.1, sigma=1.0)[source]#
Neighbor-Aware Calibration Loss (NACL) 主要用于开发图像分割中的校准模型。NACL 计算标准交叉熵损失,并附加一个线性惩罚项,该惩罚项强制对数分布与周围像素的软类别比例相匹配。
Murugesan, Balamurali, 等人。“Trust your neighbours: Penalty-based constraints for model calibration.” 国际医学影像计算和计算机辅助干预大会,MICCAI 2023。 https://arxiv.org/abs/2303.06268
Murugesan, Balamurali, 等人。“Neighbor-Aware Calibration of Segmentation Networks with Penalty-Based Constraints.” https://arxiv.org/abs/2401.14487
- __init__(classes, dim, kernel_size=3, kernel_ops='mean', distance_type='l1', alpha=0.1, sigma=1.0)[source]#
- 参数:
classes (
int
) – 类别数量dim (
int
) – 数据维度 (支持 2d 和 3d)kernel_size (
int
) – 空间核的大小distance_type (
str
) – 空间核与预测 logits 之间的 l1/l2 距离alpha (
float
) – 交叉熵与 logit 约束之间的权重sigma (
float
) – 高斯函数的 sigma
- forward(inputs, targets)[source]#
计算标准交叉熵损失,并对其邻域感知 logit 惩罚进行约束。
- 参数:
inputs (
Tensor
) – 形状应为 BNH[WD],其中 N 是类别数。targets (
Tensor
) – 形状应为 BH[WD]。
- 返回:
损失值。
- 返回值类型:
torch.Tensor
示例
>>> import torch >>> from monai.losses import NACLLoss >>> B, N, H, W = 8, 3, 64, 64 >>> input = torch.rand(B, N, H, W) >>> target = torch.randint(0, N, (B, H, W)) >>> criterion = NACLLoss(classes = N, dim = 2) >>> loss = criterion(input, target)
配准损失#
BendingEnergyLoss#
- class monai.losses.BendingEnergyLoss(normalize=False, reduction=mean)[source]#
使用中心有限差分计算
pred
的二阶导数,从而计算弯曲能量。更多信息请参阅 Project-MONAI/tutorials。
- 改编自
DeepReg (DeepRegNet/DeepReg)
DiffusionLoss#
- class monai.losses.DiffusionLoss(normalize=False, reduction=mean)[source]#
使用中心有限差分计算
pred
的一阶导数,从而计算扩散。关于原始论文,请参阅 VoxelMorph: A Learning Framework for Deformable Medical Image Registration, Guha Balakrishnan, Amy Zhao, Mert R. Sabuncu, John Guttag, Adrian V. Dalca IEEE TMI: Transactions on Medical Imaging. 2019. eprint arXiv:1809.05231。更多信息请参阅 Project-MONAI/tutorials。
- 改编自
VoxelMorph (voxelmorph/voxelmorph)
- __init__(normalize=False, reduction=mean)[source]#
- 参数:
normalize – 是否除以空间尺寸以使计算大致与图像比例(即矢量场采样分辨率)无关。默认为 False。
reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
- forward(pred)[source]#
- 参数:
pred (
Tensor
) – 预测的密集位移场 (DDF),形状为 BCH[WD],其中 C 是空间维度数。注意,只有当 DDF 沿所有空间维度的大小都大于 2 时,才能计算扩散损失。- 抛出:
ValueError – 当
self.reduction
不是 [“mean”, “sum”, “none”] 中的一个时。ValueError – 当
pred
不是 3-d, 4-d 或 5-d 时。ValueError – 当
pred
的任何空间维度大小小于或等于 2 时。ValueError – 当
pred
的通道数与空间维度数不匹配时。
- 返回值类型:
Tensor
LocalNormalizedCrossCorrelationLoss#
- class monai.losses.LocalNormalizedCrossCorrelationLoss(spatial_dims=3, kernel_size=3, kernel_type='rectangular', reduction=mean, smooth_nr=0.0, smooth_dr=1e-05)[source]#
局部平方零归一化互相关。该损失基于在 y_true/y_pred 上移动的核/窗口,在窗口内计算 zncc 的平方。核可以是矩形/三角形/高斯窗口。最终损失是所有窗口上的平均损失。
- 改编自
voxelmorph/voxelmorph DeepReg (DeepRegNet/DeepReg)
- __init__(spatial_dims=3, kernel_size=3, kernel_type='rectangular', reduction=mean, smooth_nr=0.0, smooth_dr=1e-05)[source]#
- 参数:
spatial_dims – 空间维度数,{
1
,2
,3
}。默认为 3。kernel_size – 核空间大小,必须是奇数。
kernel_type – {
"rectangular"
,"triangular"
,"gaussian"
}。默认为"rectangular"
。reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
smooth_nr – 添加到分子中的一个小数,以避免 nan。
smooth_dr – 添加到分母中的一个很小的常数,以避免出现 nan。
GlobalMutualInformationLoss#
- class monai.losses.GlobalMutualInformationLoss(kernel_type='gaussian', num_bins=23, sigma_ratio=0.5, reduction=mean, smooth_nr=1e-07, smooth_dr=1e-07)[source]#
通过 Parzen 窗口化方法计算可微分的全局互信息损失。
- 参考
https://dspace.mit.edu/handle/1721.1/123142, 第 3.1 节, 方程 3.1-3.5, 算法 1
- __init__(kernel_type='gaussian', num_bins=23, sigma_ratio=0.5, reduction=mean, smooth_nr=1e-07, smooth_dr=1e-07)[source]#
- 参数:
kernel_type –
{
"gaussian"
,"b-spline"
}"gaussian"
: 改编自 DeepReg 参考: https://dspace.mit.edu/handle/1721.1/123142, 第 3.1 节, 方程 3.1-3.5, 算法 1。"b-spline"
: 基于 Mattes 等人 [1,2] 的方法,改编自 ITK .. rubric:: 参考- [1] “Nonrigid multimodality image registration” (非刚性多模态图像配准)
D. Mattes, D. R. Haynor, H. Vesselle, T. Lewellen 和 W. Eubank Medical Imaging 2001: Image Processing, 2001, 第 1609-1620 页。
- [2] “PET-CT Image Registration in the Chest Using Free-form Deformations” (使用自由形变对胸部 PET-CT 图像进行配准)
D. Mattes, D. R. Haynor, H. Vesselle, T. Lewellen 和 W. Eubank IEEE Transactions in Medical Imaging. Vol.22, No.1, 2003 年 1 月。第 120-128 页。
num_bins – 强度直方图的箱数
sigma_ratio – 高斯函数的超参数
reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
smooth_nr – 添加到分子中的一个小数,以避免 nan。
smooth_dr – 添加到分母中的一个很小的常数,以避免出现 nan。
- forward(pred, target)[source]#
- 参数:
pred (
Tensor
) – 形状应为 B[NDHW]。target (
Tensor
) – 形状应与 pred 形状相同。
- 抛出:
ValueError – 当
self.reduction
不是 [“mean”, “sum”, “none”] 中的一个时。- 返回值类型:
Tensor
重建损失#
SSIMLoss#
- class monai.losses.ssim_loss.SSIMLoss(spatial_dims, data_range=1.0, kernel_type=gaussian, win_size=11, kernel_sigma=1.5, k1=0.01, k2=0.03, reduction=mean)[source]#
基于结构相似性指数度量 (SSIM) 指标计算损失函数。
- 更多信息,请访问
- SSIM 参考论文
Wang, Zhou, 等人。“Image quality assessment: from error visibility to structural similarity.” IEEE transactions on image processing 13.4 (2004): 600-612。
- __init__(spatial_dims, data_range=1.0, kernel_type=gaussian, win_size=11, kernel_sigma=1.5, k1=0.01, k2=0.03, reduction=mean)[source]#
- 参数:
spatial_dims – 输入图像的空间维度数。
data_range – 输入图像的值范围。(通常为 1.0 或 255)
kernel_type – 核的类型,可以是 "gaussian" 或 "uniform"。
win_size – 核的窗口大小
kernel_sigma – 高斯核的标准差。
k1 – 用于亮度分母的稳定性常数
k2 – 用于对比度分母的稳定性常数
reduction – {
"none"
,"mean"
,"sum"
} 指定应用于输出的归约方式。默认为"mean"
。 -"none"
: 不应用任何归约。 -"mean"
: 输出的总和将被输出元素的数量除以。 -"sum"
: 输出将被求和。
- forward(input, target)[source]#
- 参数:
input (
Tensor
) – 预测图像的批量,形状为 (batch_size, channels, spatial_dim1, spatial_dim2[, spatial_dim3])target (
Tensor
) – 目标图像的批量,形状为 (batch_size, channels, spatial_dim1, spatial_dim2[, spatial_dim3])
- 返回值类型:
Tensor
- 返回:
1 减去 ssim 指数(注意这是作为损失函数使用)
示例
import torch # 2D data x = torch.ones([1,1,10,10])/2 y = torch.ones([1,1,10,10])/2 print(1-SSIMLoss(spatial_dims=2)(x,y)) # pseudo-3D data x = torch.ones([1,5,10,10])/2 # 5 could represent number of slices y = torch.ones([1,5,10,10])/2 print(1-SSIMLoss(spatial_dims=2)(x,y)) # 3D data x = torch.ones([1,1,10,10,10])/2 y = torch.ones([1,1,10,10,10])/2 print(1-SSIMLoss(spatial_dims=3)(x,y))
PatchAdversarialLoss#
- class monai.losses.PatchAdversarialLoss(reduction=mean, criterion=least_squares, no_activation_leastsq=False)[source]#
计算 Patch Discriminator 或 Multi-scale Patch Discriminator 上的对抗损失。警告:由于可能使用不同的标准,判别器的输出不得经过最终激活层。这将在损失函数内部处理。
- 参数:
reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
criterion – 您希望在判别器输出上使用哪个标准(hinge、least_squares 或 bce)。根据标准,将使用不同的激活层。确保在调用损失函数之前不要让输出经过激活层。
no_activation_leastsq – 如果为 True,则在 least-squares 情况下移除激活层。
- forward(input, target_is_real, for_discriminator)[source]#
- 参数:
input – Multi-Scale Patch Discriminator 或 Patch Discriminator 的输出;可以是张量列表或单个张量;它们不应经过激活层。
target_is_real – 输入是否对应于真实或伪造图像的判别器输出
for_discriminator – 损失是否为判别器或生成器计算。在后一种情况下,target_is_real 设置为 True,因为生成器希望输入被视为真实。
- Returns: 如果 reduction 为 None,则如果激活了多尺度判别器,返回每个判别器损失张量的列表;如果只有一个判别器,则返回损失张量。否则,返回张量和判别器上的总和或平均损失。
discriminator is active, or the loss tensor if there is just one discriminator. Otherwise, it returns the summed or mean loss over the tensor and discriminator/s.
PerceptualLoss#
- class monai.losses.PerceptualLoss(spatial_dims, network_type=alex, is_fake_3d=True, fake_3d_ratio=0.5, cache_dir=None, pretrained=True, pretrained_path=None, pretrained_state_dict_key=None, channel_wise=False)[source]#
使用预训练深度神经网络的特征计算感知损失。该函数支持在以下数据集上预训练的网络:ImageNet,使用 Zhang 等人的 LPIPS 方法,“深度特征作为感知度量的非理性有效性” https://arxiv.org/abs/1801.03924;RadImagenet,来自 Mei 等人,“RadImageNet: 一个用于有效迁移学习的开放放射学深度学习研究数据集” https://pubs.rsna.org/doi/full/10.1148/ryai.210315;MedicalNet,来自 Chen 等人,“Med3D: 用于 3D 医学图像分析的迁移学习” https://arxiv.org/abs/1904.00625;以及 Torchvision 的 ResNet50: https://pytorch.ac.cn/vision/main/models/generated/torchvision.models.resnet50.html。
假 3D 实现基于 2.5D 方法,我们在所有三个轴的切片上计算 2D 感知损失并取平均值。完整的 3D 方法使用 3D 网络计算感知损失。MedicalNet 网络仅兼容 3D 输入并支持通道级损失。
- 参数:
spatial_dims – 空间维度数。
network_type – {
"alex"
,"vgg"
,"squeeze"
,"radimagenet_resnet50"
,"medicalnet_resnet10_23datasets" – 指定要使用的网络架构。默认为
"alex"
。"medicalnet_resnet50_23datasets" – 指定要使用的网络架构。默认为
"alex"
。"resnet50"} – 指定要使用的网络架构。默认为
"alex"
。is_fake_3d – 如果为 True,则对 3D 感知损失使用 2.5D 方法。
fake_3d_ratio – 2.5D 方法中每个轴使用的切片比例。
cache_dir – 用于保存预训练网络权重的缓存目录路径。
pretrained – 是否加载预训练权重。此参数仅在使用 LIPIS 或 Torchvision 的网络时有效。默认为
"True"
。pretrained_path – 如果 pretrained 为 True,用户可以使用此参数指定要加载的权重文件。此参数仅当
"network_type"
为 "resnet50" 时有效。默认为 None。pretrained_state_dict_key – 如果 pretrained_path 不为 None,此参数用于提取预期的状态字典。此参数仅当
"network_type"
为 "resnet50" 时有效。默认为 None。channel_wise – 如果为 True,则返回每个通道的损失。否则,损失将在通道上取平均。默认为
False
。
JukeboxLoss#
- class monai.losses.JukeboxLoss(spatial_dims, fft_signal_size=None, fft_norm='ortho', reduction=mean)[source]#
根据快速傅里叶变换 (FFT) 的幅度计算频谱分量。
- 基于
Dhariwal, 等人。‘Jukebox: A generative model for music.’ https://arxiv.org/abs/2005.00341
- 参数:
spatial_dims – 空间维度数。
fft_signal_size – 变换维度中的信号大小。更多信息请参阅 torch.fft.fftn()。
fft_norm – {
"forward"
,"backward"
,"ortho"
} 指定 fft 中的归一化模式。更多信息请参阅 torch.fft.fftn()。reduction –
{
"none"
,"mean"
,"sum"
} 指定要应用于输出的 reduction 方式。默认为"mean"
。"none"
: 不应用 reduction。"mean"
: 输出的总和将除以输出中的元素数量。"sum"
: 输出将求和。
SURELoss#
- class monai.losses.SURELoss(perturb_noise=None, eps=None)[source]#
计算给定算子的 Stein 无偏风险估计 (SURE) 损失。
这是一种可微分的损失函数,可用于训练/指导算子(例如神经网络),在伪真实值可用但参考真实值不可用的情况下。例如,在 MRI 重建中,伪真实值是零填充重建,参考真实值是完全采样重建。通常,由于缺乏完全采样数据,参考真实值不可用。
原始 SURE 损失在 [1] 中提出。用于指导基于扩散模型的 MRI 重建的 SURE 损失在 [2] 中提出。
参考
[1] Stein, C.M.: 多元正态分布均值的估计。统计年鉴 (Annals of Statistics)
[2] B. Ozturkler 等人。SMRD: SURE-based Robust MRI Reconstruction with Diffusion Models. (https://arxiv.org/pdf/2310.01799.pdf)
- __init__(perturb_noise=None, eps=None)[source]#
- 参数:
perturb_noise (torch.Tensor, optional) – 形状为
(B (B, 2, H, W)
C (B, 2, H, W)
H (B, 2, H, W)
input (对于实数输入,形状)
is (为)
input
is
eps (float, optional) – 扰动标量。默认为 None。
- forward(operator, x, y_pseudo_gt, y_ref=None, complex_input=False)[source]#
- 参数:
operator (function) – 输入张量 x 并返回输出张量 y 的算子函数。我们将使用它来
compute (具体计算散度。更)
specifically (具体的做法是,我们将对输入 x 进行)
a (a)
output (扰动,并计算参考)
output
x (torch.Tensor) – 算子的输入张量,形状为 (B, C, H, W)
2 (W) 用于计算 L2 损失。C=1 或) – 对于复数输入,形状为 (B, 2, H, W),即
input (B, 2, H, W)
is (的形状。)
y_pseudo_gt (same shape as) – 伪真实值张量,形状为
(B – 对于复数
C – 对于复数
H – 对于复数
2 – 对于复数
input
is
is
y_ref (torch.Tensor, optional) – 算子的参考输出张量,形状与
y_pseudo_gt
- 返回:
SURE 损失标量。
- 返回值类型:
sure_loss (torch.Tensor)
损失包装器#
MultiScaleLoss#
- class monai.losses.MultiScaleLoss(loss, scales=None, kernel='gaussian', reduction=mean)[source]#
这是一个包装类。它在将输入和目标传递到包装的损失函数之前,在不同的尺度上对它们进行平滑处理。
- 改编自
DeepReg (DeepRegNet/DeepReg)
MaskedLoss#
DeepSupervisionLoss#
- class monai.losses.DeepSupervisionLoss(loss, weight_mode='exp', weights=None)[source]#
主损失函数的包装类,用于接受深度监督网络返回的张量列表。最终损失计算为每个深度监督层加权损失的总和。
- __init__(loss, weight_mode='exp', weights=None)[source]#
- 参数:
loss – 主损失实例,例如 DiceLoss()。
weight_mode – {
"same"
,"exp"
,"two"
} 指定每个图像级别的权重计算。默认为"exp"
。-"same"
: 所有权重都等于 1。-"exp"
: 权重以 2 的幂次指数递减:1, 0.5, 0.25, 0.125 等。-"two"
: 对较低级别使用相等的较小权重:1, 0.5, 0.5, 0.5, 0.5 等weights – 应用于每个深度监督子损失的权重列表,如果提供,无论 weight_mode 如何都将使用此列表
- forward(input, target)[source]#
定义每次调用时执行的计算。
应由所有子类覆盖。
Note
虽然前向传播的步骤需要在函数内部定义,但之后应该调用
Module
实例,而不是直接调用这个函数,因为前者会处理已注册的钩子,而后者会静默忽略它们。- 返回值类型:
Tensor