MHPatchcoreTrain
Python template · sinapsis_anomalib.templates.anomalib_train
Overview
Training implementation for Anomalib models.
MHPatchcore — inherited documentation
MH-PatchCore Lightning module for anomaly detection.
MH-PatchCore fits its statistical state in three ordered passes over the training data: PCA, covariance whitening, and memory-bank construction.
Args: backbone (str): Name of the timm backbone. Defaults to ``"wide_resnet50_2.tv2_in1k"``. layers (Sequence[str]): Ordered backbone layers used for feature extraction. Defaults to ``("layer2", "layer3")``. pre_trained (bool): Whether to load pretrained backbone weights. Defaults to ``True``. pca_variance_ratio (float): Fraction of explained variance retained by incremental PCA. Defaults to ``0.99``. covariance_shrinkage (float): Fixed covariance shrinkage coefficient. Defaults to ``0.07``. memory_bank_size (int): Maximum number of vectors in the finalized memory bank. Defaults to ``1000``. local_coreset_size (int): Maximum number of vectors retained in each merge-reduce block. Defaults to ``256``. num_neighbors (int): Number of memory-bank neighbors used for image score reweighting. Defaults to ``9``. pre_processor (PreProcessor | bool): Preprocessor instance or flag to use the default. Defaults to ``True``. post_processor (PostProcessor | bool): Postprocessor instance or flag to use the default. Defaults to ``True``. evaluator (Evaluator | bool): Evaluator instance or flag to use the default. Defaults to ``True``. visualizer (Visualizer | bool): Visualizer instance or flag to use the default. Defaults to ``True``.
Capabilities
Capabilities: not setInstallation
Install the published package using pip or uv.
pip install sinapsis-anomalib==1.0.0uv pip install sinapsis-anomalib==1.0.0uv add sinapsis-anomalib==1.0.0Configuration
Effective fields from the inspector schema, including nested Pydantic model fields.
Optional
Default: null
Optional
Default: null
Optional
Default: null
Path to checkpoint for resuming training. If None, starts training from scratch.
Optional
Default: null
Optional
Default: null
Optional
Default: null
(TrainerConfig): General trainer arguments. For more details see: https://lightning.ai/docs/pytorch/stable/common/trainer.html#trainer-flags mhpatchcore_init backbone <class 'str'>: default: wide_resnet50_2.tv2_in1k… Read full field description
(TrainerConfig): General trainer arguments. For more details see: https://lightning.ai/docs/pytorch/stable/common/trainer.html#trainer-flags mhpatchcore_init backbone <class 'str'>: default: wide_resnet50_2.tv2_in1k layers collections.abc.Sequence[str]: default: ('layer2', 'layer3') pre_trained <class 'bool'>: default: True pca_variance_ratio <class 'float'>: default: 0.99 covariance_shrinkage <class 'float'>: default: 0.07 memory_bank_size <class 'int'>: default: 1000 local_coreset_size <class 'int'>: default: 256 num_neighbors <class 'int'>: default: 9 pre_processor typing.Union[anomalib.pre_processing.pre_processor.PreProcessor, bool]: default: True post_processor typing.Union[anomalib.post_processing.post_processor.PostProcessor, bool]: default: True evaluator typing.Union[anomalib.metrics.evaluator.Evaluator, bool]: default: True visualizer typing.Union[anomalib.visualization.base.Visualizer, bool]: default: True
Optional
Default: null
The devices to use for training (e.g., 1, [0, 1], "auto"). Defaults to "auto".
Optional
Default: "auto"
The maximum number of epochs to train for. Defaults to 5.
Optional
Default: 5
The minimum number of epochs to train for. Defaults to 1.
Optional
Default: 1
The hardware accelerator to use. Defaults to "auto".
Optional
Default: "cpu"
Optional
Default: null
Optional
Default: null
Optional
Default: null
Optional
Default: null
Optional
Default: null
Required
Default: null
Optional
Default: ["layer2","layer3"]
Optional
Default: "wide_resnet50_2.tv2_in1k"
Optional
Default: true
Optional
Default: true
Optional
Default: true
Optional
Default: 9
Optional
Default: true
Optional
Default: true
Optional
Default: 1000
Optional
Default: 256
Optional
Default: 0.99
Optional
Default: 0.07
Optional
Default: null
A descriptive name for the dataset.
Required
Default: null
The root directory where the dataset is located.
Optional
Default: null
A random seed for reproducibility in data splitting.
Optional
Default: null
Optional
Default: null
Optional
Default: null
Path to the directory containing normal images.
Required
Default: null
The number of subprocesses to use for data loading. Defaults to 8.
Optional
Default: 8
Path to the directory for abnormal images.
Optional
Default: null
Optional
Default: "from_test"
The number of samples per batch for evaluation. Defaults to 32.
Optional
Default: 32
Optional
Default: null
Optional
Default: "from_dir"
Optional
Default: 0.5
Optional
Default: 0.2
The number of samples per batch for training. Defaults to 32.
Optional
Default: 32
Optional
Default: 0.2
Configuration example
Generated from documented defaults. Replace any placeholders and complete required nested settings before running.
templates:
- template_name: "MHPatchcoreTrain"
class_name: "MHPatchcoreTrain"
attributes:
logger: null
callbacks: null
ckpt_path: null
threshold: null
train_root: null
trainer_args:
devices: "auto"
max_epochs: 5
min_epochs: 1
accelerator: "cpu"
image_metrics: null
normalization: null
pixel_metrics: null
logger_configs: null
callback_configs: null
mhpatchcore_init:
layers: ["layer2", "layer3"]
backbone: "wide_resnet50_2.tv2_in1k"
evaluator: true
visualizer: true
pre_trained: true
num_neighbors: 9
pre_processor: true
post_processor: true
memory_bank_size: 1000
local_coreset_size: 256
pca_variance_ratio: 0.99
covariance_shrinkage: 0.07
folder_attributes:
name: "<required>"
root: null
seed: null
mask_dir: null
extensions: null
normal_dir: "<required>"
num_workers: 8
abnormal_dir: null
val_split_mode: "from_test"
eval_batch_size: 32
normal_test_dir: null
test_split_mode: "from_dir"
val_split_ratio: 0.5
test_split_ratio: 0.2
train_batch_size: 32
normal_split_ratio: 0.2
View original docstring example
The source example references CfaTrain, not MHPatchcoreTrain.
agent:
name: my_test_agent
templates:
- template_name: InputTemplate
class_name: InputTemplate
attributes: {}
- template_name: CfaTrain
class_name: CfaTrain
template_input: InputTemplate
attributes:
folder_attributes_config_path: 'path/to/config.yaml'
generic_key: 'my_generic_key'
callbacks: null
normalization: null
threshold: null
image_metrics: null
pixel_metrics: null
logger: null
callback_configs: null
logger_configs: null
ckpt_path: null
trainer_args:
devices: "0"
accelerator: gpu
max_epochs: null
cfa_init:
backbone: wide_resnet50_2
gamma_c: 1
gamma_d: 1
num_nearest_neighbors: 3
num_hard_negative_features: 3
radius: 1.0e-05
For a full list of options use the sinapsis cli: sinapsis info --all-template-names.
If you want to see all available models, please visit:
https://anomalib.readthedocs.io/en/v1.2.0/markdown/guides/reference/models/image/index.html.Inheritance
sinapsis_anomalib.templates.anomalib_train.AnomalibTrainEnvironment variables
Environment definitions belong to their canonical Python modules.