-
Notifications
You must be signed in to change notification settings - Fork 709
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to load metrics with kwargs (#688)
* add option to load metrics with kwargs * make subfunctions private * add tests * address pr comments * fix issu #685 * fix import * address check issues in tests * address pr requests * doc it * Apply suggestions from code review * Apply suggestions from code review * fix test import * indent example Co-authored-by: Samet Akcay <samet.akcay@intel.com>
- Loading branch information
1 parent
034953f
commit b430573
Showing
12 changed files
with
335 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import shutil | ||
import tempfile | ||
from pathlib import Path | ||
|
||
import pytorch_lightning as pl | ||
import torch | ||
from torch import nn | ||
from torch.utils.data import DataLoader, Dataset | ||
|
||
from anomalib.utils.loggers.tensorboard import AnomalibTensorBoardLogger | ||
|
||
|
||
class DummyDataset(Dataset): | ||
def __len__(self): | ||
return 1 | ||
|
||
def __getitem__(self, idx): | ||
return torch.ones(1) | ||
|
||
|
||
class DummyDataModule(pl.LightningDataModule): | ||
def train_dataloader(self) -> DataLoader: | ||
return DataLoader(DummyDataset()) | ||
|
||
def val_dataloader(self) -> DataLoader: | ||
return DataLoader(DummyDataset()) | ||
|
||
def test_dataloader(self) -> DataLoader: | ||
return DataLoader(DummyDataset()) | ||
|
||
|
||
class DummyModel(nn.Module): | ||
pass | ||
|
||
|
||
class DummyLogger(AnomalibTensorBoardLogger): | ||
def __init__(self): | ||
self.tempdir = Path(tempfile.mkdtemp()) | ||
super().__init__(name="tensorboard_logs", save_dir=self.tempdir) | ||
|
||
def __del__(self): | ||
if self.tempdir.exists(): | ||
shutil.rmtree(self.tempdir) |
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,27 @@ | ||
"""Helpers for metrics tests.""" | ||
|
||
from typing import Tuple, Union | ||
|
||
from omegaconf import DictConfig, ListConfig | ||
|
||
from anomalib.utils.metrics import ( | ||
AnomalibMetricCollection, | ||
metric_collection_from_names, | ||
) | ||
|
||
|
||
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 |
Empty file.
13 changes: 13 additions & 0 deletions
13
tests/pre_merge/utils/callbacks/metrics_configuration_callback/data/config-good-00.yaml
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,13 @@ | ||
metrics: | ||
pixel: | ||
F1Score: | ||
class_path: torchmetrics.F1Score | ||
init_args: | ||
compute_on_cpu: true | ||
AUROC: | ||
class_path: anomalib.utils.metrics.AUROC | ||
init_args: | ||
compute_on_cpu: true | ||
image: | ||
- F1Score | ||
- AUROC |
13 changes: 13 additions & 0 deletions
13
tests/pre_merge/utils/callbacks/metrics_configuration_callback/data/config-good-01.yaml
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,13 @@ | ||
metrics: | ||
pixel: | ||
- F1Score | ||
- AUROC | ||
image: | ||
F1Score: | ||
class_path: torchmetrics.F1Score | ||
init_args: | ||
compute_on_cpu: true | ||
AUROC: | ||
class_path: anomalib.utils.metrics.AUROC | ||
init_args: | ||
compute_on_cpu: true |
Oops, something went wrong.