Skip to content

Commit

Permalink
Refactor (#87)
Browse files Browse the repository at this point in the history
* First round of refactor

* 🔨 fix merge issues

* 🚚 Move components to model

* 🔥 remove artifacts from merge

* Rename logger name

* 🔨 Add wandb to pyproject config

* Address PR comments

* Fix black version

* Fix license + minor refactor

* isort

* Properly import AnomalyModule

* Fix circular import

* address pre-commit

* Rebase development

* fix cyclic imports reported by pylint

* absolute import of AnomalyModule

* Moved anomaly module and dynamic module out of base.anomaly_models

* import AnomalyModule and DynamicBufferModule from anomalib.models.components.base

* reduced coverag percentage

* Refactor import

Co-authored-by: Ashwin Vaidya <ashwinitinvaidya@gmail.com>
Co-authored-by: Samet Akcay <samet.akcay@intel.com>
  • Loading branch information
3 people committed Feb 8, 2022
1 parent 5f2dda2 commit d13e452
Show file tree
Hide file tree
Showing 102 changed files with 1,183 additions and 97 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ instance/
# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/build/
docs/source/_build/

# PyBuilder
.pybuilder/
target/
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ You accept that your contributions will be licensed under the [Apache-2.0 Licens


## References
This document was adapted from [here](https://gist.github.com/briandk/3d2e8b3ec8daf5a27a62).
This document was adapted from [here](https://gist.github.com/briandk/3d2e8b3ec8daf5a27a62).
10 changes: 3 additions & 7 deletions anomalib/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Utilities to get configurable parameters."""
"""Utilities for parsing model configuration."""

# Copyright (C) 2020 Intel Corporation
#
Expand All @@ -14,10 +14,6 @@
# See the License for the specific language governing permissions
# and limitations under the License.

from .config import (
get_configurable_parameters,
update_input_size_config,
update_nncf_config,
)
from .config import get_configurable_parameters, update_nncf_config

__all__ = ["get_configurable_parameters", "update_input_size_config", "update_nncf_config"]
__all__ = ["get_configurable_parameters", "update_nncf_config"]
2 changes: 1 addition & 1 deletion anomalib/data/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import albumentations as A
from torch.utils.data.dataset import Dataset

from anomalib.data.transforms import PreProcessor
from anomalib.data.utils import get_image_filenames, read_image
from anomalib.pre_processing import PreProcessor


class InferenceDataset(Dataset):
Expand Down
5 changes: 2 additions & 3 deletions anomalib/data/mvtec.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@
from torchvision.datasets.folder import VisionDataset

from anomalib.data.inference import InferenceDataset
from anomalib.data.transforms import PreProcessor
from anomalib.data.utils import read_image
from anomalib.utils.download_progress_bar import DownloadProgressBar
from anomalib.data.utils import DownloadProgressBar, read_image
from anomalib.pre_processing import PreProcessor

logger = logging.getLogger(name="Dataset: MVTec")
logger.setLevel(logging.DEBUG)
Expand Down
20 changes: 20 additions & 0 deletions anomalib/data/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Helper utilities for data."""

# Copyright (C) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from .download_progress_bar import DownloadProgressBar
from .image import get_image_filenames, read_image

__all__ = ["get_image_filenames", "read_image", "DownloadProgressBar"]
File renamed without changes.
2 changes: 1 addition & 1 deletion anomalib/data/utils.py → anomalib/data/utils/image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Dataset Utils."""
"""Image Utils."""

# Copyright (C) 2020 Intel Corporation
#
Expand Down
7 changes: 6 additions & 1 deletion anomalib/deploy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Utilities for inference and deployment."""
"""Functions for Inference and model deployment."""

# Copyright (C) 2020 Intel Corporation
#
Expand All @@ -13,3 +13,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from .inferencers import OpenVINOInferencer, TorchInferencer
from .optimize import export_convert, get_model_metadata

__all__ = ["OpenVINOInferencer", "TorchInferencer", "export_convert", "get_model_metadata"]
10 changes: 6 additions & 4 deletions anomalib/deploy/inferencers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
from torch import Tensor

from anomalib.data.utils import read_image
from anomalib.utils.normalization.cdf import normalize as normalize_cdf
from anomalib.utils.normalization.cdf import standardize
from anomalib.utils.normalization.min_max import normalize as normalize_min_max
from anomalib.utils.post_process import superimpose_anomaly_map
from anomalib.post_processing import superimpose_anomaly_map
from anomalib.post_processing.normalization.cdf import normalize as normalize_cdf
from anomalib.post_processing.normalization.cdf import standardize
from anomalib.post_processing.normalization.min_max import (
normalize as normalize_min_max,
)


class Inferencer(ABC):
Expand Down
2 changes: 1 addition & 1 deletion anomalib/deploy/inferencers/openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from omegaconf import DictConfig, ListConfig
from openvino.inference_engine import IECore # pylint: disable=no-name-in-module

from anomalib.data.transforms.pre_process import PreProcessor
from anomalib.pre_processing import PreProcessor

from .base import Inferencer

Expand Down
4 changes: 2 additions & 2 deletions anomalib/deploy/inferencers/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from omegaconf import DictConfig, ListConfig
from torch import Tensor

from anomalib.core.model import AnomalyModule
from anomalib.data.transforms.pre_process import PreProcessor
from anomalib.deploy.optimize import get_model_metadata
from anomalib.models import get_model
from anomalib.models.components import AnomalyModule
from anomalib.pre_processing import PreProcessor

from .base import Inferencer

Expand Down
2 changes: 1 addition & 1 deletion anomalib/deploy/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import torch
from torch import Tensor

from anomalib.core.model.anomaly_module import AnomalyModule
from anomalib.models.components import AnomalyModule


def get_model_metadata(model: AnomalyModule) -> Dict[str, Tensor]:
Expand Down
2 changes: 1 addition & 1 deletion anomalib/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from omegaconf import DictConfig, ListConfig
from torch import load

from anomalib.core.model import AnomalyModule
from anomalib.models.components import AnomalyModule


def get_model(config: Union[DictConfig, ListConfig]) -> AnomalyModule:
Expand Down
3 changes: 1 addition & 2 deletions anomalib/models/cflow/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
from pytorch_lightning.callbacks import EarlyStopping
from torch import Tensor, nn, optim

from anomalib.core.model import AnomalyModule
from anomalib.core.model.feature_extractor import FeatureExtractor
from anomalib.models.cflow.backbone import cflow_head, positional_encoding_2d
from anomalib.models.components import AnomalyModule, FeatureExtractor

__all__ = ["AnomalyMapGenerator", "CflowModel", "CflowLightning"]

Expand Down
32 changes: 32 additions & 0 deletions anomalib/models/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Components used within the models."""

# Copyright (C) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from .base import AnomalyModule, DynamicBufferModule
from .dimensionality_reduction import PCA, SparseRandomProjection
from .feature_extractors import FeatureExtractor
from .sampling import KCenterGreedy
from .stats import GaussianKDE, MultiVariateGaussian

__all__ = [
"AnomalyModule",
"DynamicBufferModule",
"PCA",
"SparseRandomProjection",
"FeatureExtractor",
"KCenterGreedy",
"GaussianKDE",
"MultiVariateGaussian",
]
20 changes: 20 additions & 0 deletions anomalib/models/components/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Base classes for all anomaly components."""

# Copyright (C) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from .anomaly_module import AnomalyModule
from .dynamic_module import DynamicBufferModule

__all__ = ["AnomalyModule", "DynamicBufferModule"]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from torch import Tensor, nn
from torchmetrics import F1, MetricCollection

from anomalib.core.metrics import (
from anomalib.utils.metrics import (
AUROC,
AdaptiveThreshold,
AnomalyScoreDistribution,
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions anomalib/models/components/dimensionality_reduction/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Algorithms for decomposition and dimensionality reduction."""

# Copyright (C) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from .pca import PCA
from .random_projection import SparseRandomProjection

__all__ = ["PCA", "SparseRandomProjection"]
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import torch
from torch import Tensor

from anomalib.core.model.dynamic_module import DynamicBufferModule
from anomalib.models.components.base import DynamicBufferModule


class PCA(DynamicBufferModule):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Anomalib Core Model Entities."""
"""Feature extractors."""

# Copyright (C) 2020 Intel Corporation
#
Expand All @@ -14,6 +14,6 @@
# See the License for the specific language governing permissions
# and limitations under the License.

from .anomaly_module import AnomalyModule
from .feature_extractor import FeatureExtractor

__all__ = ["AnomalyModule"]
__all__ = ["FeatureExtractor"]
File renamed without changes.
19 changes: 19 additions & 0 deletions anomalib/models/components/sampling/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Sampling methods."""

# Copyright (C) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from .k_center_greedy import KCenterGreedy

__all__ = ["KCenterGreedy"]
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import torch.nn.functional as F
from torch import Tensor

from anomalib.core.model.random_projection import SparseRandomProjection
from anomalib.models.components.dimensionality_reduction import SparseRandomProjection


class KCenterGreedy:
Expand Down
20 changes: 20 additions & 0 deletions anomalib/models/components/stats/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Statistical functions."""

# Copyright (C) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from .kde import GaussianKDE
from .multi_variate_gaussian import MultiVariateGaussian

__all__ = ["GaussianKDE", "MultiVariateGaussian"]
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import torch
from torch import Tensor

from anomalib.core.model.dynamic_module import DynamicBufferModule
from anomalib.models.components.base import DynamicBufferModule


class GaussianKDE(DynamicBufferModule):
Expand Down
6 changes: 3 additions & 3 deletions anomalib/models/dfkde/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from omegaconf.listconfig import ListConfig
from torch import Tensor

from anomalib.core.model import AnomalyModule
from anomalib.core.model.feature_extractor import FeatureExtractor
from anomalib.models.dfkde.normality_model import NormalityModel
from anomalib.models.components import AnomalyModule, FeatureExtractor

from .normality_model import NormalityModel


class DfkdeLightning(AnomalyModule):
Expand Down
3 changes: 1 addition & 2 deletions anomalib/models/dfkde/normality_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import torch
from torch import Tensor, nn

from anomalib.core.model.kde import GaussianKDE
from anomalib.core.model.pca import PCA
from anomalib.models.components import PCA, GaussianKDE


class NormalityModel(nn.Module):
Expand Down
3 changes: 1 addition & 2 deletions anomalib/models/dfm/dfm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import torch
from torch import Tensor, nn

from anomalib.core.model.dynamic_module import DynamicBufferModule
from anomalib.core.model.pca import PCA
from anomalib.models.components import PCA, DynamicBufferModule


class SingleClassGaussian(DynamicBufferModule):
Expand Down
6 changes: 3 additions & 3 deletions anomalib/models/dfm/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
from omegaconf import DictConfig, ListConfig
from torch import Tensor

from anomalib.core.model import AnomalyModule
from anomalib.core.model.feature_extractor import FeatureExtractor
from anomalib.models.dfm.dfm_model import DFMModel
from anomalib.models.components import AnomalyModule, FeatureExtractor

from .dfm_model import DFMModel


class DfmLightning(AnomalyModule):
Expand Down
2 changes: 1 addition & 1 deletion anomalib/models/ganomaly/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from pytorch_lightning.callbacks import EarlyStopping
from torch import Tensor, nn, optim

from anomalib.core.model import AnomalyModule
from anomalib.models.components import AnomalyModule

from .torch_model import Discriminator, Generator

Expand Down
Loading

0 comments on commit d13e452

Please sign in to comment.