Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Kornia Kornia Hausdorff Loss

From Leeroopedia
Revision as of 15:21, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Kornia_Kornia_Hausdorff_Loss.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Vision, Loss_Functions
Last Updated 2026-02-09 15:00 GMT

Overview

Hausdorff Loss provides a differentiable approximation of the Hausdorff distance using morphological erosion, suitable for boundary-aware segmentation tasks in 2D and 3D.

Description

The Hausdorff Distance (HD) 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 is:

hd(X,Y)=maxxXminyYxy2

The bidirectional HD is:

HD(X,Y)=max(hd(X,Y),hd(Y,X))

Since the exact Hausdorff distance is not differentiable, this module provides a differentiable approximation based on morphological erosion as described by Karimi et al. (2019). The erosion is performed iteratively using convolutional kernels, and the accumulated erosion values weighted by iteration count raised to the alpha power provide the loss.

Two variants are provided:

  • HausdorffERLoss: For 2D images with shape (B, C, H, W), using 3x3 cross-shaped kernels.
  • HausdorffERLoss3D: For 3D volumes with shape (B, C, D, H, W), using 3x3x3 cross-shaped kernels.

Usage

Import this loss for segmentation tasks where boundary accuracy is important, such as medical image segmentation. It complements region-based losses like Dice loss by explicitly penalizing boundary deviations, encouraging the model to produce sharper and more accurate boundaries.

Code Reference

Source Location

Signature

class HausdorffERLoss(nn.Module):
    def __init__(self, alpha: float = 2.0, k: int = 10, reduction: str = "mean") -> None: ...
    def forward(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor: ...

class HausdorffERLoss3D(nn.Module):
    def __init__(self, alpha: float = 2.0, k: int = 10, reduction: str = "mean") -> None: ...
    def forward(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor: ...

Import

from kornia.losses import HausdorffERLoss
from kornia.losses import HausdorffERLoss3D

I/O Contract

Inputs (HausdorffERLoss)

Name Type Required Description
alpha float No Controls the erosion rate in each iteration (default: 2.0)
k int No Number of erosion iterations (default: 10)
reduction str No Reduction mode: 'none', 'mean' (default), or 'sum'
pred torch.Tensor Yes Predicted tensor with shape (B, C, H, W); each channel is binary (1=fg, 0=bg)
target torch.Tensor Yes Target tensor with shape (B, 1, H, W) containing long class indices

Inputs (HausdorffERLoss3D)

Name Type Required Description
alpha float No Controls the erosion rate in each iteration (default: 2.0)
k int No Number of erosion iterations (default: 10)
reduction str No Reduction mode: 'none', 'mean' (default), or 'sum'
pred torch.Tensor Yes Predicted tensor with shape (B, C, D, H, W); each channel is binary
target torch.Tensor Yes Target tensor with shape (B, 1, D, H, W)

Outputs

Name Type Description
loss torch.Tensor Estimated Hausdorff distance loss; scalar for 'mean'/'sum', full tensor for 'none'

Usage Examples

import torch
from kornia.losses import HausdorffERLoss, HausdorffERLoss3D

# 2D Hausdorff loss
hdloss = HausdorffERLoss()
pred_2d = torch.randn(5, 3, 20, 20)
target_2d = (torch.rand(5, 1, 20, 20) * 2).long()
loss_2d = hdloss(pred_2d, target_2d)

# 3D Hausdorff loss
hdloss_3d = HausdorffERLoss3D()
pred_3d = torch.randn(5, 3, 20, 20, 20)
target_3d = (torch.rand(5, 1, 20, 20, 20) * 2).long()
loss_3d = hdloss_3d(pred_3d, target_3d)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment