Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix an error message of confusion_matrix.IoU #2613

Merged
Merged
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
8 changes: 6 additions & 2 deletions ignite/metrics/confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ def IoU(cm: ConfusionMatrix, ignore_index: Optional[int] = None) -> MetricsLambd

if ignore_index is not None:
if not (isinstance(ignore_index, numbers.Integral) and 0 <= ignore_index < cm.num_classes):
raise ValueError(f"ignore_index should be non-negative integer, but given {ignore_index}")
raise ValueError(
f"ignore_index should be integer and in the range of [0, {cm.num_classes}), but given {ignore_index}"
)

# Increase floating point precision and pass to CPU
cm = cm.to(torch.double)
Expand Down Expand Up @@ -393,7 +395,9 @@ def DiceCoefficient(cm: ConfusionMatrix, ignore_index: Optional[int] = None) ->

if ignore_index is not None:
if not (isinstance(ignore_index, numbers.Integral) and 0 <= ignore_index < cm.num_classes):
raise ValueError(f"ignore_index should be non-negative integer, but given {ignore_index}")
raise ValueError(
f"ignore_index should be integer and in the range of [0, {cm.num_classes}), but given {ignore_index}"
)

# Increase floating point precision and pass to CPU
cm = cm.to(torch.double)
Expand Down
16 changes: 8 additions & 8 deletions tests/ignite/metrics/test_confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,16 @@ def test_iou_wrong_input():
IoU(None)

cm = ConfusionMatrix(num_classes=10)
with pytest.raises(ValueError, match="ignore_index should be non-negative integer"):
with pytest.raises(ValueError, match=r"ignore_index should be integer and in the range of \[0, 10\), but given -1"):
IoU(cm, ignore_index=-1)

with pytest.raises(ValueError, match="ignore_index should be non-negative integer"):
with pytest.raises(ValueError, match=r"ignore_index should be integer and in the range of \[0, 10\), but given a"):
IoU(cm, ignore_index="a")

with pytest.raises(ValueError, match="ignore_index should be non-negative integer"):
with pytest.raises(ValueError, match=r"ignore_index should be integer and in the range of \[0, 10\), but given 10"):
IoU(cm, ignore_index=10)

with pytest.raises(ValueError, match="ignore_index should be non-negative integer"):
with pytest.raises(ValueError, match=r"ignore_index should be integer and in the range of \[0, 10\), but given 11"):
IoU(cm, ignore_index=11)


Expand Down Expand Up @@ -403,16 +403,16 @@ def test_dice_coefficient_wrong_input():
DiceCoefficient(None)

cm = ConfusionMatrix(num_classes=10)
with pytest.raises(ValueError, match="ignore_index should be non-negative integer"):
with pytest.raises(ValueError, match=r"ignore_index should be integer and in the range of \[0, 10\), but given -1"):
DiceCoefficient(cm, ignore_index=-1)

with pytest.raises(ValueError, match="ignore_index should be non-negative integer"):
with pytest.raises(ValueError, match=r"ignore_index should be integer and in the range of \[0, 10\), but given a"):
DiceCoefficient(cm, ignore_index="a")

with pytest.raises(ValueError, match="ignore_index should be non-negative integer"):
with pytest.raises(ValueError, match=r"ignore_index should be integer and in the range of \[0, 10\), but given 10"):
DiceCoefficient(cm, ignore_index=10)

with pytest.raises(ValueError, match="ignore_index should be non-negative integer"):
with pytest.raises(ValueError, match=r"ignore_index should be integer and in the range of \[0, 10\), but given 11"):
DiceCoefficient(cm, ignore_index=11)


Expand Down