From 501afb6d425ef7f0458d003ee952613e58369a47 Mon Sep 17 00:00:00 2001 From: Dick Ameln Date: Fri, 28 Jan 2022 17:19:21 +0100 Subject: [PATCH 1/4] reorder fpr array when needed --- anomalib/core/metrics/auroc.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/anomalib/core/metrics/auroc.py b/anomalib/core/metrics/auroc.py index 35165a9090..f6d0026c3b 100644 --- a/anomalib/core/metrics/auroc.py +++ b/anomalib/core/metrics/auroc.py @@ -1,4 +1,5 @@ """Implementation of AUROC metric based on TorchMetrics.""" +import torch from torch import Tensor from torchmetrics import ROC from torchmetrics.functional import auc @@ -14,4 +15,6 @@ def compute(self) -> Tensor: Value of the AUROC metric """ fpr, tpr, _thresholds = super().compute() + if not (torch.all(fpr.diff() <= 0) or torch.all(fpr.diff() >= 0)): + return auc(fpr, tpr, reorder=True) # only reorder if fpr is not increasing or decreasing return auc(fpr, tpr) From bd00a796c5d0051e2613aa741029597ea22ede4b Mon Sep 17 00:00:00 2001 From: Dick Ameln Date: Fri, 28 Jan 2022 18:00:59 +0100 Subject: [PATCH 2/4] add todo for adding stable sort after pytorch upgrade --- anomalib/core/metrics/auroc.py | 1 + 1 file changed, 1 insertion(+) diff --git a/anomalib/core/metrics/auroc.py b/anomalib/core/metrics/auroc.py index f6d0026c3b..ab352fbb38 100644 --- a/anomalib/core/metrics/auroc.py +++ b/anomalib/core/metrics/auroc.py @@ -15,6 +15,7 @@ def compute(self) -> Tensor: Value of the AUROC metric """ fpr, tpr, _thresholds = super().compute() + # TODO: use stable sort after upgrading to pytorch 1.9.x (https://github.com/openvinotoolkit/anomalib/issues/92) if not (torch.all(fpr.diff() <= 0) or torch.all(fpr.diff() >= 0)): return auc(fpr, tpr, reorder=True) # only reorder if fpr is not increasing or decreasing return auc(fpr, tpr) From 95357e368ca66d1c9c018c712243a977540dd5e1 Mon Sep 17 00:00:00 2001 From: Dick Ameln Date: Wed, 2 Feb 2022 09:11:58 +0100 Subject: [PATCH 3/4] mypy --- anomalib/core/metrics/auroc.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/anomalib/core/metrics/auroc.py b/anomalib/core/metrics/auroc.py index ab352fbb38..5ce844b989 100644 --- a/anomalib/core/metrics/auroc.py +++ b/anomalib/core/metrics/auroc.py @@ -14,6 +14,9 @@ def compute(self) -> Tensor: Returns: Value of the AUROC metric """ + tpr: Tensor + fpr: Tensor + fpr, tpr, _thresholds = super().compute() # TODO: use stable sort after upgrading to pytorch 1.9.x (https://github.com/openvinotoolkit/anomalib/issues/92) if not (torch.all(fpr.diff() <= 0) or torch.all(fpr.diff() >= 0)): From 9ef16fa3d793fb53642d4b4ea898ffc5a7a59bb7 Mon Sep 17 00:00:00 2001 From: Dick Ameln Date: Wed, 2 Feb 2022 09:38:51 +0100 Subject: [PATCH 4/4] set black version to match precommit --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index e80af51c8f..605b7f223a 100644 --- a/tox.ini +++ b/tox.ini @@ -12,7 +12,7 @@ envlist = [testenv:black] basepython = python3 -deps = black +deps = black==20.8b1 commands = black --check --diff anomalib -l 120 [testenv:isort]