Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion segmentation_models_pytorch/metrics/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def get_stats(

if torch.is_floating_point(output) and threshold is None:
raise ValueError(
f"Output should be one of the integer types if ``threshold`` is not None, got {output.dtype}."
f"Output should be one of the integer types if ``threshold`` is None, got {output.dtype}."
)

if torch.is_floating_point(output) and mode == "multiclass":
Expand Down
17 changes: 17 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
import torch

from segmentation_models_pytorch.metrics import get_stats


def test_get_stats_explains_float_output_requires_threshold() -> None:
output = torch.tensor([[[0.1, 0.8], [0.3, 0.7]]], dtype=torch.float32)
target = torch.tensor([[[0, 1], [0, 1]]], dtype=torch.long)

with pytest.raises(ValueError) as error:
get_stats(output, target, mode="binary", threshold=None)

assert str(error.value) == (
"Output should be one of the integer types if ``threshold`` is None, "
"got torch.float32."
)