Skip to content

Commit

Permalink
Ignore pixel metrics in classification task (#516)
Browse files Browse the repository at this point in the history
* ignore pixel metrics and show warning in classification task

* add task parameter to cli and add default value
  • Loading branch information
djdameln committed Aug 29, 2022
1 parent 2a323c8 commit 65169ed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions anomalib/utils/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def get_callbacks(config: Union[ListConfig, DictConfig]) -> List[Callback]:
)
metrics_callback = MetricsConfigurationCallback(
config.metrics.threshold.adaptive,
config.dataset.task,
image_threshold,
pixel_threshold,
image_metric_names,
Expand Down
20 changes: 19 additions & 1 deletion anomalib/utils/callbacks/metrics_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# SPDX-License-Identifier: Apache-2.0


import logging
from typing import List, Optional

import pytorch_lightning as pl
Expand All @@ -14,6 +15,8 @@
from anomalib.models.components.base.anomaly_module import AnomalyModule
from anomalib.utils.metrics import metric_collection_from_names

logger = logging.getLogger(__name__)

__all__ = ["MetricsConfigurationCallback"]


Expand All @@ -24,6 +27,7 @@ class MetricsConfigurationCallback(Callback):
def __init__(
self,
adaptive_threshold: bool,
task: str = "segmentation",
default_image_threshold: Optional[float] = None,
default_pixel_threshold: Optional[float] = None,
image_metric_names: Optional[List[str]] = None,
Expand All @@ -38,6 +42,7 @@ def __init__(
these to the lightning module.
Args:
task (str): Task type of the current run.
adaptive_threshold (bool): Flag indicating whether threshold should be adaptive.
default_image_threshold (Optional[float]): Default image threshold value.
default_pixel_threshold (Optional[float]): Default pixel threshold value.
Expand All @@ -46,6 +51,7 @@ def __init__(
normalization_method(Optional[str]): Normalization method. <None, min_max, cdf>
"""
# TODO: https://github.com/openvinotoolkit/anomalib/issues/384
self.task = task
self.image_metric_names = image_metric_names
self.pixel_metric_names = pixel_metric_names

Expand Down Expand Up @@ -76,7 +82,19 @@ def setup(
stage (Optional[str], optional): fit, validate, test or predict. Defaults to None.
"""
image_metric_names = [] if self.image_metric_names is None else self.image_metric_names
pixel_metric_names = [] if self.pixel_metric_names is None else self.pixel_metric_names

pixel_metric_names: List[str]
if self.pixel_metric_names is None:
pixel_metric_names = []
elif self.task == "classification":
pixel_metric_names = []
logger.warning(
"Cannot perform pixel-level evaluation when task type is classification. "
"Ignoring the following pixel-level metrics: %s",
self.pixel_metric_names,
)
else:
pixel_metric_names = self.pixel_metric_names

if isinstance(pl_module, AnomalyModule):
pl_module.adaptive_threshold = self.adaptive_threshold
Expand Down
1 change: 1 addition & 0 deletions anomalib/utils/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def add_arguments_to_parser(self, parser: LightningArgumentParser) -> None:
parser.set_defaults(
{
"metrics.adaptive_threshold": True,
"metrics.task": "segmentation",
"metrics.default_image_threshold": None,
"metrics.default_pixel_threshold": None,
"metrics.image_metric_names": ["F1Score", "AUROC"],
Expand Down

0 comments on commit 65169ed

Please sign in to comment.