From d3ebced165b0949afa5c3eaee8cbd1a76d224dc3 Mon Sep 17 00:00:00 2001 From: Samet Akcay Date: Thu, 20 Jul 2023 14:40:12 +0100 Subject: [PATCH] Revert "Add default values to algorithms " (#1193) Revert "Add default values to algorithms (#1169)" This reverts commit 27876c851eb97a5b081b89a0059b7cbb2cad9688. --- CHANGELOG.md | 1 - src/anomalib/models/cfa/lightning_model.py | 14 +++++----- src/anomalib/models/cfa/torch_model.py | 28 +++++++++---------- src/anomalib/models/cflow/lightning_model.py | 6 ++-- src/anomalib/models/cflow/torch_model.py | 6 ++-- src/anomalib/models/csflow/lightning_model.py | 21 +++++++------- src/anomalib/models/csflow/torch_model.py | 15 +++++----- src/anomalib/models/dfkde/lightning_model.py | 9 +++--- src/anomalib/models/dfkde/torch_model.py | 14 ++++++---- src/anomalib/models/dfm/lightning_model.py | 19 ++++++------- src/anomalib/models/dfm/torch_model.py | 14 +++++----- .../models/efficient_ad/lightning_model.py | 9 +++--- .../models/efficient_ad/torch_model.py | 9 +++--- src/anomalib/models/fastflow/config.yaml | 10 +++---- .../models/fastflow/lightning_model.py | 18 ++++++------ src/anomalib/models/fastflow/torch_model.py | 18 ++++++------ .../models/ganomaly/lightning_model.py | 16 +++++------ src/anomalib/models/ganomaly/torch_model.py | 16 +++++------ src/anomalib/models/padim/config.yaml | 2 +- src/anomalib/models/padim/lightning_model.py | 17 ++++++----- src/anomalib/models/padim/torch_model.py | 16 +++++------ .../models/patchcore/lightning_model.py | 15 +++++----- src/anomalib/models/patchcore/torch_model.py | 10 +++++-- .../reverse_distillation/lightning_model.py | 26 ++++++++--------- .../reverse_distillation/torch_model.py | 23 ++++++++------- src/anomalib/models/stfpm/config.yaml | 2 +- src/anomalib/models/stfpm/lightning_model.py | 13 ++++----- src/anomalib/models/stfpm/torch_model.py | 6 ++-- 28 files changed, 186 insertions(+), 187 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a80831317e..e5b17e8d51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed -- Add default values to algorithms, based on the original papers. - Improve default settings of EfficientAD ### Deprecated diff --git a/src/anomalib/models/cfa/lightning_model.py b/src/anomalib/models/cfa/lightning_model.py index 4b3bd65333..0e9e983184 100644 --- a/src/anomalib/models/cfa/lightning_model.py +++ b/src/anomalib/models/cfa/lightning_model.py @@ -33,19 +33,19 @@ class Cfa(AnomalyModule): """CFA: Coupled-hypersphere-based Feature Adaptation for Target-Oriented Anomaly Localization. Args: - input_size (tuple[int, int], optional): Size of the model input. Defaults to (224, 224). - backbone (str, optional): Backbone CNN network. Defaults to "wide_resnet50_2". + input_size (tuple[int, int]): Size of the model input. + backbone (str): Backbone CNN network gamma_c (int, optional): gamma_c value from the paper. Defaults to 1. gamma_d (int, optional): gamma_d value from the paper. Defaults to 1. - num_nearest_neighbors (int, optional): Number of nearest neighbors. Defaults to 3. - num_hard_negative_features (int, optional): Number of hard negative features. Defaults to 3. - radius (float, optional): Radius of the hypersphere to search the soft boundary. Defaults to 1e-5. + num_nearest_neighbors (int): Number of nearest neighbors. + num_hard_negative_features (int): Number of hard negative features. + radius (float): Radius of the hypersphere to search the soft boundary. """ def __init__( self, - input_size: tuple[int, int] = (224, 224), - backbone: str = "wide_resnet50_2", + input_size: tuple[int, int], + backbone: str, gamma_c: int = 1, gamma_d: int = 1, num_nearest_neighbors: int = 3, diff --git a/src/anomalib/models/cfa/torch_model.py b/src/anomalib/models/cfa/torch_model.py index 4afe2b52cb..cebc5297dd 100644 --- a/src/anomalib/models/cfa/torch_model.py +++ b/src/anomalib/models/cfa/torch_model.py @@ -82,24 +82,24 @@ class CfaModel(DynamicBufferModule): """Torch implementation of the CFA Model. Args: - input_size: (tuple[int, int], optional): Input size of the image tensor. Defaults to (224, 224). - backbone (str, optional): Backbone CNN network. Defaults to "wide_resnet50_2". - gamma_c (int, optional): gamma_c parameter from the paper. Defaults to 1. - gamma_d (int, optional): gamma_d parameter from the paper. Defaults to 1. - num_nearest_neighbors (int, optional): Number of nearest neighbors. Defaults to 3. - num_hard_negative_features (int, optional): Number of hard negative features. Defaults to 3. - radius (float, optional): Radius of the hypersphere to search the soft boundary. Defaults to 1e-5. + input_size: (tuple[int, int]): Input size of the image tensor. + backbone (str): Backbone CNN network. + gamma_c (int): gamma_c parameter from the paper. + gamma_d (int): gamma_d parameter from the paper. + num_nearest_neighbors (int): Number of nearest neighbors. + num_hard_negative_features (int): Number of hard negative features. + radius (float): Radius of the hypersphere to search the soft boundary. """ def __init__( self, - input_size: tuple[int, int] = (224, 224), - backbone: str = "wide_resnet50_2", - gamma_c: int = 1, - gamma_d: int = 1, - num_nearest_neighbors: int = 3, - num_hard_negative_features: int = 3, - radius: float = 1e-5, + input_size: tuple[int, int], + backbone: str, + gamma_c: int, + gamma_d: int, + num_nearest_neighbors: int, + num_hard_negative_features: int, + radius: float, ) -> None: super().__init__() self.input_size = torch.Size(input_size) diff --git a/src/anomalib/models/cflow/lightning_model.py b/src/anomalib/models/cflow/lightning_model.py index a57371f6ac..f3b604b04f 100644 --- a/src/anomalib/models/cflow/lightning_model.py +++ b/src/anomalib/models/cflow/lightning_model.py @@ -29,9 +29,9 @@ class Cflow(AnomalyModule): def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer1", "layer2", "layer3"], + input_size: tuple[int, int], + backbone: str, + layers: list[str], pre_trained: bool = True, fiber_batch_size: int = 64, decoder: str = "freia-cflow", diff --git a/src/anomalib/models/cflow/torch_model.py b/src/anomalib/models/cflow/torch_model.py index ed7ea570db..2c518475d1 100644 --- a/src/anomalib/models/cflow/torch_model.py +++ b/src/anomalib/models/cflow/torch_model.py @@ -21,9 +21,9 @@ class CflowModel(nn.Module): def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer1", "layer2", "layer3"], + input_size: tuple[int, int], + backbone: str, + layers: list[str], pre_trained: bool = True, fiber_batch_size: int = 64, decoder: str = "freia-cflow", diff --git a/src/anomalib/models/csflow/lightning_model.py b/src/anomalib/models/csflow/lightning_model.py index eedb9b0927..6b829e712c 100644 --- a/src/anomalib/models/csflow/lightning_model.py +++ b/src/anomalib/models/csflow/lightning_model.py @@ -30,21 +30,20 @@ class Csflow(AnomalyModule): """Fully Convolutional Cross-Scale-Flows for Image-based Defect Detection. Args: - input_size (tuple[int, int], optional): Size of the model input. Defaults to (768, 768). - cross_conv_hidden_channels (int, optional): Number of hidden channels in the cross convolution. - Defaults to 1024. - n_coupling_blocks (int, optional): Number of coupling blocks in the model. Defaults to 4. - clamp (int, optional): Clamp value for glow layer. Defaults to 3. - num_channels (int, optional): Number of channels in the model. Defaults to 3. + input_size (tuple[int, int]): Size of the model input. + n_coupling_blocks (int): Number of coupling blocks in the model. + cross_conv_hidden_channels (int): Number of hidden channels in the cross convolution. + clamp (int): Clamp value for glow layer. + num_channels (int): Number of channels in the model. """ def __init__( self, - input_size: tuple[int, int] = (768, 768), - cross_conv_hidden_channels: int = 1024, - n_coupling_blocks: int = 4, - clamp: int = 3, - num_channels: int = 3, + input_size: tuple[int, int], + cross_conv_hidden_channels: int, + n_coupling_blocks: int, + clamp: int, + num_channels: int, ) -> None: super().__init__() self.model: CsFlowModel = CsFlowModel( diff --git a/src/anomalib/models/csflow/torch_model.py b/src/anomalib/models/csflow/torch_model.py index 467cfbc01a..05129c076d 100644 --- a/src/anomalib/models/csflow/torch_model.py +++ b/src/anomalib/models/csflow/torch_model.py @@ -504,18 +504,17 @@ class CsFlowModel(nn.Module): """CS Flow Module. Args: - input_size (tuple[int, int], optional): Size of the model input. Defaults to (768, 768). - cross_conv_hidden_channels (int, optional): Number of hidden channels in the cross convolution. - Defaults to 1024. - n_coupling_blocks (int, optional): Number of coupling blocks in the model. Defaults to 4. - clamp (int, optional): Clamp value for glow layer. Defaults to 3. - num_channels (int, optional): Number of channels in the model. Defaults to 3. + input_size (tuple[int, int]): Input image size. + cross_conv_hidden_channels (int): Number of hidden channels in the cross convolution. + n_coupling_blocks (int): Number of coupling blocks. + clamp (float): Clamp value for the coupling blocks. + num_channels (int): Number of channels in the input image. """ def __init__( self, - input_size: tuple[int, int] = (768, 768), - cross_conv_hidden_channels: int = 1024, + input_size: tuple[int, int], + cross_conv_hidden_channels: int, n_coupling_blocks: int = 4, clamp: int = 3, num_channels: int = 3, diff --git a/src/anomalib/models/dfkde/lightning_model.py b/src/anomalib/models/dfkde/lightning_model.py index 0ab9386cb1..0a63648fc6 100644 --- a/src/anomalib/models/dfkde/lightning_model.py +++ b/src/anomalib/models/dfkde/lightning_model.py @@ -24,9 +24,8 @@ class Dfkde(AnomalyModule): """DFKDE: Deep Feature Kernel Density Estimation. Args: - backbone (str, optional): Pre-trained model backbone. Defaults to "resnet18". - layers (list[str], optional): List of layers to extract features from. Defaults to ["layer4"]. - pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. Defaults to True. + backbone (str): Pre-trained model backbone. + pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. max_training_points (int, optional): Number of training points to fit the KDE model. Defaults to 40000. pre_processing (str, optional): Preprocess features before passing to KDE. @@ -39,8 +38,8 @@ class Dfkde(AnomalyModule): def __init__( self, - backbone: str = "resnet18", - layers: list[str] = ["layer4"], + layers: list[str], + backbone: str, pre_trained: bool = True, n_pca_components: int = 16, feature_scaling_method: FeatureScalingMethod = FeatureScalingMethod.SCALE, diff --git a/src/anomalib/models/dfkde/torch_model.py b/src/anomalib/models/dfkde/torch_model.py index 2c7996ac08..5746442820 100644 --- a/src/anomalib/models/dfkde/torch_model.py +++ b/src/anomalib/models/dfkde/torch_model.py @@ -12,7 +12,10 @@ from torch import Tensor, nn from anomalib.models.components import FeatureExtractor -from anomalib.models.components.classification import FeatureScalingMethod, KDEClassifier +from anomalib.models.components.classification import ( + FeatureScalingMethod, + KDEClassifier, +) logger = logging.getLogger(__name__) @@ -21,9 +24,8 @@ class DfkdeModel(nn.Module): """Normality Model for the DFKDE algorithm. Args: - backbone (str, optional): Pre-trained model backbone. Defaults to "resnet18". - layers (list[str], optional): List of layers to extract features from. Defaults to ["layer4"]. - pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. Defaults to True. + backbone (str): Pre-trained model backbone. + pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. n_comps (int, optional): Number of PCA components. Defaults to 16. pre_processing (str, optional): Preprocess features before passing to KDE. Options are between `norm` and `scale`. Defaults to "scale". @@ -34,8 +36,8 @@ class DfkdeModel(nn.Module): def __init__( self, - backbone: str = "resnet18", - layers: list[str] = ["layer4"], + layers: list[str], + backbone: str, pre_trained: bool = True, n_pca_components: int = 16, feature_scaling_method: FeatureScalingMethod = FeatureScalingMethod.SCALE, diff --git a/src/anomalib/models/dfm/lightning_model.py b/src/anomalib/models/dfm/lightning_model.py index ba70b0b7a5..4910f4b041 100644 --- a/src/anomalib/models/dfm/lightning_model.py +++ b/src/anomalib/models/dfm/lightning_model.py @@ -23,25 +23,24 @@ class Dfm(AnomalyModule): """DFM: Deep Featured Kernel Density Estimation. Args: - input_size (tuple[int, int], optional): Input size for the model. Defaults to (256, 256). - backbone (str, optional): Backbone CNN network. Defaults to "resnet50". - layer (str, optional): Layer to extract features from the backbone CNN. Defaults to "layer3". + backbone (str): Backbone CNN network + layer (str): Layer to extract features from the backbone CNN + input_size (tuple[int, int]): Input size for the model. pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. pooling_kernel_size (int, optional): Kernel size to pool features extracted from the CNN. Defaults to 4. pca_level (float, optional): Ratio from which number of components for PCA are calculated. Defaults to 0.97. - score_type (str, optional): Scoring type. Options are `fre` and `nll`. - nll: for Gaussian modeling, fre: pca feature-reconstruction error. Anomaly segmentation is - supported with `fre` only. If using `nll`, set `task` in config.yaml to classification - Defaults to "fre". + score_type (str, optional): Scoring type. Options are `fre` and `nll`. Defaults to "fre". + nll: for Gaussian modeling, fre: pca feature-reconstruction error. Anomaly segmentation is + supported with `fre` only. If using `nll`, set `task` in config.yaml to classification """ def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "resnet50", - layer: str = "layer3", + backbone: str, + layer: str, + input_size: tuple[int, int], pre_trained: bool = True, pooling_kernel_size: int = 4, pca_level: float = 0.97, diff --git a/src/anomalib/models/dfm/torch_model.py b/src/anomalib/models/dfm/torch_model.py index 5c9ab47f04..05a61c37c8 100644 --- a/src/anomalib/models/dfm/torch_model.py +++ b/src/anomalib/models/dfm/torch_model.py @@ -75,9 +75,9 @@ class DFMModel(nn.Module): """Model for the DFM algorithm. Args: - input_size (tuple[int, int], optional): Input size for the model. Defaults to (256, 256). - backbone (str, optional): Backbone CNN network. Defaults to "resnet50". - layer (str, optional): Layer to extract features from the backbone CNN. Defaults to "layer3". + backbone (str): Pre-trained model backbone. + layer (str): Layer from which to extract features. + input_size (tuple[int, int]): Input size for the model. pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. pooling_kernel_size (int, optional): Kernel size to pool features extracted from the CNN. n_comps (float, optional): Ratio from which number of components for PCA are calculated. Defaults to 0.97. @@ -87,14 +87,14 @@ class DFMModel(nn.Module): def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "resnet50", - layer: str = "layer3", + backbone: str, + layer: str, + input_size: tuple[int, int], pre_trained: bool = True, pooling_kernel_size: int = 4, n_comps: float = 0.97, score_type: str = "fre", - ) -> None: + ): super().__init__() self.backbone = backbone self.pooling_kernel_size = pooling_kernel_size diff --git a/src/anomalib/models/efficient_ad/lightning_model.py b/src/anomalib/models/efficient_ad/lightning_model.py index e446f520a1..0927abed35 100644 --- a/src/anomalib/models/efficient_ad/lightning_model.py +++ b/src/anomalib/models/efficient_ad/lightning_model.py @@ -53,8 +53,9 @@ class EfficientAd(AnomalyModule): """PL Lightning Module for the EfficientAd algorithm. Args: - image_size (tuple): size of input images. Defaults to (256, 256). - teacher_out_channels (int): number of convolution output channels. Defaults to 384. + teacher_file_name (str): path to the pre-trained teacher model + teacher_out_channels (int): number of convolution output channels + image_size (tuple): size of input images model_size (str): size of student and teacher model lr (float): learning rate weight_decay (float): optimizer weight decay @@ -66,8 +67,8 @@ class EfficientAd(AnomalyModule): def __init__( self, - image_size: tuple[int, int] = (256, 256), - teacher_out_channels: int = 384, + teacher_out_channels: int, + image_size: tuple[int, int], model_size: EfficientAdModelSize = EfficientAdModelSize.S, lr: float = 0.0001, weight_decay: float = 0.00001, diff --git a/src/anomalib/models/efficient_ad/torch_model.py b/src/anomalib/models/efficient_ad/torch_model.py index 6370a1478f..13aa56cdad 100644 --- a/src/anomalib/models/efficient_ad/torch_model.py +++ b/src/anomalib/models/efficient_ad/torch_model.py @@ -188,8 +188,9 @@ class EfficientAdModel(nn.Module): """EfficientAd model. Args: - input_size (tuple): size of input images. Default: (256, 256) - teacher_out_channels (int): number of convolution output channels of the pre-trained teacher model. Default: 384 + teacher_out_channels (int): number of convolution output channels of the pre-trained teacher model + pretrained_models_dir (str): path to the pretrained model weights + input_size (tuple): size of input images model_size (str): size of student and teacher model padding (bool): use padding in convoluional layers pad_maps (bool): relevant if padding is set to False. In this case, pad_maps = True pads the @@ -199,8 +200,8 @@ class EfficientAdModel(nn.Module): def __init__( self, - input_size: tuple[int, int] = (256, 256), - teacher_out_channels: int = 384, + teacher_out_channels: int, + input_size: tuple[int, int], model_size: EfficientAdModelSize = EfficientAdModelSize.S, padding=False, pad_maps=True, diff --git a/src/anomalib/models/fastflow/config.yaml b/src/anomalib/models/fastflow/config.yaml index 48ddbebbb8..525b6a3412 100644 --- a/src/anomalib/models/fastflow/config.yaml +++ b/src/anomalib/models/fastflow/config.yaml @@ -7,7 +7,7 @@ dataset: train_batch_size: 32 eval_batch_size: 32 num_workers: 8 - image_size: 384 # dimensions to which images are resized (mandatory) options: [256, 256, 448, 384] + image_size: 256 # dimensions to which images are resized (mandatory) options: [256, 256, 448, 384] center_crop: null # dimensions to which images are center-cropped after resizing (optional) normalization: imagenet # data distribution to which the images will be normalized: [none, imagenet] transform_config: @@ -27,11 +27,11 @@ dataset: model: name: fastflow - backbone: deit_base_distilled_patch16_384 # options: [resnet18, wide_resnet50_2, cait_m48_448, deit_base_distilled_patch16_384] + backbone: resnet18 # options: [resnet18, wide_resnet50_2, cait_m48_448, deit_base_distilled_patch16_384] pre_trained: true - flow_steps: 20 # options: [8, 8, 20, 20] - for each supported backbone - hidden_ratio: 0.16 # options: [1.0, 1.0, 0.16, 0.16] - for each supported backbone - conv3x3_only: False # options: [True, False, False, False] - for each supported backbone + flow_steps: 8 # options: [8, 8, 20, 20] - for each supported backbone + hidden_ratio: 1.0 # options: [1.0, 1.0, 0.16, 0.16] - for each supported backbone + conv3x3_only: True # options: [True, False, False, False] - for each supported backbone lr: 0.001 weight_decay: 0.00001 early_stopping: diff --git a/src/anomalib/models/fastflow/lightning_model.py b/src/anomalib/models/fastflow/lightning_model.py index 80a839f576..c2d203b926 100644 --- a/src/anomalib/models/fastflow/lightning_model.py +++ b/src/anomalib/models/fastflow/lightning_model.py @@ -20,22 +20,22 @@ class Fastflow(AnomalyModule): """PL Lightning Module for the FastFlow algorithm. Args: - input_size (tuple[int, int]): Model input size. Defaults to (384, 384). - backbone (str): Backbone CNN network. Defaults to "deit_base_distilled_patch16_384". - pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. Defaults to True. - flow_steps (int, optional): Flow steps. Defaults to 20. - hidden_ratio (float, optional): Ratio to calculate hidden var channels. Defaults to 0.16. + input_size (tuple[int, int]): Model input size. + backbone (str): Backbone CNN network + pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. + flow_steps (int, optional): Flow steps. conv3x3_only (bool, optinoal): Use only conv3x3 in fast_flow model. Defaults to False. + hidden_ratio (float, optional): Ratio to calculate hidden var channels. Defaults to 1.0. """ def __init__( self, - input_size: tuple[int, int] = (384, 384), - backbone: str = "deit_base_distilled_patch16_384", + input_size: tuple[int, int], + backbone: str, pre_trained: bool = True, - flow_steps: int = 20, - hidden_ratio: float = 0.16, + flow_steps: int = 8, conv3x3_only: bool = False, + hidden_ratio: float = 1.0, ) -> None: super().__init__() diff --git a/src/anomalib/models/fastflow/torch_model.py b/src/anomalib/models/fastflow/torch_model.py index 4edbeb8b93..1fbeba9564 100644 --- a/src/anomalib/models/fastflow/torch_model.py +++ b/src/anomalib/models/fastflow/torch_model.py @@ -98,12 +98,12 @@ class FastflowModel(nn.Module): Unsupervised Anomaly Detection and Localization via 2D Normalizing Flows. Args: - input_size (tuple[int, int]): Model input size. Defaults to (384, 384). - backbone (str): Backbone CNN network. Defaults to "deit_base_distilled_patch16_384". - pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. Defaults to True. - flow_steps (int, optional): Flow steps. Defaults to 20. - hidden_ratio (float, optional): Ratio to calculate hidden var channels. Defaults to 0.16. + input_size (tuple[int, int]): Model input size. + backbone (str): Backbone CNN network + pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. + flow_steps (int, optional): Flow steps. conv3x3_only (bool, optinoal): Use only conv3x3 in fast_flow model. Defaults to False. + hidden_ratio (float, optional): Ratio to calculate hidden var channels. Defaults to 1.0. Raises: ValueError: When the backbone is not supported. @@ -111,12 +111,12 @@ class FastflowModel(nn.Module): def __init__( self, - input_size: tuple[int, int] = (384, 384), - backbone: str = "deit_base_distilled_patch16_384", + input_size: tuple[int, int], + backbone: str, pre_trained: bool = True, - flow_steps: int = 20, - hidden_ratio: float = 0.16, + flow_steps: int = 8, conv3x3_only: bool = False, + hidden_ratio: float = 1.0, ) -> None: super().__init__() diff --git a/src/anomalib/models/ganomaly/lightning_model.py b/src/anomalib/models/ganomaly/lightning_model.py index 9f8622e636..d8153fd6a5 100644 --- a/src/anomalib/models/ganomaly/lightning_model.py +++ b/src/anomalib/models/ganomaly/lightning_model.py @@ -28,10 +28,10 @@ class Ganomaly(AnomalyModule): """PL Lightning Module for the GANomaly Algorithm. Args: - batch_size (int, optional): Batch size. Defaults to 32. - input_size (tuple[int, int], optional): Input dimension. Defaults to (32, 32). - n_features (int, optional): Number of features layers in the CNNs. Defaults to 64. - latent_vec_size (int, optional): Size of autoencoder latent vector. Defaults to 100. + batch_size (int): Batch size. + input_size (tuple[int, int]): Input dimension. + n_features (int): Number of features layers in the CNNs. + latent_vec_size (int): Size of autoencoder latent vector. extra_layers (int, optional): Number of extra layers for encoder/decoder. Defaults to 0. add_final_conv_layer (bool, optional): Add convolution layer at the end. Defaults to True. wadv (int, optional): Weight for adversarial loss. Defaults to 1. @@ -41,10 +41,10 @@ class Ganomaly(AnomalyModule): def __init__( self, - batch_size: int = 32, - input_size: tuple[int, int] = (32, 32), - n_features: int = 64, - latent_vec_size: int = 100, + batch_size: int, + input_size: tuple[int, int], + n_features: int, + latent_vec_size: int, extra_layers: int = 0, add_final_conv_layer: bool = True, wadv: int = 1, diff --git a/src/anomalib/models/ganomaly/torch_model.py b/src/anomalib/models/ganomaly/torch_model.py index 07d58704b5..bc707e9ab7 100644 --- a/src/anomalib/models/ganomaly/torch_model.py +++ b/src/anomalib/models/ganomaly/torch_model.py @@ -281,20 +281,20 @@ class GanomalyModel(nn.Module): """Ganomaly Model. Args: - input_size (tuple[int, int], optional): Input dimension. Defaults to (32, 32). - num_input_channels (int, optional): Number of input channels. Defaults to 3. - n_features (int, optional): Number of features layers in the CNNs. Defaults to 64. - latent_vec_size (int, optional): Size of autoencoder latent vector. Defaults to 100. + input_size (tuple[int, int]): Input dimension. + num_input_channels (int): Number of input channels. + n_features (int): Number of features layers in the CNNs. + latent_vec_size (int): Size of autoencoder latent vector. extra_layers (int, optional): Number of extra layers for encoder/decoder. Defaults to 0. add_final_conv_layer (bool, optional): Add convolution layer at the end. Defaults to True. """ def __init__( self, - input_size: tuple[int, int] = (32, 32), - num_input_channels: int = 3, - n_features: int = 64, - latent_vec_size: int = 100, + input_size: tuple[int, int], + num_input_channels: int, + n_features: int, + latent_vec_size: int, extra_layers: int = 0, add_final_conv_layer: bool = True, ) -> None: diff --git a/src/anomalib/models/padim/config.yaml b/src/anomalib/models/padim/config.yaml index 1cde9d4812..84b0d2d421 100644 --- a/src/anomalib/models/padim/config.yaml +++ b/src/anomalib/models/padim/config.yaml @@ -27,7 +27,7 @@ dataset: model: name: padim - backbone: wide_resnet50_2 + backbone: resnet18 pre_trained: true layers: - layer1 diff --git a/src/anomalib/models/padim/lightning_model.py b/src/anomalib/models/padim/lightning_model.py index b97b1543e6..3e71163422 100644 --- a/src/anomalib/models/padim/lightning_model.py +++ b/src/anomalib/models/padim/lightning_model.py @@ -27,20 +27,19 @@ class Padim(AnomalyModule): """PaDiM: a Patch Distribution Modeling Framework for Anomaly Detection and Localization. Args: - input_size (tuple[int, int], optional): Size of the model input. Default: (256, 256) - backbone (str, optional): Backbone CNN network. Default: "wide_resnet50_2" - layers (list[str], optional): Layers to extract features from the backbone CNN. - Default: ["layer1", "layer2", "layer3"] - pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. Default: True + layers (list[str]): Layers to extract features from the backbone CNN + input_size (tuple[int, int]): Size of the model input. + backbone (str): Backbone CNN network + pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. n_features (int, optional): Number of features to retain in the dimension reduction step. - Default values from the paper are available for: resnet18 (100), wide_resnet50_2 (550). + Default values from the paper are available for: resnet18 (100), wide_resnet50_2 (550). """ def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer1", "layer2", "layer3"], + layers: list[str], + input_size: tuple[int, int], + backbone: str, pre_trained: bool = True, n_features: int | None = None, ) -> None: diff --git a/src/anomalib/models/padim/torch_model.py b/src/anomalib/models/padim/torch_model.py index f481a4f49c..702a788bfb 100644 --- a/src/anomalib/models/padim/torch_model.py +++ b/src/anomalib/models/padim/torch_model.py @@ -50,19 +50,19 @@ class PadimModel(nn.Module): """Padim Module. Args: - input_size (tuple[int, int] optional): Input size for the model. Defaults to (256, 256). - backbone (str, optional): Pre-trained model backbone. Defaults to "wide_resnet50_2". - layers (list[str], optional): Layers used for feature extraction. Defaults to ["layer1", "layer2", "layer3"]. - pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. Defaults to True. + input_size (tuple[int, int]): Input size for the model. + layers (list[str]): Layers used for feature extraction + backbone (str, optional): Pre-trained model backbone. Defaults to "resnet18". + pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. n_features (int, optional): Number of features to retain in the dimension reduction step. - Default values from the paper are available for: resnet18 (100), wide_resnet50_2 (550). + Default values from the paper are available for: resnet18 (100), wide_resnet50_2 (550). """ def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer1", "layer2", "layer3"], + input_size: tuple[int, int], + layers: list[str], + backbone: str = "resnet18", pre_trained: bool = True, n_features: int | None = None, ) -> None: diff --git a/src/anomalib/models/patchcore/lightning_model.py b/src/anomalib/models/patchcore/lightning_model.py index c3d95edac6..fbdb8949a2 100644 --- a/src/anomalib/models/patchcore/lightning_model.py +++ b/src/anomalib/models/patchcore/lightning_model.py @@ -25,11 +25,10 @@ class Patchcore(AnomalyModule): """PatchcoreLightning Module to train PatchCore algorithm. Args: - input_size (tuple[int, int], optional): Size of the model input. Defaults to (224, 224). - backbone (str, optional): Backbone CNN network. Defaults to "wide_resnet50_2". - layers (list[str], optional): Layers to extract features from the backbone CNN. - Defaults to ["layer2", "layer3"]. - pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. Defaults to True. + input_size (tuple[int, int]): Size of the model input. + backbone (str): Backbone CNN network + layers (list[str]): Layers to extract features from the backbone CNN + pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. coreset_sampling_ratio (float, optional): Coreset sampling ratio to subsample embedding. Defaults to 0.1. num_neighbors (int, optional): Number of nearest neighbors. Defaults to 9. @@ -37,9 +36,9 @@ class Patchcore(AnomalyModule): def __init__( self, - input_size: tuple[int, int] = (224, 224), - backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer2", "layer3"], + input_size: tuple[int, int], + backbone: str, + layers: list[str], pre_trained: bool = True, coreset_sampling_ratio: float = 0.1, num_neighbors: int = 9, diff --git a/src/anomalib/models/patchcore/torch_model.py b/src/anomalib/models/patchcore/torch_model.py index e269dd0720..7f4f11f5fc 100644 --- a/src/anomalib/models/patchcore/torch_model.py +++ b/src/anomalib/models/patchcore/torch_model.py @@ -9,7 +9,11 @@ import torch.nn.functional as F from torch import Tensor, nn -from anomalib.models.components import DynamicBufferModule, FeatureExtractor, KCenterGreedy +from anomalib.models.components import ( + DynamicBufferModule, + FeatureExtractor, + KCenterGreedy, +) from anomalib.models.patchcore.anomaly_map import AnomalyMapGenerator from anomalib.pre_processing import Tiler @@ -19,9 +23,9 @@ class PatchcoreModel(DynamicBufferModule, nn.Module): def __init__( self, - input_size: tuple[int, int] = (224, 224), + input_size: tuple[int, int], + layers: list[str], backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer2", "layer3"], pre_trained: bool = True, num_neighbors: int = 9, ) -> None: diff --git a/src/anomalib/models/reverse_distillation/lightning_model.py b/src/anomalib/models/reverse_distillation/lightning_model.py index 2a6fae1826..5489daab5b 100644 --- a/src/anomalib/models/reverse_distillation/lightning_model.py +++ b/src/anomalib/models/reverse_distillation/lightning_model.py @@ -24,26 +24,22 @@ class ReverseDistillation(AnomalyModule): """PL Lightning Module for Reverse Distillation Algorithm. Args: - input_size (tuple[int, int], optional): Size of model input. Defaults to (256, 256). - backbone (str, optional): Backbone of CNN network. Defaults to "wide_resnet50_2". - layers (list[str], optional): Layers to extract features from the backbone CNN. - Defaults to ["layer1", "layer2", "layer3"]. - pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. Defaults to True. - lr (float, optional): Learning rate for the optimizer. Defaults to 0.005. - beta1 (float, optional): Beta1 for the optimizer. Defaults to 0.5. - beta2 (float, optional): Beta2 for the optimizer. Defaults to 0.99. + input_size (tuple[int, int]): Size of model input + backbone (str): Backbone of CNN network + layers (list[str]): Layers to extract features from the backbone CNN + pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. """ def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer1", "layer2", "layer3"], - anomaly_map_mode: AnomalyMapGenerationMode = AnomalyMapGenerationMode.MULTIPLY, + input_size: tuple[int, int], + backbone: str, + layers: list[str], + anomaly_map_mode: AnomalyMapGenerationMode, + lr: float, + beta1: float, + beta2: float, pre_trained: bool = True, - lr: float = 0.005, - beta1: float = 0.5, - beta2: float = 0.99, ) -> None: super().__init__() self.model = ReverseDistillationModel( diff --git a/src/anomalib/models/reverse_distillation/torch_model.py b/src/anomalib/models/reverse_distillation/torch_model.py index 178fee9ed1..5ada84573e 100644 --- a/src/anomalib/models/reverse_distillation/torch_model.py +++ b/src/anomalib/models/reverse_distillation/torch_model.py @@ -9,7 +9,10 @@ from anomalib.models.components import FeatureExtractor from anomalib.models.reverse_distillation.anomaly_map import AnomalyMapGenerator -from anomalib.models.reverse_distillation.components import get_bottleneck_layer, get_decoder +from anomalib.models.reverse_distillation.components import ( + get_bottleneck_layer, + get_decoder, +) from anomalib.pre_processing import Tiler from .anomaly_map import AnomalyMapGenerationMode @@ -19,19 +22,19 @@ class ReverseDistillationModel(nn.Module): """Reverse Distillation Model. Args: - input_size (tuple[int, int], optional): Size of model input. Defaults to (256, 256). - backbone (str, optional): Backbone of CNN network. Defaults to "wide_resnet50_2". - layers (list[str], optional): Layers to extract features from the backbone CNN. - Defaults to ["layer1", "layer2", "layer3"]. - pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. Defaults to True. + backbone (str): Name of the backbone used for encoder and decoder + input_size (tuple[int, int]): Size of input image + layers (list[str]): Name of layers from which the features are extracted. + anomaly_map_mode (str): Mode used to generate anomaly map. Options are between ``multiply`` and ``add``. + pre_trained (bool, optional): Boolean to check whether to use a pre_trained backbone. """ def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer1", "layer2", "layer3"], - anomaly_map_mode: AnomalyMapGenerationMode = AnomalyMapGenerationMode.MULTIPLY, + backbone: str, + input_size: tuple[int, int], + layers: list[str], + anomaly_map_mode: AnomalyMapGenerationMode, pre_trained: bool = True, ) -> None: super().__init__() diff --git a/src/anomalib/models/stfpm/config.yaml b/src/anomalib/models/stfpm/config.yaml index 6c1616300c..1f92f3a187 100644 --- a/src/anomalib/models/stfpm/config.yaml +++ b/src/anomalib/models/stfpm/config.yaml @@ -28,7 +28,7 @@ dataset: model: name: stfpm - backbone: wide_resnet50_2 + backbone: resnet18 layers: - layer1 - layer2 diff --git a/src/anomalib/models/stfpm/lightning_model.py b/src/anomalib/models/stfpm/lightning_model.py index 6383138536..e05f0aa783 100644 --- a/src/anomalib/models/stfpm/lightning_model.py +++ b/src/anomalib/models/stfpm/lightning_model.py @@ -25,17 +25,16 @@ class Stfpm(AnomalyModule): """PL Lightning Module for the STFPM algorithm. Args: - input_size (tuple[int, int], optional): Size of the model input. Defaults to (256, 256). - backbone (str, optional): Backbone CNN network. Defaults to "wide_resnet50_2". - layers (list[str], optional): Layers to extract features from the backbone CNN. - Defaults to ["layer1", "layer2", "layer3"]. + input_size (tuple[int, int]): Size of the model input. + backbone (str): Backbone CNN network + layers (list[str]): Layers to extract features from the backbone CNN """ def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer1", "layer2", "layer3"], + input_size: tuple[int, int], + backbone: str, + layers: list[str], ) -> None: super().__init__() diff --git a/src/anomalib/models/stfpm/torch_model.py b/src/anomalib/models/stfpm/torch_model.py index 7235e0d40e..a730c1fe50 100644 --- a/src/anomalib/models/stfpm/torch_model.py +++ b/src/anomalib/models/stfpm/torch_model.py @@ -23,9 +23,9 @@ class STFPMModel(nn.Module): def __init__( self, - input_size: tuple[int, int] = (256, 256), - backbone: str = "wide_resnet50_2", - layers: list[str] = ["layer1", "layer2", "layer3"], + layers: list[str], + input_size: tuple[int, int], + backbone: str = "resnet18", ) -> None: super().__init__() self.tiler: Tiler | None = None