class_docstr: Compute channel-wise binary Hausdorff loss based on distance transform. It can support both multi-classes and
  multi-labels tasks. The data `input` (BNHW[D] where N is number of classes) is compared with ground truth `target`
  (BNHW[D]).
  
  Note that axis N of `input` is expected to be logits or probabilities for each class, if passing logits as input,
  must set `sigmoid=True` or `softmax=True`, or specifying `other_act`. And the same axis of `target`
  can be 1 or N (one-hot format).
  
  The original paper: 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
docstr: Generate distance transform.
  
  Args:
      img (np.ndarray): input mask as NCHWD or NCHW.
  
  Returns:
      np.ndarray: Distance field.
docstr_tok: ["Generate","distance","transform",".","Args",":","img","(","np.ndarray",")",":","input","mask","as","NCHWD","or","NCHW",".","Returns",":","np.ndarray",":","Distance","field","."]
code: def distance_field(self, img: torch.Tensor) -> torch.Tensor:
      """Generate distance transform.
  
      Args:
          img (np.ndarray): input mask as NCHWD or NCHW.
  
      Returns:
          np.ndarray: Distance field.
      """
      field = torch.zeros_like(img)
  
      for batch_idx in range(len(img)):
          fg_mask = img[batch_idx] > 0.5
  
          # For cases where the mask is entirely background or entirely foreground
          # the distance transform is not well defined for all 1s,
          # which always would happen on either foreground or background, so skip
          if fg_mask.any() and not fg_mask.all():
              fg_dist: torch.Tensor = distance_transform_edt(fg_mask)  # type: ignore
              bg_mask = ~fg_mask
              bg_dist: torch.Tensor = distance_transform_edt(bg_mask)  # type: ignore
  
              field[batch_idx] = fg_dist + bg_dist
  
      return field
  
file_path: MONAI/monai/losses/hausdorff_loss.py
imports: from __future__ import annotations
  import warnings
  from typing import Callable
  import torch
  from torch.nn.modules.loss import _Loss
  from monai.networks import one_hot
  from monai.transforms.utils import distance_transform_edt
  from monai.utils import LossReduction
label_desc: Use image node_id 2 for calling the HausdorffDTLoss obj's underlying member method code with example usage: obj.distance_field(img) and returns: field