def test_nans(self, input_data): [seg_1, seg_2] = input_data seg_1 = torch.tensor(seg_1) seg_2 = torch.tensor(seg_2) sur_metric = SurfaceDistanceMetric(include_background=False, get_not_nans=True) # test list of channel-first Tensor batch_seg_1 = [seg_1.unsqueeze(0)] batch_seg_2 = [seg_2.unsqueeze(0)] sur_metric(batch_seg_1, batch_seg_2) result, not_nans = sur_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, symmetric: bool = False, distance_metric: str = "euclidean", 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``. symmetric: whether to calculate the symmetric average surface distance between `seg_pred` and `seg_gt`. Defaults to ``False``. distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``] the metric used to compute surface distance. Defaults to ``"euclidean"``. output_transform: transform the ignite.engine.state.output into [y_pred, y] pair. device: device specification in case of distributed computation usage. """ metric_fn = SurfaceDistanceMetric( include_background=include_background, symmetric=symmetric, distance_metric=distance_metric, reduction=MetricReduction.NONE, ) super().__init__(metric_fn=metric_fn, output_transform=output_transform, device=device)
def test_nans(self, input_data): [seg_1, seg_2] = input_data seg_1 = torch.tensor(seg_1) seg_2 = torch.tensor(seg_2) sur_metric = SurfaceDistanceMetric(include_background=False) batch_seg_1 = seg_1.unsqueeze(0).unsqueeze(0) batch_seg_2 = seg_2.unsqueeze(0).unsqueeze(0) result, not_nans = sur_metric(batch_seg_1, batch_seg_2) np.testing.assert_allclose(0, result, rtol=1e-7) np.testing.assert_allclose(0, not_nans, rtol=1e-7)
def test_value(self, input_data, expected_value): if len(input_data) == 3: [seg_1, seg_2, metric] = input_data else: [seg_1, seg_2] = input_data metric = "euclidean" ct = 0 seg_1 = torch.tensor(seg_1) seg_2 = torch.tensor(seg_2) for symmetric in [True, False]: sur_metric = SurfaceDistanceMetric(include_background=False, symmetric=symmetric, distance_metric=metric) # 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]) sur_metric(batch_seg_1, batch_seg_2) result = sur_metric.aggregate() expected_value_curr = expected_value[ct] np.testing.assert_allclose(expected_value_curr, result, rtol=1e-7) ct += 1
def __init__( self, include_background: bool = False, symmetric: bool = False, distance_metric: str = "euclidean", 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``. symmetric: whether to calculate the symmetric average surface distance between `seg_pred` and `seg_gt`. Defaults to ``False``. distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``] the metric used to compute surface distance. Defaults to ``"euclidean"``. 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: surface dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. """ metric_fn = SurfaceDistanceMetric( include_background=include_background, symmetric=symmetric, distance_metric=distance_metric, reduction=reduction, ) super().__init__(metric_fn=metric_fn, output_transform=output_transform, save_details=save_details)
def __init__( self, include_background: bool = False, symmetric: bool = False, distance_metric: str = "euclidean", 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``. symmetric: whether to calculate the symmetric average surface distance between `seg_pred` and `seg_gt`. Defaults to ``False``. distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``] the metric used to compute surface distance. Defaults to ``"euclidean"``. 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: surface dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. """ metric_fn = SurfaceDistanceMetric( include_background=include_background, symmetric=symmetric, distance_metric=distance_metric, reduction=MetricReduction.MEAN, ) super().__init__( metric_fn=metric_fn, output_transform=output_transform, save_details=save_details, )
def __init__( self, include_background: bool = False, symmetric: bool = False, distance_metric: str = "euclidean", output_transform: Callable = lambda x: x, device: Union[str, torch.device] = "cpu", save_details: bool = True, ) -> None: """ Args: include_background: whether to include distance computation on the first channel of the predicted output. Defaults to ``False``. symmetric: whether to calculate the symmetric average surface distance between `seg_pred` and `seg_gt`. Defaults to ``False``. distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``] the metric used to compute surface distance. Defaults to ``"euclidean"``. output_transform: transform the ignite.engine.state.output into [y_pred, y] pair. device: device specification in case of distributed computation usage. save_details: whether to save metric computation details per image, for example: surface dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. """ metric_fn = SurfaceDistanceMetric( include_background=include_background, symmetric=symmetric, distance_metric=distance_metric, reduction=MetricReduction.NONE, ) super().__init__( metric_fn=metric_fn, output_transform=output_transform, device=device, save_details=save_details, )