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 the bug in F1AdaptiveThreshold which occurs only when there are no anomalous images in a validation set #2437

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions src/anomalib/metrics/threshold/f1_adaptive_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging

import torch
from torchmetrics.utilities.data import dim_zero_cat

from anomalib.metrics.precision_recall_curve import BinaryPrecisionRecallCurve

Expand Down Expand Up @@ -69,6 +70,23 @@ def compute(self) -> torch.Tensor:
)
logging.warning(msg)

self.value = torch.max(dim_zero_cat(self.preds))

return self.value

if not any(0 in batch for batch in self.target):
msg = (
"The validation set does not contain any normal images. As a result, the adaptive threshold will "
"take the value of the lowest anomaly score observed in the anomalous validation images, which may "
"lead to poor predictions. For a more reliable adaptive threshold computation, please add some normal "
"images to the validation set."
)
logging.warning(msg)

self.value = torch.min(dim_zero_cat(self.preds))

return self.value

precision, recall, thresholds = super().compute()
f1_score = (2 * precision * recall) / (precision + recall + 1e-10)
if thresholds.dim() == 0:
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/metrics/test_adaptive_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
[
(torch.Tensor([0, 0, 0, 1, 1]), torch.Tensor([2.3, 1.6, 2.6, 7.9, 3.3]), 3.3), # standard case
(torch.Tensor([1, 0, 0, 0]), torch.Tensor([4, 3, 2, 1]), 4), # 100% recall for all thresholds
(torch.Tensor([1, 1, 1, 1]), torch.Tensor([4, 3, 2, 1]), 1), # use minimum value when all images are anomalous
(torch.Tensor([0, 0, 0, 0]), torch.Tensor([4, 3, 2, 1]), 4), # use maximum value when all images are normal
],
)
def test_adaptive_threshold(labels: torch.Tensor, preds: torch.Tensor, target_threshold: int | float) -> None:
Expand Down
Loading