-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make metrics configurable * simplify import * allow omitting image or pixel metrics * upgrade torch metrics * update all config files * small bugfix * update threshold test * disable compute groups * fix visualizer tests * fix normalizer tests
- Loading branch information
Showing
17 changed files
with
176 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,63 @@ | ||
"""Custom anomaly evaluation metrics.""" | ||
import importlib | ||
import warnings | ||
from typing import List, Optional, Tuple, Union | ||
|
||
import torchmetrics | ||
from omegaconf import DictConfig, ListConfig | ||
|
||
from .adaptive_threshold import AdaptiveThreshold | ||
from .anomaly_score_distribution import AnomalyScoreDistribution | ||
from .auroc import AUROC | ||
from .collection import AnomalibMetricCollection | ||
from .min_max import MinMax | ||
from .optimal_f1 import OptimalF1 | ||
|
||
__all__ = ["AUROC", "OptimalF1", "AdaptiveThreshold", "AnomalyScoreDistribution", "MinMax"] | ||
|
||
|
||
def get_metrics(config: Union[ListConfig, DictConfig]) -> Tuple[AnomalibMetricCollection, AnomalibMetricCollection]: | ||
"""Create metric collections based on the config. | ||
Args: | ||
config (Union[DictConfig, ListConfig]): Config.yaml loaded using OmegaConf | ||
Returns: | ||
AnomalibMetricCollection: Image-level metric collection | ||
AnomalibMetricCollection: Pixel-level metric collection | ||
""" | ||
image_metric_names = config.metrics.image if "image" in config.metrics.keys() else [] | ||
pixel_metric_names = config.metrics.pixel if "pixel" in config.metrics.keys() else [] | ||
image_metrics = metric_collection_from_names(image_metric_names, "image_") | ||
pixel_metrics = metric_collection_from_names(pixel_metric_names, "pixel_") | ||
return image_metrics, pixel_metrics | ||
|
||
|
||
def metric_collection_from_names(metric_names: List[str], prefix: Optional[str]) -> AnomalibMetricCollection: | ||
"""Create a metric collection from a list of metric names. | ||
The function will first try to retrieve the metric from the metrics defined in Anomalib metrics module, | ||
then in TorchMetrics package. | ||
Args: | ||
metric_names (List[str]): List of metric names to be included in the collection. | ||
prefix (Optional[str]): prefix to assign to the metrics in the collection. | ||
Returns: | ||
AnomalibMetricCollection: Collection of metrics. | ||
""" | ||
metrics_module = importlib.import_module("anomalib.utils.metrics") | ||
metrics = AnomalibMetricCollection([], prefix=prefix, compute_groups=False) | ||
for metric_name in metric_names: | ||
if hasattr(metrics_module, metric_name): | ||
metric_cls = getattr(metrics_module, metric_name) | ||
metrics.add_metrics(metric_cls(compute_on_step=False)) | ||
elif hasattr(torchmetrics, metric_name): | ||
try: | ||
metric_cls = getattr(torchmetrics, metric_name) | ||
metrics.add_metrics(metric_cls(compute_on_step=False)) | ||
except TypeError: | ||
warnings.warn(f"Incorrect constructor arguments for {metric_name} metric from TorchMetrics package.") | ||
else: | ||
warnings.warn(f"No metric with name {metric_name} found in Anomalib metrics or TorchMetrics.") | ||
return metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Anomalib Metric Collection.""" | ||
|
||
# Copyright (C) 2020 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions | ||
# and limitations under the License. | ||
|
||
from torchmetrics import MetricCollection | ||
|
||
|
||
class AnomalibMetricCollection(MetricCollection): | ||
"""Extends the MetricCollection class for use in the Anomalib pipeline.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._update_called = False | ||
self._threshold = 0.5 | ||
|
||
def set_threshold(self, threshold_value): | ||
"""Update the threshold value for all metrics that have the threshold attribute.""" | ||
self._threshold = threshold_value | ||
for metric in self.values(): | ||
if hasattr(metric, "threshold"): | ||
metric.threshold = threshold_value | ||
|
||
def update(self, *args, **kwargs) -> None: | ||
"""Add data to the metrics.""" | ||
super().update(*args, **kwargs) | ||
self._update_called = True | ||
|
||
@property | ||
def update_called(self) -> bool: | ||
"""Returns a boolean indicating if the update method has been called at least once.""" | ||
return self._update_called | ||
|
||
@property | ||
def threshold(self) -> float: | ||
"""Return the value of the anomaly threshold.""" | ||
return self._threshold |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters