Skip to content

Commit e90bb84

Browse files
5919 unify output tensor device for multiple metrics (#5924)
Signed-off-by: Yiheng Wang <vennw@nvidia.com> Fixes #5919 . ### Description This PR is used to unify input output tensor devices for the following metrics: 1. HausdorffDistanceMetric 2. SurfaceDiceMetric 3. SurfaceDistanceMetric ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Yiheng Wang <vennw@nvidia.com>
1 parent b592164 commit e90bb84

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

monai/metrics/hausdorff_distance.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ignore_background,
2424
is_binary_tensor,
2525
)
26-
from monai.utils import MetricReduction
26+
from monai.utils import MetricReduction, convert_data_type
2727

2828
from .metric import CumulativeIterationMetric
2929

@@ -153,10 +153,8 @@ def compute_hausdorff_distance(
153153

154154
if not include_background:
155155
y_pred, y = ignore_background(y_pred=y_pred, y=y)
156-
if isinstance(y, torch.Tensor):
157-
y = y.float()
158-
if isinstance(y_pred, torch.Tensor):
159-
y_pred = y_pred.float()
156+
y_pred = convert_data_type(y_pred, output_type=torch.Tensor, dtype=torch.float)[0]
157+
y = convert_data_type(y, output_type=torch.Tensor, dtype=torch.float)[0]
160158

161159
if y.shape != y_pred.shape:
162160
raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.")
@@ -176,7 +174,7 @@ def compute_hausdorff_distance(
176174
else:
177175
distance_2 = compute_percent_hausdorff_distance(edges_gt, edges_pred, distance_metric, percentile)
178176
hd[b, c] = max(distance_1, distance_2)
179-
return torch.from_numpy(hd)
177+
return convert_data_type(hd, output_type=torch.Tensor, device=y_pred.device, dtype=torch.float)[0]
180178

181179

182180
def compute_percent_hausdorff_distance(

monai/metrics/surface_dice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,4 @@ def compute_surface_dice(
238238
else:
239239
nsd[b, c] = boundary_correct / boundary_complete
240240

241-
return convert_data_type(nsd, torch.Tensor)[0]
241+
return convert_data_type(nsd, output_type=torch.Tensor, device=y_pred.device, dtype=torch.float)[0]

monai/metrics/surface_distance.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,8 @@ def compute_average_surface_distance(
145145
if not include_background:
146146
y_pred, y = ignore_background(y_pred=y_pred, y=y)
147147

148-
if isinstance(y, torch.Tensor):
149-
y = y.float()
150-
if isinstance(y_pred, torch.Tensor):
151-
y_pred = y_pred.float()
148+
y_pred = convert_data_type(y_pred, output_type=torch.Tensor, dtype=torch.float)[0]
149+
y = convert_data_type(y, output_type=torch.Tensor, dtype=torch.float)[0]
152150

153151
if y.shape != y_pred.shape:
154152
raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.")
@@ -168,4 +166,4 @@ def compute_average_surface_distance(
168166
surface_distance = np.concatenate([surface_distance, surface_distance_2])
169167
asd[b, c] = np.nan if surface_distance.shape == (0,) else surface_distance.mean()
170168

171-
return convert_data_type(asd, torch.Tensor)[0]
169+
return convert_data_type(asd, output_type=torch.Tensor, device=y_pred.device, dtype=torch.float)[0]

tests/test_surface_dice.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,17 @@ def test_not_predicted_not_present(self):
277277

278278
# test aggregation
279279
res_bgr = sur_metric_bgr.aggregate(reduction="mean")
280-
np.testing.assert_equal(res_bgr, torch.tensor([1 / 3], dtype=torch.float64))
280+
np.testing.assert_equal(res_bgr, torch.tensor([1 / 3], dtype=torch.float))
281281
res = sur_metric.aggregate()
282-
np.testing.assert_equal(res, torch.tensor([0], dtype=torch.float64))
282+
np.testing.assert_equal(res, torch.tensor([0], dtype=torch.float))
283283

284284
predictions_empty = torch.zeros((2, 3, 1, 1))
285285
sur_metric_nans = SurfaceDiceMetric(class_thresholds=[1, 1, 1], include_background=True, get_not_nans=True)
286286
res_classes = sur_metric_nans(predictions_empty, predictions_empty)
287287
res, not_nans = sur_metric_nans.aggregate()
288288
np.testing.assert_array_equal(res_classes, [[np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]])
289-
np.testing.assert_equal(res, torch.tensor([0], dtype=torch.float64))
290-
np.testing.assert_equal(not_nans, torch.tensor([0], dtype=torch.float64))
289+
np.testing.assert_equal(res, torch.tensor([0], dtype=torch.float))
290+
np.testing.assert_equal(not_nans, torch.tensor([0], dtype=torch.float))
291291

292292

293293
if __name__ == "__main__":

0 commit comments

Comments
 (0)