def test_value(self, input_data, expected_value): percentile = None if len(input_data) == 3: [seg_1, seg_2, percentile] = input_data else: [seg_1, seg_2] = input_data ct = 0 seg_1 = torch.tensor(seg_1) seg_2 = torch.tensor(seg_2) for metric in ["euclidean", "chessboard", "taxicab"]: for directed in [True, False]: hd_metric = HausdorffDistanceMetric(include_background=False, distance_metric=metric, percentile=percentile, directed=directed) # shape of seg_1, seg_2 are: HWD, converts to BNHWD batch, n_class = 2, 3 batch_seg_1 = seg_1.unsqueeze(0).unsqueeze(0).repeat( [batch, n_class, 1, 1, 1]) batch_seg_2 = seg_2.unsqueeze(0).unsqueeze(0).repeat( [batch, n_class, 1, 1, 1]) hd_metric(batch_seg_1, batch_seg_2) result = hd_metric.aggregate() expected_value_curr = expected_value[ct] np.testing.assert_allclose(expected_value_curr, result, rtol=1e-7) ct += 1
def test_nans(self, input_data): [seg_1, seg_2] = input_data seg_1 = torch.tensor(seg_1) seg_2 = torch.tensor(seg_2) hd_metric = HausdorffDistanceMetric(include_background=False, get_not_nans=True) batch_seg_1 = seg_1.unsqueeze(0).unsqueeze(0) batch_seg_2 = seg_2.unsqueeze(0).unsqueeze(0) hd_metric(batch_seg_1, batch_seg_2) result, not_nans = hd_metric.aggregate() np.testing.assert_allclose(0, result, rtol=1e-7) np.testing.assert_allclose(0, not_nans, rtol=1e-7)
def __init__( self, include_background: bool = False, distance_metric: str = "euclidean", percentile: Optional[float] = None, directed: bool = False, output_transform: Callable = lambda x: x, device: Optional[torch.device] = None, ) -> None: """ Args: include_background: whether to include distance computation on the first channel of the predicted output. Defaults to ``False``. distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``] the metric used to compute surface distance. Defaults to ``"euclidean"``. percentile: an optional float number between 0 and 100. If specified, the corresponding percentile of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to ``None``. directed: whether to calculate directed Hausdorff distance. Defaults to ``False``. output_transform: transform the ignite.engine.state.output into [y_pred, y] pair. device: device specification in case of distributed computation usage. """ super().__init__(output_transform, device=device) metric_fn = HausdorffDistanceMetric( include_background=include_background, distance_metric=distance_metric, percentile=percentile, directed=directed, reduction=MetricReduction.NONE, ) super().__init__(metric_fn=metric_fn, output_transform=output_transform, device=device)
def __init__( self, include_background: bool = False, distance_metric: str = "euclidean", percentile: Optional[float] = None, directed: bool = False, reduction: Union[MetricReduction, str] = MetricReduction.MEAN, output_transform: Callable = lambda x: x, save_details: bool = True, ) -> None: """ Args: include_background: whether to include distance computation on the first channel of the predicted output. Defaults to ``False``. distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``] the metric used to compute surface distance. Defaults to ``"euclidean"``. percentile: an optional float number between 0 and 100. If specified, the corresponding percentile of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to ``None``. directed: whether to calculate directed Hausdorff distance. Defaults to ``False``. reduction: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, ``"mean_channel"``, ``"sum_channel"``} Define the mode to reduce computation result. Defaults to ``"mean"``. output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. `engine.state` and `output_transform` inherit from the ignite concept: https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: hausdorff distance of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. """ metric_fn = HausdorffDistanceMetric( include_background=include_background, distance_metric=distance_metric, percentile=percentile, directed=directed, reduction=reduction, ) super().__init__(metric_fn=metric_fn, output_transform=output_transform, save_details=save_details)
def __init__( self, include_background: bool = False, distance_metric: str = "euclidean", percentile: Optional[float] = None, directed: bool = False, output_transform: Callable = lambda x: x, save_details: bool = True, ) -> None: """ Args: include_background: whether to include distance computation on the first channel of the predicted output. Defaults to ``False``. distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``] the metric used to compute surface distance. Defaults to ``"euclidean"``. percentile: an optional float number between 0 and 100. If specified, the corresponding percentile of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to ``None``. directed: whether to calculate directed Hausdorff distance. Defaults to ``False``. output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. for example: if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, output_transform can be `lambda x: (x["pred"], x["label"])`. save_details: whether to save metric computation details per image, for example: hausdorff distance of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. """ metric_fn = HausdorffDistanceMetric( include_background=include_background, distance_metric=distance_metric, percentile=percentile, directed=directed, reduction=MetricReduction.MEAN, ) super().__init__( metric_fn=metric_fn, output_transform=output_transform, save_details=save_details, )