Helper Module for Deep Learning.
Module that provides segmentation losses.
-
class
pynet.losses.segmentation.FocalLoss(n_classes, gamma=2, alpha=None, reduction='mean', with_logit=True)[source]¶ Define a Focal Loss.
Loss(pt) = −αt mt (1−pt)γ log(pt)
where pt is the model’s estimated probability for each class.
When an example is misclassified and pt is small, the modulating factor is near 1 and the loss is unaffected. As pt goes to 1, the factor goes to 0 and the loss for well-classified examples is down-weighted. The focusing parameter γ smoothly adjusts the rate at which easy examples are down-weighted. When γ= 0, the loss is equivalent to cross entropy, and as γ isincreased the effect of the modulating factor is likewise increased. For instance, with γ= 2, an example classified with pt= 0.9 would have 100×lower loss compared with cross entropy and with pt≈0.968 it would have 1000×lower loss. Then we use an α-balanced variant of the focal loss for addressing class imbalance with a weighting factor α ∈ [0,1]. In practice α may be set by inverse class frequency.
Reference: https://arxiv.org/abs/1708.02002
-
__init__(n_classes, gamma=2, alpha=None, reduction='mean', with_logit=True)[source]¶ Class instanciation.
- Parameters
n_classes: int
the number of classes.
gamma: float, default 2
the focusing parameter >=0.
alpha: float or list of float, default None
if set use alpha-balanced variant of the focal loss.
reduction: str, default ‘mean’
specifies the reduction to apply to the output: ‘none’ - no reduction will be applied, ‘mean’ - the sum of the output will be divided by the number of elements in the output, ‘sum’ - the output will be summed.
with_logit: bool, default True
apply the softmax logit function to the result.
-
-
class
pynet.losses.segmentation.MaskLoss(n_classes, beta=0.2, alpha=None, reduction='mean', with_logit=True)[source]¶ Define a Masked Loss.
Loss(pt) = −αt mt log(pt)
where pt is the model’s estimated probability for each class.
-
__init__(n_classes, beta=0.2, alpha=None, reduction='mean', with_logit=True)[source]¶ Class instanciation.
- Parameters
n_classes: int
the number of classes.
beta: float, default 0.2
the minimum value in the mask.
alpha: float or list of float, default None
if set use alpha-balanced variant of the focal loss.
reduction: str, default ‘mean’
specifies the reduction to apply to the output: ‘none’ - no reduction will be applied, ‘mean’ - the sum of the output will be divided by the number of elements in the output, ‘sum’ - the output will be summed.
with_logit: bool, default True
apply the log softmax logit function to the result.
-
-
class
pynet.losses.segmentation.NvNetCombinedLoss(num_classes, k1=0.1, k2=0.1)[source]¶ Combined Loss.
Cross Entropy loss + k1 * L2 loss + k2 * KL loss Since the output of the segmentation decoder has N channels (prediction for each tumor subregion), we simply add the N dice loss functions. A hyper-parameter weight of k1=0.1, k2=0.1 was found empirically in the paper.
-
class
pynet.losses.segmentation.SoftDiceLoss(with_logit=True, reduction='mean')[source]¶ Define a multi class Dice Loss.
Dice = (2 intersec Y) / (X + Y)
Note that PyTorch optimizers minimize a loss. In this case, we would like to maximize the dice loss so we return 1 - Dice.
-
__init__(with_logit=True, reduction='mean')[source]¶ Class instanciation.
- Parameters
with_logit: bool, default True
apply the softmax logit function to the result.
reduction: str, default ‘mean’
specifies the reduction to apply to the output: ‘none’ - no reduction will be applied, ‘mean’ - the sum of the output will be divided by the number of elements in the output, ‘sum’ - the output will be summed.
-
Follow us
Inspired by AZMIND template.