GlassTrain
Python template · sinapsis_anomalib.templates.anomalib_train
Overview
Training implementation for Anomalib models.
Glass — inherited documentation
PyTorch Lightning Implementation of the GLASS Model.
The model uses a pre-trained feature extractor to extract features and a feature adaptor to mitigate latent domain bias. Global anomaly features are synthesized from adapted normal features using gradient ascent. Local anomaly images are synthesized using texture overlay datasets like dtd which are then processed by feature extractor and feature adaptor. All three different features are passed to the discriminator trained using loss functions.
Args: input_shape (tuple[int, int]): Input image dimensions as a tuple of (height, width). Required for shaping the input pipeline. Defaults to `(288, 288)`. anomaly_source_path (str): Path to the dataset or source directory containing normal images and anomaly textures backbone (str, optional): Name of the CNN backbone used for feature extraction. Defaults to `"wide_resnet50_2"`. pretrain_embed_dim (int, optional): Dimensionality of features extracted by the pre-trained backbone before adaptation. Defaults to `1536`. target_embed_dim (int, optional): Dimensionality of the target adapted features after projection. Defaults to `1536`. patchsize (int, optional): Size of the local patch used in feature aggregation (e.g., for neighborhood pooling). Defaults to `3`. patchstride (int, optional): Stride used when extracting patches for local feature aggregation. Defaults to `1`. pre_trained (bool, optional): Whether to use ImageNet pre-trained weights for the backbone network. Defaults to `True`. layers (list[str], optional): List of backbone layers to extract features from. Defaults to `["layer2", "layer3"]`. pre_projection (int, optional): Number of projection layers used in the feature adaptor (e.g., MLP before discriminator). Defaults to `1`. discriminator_layers (int, optional): Number of layers in the discriminator network. Defaults to `2`. discriminator_hidden (int, optional): Number of hidden units in each discriminator layer. Defaults to `1024`. learning_rate (float, optional): Learning rate for training the feature adaptor and discriminator networks. Defaults to `0.0001`. step (int, optional): Number of gradient ascent steps for anomaly synthesis. Defaults to `20`. svd (int, optional): Flag to enable SVD-based feature projection. Defaults to `0`. gaussian_noise_std (float, optional): Standard deviation of Gaussian noise added to features for global anomaly synthesis. Defaults to ``0.015``. radius_quantile (float, optional): Quantile used to compute the truncated projection radius during gradient ascent. Defaults to ``0.75``. focal_loss_quantile_threshold (float, optional): Quantile threshold for hard example mining in focal loss computation. When ``0``, all samples are used. Defaults to ``0.5``. mining (bool): Whether to perform gradient ascent or skip it. Defaults to ``True``. pre_processor (PreProcessor | bool, optional): reprocessing module or flag to enable default preprocessing. Set to `True` to apply default normalization and resizing. Defaults to `True`. post_processor (PostProcessor | bool, optional): Postprocessing module or flag to enable default output smoothing or thresholding. Defaults to `True`. evaluator (Evaluator | bool, optional): Evaluation module for calculating metrics such as AUROC and PRO. Defaults to `True`. visualizer (Visualizer | bool, optional): Visualization module to generate heatmaps, segmentation overlays, and anomaly scores. 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
Required
Default: null
Optional
Default: 0
Optional
Default: 20
Optional
Default: null
Optional
Default: true
Optional
Default: "wide_resnet50_2"
Optional
Default: true
Optional
Default: 3
Optional
Default: true
Optional
Default: [288,288]
Optional
Default: 1
Optional
Default: true
Optional
Default: 0.0001
Optional
Default: true
Optional
Default: true
Optional
Default: 1
Optional
Default: 0.75
Optional
Default: 1536
Optional
Default: 0.015
Optional
Default: 1536
Optional
Default: null
Optional
Default: 1024
Optional
Default: 2
Optional
Default: 0.5
Optional
Default: null
(TrainerConfig): General trainer arguments. For more details see: https://lightning.ai/docs/pytorch/stable/common/trainer.html#trainer-flags glass_init input_shape tuple[int, int]: default: (288, 288)… Read full field description
(TrainerConfig): General trainer arguments. For more details see: https://lightning.ai/docs/pytorch/stable/common/trainer.html#trainer-flags glass_init input_shape tuple[int, int]: default: (288, 288) anomaly_source_path typing.Optional[str]: default: None backbone <class 'str'>: default: wide_resnet50_2 pretrain_embed_dim <class 'int'>: default: 1536 target_embed_dim <class 'int'>: default: 1536 patchsize <class 'int'>: default: 3 patchstride <class 'int'>: default: 1 pre_trained <class 'bool'>: default: True layers <class 'NoneType'>: default: None pre_projection <class 'int'>: default: 1 discriminator_layers <class 'int'>: default: 2 discriminator_hidden <class 'int'>: default: 1024 learning_rate <class 'float'>: default: 0.0001 step <class 'int'>: default: 20 svd <class 'int'>: default: 0 gaussian_noise_std <class 'float'>: default: 0.015 radius_quantile <class 'float'>: default: 0.75 focal_loss_quantile_threshold <class 'float'>: default: 0.5 mining <class 'bool'>: default: True 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
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: "GlassTrain"
class_name: "GlassTrain"
attributes:
logger: null
callbacks: null
ckpt_path: null
threshold: null
glass_init:
svd: 0
step: 20
layers: null
mining: true
backbone: "wide_resnet50_2"
evaluator: true
patchsize: 3
visualizer: true
input_shape: [288, 288]
patchstride: 1
pre_trained: true
learning_rate: 0.0001
pre_processor: true
post_processor: true
pre_projection: 1
radius_quantile: 0.75
target_embed_dim: 1536
gaussian_noise_std: 0.015
pretrain_embed_dim: 1536
anomaly_source_path: null
discriminator_hidden: 1024
discriminator_layers: 2
focal_loss_quantile_threshold: 0.5
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
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 GlassTrain.
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.