Skip to content

Commit

Permalink
Add PIMO (#2329)
Browse files Browse the repository at this point in the history
* PIMO (#1726)

* update

Signed-off-by: jpcbertoldo <[email protected]>

* test binclf curves numpy and numba and fixes

Signed-off-by: jpcbertoldo <[email protected]>

* correct som docstrings

Signed-off-by: jpcbertoldo <[email protected]>

* torch interface and tests

Signed-off-by: jpcbertoldo <[email protected]>

* torch interface and tests

Signed-off-by: jpcbertoldo <[email protected]>

* constants regrouped in dataclass as class vars

Signed-off-by: jpcbertoldo <[email protected]>

* result class was unneccesary for per_image_binclf_curve

Signed-off-by: jpcbertoldo <[email protected]>

* factorize function _get_threshs_minmax_linspace

Signed-off-by: jpcbertoldo <[email protected]>

* small docs fixes

Signed-off-by: jpcbertoldo <[email protected]>

* add pimo numpy version and test

Signed-off-by: jpcbertoldo <[email protected]>

* move validation

Signed-off-by: jpcbertoldo <[email protected]>

* add `shared_fpr_metric` option

Signed-off-by: jpcbertoldo <[email protected]>

* add pimo torch functional version and test

Signed-off-by: jpcbertoldo <[email protected]>

* add torchmetrics interface and test

Signed-off-by: jpcbertoldo <[email protected]>

* renames and put things in init

Signed-off-by: jpcbertoldo <[email protected]>

* validate inputs in result objects

Signed-off-by: jpcbertoldo <[email protected]>

* result objects to from dict and tests

Signed-off-by: jpcbertoldo <[email protected]>

* add save and load methods to result objects and test

Signed-off-by: jpcbertoldo <[email protected]>

* refactor validations and minor changes

Signed-off-by: jpcbertoldo <[email protected]>

* test result objects' properties

Signed-off-by: jpcbertoldo <[email protected]>

* minor refactors

Signed-off-by: jpcbertoldo <[email protected]>

* add missing docstrings

Signed-off-by: jpcbertoldo <[email protected]>

* minore vocabulary fix for consistency

Signed-off-by: jpcbertoldo <[email protected]>

* add per image scores statistics and test it

Signed-off-by: jpcbertoldo <[email protected]>

* refactor constants notation

Signed-off-by: jpcbertoldo <[email protected]>

* add stats tests and test it

Signed-off-by: jpcbertoldo <[email protected]>

* change the meaning of AUPIMO.num_thresh

Signed-off-by: jpcbertoldo <[email protected]>

* interface to format pairwise test results

Signed-off-by: jpcbertoldo <[email protected]>

* improve doc

Signed-off-by: jpcbertoldo <[email protected]>

* add optional `paths` to result objects and some minor fixes and refactors

Signed-off-by: jpcbertoldo <[email protected]>

* remove frozen from dataclasses and some done todos

Signed-off-by: jpcbertoldo <[email protected]>

* review headers

Signed-off-by: jpcbertoldo <[email protected]>

* doc modifs

Signed-off-by: jpcbertoldo <[email protected]>

* refactor `score_less_than_thresh` in `_binclf_one_curve_python`

Signed-off-by: jpcbertoldo <[email protected]>

* correct license comments

Signed-off-by: jpcbertoldo <[email protected]>

* fix doc

Signed-off-by: jpcbertoldo <[email protected]>

* numba as extra requirement

Signed-off-by: jpcbertoldo <[email protected]>

* refactor copyrights from jpcbertoldo

Signed-off-by: jpcbertoldo <[email protected]>

* remove from __future__ import annotations

Signed-off-by: jpcbertoldo <[email protected]>

* refactor validations names

Signed-off-by: jpcbertoldo <[email protected]>

* dedupe file path validation

Signed-off-by: jpcbertoldo <[email protected]>

* fix tests

Signed-off-by: jpcbertoldo <[email protected]>

* Add todo

Signed-off-by: jpcbertoldo <[email protected]>

* refactor enums

Signed-off-by: jpcbertoldo <[email protected]>

* only logger.warning

Signed-off-by: jpcbertoldo <[email protected]>

* refactor test imports

Signed-off-by: jpcbertoldo <[email protected]>

* refactor docs

Signed-off-by: jpcbertoldo <[email protected]>

* refactor some docs

Signed-off-by: jpcbertoldo <[email protected]>

* correct pre commit errors

Signed-off-by: jpcbertoldo <[email protected]>

* remove author tag

Signed-off-by: jpcbertoldo <[email protected]>

* add thrid party program

Signed-off-by: jpcbertoldo <[email protected]>

* Update src/anomalib/metrics/per_image/pimo.py

* move HAS_NUMBA

Signed-off-by: jpcbertoldo <[email protected]>

* remove PIMOSharedFPRMetric

Signed-off-by: jpcbertoldo <[email protected]>

* make torchmetrics compute avg by dft

Signed-off-by: jpcbertoldo <[email protected]>

* pre-commit hooks corrections

Signed-off-by: jpcbertoldo <[email protected]>

* correct numpy.trapezoid

Signed-off-by: jpcbertoldo <[email protected]>

---------

Signed-off-by: jpcbertoldo <[email protected]>
Co-authored-by: Samet Akcay <[email protected]>

* 🗑️ Remove numba (#2313)

* remove numba

Signed-off-by: Ashwin Vaidya <[email protected]>

* fix pre-commit checks

Signed-off-by: Ashwin Vaidya <[email protected]>

* add third-party-programs.txt

Signed-off-by: Ashwin Vaidya <[email protected]>

---------

Signed-off-by: Ashwin Vaidya <[email protected]>

* 🗑️ Remove unused methods (#2315)

* remove numba

Signed-off-by: Ashwin Vaidya <[email protected]>

* fix pre-commit checks

Signed-off-by: Ashwin Vaidya <[email protected]>

* remove all unused methods

Signed-off-by: Ashwin Vaidya <[email protected]>

---------

Signed-off-by: Ashwin Vaidya <[email protected]>

* PIMO: Port Numpy → Torch (#2316)

* remove numba

Signed-off-by: Ashwin Vaidya <[email protected]>

* fix pre-commit checks

Signed-off-by: Ashwin Vaidya <[email protected]>

* remove all unused methods

Signed-off-by: Ashwin Vaidya <[email protected]>

* replace numpy with torch

Signed-off-by: Ashwin Vaidya <[email protected]>

---------

Signed-off-by: Ashwin Vaidya <[email protected]>

* 🔨Refactor methods across files (#2321)

* remove numba

Signed-off-by: Ashwin Vaidya <[email protected]>

* fix pre-commit checks

Signed-off-by: Ashwin Vaidya <[email protected]>

* remove all unused methods

Signed-off-by: Ashwin Vaidya <[email protected]>

* replace numpy with torch

Signed-off-by: Ashwin Vaidya <[email protected]>

* refactor code

Signed-off-by: Ashwin Vaidya <[email protected]>

* refactor
move functional inside update
remove path from the metric

* Add changes from comments

Signed-off-by: Ashwin Vaidya <[email protected]>

---------

Signed-off-by: Ashwin Vaidya <[email protected]>

* Remove model to model comparison (#2325)

* rename to pimo

Signed-off-by: Ashwin Vaidya <[email protected]>

* minor refactor

Signed-off-by: Ashwin Vaidya <[email protected]>

* remove model to model comparison

Signed-off-by: Ashwin Vaidya <[email protected]>

* fix test

Signed-off-by: Ashwin Vaidya <[email protected]>

* PR comments

Signed-off-by: Ashwin Vaidya <[email protected]>

* Minor refactor

Signed-off-by: Ashwin Vaidya <[email protected]>

---------

Signed-off-by: Ashwin Vaidya <[email protected]>

* PR comments

Signed-off-by: Ashwin Vaidya <[email protected]>

* Remove unused enums

Signed-off-by: Ashwin Vaidya <[email protected]>

* update doc strings

Signed-off-by: Ashwin Vaidya <[email protected]>

* update param names

Signed-off-by: Ashwin Vaidya <[email protected]>

* add aupimo basic usage tutorial notebook (#2330)

* add aupimo basic usage tutorial notebook

Signed-off-by: jpcbertoldo <[email protected]>

* update scipy import

Signed-off-by: jpcbertoldo <[email protected]>

* add cite us

Signed-off-by: jpcbertoldo <[email protected]>

* minor

Signed-off-by: jpcbertoldo <[email protected]>

* modify texts and add illustration

Signed-off-by: jpcbertoldo <[email protected]>

* udpate working dir

Signed-off-by: jpcbertoldo <[email protected]>

---------

Signed-off-by: jpcbertoldo <[email protected]>

---------

Signed-off-by: jpcbertoldo <[email protected]>
Signed-off-by: Ashwin Vaidya <[email protected]>
Co-authored-by: Joao P C Bertoldo <[email protected]>
Co-authored-by: Samet Akcay <[email protected]>
  • Loading branch information
3 people authored Oct 2, 2024
1 parent f473df8 commit 191e21f
Show file tree
Hide file tree
Showing 16 changed files with 3,744 additions and 1 deletion.
549 changes: 549 additions & 0 deletions notebooks/700_metrics/701a_aupimo.ipynb

Large diffs are not rendered by default.

690 changes: 690 additions & 0 deletions notebooks/700_metrics/roc_pro_pimo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion src/anomalib/data/utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,20 @@ def contains_non_printable_characters(path: str | Path) -> bool:
return not printable_pattern.match(str(path))


def validate_path(path: str | Path, base_dir: str | Path | None = None, should_exist: bool = True) -> Path:
def validate_path(
path: str | Path,
base_dir: str | Path | None = None,
should_exist: bool = True,
extensions: tuple[str, ...] | None = None,
) -> Path:
"""Validate the path.
Args:
path (str | Path): Path to validate.
base_dir (str | Path): Base directory to restrict file access.
should_exist (bool): If True, do not raise an exception if the path does not exist.
extensions (tuple[str, ...] | None): Accepted extensions for the path. An exception is raised if the
path does not have one of the accepted extensions. If None, no check is performed. Defaults to None.
Returns:
Path: Validated path.
Expand Down Expand Up @@ -213,6 +220,11 @@ def validate_path(path: str | Path, base_dir: str | Path | None = None, should_e
msg = f"Read or execute permissions denied for the path: {path}"
raise PermissionError(msg)

# Check if the path has one of the accepted extensions
if extensions is not None and path.suffix not in extensions:
msg = f"Path extension is not accepted. Accepted extensions: {extensions}. Path: {path}"
raise ValueError(msg)

return path


Expand Down
3 changes: 3 additions & 0 deletions src/anomalib/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .f1_max import F1Max
from .f1_score import F1Score
from .min_max import MinMax
from .pimo import AUPIMO, PIMO
from .precision_recall_curve import BinaryPrecisionRecallCurve
from .pro import PRO
from .threshold import F1AdaptiveThreshold, ManualThreshold
Expand All @@ -35,6 +36,8 @@
"ManualThreshold",
"MinMax",
"PRO",
"PIMO",
"AUPIMO",
]

logger = logging.getLogger(__name__)
Expand Down
23 changes: 23 additions & 0 deletions src/anomalib/metrics/pimo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Per-Image Metrics."""

# Original Code
# https://github.com/jpcbertoldo/aupimo
#
# Modified
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from .binary_classification_curve import ThresholdMethod
from .pimo import AUPIMO, PIMO, AUPIMOResult, PIMOResult

__all__ = [
# constants
"ThresholdMethod",
# result classes
"PIMOResult",
"AUPIMOResult",
# torchmetrics interfaces
"PIMO",
"AUPIMO",
"StatsOutliersPolicy",
]
Loading

0 comments on commit 191e21f

Please sign in to comment.