sinapsis-anomalib 0.1.7__py3-none-any.whl → 0.1.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sinapsis-anomalib might be problematic. Click here for more details.

@@ -0,0 +1,81 @@
1
+ from collections.abc import Sequence
2
+ from pathlib import Path
3
+ from typing import Literal
4
+
5
+ from anomalib.data.utils import TestSplitMode, ValSplitMode
6
+ from pydantic import BaseModel, ConfigDict
7
+
8
+
9
+ class FolderConfig(BaseModel):
10
+ """Configuration for the Anomalib Folder datamodule.
11
+
12
+ This model defines parameters for loading image data from a directory structure.
13
+ It is flexible and allows any other valid `Folder` arguments
14
+ (like `train_augmentations`) to be passed through.
15
+
16
+ Attributes:
17
+ name (str): A descriptive name for the dataset.
18
+ root (str | Path | None): The root directory where the dataset is located.
19
+ normal_dir (str | Path | Sequence[str | Path]): Path to the directory containing normal images.
20
+ abnormal_dir (str | Path | Sequence[str | Path] | None): Path to the directory for abnormal images.
21
+ train_batch_size (int): The number of samples per batch for training. Defaults to 32.
22
+ eval_batch_size (int): The number of samples per batch for evaluation. Defaults to 32.
23
+ num_workers (int): The number of subprocesses to use for data loading. Defaults to 8.
24
+ seed (int | None): A random seed for reproducibility in data splitting.
25
+ """
26
+
27
+ name: str
28
+ root: str | Path | None = None
29
+ normal_dir: str | Path | Sequence[str | Path]
30
+ abnormal_dir: str | Path | Sequence[str | Path] | None = None
31
+ normal_test_dir: str | Path | Sequence[str | Path] | None = None
32
+ mask_dir: str | Path | Sequence[str | Path] | None = None
33
+ normal_split_ratio: float = 0.2
34
+ extensions: tuple[str] | None = None
35
+ train_batch_size: int = 32
36
+ eval_batch_size: int = 32
37
+ num_workers: int = 8
38
+ test_split_mode: TestSplitMode | str = TestSplitMode.FROM_DIR
39
+ test_split_ratio: float = 0.2
40
+ val_split_mode: ValSplitMode | str = ValSplitMode.FROM_TEST
41
+ val_split_ratio: float = 0.5
42
+ seed: int | None = None
43
+ model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True)
44
+
45
+
46
+ class TrainerConfig(BaseModel):
47
+ """Defines and validates parameters for the Trainer.
48
+
49
+ This model is flexible and allows any other valid Trainer arguments to be passed.
50
+
51
+ Attributes:
52
+ devices (int | list[int] | str | None): The devices to use for training (e.g., 1, [0, 1], "auto").
53
+ Defaults to "auto".
54
+ accelerator (Literal["gpu", "cpu", "auto"]): The hardware accelerator to use.
55
+ Defaults to "auto".
56
+ min_epochs (int): The minimum number of epochs to train for. Defaults to 1.
57
+ max_epochs (int): The maximum number of epochs to train for. Defaults to 1000.
58
+ """
59
+
60
+ devices: int | list[int] | str | None = "auto"
61
+ accelerator: Literal["cpu", "gpu", "tpu", "hpu", "auto"] = "cpu"
62
+ min_epochs: int = 1
63
+ max_epochs: int | None = None
64
+ model_config = ConfigDict(extra="allow")
65
+
66
+
67
+ class OpenVINOArgs(BaseModel):
68
+ """Defines and validates parameters for OpenVINO model export.
69
+
70
+ This model is flexible and allows any other valid arguments for `openvino.save_model`.
71
+ For a full list of options, see the OpenVINO documentation.
72
+
73
+ Attributes:
74
+ compress_to_fp16 (bool | None): If True, compresses the model weights to FP16 precision.
75
+ Defaults to None.
76
+ compress_to_fp16 (bool | None): Print detailed information about conversion.
77
+ """
78
+
79
+ compress_to_fp16: bool | None = None
80
+ verbose: bool = False
81
+ model_config = ConfigDict(extra="allow")
@@ -23,6 +23,7 @@ from sinapsis_core.template_base.base_models import (
23
23
  from sinapsis_core.template_base.dynamic_template import BaseDynamicWrapperTemplate, WrapperEntryConfig
24
24
 
25
25
  from sinapsis_anomalib.helpers.config_factory import CallbackFactory, LoggerFactory
26
+ from sinapsis_anomalib.helpers.configs import FolderConfig
26
27
  from sinapsis_anomalib.helpers.tags import Tags
27
28
 
28
29
  EXCLUDED_MODELS = [
@@ -117,7 +118,7 @@ class AnomalibBaseAttributes(TemplateAttributes):
117
118
  """Base attributes for Anomalib model templates.
118
119
 
119
120
  Attributes:
120
- folder_attributes (dict[str, Any]): Configuration for Folder datamodule. Required for training, optional
121
+ folder_attributes (FolderConfig): Configuration for Folder datamodule. Required for training, optional
121
122
  for export.
122
123
  callbacks (list[Callback] | None): Lightning callbacks
123
124
  normalization (NORMALIZATION | None): Input normalization
@@ -130,7 +131,7 @@ class AnomalibBaseAttributes(TemplateAttributes):
130
131
  logger_configs (dict[str, dict[str, Any]] | None): Logger configs
131
132
  """
132
133
 
133
- folder_attributes: dict[str, Any] | None = None
134
+ folder_attributes: FolderConfig | None = None
134
135
  callbacks: list[Callback] | None = None
135
136
  normalization: NORMALIZATION | None = None
136
137
  threshold: THRESHOLD | None = None
@@ -209,7 +210,9 @@ class AnomalibBase(BaseDynamicWrapperTemplate):
209
210
  Returns:
210
211
  Folder: Configured data module instance
211
212
  """
212
- return Folder(**self.attributes.folder_attributes)
213
+ if self.attributes.folder_attributes is None:
214
+ raise ValueError("'folder_attributes' must be provided to set up the data loader.")
215
+ return Folder(**self.attributes.folder_attributes.model_dump(exclude_none=True))
213
216
 
214
217
  @abstractmethod
215
218
  def execute(self, container: DataContainer) -> DataContainer:
@@ -225,4 +228,4 @@ class AnomalibBase(BaseDynamicWrapperTemplate):
225
228
  def reset_state(self, template_name: str | None = None) -> None:
226
229
  if torch.cuda.is_available():
227
230
  torch.cuda.empty_cache()
228
- super().reset_state(template_name)
231
+ super().reset_state(template_name)
@@ -1,6 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  from pathlib import Path
3
- from typing import Any
4
3
 
5
4
  from anomalib.deploy import CompressionType, ExportType
6
5
  from pydantic.dataclasses import dataclass
@@ -11,6 +10,7 @@ from sinapsis_core.template_base.dynamic_template_factory import make_dynamic_te
11
10
  from sinapsis_core.utils.env_var_keys import SINAPSIS_BUILD_DOCS
12
11
  from torchmetrics import Metric
13
12
 
13
+ from sinapsis_anomalib.helpers.configs import OpenVINOArgs
14
14
  from sinapsis_anomalib.helpers.tags import Tags
15
15
  from sinapsis_anomalib.templates.anomalib_base import (
16
16
  AnomalibBase,
@@ -53,7 +53,7 @@ class AnomalibExportAttributes(AnomalibBaseAttributes):
53
53
  input_size: tuple[int, int] | None = None
54
54
  compression_type: CompressionType | None = None
55
55
  metric: Metric | str | None = None
56
- ov_args: dict[str, Any] | None = None
56
+ ov_args: OpenVINOArgs | None = None
57
57
  ckpt_path: str | None = None
58
58
  generic_key_chkpt: str | None = None
59
59
 
@@ -123,6 +123,11 @@ class AnomalibExport(AnomalibBase):
123
123
  CompressionType.INT8_ACQ,
124
124
  CompressionType.INT8_PTQ,
125
125
  ):
126
+ if self.attributes.folder_attributes is None:
127
+ raise ValueError(
128
+ f"Compression type '{self.attributes.compression_type.value}' requires "
129
+ "'folder_attributes' to be provided."
130
+ )
126
131
  self.data_module = self.setup_data_loader()
127
132
 
128
133
  def _get_checkpoint_path(self, container: DataContainer) -> str | Path:
@@ -170,7 +175,7 @@ class AnomalibExport(AnomalibBase):
170
175
  compression_type=self.attributes.compression_type,
171
176
  datamodule=self.data_module,
172
177
  metric=self.attributes.metric,
173
- ov_args=self.attributes.ov_args,
178
+ ov_args=self.attributes.ov_args.model_dump(exclude_none=True) if self.attributes.ov_args else None,
174
179
  ckpt_path=ckpt_path,
175
180
  )
176
181
 
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  from pathlib import Path
3
- from typing import Any, Literal
3
+ from typing import Any
4
4
 
5
5
  from anomalib.engine.engine import _TrainerArgumentsCache
6
6
  from pydantic import Field
@@ -11,6 +11,7 @@ from sinapsis_core.template_base.base_models import TemplateAttributeType
11
11
  from sinapsis_core.template_base.dynamic_template_factory import make_dynamic_template
12
12
  from sinapsis_core.utils.env_var_keys import SINAPSIS_BUILD_DOCS
13
13
 
14
+ from sinapsis_anomalib.helpers.configs import TrainerConfig
14
15
  from sinapsis_anomalib.helpers.tags import Tags
15
16
  from sinapsis_anomalib.templates.anomalib_base import (
16
17
  AnomalibBase,
@@ -59,20 +60,14 @@ class AnomalibTrainAttributes(AnomalibBaseAttributes):
59
60
  """Training-specific configuration attributes.
60
61
 
61
62
  Attributes:
62
- max_epochs (int | None): Maximum number of training epochs.
63
- If None, uses default from model configuration.
64
- accelerator: (Literal["cpu", "gpu", "tpu", "hpu", "auto"]): Define the device to be used during training.
65
- Defaults to "cpu".
66
63
  ckpt_path (str | Path | None): Path to checkpoint for resuming training.
67
64
  If None, starts training from scratch.
68
- trainer_args: (dict[str, Any]): General trainer arguments. For more details see:
65
+ trainer_args: (TrainerConfig): General trainer arguments. For more details see:
69
66
  https://lightning.ai/docs/pytorch/stable/common/trainer.html#trainer-flags
70
67
  """
71
68
 
72
- max_epochs: int | None = None
73
- accelerator: Literal["cpu", "gpu", "tpu", "hpu", "auto"] = "cpu"
74
69
  ckpt_path: str | Path | None = None
75
- trainer_args: dict[str, Any] = Field(default_factory=dict)
70
+ trainer_args: TrainerConfig = Field(default_factory=TrainerConfig)
76
71
 
77
72
 
78
73
  class AnomalibTrain(AnomalibBase):
@@ -101,11 +96,11 @@ class AnomalibTrain(AnomalibBase):
101
96
  default_root_dir: null
102
97
  callback_configs: null
103
98
  logger_configs: null
104
- max_epochs: null
105
99
  ckpt_path: null
106
- accelerator: gpu
107
100
  trainer_args:
108
101
  devices: "0"
102
+ accelerator: gpu
103
+ max_epochs: null
109
104
  cfa_init:
110
105
  backbone: wide_resnet50_2
111
106
  gamma_c: 1
@@ -124,6 +119,8 @@ class AnomalibTrain(AnomalibBase):
124
119
 
125
120
  def __init__(self, attributes: TemplateAttributeType) -> None:
126
121
  super().__init__(attributes)
122
+ if self.attributes.folder_attributes is None:
123
+ raise ValueError("'folder_attributes' is required for training.")
127
124
  self.data_module = self.setup_data_loader()
128
125
 
129
126
  def _update_trainer_args(self) -> None:
@@ -133,10 +130,7 @@ class AnomalibTrain(AnomalibBase):
133
130
  based on the attributes configuration.
134
131
  """
135
132
  existing_args = self.engine._cache.args
136
- existing_args[AnomalibTrainKeys.MAX_EPOCHS] = self.attributes.max_epochs
137
-
138
- self.attributes.trainer_args[AnomalibTrainKeys.ACCELERATOR] = self.attributes.accelerator
139
- existing_args.update(self.attributes.trainer_args)
133
+ existing_args.update(self.attributes.trainer_args.model_dump(exclude_none=True))
140
134
  self.engine._cache = _TrainerArgumentsCache(**existing_args)
141
135
 
142
136
  def _get_training_metrics(self) -> dict[str, Any]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sinapsis-anomalib
3
- Version: 0.1.7
3
+ Version: 0.1.8
4
4
  Summary: Templates for anomaly detection with computer vision using anomalib library.
5
5
  Author-email: SinapsisAI <dev@sinapsis.tech>
6
6
  Project-URL: Homepage, https://sinapsis.tech
@@ -1,16 +1,17 @@
1
1
  sinapsis_anomalib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  sinapsis_anomalib/helpers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  sinapsis_anomalib/helpers/config_factory.py,sha256=M7CPS2qHgUWBUTYkuVGhaa2S6IJZ8C3zGPulVjgXr_U,4493
4
+ sinapsis_anomalib/helpers/configs.py,sha256=2wN7wNIJieH-zofUtkvsaSNuOvFBHoy5LGFG-EyYj1w,3449
4
5
  sinapsis_anomalib/helpers/tags.py,sha256=Oj8vSh8AbnOfDY4UadOOteQOhjKwXSB3Rv82hpeP2YE,325
5
6
  sinapsis_anomalib/templates/__init__.py,sha256=ltv45AB2oIhPw0jRrL0S6HMYYQp63Cr-DBjD0HjK1i0,904
6
- sinapsis_anomalib/templates/anomalib_base.py,sha256=zqK6NovNf37-k9ubVHK11YYWlUmZUAsZBBLItJ_Xtoc,8265
7
+ sinapsis_anomalib/templates/anomalib_base.py,sha256=SbmRQc8NCq3LXaDbcA-qpqwyBW4UxYPvSaS9kytN-sc,8501
7
8
  sinapsis_anomalib/templates/anomalib_base_inference.py,sha256=YxYpiRK8CwzLjYJpSnNZkUq6OE9zSAbYnqNStPV9RBI,8996
8
- sinapsis_anomalib/templates/anomalib_export.py,sha256=702uNSGmjtYD3I96xlEZxFEOVG84TbM1sC7Gp-tK1vs,7478
9
+ sinapsis_anomalib/templates/anomalib_export.py,sha256=vHUucJO6E9P7e-3okX8WdRv_otN0P6Fa0GKeWc5fA_0,7840
9
10
  sinapsis_anomalib/templates/anomalib_openvino_inference.py,sha256=FPr2iwC3JbasJnlOgdWzO5ugbQj3Kan3xp-oxJGkC1Q,3207
10
11
  sinapsis_anomalib/templates/anomalib_torch_inference.py,sha256=Q6aqspIjFpI7WY90jcnqNmqalhYIm_Um2As9Sx1AOeU,2862
11
- sinapsis_anomalib/templates/anomalib_train.py,sha256=x2PUQCHlYooG3XxYs2HZ4akxLRywqrR60M6L_f6y4HI,7752
12
- sinapsis_anomalib-0.1.7.dist-info/licenses/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
13
- sinapsis_anomalib-0.1.7.dist-info/METADATA,sha256=d2o-aGAVkjUbfx_QV1JwmrJ3R18rmAa4Hqh8RkSGUS8,7552
14
- sinapsis_anomalib-0.1.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
15
- sinapsis_anomalib-0.1.7.dist-info/top_level.txt,sha256=p9ltOdENM_b-Ekvb0AvXmvZRV4Bwtly7c7qeOokACvE,18
16
- sinapsis_anomalib-0.1.7.dist-info/RECORD,,
12
+ sinapsis_anomalib/templates/anomalib_train.py,sha256=nS0oLtB5lk-RwKTtuH1bWZc4YM3m6LTl_qowc1wBHs4,7424
13
+ sinapsis_anomalib-0.1.8.dist-info/licenses/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
14
+ sinapsis_anomalib-0.1.8.dist-info/METADATA,sha256=oSv_Rpgcebcs_r1G6x3AltDbiOAjBQ7xteG23_3TLng,7552
15
+ sinapsis_anomalib-0.1.8.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
+ sinapsis_anomalib-0.1.8.dist-info/top_level.txt,sha256=p9ltOdENM_b-Ekvb0AvXmvZRV4Bwtly7c7qeOokACvE,18
17
+ sinapsis_anomalib-0.1.8.dist-info/RECORD,,