mindspore.nn.HausdorffDistance

class mindspore.nn.HausdorffDistance(distance_metric="euclidean", percentile=None, directed=False, crop=True)[source]

Calculates the Hausdorff distance. Hausdorff distance is the maximum and minimum distance between two point sets. Given two feature sets A and B, the Hausdorff distance between two point sets A and B is defined as follows:

\[H(A, B) = \text{max}[h(A, B), h(B, A)] h(A, B) = \underset{a \in A}{\text{max}}\{\underset{b \in B}{\text{min}} \rVert a - b \rVert \} h(A, B) = \underset{b \in B}{\text{max}}\{\underset{a \in A}{\text{min}} \rVert b - a \rVert \}\]
Parameters
  • distance_metric (string) – The parameter of calculating Hausdorff distance supports three measurement methods, “euclidean”, “chessboard” or “taxicab”. Default: “euclidean”.

  • percentile (float) – Floating point numbers between 0 and 100. Specify the percentile parameter to get the percentile of the Hausdorff distance. Default: None.

  • directed (bool) – It can be divided into directional and non directional Hausdorff distance, and the default is non directional Hausdorff distance, specify the percentile parameter to get the percentile of the Hausdorff distance. Default: False.

  • crop (bool) – Crop input images and only keep the foregrounds. In order to maintain two inputs’ shapes, here the bounding box is achieved by (y_pred | y) which represents the union set of two images. Default: True.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]]))
>>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]]))
>>> metric = nn.HausdorffDistance()
>>> metric.clear()
>>> metric.update(x, y, 0)
>>> mean_average_distance = metric.eval()
>>> print(mean_average_distance)
1.4142135623730951
clear()[source]

Clears the internal evaluation result.

eval()[source]

Calculate the no-directed or directed Hausdorff distance.

Returns

A float with hausdorff_distance.

Raises

RuntimeError – If the update method is not called first, an error will be reported.

update(*inputs)[source]

Updates the internal evaluation result ‘y_pred’, ‘y’ and ‘label_idx’.

Parameters

inputs – Input ‘y_pred’, ‘y’ and ‘label_idx’. ‘y_pred’ and ‘y’ are Tensor or numpy.ndarray. ‘y_pred’ is the predicted binary image. ‘y’ is the actual binary image. ‘label_idx’, the data type of label_idx is int.

Raises

ValueError – If the number of the inputs is not 3.