Morphology#
- class kornia.losses.HausdorffERLoss(alpha=2.0, k=10, reduction='mean')[source]#
Hausdorff loss based on morphological erosion, one binary map per class channel.
Hausdorff Distance loss measures the maximum distance of a predicted segmentation boundary to the nearest ground-truth edge pixel. For two segmentation point sets X and Y , the one-sided HD from X to Y is defined as:
\[hd(X,Y) = \max_{x \in X} \min_{y \in Y}||x - y||_2\]Furthermore, the bidirectional HD is:
\[HD(X,Y) = max(hd(X, Y), hd(Y, X))\]This is an Hausdorff Distance (HD) Loss that based on morphological erosion, which provided a differentiable approximation of Hausdorff distance as stated in [KS19]. The code is refactored on top of here.
- Convention:
predholds per-class probabilities(B, C, H, W)and nothing is applied to it, unlike the logits offocal_loss(): pass a softmax.targetholds int64 labels(B, 1, H, W)in[0, C). Channelcis compared withtarget == c, class 0 included.The
[0, C)check reads the data, so it is skipped wherever the call captures a graph (torch.compileortorch.export). A captured call whose target holds an out-of-range label scores those voxels in no class and returns a finite loss instead of raising; an eager call still raises.reduction='none'returns(C, B, 1, H, W), class axis first; the default'mean'is the plain mean of that tensor.Each of the
kerosions is min-max normalised per image and class and weighted by \((i + 1)^\alpha\) for \(i = 0, \dots, k - 1\): the value is a unitless surrogate, not a distance in pixels.
- Parameters:
alpha (
float, optional) – the exponent of the weight \((i + 1)^\alpha\) of erosion \(i = 0, \dots, k - 1\). Default:2.0k (
int, optional) – the number of iterations of erosion. Default:10reduction (
str, optional) – Specifies the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. ‘none’: no reduction will be applied, ‘mean’: the mean of the output is taken, ‘sum’: the output will be summed. Default:"mean"
Examples
>>> hdloss = HausdorffERLoss() >>> input = torch.randn(5, 3, 20, 20).softmax(1) >>> target = (torch.rand(5, 1, 20, 20) * 2).long() >>> res = hdloss(input, target)
- class kornia.losses.HausdorffERLoss3D(alpha=2.0, k=10, reduction='mean')[source]#
3D Hausdorff loss based on morphological erosion, one binary map per class channel.
Hausdorff Distance loss measures the maximum distance of a predicted segmentation boundary to the nearest ground-truth edge pixel. For two segmentation point sets X and Y , the one-sided HD from X to Y is defined as:
\[hd(X,Y) = \max_{x \in X} \min_{y \in Y}||x - y||_2\]Furthermore, the bidirectional HD is:
\[HD(X,Y) = max(hd(X, Y), hd(Y, X))\]This is a 3D Hausdorff Distance (HD) Loss that based on morphological erosion, which provided a differentiable approximation of Hausdorff distance as stated in [KS19]. The code is refactored on top of here.
- Convention:
As in
HausdorffERLoss(see its Convention block), forpredof shape(B, C, D, H, W)andtargetof shape(B, 1, D, H, W);reduction='none'returns(C, B, 1, D, H, W). The erosion kernel is the voxel and its six face neighbours, so the erosion also runs alongD.
- Parameters:
alpha (
float, optional) – the exponent of the weight \((i + 1)^\alpha\) of erosion \(i = 0, \dots, k - 1\). Default:2.0k (
int, optional) – the number of iterations of erosion. Default:10reduction (
str, optional) – Specifies the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. ‘none’: no reduction will be applied, ‘mean’: the mean of the output is taken, ‘sum’: the output will be summed. Default:"mean"
Examples
>>> hdloss = HausdorffERLoss3D() >>> input = torch.randn(5, 3, 20, 20, 20).softmax(1) >>> target = (torch.rand(5, 1, 20, 20, 20) * 2).long() >>> res = hdloss(input, target)