Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to new torch metrics #235

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions anomalib/models/components/base/anomaly_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from omegaconf import DictConfig, ListConfig
from pytorch_lightning.callbacks.base import Callback
from torch import Tensor, nn
from torchmetrics import F1, MetricCollection
from torchmetrics import F1Score, MetricCollection

from anomalib.utils.metrics import (
AUROC,
Expand Down Expand Up @@ -59,11 +59,11 @@ def __init__(self, params: Union[DictConfig, ListConfig]):

# metrics
image_auroc = AUROC(num_classes=1, pos_label=1, compute_on_step=False)
image_f1 = F1(num_classes=1, compute_on_step=False, threshold=self.hparams.model.threshold.image_default)
image_f1 = F1Score(num_classes=1, compute_on_step=False, threshold=self.hparams.model.threshold.image_default)
pixel_auroc = AUROC(num_classes=1, pos_label=1, compute_on_step=False)
pixel_f1 = F1(num_classes=1, compute_on_step=False, threshold=self.hparams.model.threshold.pixel_default)
self.image_metrics = MetricCollection([image_auroc, image_f1], prefix="image_").cpu()
self.pixel_metrics = MetricCollection([pixel_auroc, pixel_f1], prefix="pixel_").cpu()
pixel_f1 = F1Score(num_classes=1, compute_on_step=False, threshold=self.hparams.model.threshold.pixel_default)
self.image_metrics = MetricCollection([image_auroc, image_f1], prefix="image_", compute_groups=False).cpu()
self.pixel_metrics = MetricCollection([pixel_auroc, pixel_f1], prefix="pixel_", compute_groups=False).cpu()

def forward(self, batch): # pylint: disable=arguments-differ
"""Forward-pass input tensor to the module.
Expand Down Expand Up @@ -154,8 +154,8 @@ def _compute_adaptive_threshold(self, outputs):
else:
self.pixel_threshold.value = self.image_threshold.value

self.image_metrics.F1.threshold = self.image_threshold.value.item()
self.pixel_metrics.F1.threshold = self.pixel_threshold.value.item()
self.image_metrics.F1Score.threshold = self.image_threshold.value.item()
self.pixel_metrics.F1Score.threshold = self.pixel_threshold.value.item()

def _collect_outputs(self, image_metric, pixel_metric, outputs):
for output in outputs:
Expand Down
4 changes: 2 additions & 2 deletions anomalib/utils/callbacks/cdf_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def __init__(self):

def on_test_start(self, _trainer: pl.Trainer, pl_module: AnomalyModule) -> None:
"""Called when the test begins."""
pl_module.image_metrics.F1.threshold = 0.5
pl_module.pixel_metrics.F1.threshold = 0.5
pl_module.image_metrics.F1Score.threshold = 0.5
pl_module.pixel_metrics.F1Score.threshold = 0.5

def on_validation_epoch_start(self, trainer: "pl.Trainer", pl_module: AnomalyModule) -> None:
"""Called when the validation starts after training.
Expand Down
4 changes: 2 additions & 2 deletions anomalib/utils/callbacks/min_max_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class MinMaxNormalizationCallback(Callback):

def on_test_start(self, _trainer: pl.Trainer, pl_module: AnomalyModule) -> None:
"""Called when the test begins."""
pl_module.image_metrics.F1.threshold = 0.5
pl_module.pixel_metrics.F1.threshold = 0.5
pl_module.image_metrics.F1Score.threshold = 0.5
pl_module.pixel_metrics.F1Score.threshold = 0.5

def on_validation_batch_end(
self,
Expand Down
2 changes: 1 addition & 1 deletion anomalib/utils/callbacks/visualizer_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def on_test_batch_end(
normalize = False # anomaly maps are already normalized
else:
normalize = True # raw anomaly maps. Still need to normalize
threshold = pl_module.pixel_metrics.F1.threshold
threshold = pl_module.pixel_metrics.F1Score.threshold

for i, (filename, image, anomaly_map, pred_score, gt_label) in enumerate(
zip(
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ omegaconf>=2.1.1
opencv-python>=4.5.3.56
pandas>=1.1.0
pytorch-lightning>=1.6.0
torchmetrics>=0.8.0
torchvision>=0.9.1
torchtext>=0.9.1
wandb==0.12.9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_normalizer(path=get_dataset_path(), category="shapes"):
results_with_minmax_normalization = run_train_test(config)

# performance should be the same
for metric in ["image_AUROC", "image_F1"]:
for metric in ["image_AUROC", "image_F1Score"]:
assert round(results_without_normalization[0][metric], 3) == round(results_with_cdf_normalization[0][metric], 3)
assert round(results_without_normalization[0][metric], 3) == round(
results_with_minmax_normalization[0][metric], 3
Expand Down
4 changes: 2 additions & 2 deletions tests/pre_merge/utils/metrics/test_adaptive_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ def test_non_adaptive_threshold():

trainer = Trainer(**config.trainer, callbacks=callbacks)
trainer.fit(model=model, datamodule=datamodule)
assert trainer.model.image_metrics.F1.threshold == image_threshold
assert trainer.model.pixel_metrics.F1.threshold == pixel_threshold
assert trainer.model.image_metrics.F1Score.threshold == image_threshold
assert trainer.model.pixel_metrics.F1Score.threshold == pixel_threshold