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

DFKDE refactor to accept any layer name like other models #482

Merged
merged 2 commits into from
Aug 4, 2022
Merged
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
2 changes: 2 additions & 0 deletions anomalib/models/dfkde/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ model:
threshold_steepness: 0.05
threshold_offset: 12
normalization_method: min_max # options: [null, min_max, cdf]
layers:
- avgpool

metrics:
image:
Expand Down
3 changes: 3 additions & 0 deletions anomalib/models/dfkde/lightning_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Dfkde(AnomalyModule):

def __init__(
self,
layers: List[str],
backbone: str,
pre_trained: bool = True,
max_training_points: int = 40000,
Expand All @@ -58,6 +59,7 @@ def __init__(
super().__init__()

self.model = DfkdeModel(
layers=layers,
backbone=backbone,
pre_trained=pre_trained,
n_comps=n_components,
Expand Down Expand Up @@ -126,6 +128,7 @@ class DfkdeLightning(Dfkde):

def __init__(self, hparams: Union[DictConfig, ListConfig]) -> None:
super().__init__(
layers=hparams.model.layers,
backbone=hparams.model.backbone,
max_training_points=hparams.model.max_training_points,
pre_processing=hparams.model.pre_processing,
Expand Down
3 changes: 2 additions & 1 deletion anomalib/models/dfkde/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DfkdeModel(nn.Module):

def __init__(
self,
layers: List[str],
backbone: str,
pre_trained: bool = True,
n_comps: int = 16,
Expand All @@ -59,7 +60,7 @@ def __init__(
self.threshold_offset = threshold_offset

_backbone = getattr(torchvision.models, backbone)
self.feature_extractor = FeatureExtractor(backbone=_backbone(pretrained=pre_trained), layers=["avgpool"]).eval()
self.feature_extractor = FeatureExtractor(backbone=_backbone(pretrained=pre_trained), layers=layers).eval()

self.pca_model = PCA(n_components=self.n_components)
self.kde_model = GaussianKDE()
Expand Down
2 changes: 2 additions & 0 deletions configs/model/dfkde.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ model:
init_args:
backbone: resnet18
pre_trained: true
layers:
- avgpool
max_training_points: 40000
pre_processing: scale
n_components: 16
Expand Down