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

🐞 Set normalization method from anomaly module #530

Merged
merged 1 commit into from
Sep 2, 2022
Merged
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
28 changes: 26 additions & 2 deletions anomalib/models/components/base/anomaly_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@

import logging
from abc import ABC
from typing import Any, List, Optional
from typing import Any, List, Optional, OrderedDict
from warnings import warn

import pytorch_lightning as pl
from pytorch_lightning.callbacks.base import Callback
from torch import Tensor, nn
from torchmetrics import Metric

from anomalib.utils.metrics import AdaptiveThreshold, AnomalibMetricCollection
from anomalib.utils.metrics import (
AdaptiveThreshold,
AnomalibMetricCollection,
AnomalyScoreDistribution,
MinMax,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -166,3 +172,21 @@ def _log_metrics(self):
self.log_dict(self.image_metrics, prog_bar=False)
else:
self.log_dict(self.image_metrics, prog_bar=True)

def _load_normalization_class(self, state_dict: OrderedDict[str, Tensor]):
"""Assigns the normalization method to use."""
if "normalization_metrics.max" in state_dict.keys():
self.normalization_metrics = MinMax()
elif "normalization_metrics.image_mean" in state_dict.keys():
self.normalization_metrics = AnomalyScoreDistribution()
else:
warn("No known normalization found in model weights.")

def load_state_dict(self, state_dict: OrderedDict[str, Tensor], strict: bool = True):
"""Load state dict from checkpoint.

Ensures that normalization and thresholding attributes is properly setup before model is loaded.
"""
# Used to load missing normalization and threshold parameters
self._load_normalization_class(state_dict)
super().load_state_dict(state_dict, strict=strict)