class_docstr: Base class for binary Hausdorff loss based on morphological erosion.
  
  This is an Hausdorff Distance (HD) Loss that based on morphological erosion,which provided
  a differentiable approximation of Hausdorff distance as stated in :cite:`karimi2019reducing`.
  The code is refactored on top of `here <https://github.com/PatRyg99/HausdorffLoss/
      blob/master/hausdorff_loss.py>`__.
  
  Args:
      alpha: controls the erosion rate in each iteration.
      k: the number of iterations of erosion.
      reduction: Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'.
          'none': no reduction will be applied, 'mean': the weighted mean of the output is taken,
          'sum': the output will be summed.
  
  Returns:
      Estimated Hausdorff Loss.
class_docstr_tok: ["Base","class","for","binary","Hausdorff","loss","based","on","morphological","erosion",".","This","is","an","Hausdorff","Distance","(","HD",")","Loss","that","based","on","morphological","erosion",",","which","provided","a","differentiable","approximation","of","Hausdorff","distance","as","stated","in",":","cite",":","`","karimi2019reducing","`",".","The","code","is","refactored","on","top","of","`","here","<","https",":","\/\/github.com\/PatRyg99\/HausdorffLoss\/","blob\/master\/hausdorff_loss.py",">","`","__",".","Args",":","alpha",":","controls","the","erosion","rate","in","each","iteration",".","k",":","the","number","of","iterations","of","erosion",".","reduction",":","Specifies","the","reduction","to","apply","to","the","output",":","'none","'","|","'mean","'","|","'sum","'",".","'none","'",":","no","reduction","will","be","applied",",","'mean","'",":","the","weighted","mean","of","the","output","is","taken",",","'sum","'",":","the","output","will","be","summed",".","Returns",":","Estimated","Hausdorff","Loss","."]
code: def perform_erosion(self, pred: Tensor, target: Tensor) -> Tensor:
      bound = (pred - target) ** 2
  
      kernel = as_tensor(
          self.kernel, device=pred.device, dtype=pred.dtype
      )
      eroded = zeros_like(bound, device=pred.device, dtype=pred.dtype)
      mask = torch.ones_like(
          bound, device=pred.device, dtype=torch.bool
      )
  
      # Same padding, assuming kernel is odd and square (cube) shaped.
      padding = (kernel.size(-1) - 1) // 2
      for k in range(self.k):
          # compute convolution with kernel
          dilation = self.conv(
              bound, weight=kernel, padding=padding, groups=1
          )
          # apply soft thresholding at 0.5 and normalize
          erosion = dilation - 0.5
          erosion[erosion < 0] = 0
  
          # image-wise differences for 2D images
          erosion_max = self.max_pool(erosion)
          erosion_min = -self.max_pool(-erosion)
          # No normalization needed if `max - min = 0`
          _to_norm = (erosion_max - erosion_min) != 0
          to_norm = _to_norm.squeeze()
          if to_norm.any():
              # NOTE: avoid in-place ops like below, which will not pass gradcheck:
              #       erosion[to_norm] = (erosion[to_norm] - erosion_min[to_norm]) / (
              #           erosion_max[to_norm] - erosion_min[to_norm])
              _erosion_to_fill = (erosion - erosion_min) / (
                  erosion_max - erosion_min
              )
              erosion = where(
                  mask * _to_norm, _erosion_to_fill, erosion
              )
  
          # save erosion and add to loss
          eroded = eroded + erosion * (k + 1) ** self.alpha
          bound = erosion
  
      return eroded
  
file_path: kornia/kornia/losses/hausdorff.py
imports: from __future__ import annotations
  from typing import Callable
  import torch
  from torch import nn
  from kornia.core import Module, Tensor, as_tensor, stack, tensor, where, zeros_like
label_desc: Use image node_id 3 for calling the _HausdorffERLossBase obj's underlying member method code with example usage: obj.perform_erosion(pred, target) and returns: eroded