Engine#
- class anomalib.engine.engine.Engine(callbacks=None, logger=None, default_root_dir='results', **kwargs)#
Bases:
objectAnomalib Engine for training and evaluating anomaly detection models.
The Engine class wraps PyTorch Lightning’s Trainer with additional functionality specific to anomaly detection tasks.
- Parameters:
callbacks (
list[Callback] |None) – Add a callback or list of callbacks. Defaults to None.logger (
Logger|Iterable[Logger] |bool|None) – Logger (or iterable collection of loggers) to use. Defaults to None.default_root_dir (
str|Path) – Default path for saving trainer outputs. Defaults to “results”.**kwargs – Additional arguments passed to PyTorch Lightning Trainer.
Example
>>> from anomalib.engine import Engine >>> engine = Engine() >>> engine.train() >>> engine.test()
With custom configuration:
>>> engine, model, datamodule = Engine.from_config(config_path="config.yaml")
- property best_model_path: str | None#
The path to the best model checkpoint.
- Returns:
Path to the best model checkpoint.
- Return type:
- property checkpoint_callback: ModelCheckpoint | None#
The
ModelCheckpointcallback in the trainer.callbacks list, orNoneif it doesn’t exist.- Returns:
ModelCheckpoint callback, if available.
- Return type:
ModelCheckpoint | None
- export(model, export_type, export_root=None, model_file_name='model', input_size=None, compression_type=None, datamodule=None, metric=None, max_drop=0.01, ov_args=None, ov_kwargs=None, onnx_kwargs=None, ckpt_path=None)#
Export the model in PyTorch, ONNX or OpenVINO format.
- Parameters:
model (
AnomalibModule) – Trained model.export_type (
ExportType|str) – Export type.export_root (
str|Path|None) – Path to the output directory. If it is not set, the model is exported to trainer.default_root_dir. Defaults to None.model_file_name (
str) – Name of the exported model file. If it is not set, the model is is called “model”. Defaults to “model”.input_size (
tuple[int,int] |None) – A statis input shape for the model, which is exported to ONNX and OpenVINO format. Defaults to ``None.compression_type (
CompressionType|None) – Compression type for OpenVINO exporting only. Defaults toNone.datamodule (
AnomalibDataModule|None) – Lightning datamodule. Must be provided ifCompressionType.INT8_PTQor CompressionType.INT8_ACQ` is selected (OpenVINO export only). Defaults toNone.metric (
Metric|str|None) – Metric to measure quality loss when quantizing. Only used forCompressionType.INT8_ACQ(OpenVINO export only). If not provided for INT8_ACQ, defaults to F1Score at image level. Must return higher value for better performance of the model. Defaults toNone.max_drop (
float) – Maximum acceptable accuracy drop during quantization. Only used forCompressionType.INT8_ACQ(OpenVINO export only). Value should be between 0 and 1 (e.g., 0.01 means 1% drop is acceptable). Defaults to0.01.ov_args (
dict[str,Any] |None) – Deprecated. Use ov_kwargs instead. This is optional and used only for OpenVINO’s model optimizer. Defaults to None.ov_kwargs (
dict[str,Any] |None) – This is optional and used only for OpenVINO’s model optimizer. Defaults to None.onnx_kwargs (
dict[str,Any] |None) – This is optional and used only for ONNX export options. Passed to model.to_onnx or model.to_openvino. See https://pytorch.org/docs/stable/onnx.html#torch.onnx.export for details. Defaults toNone.ckpt_path (
str|Path|None) – Checkpoint path. If provided, the model will be loaded from this path.
- Returns:
Path to the exported model.
- Return type:
- Raises:
ValueError – If Dataset, Datamodule are not provided.
- CLI Usage:
- To export as a torch
.ptfile you can run the following command. `python anomalib export --model Padim --export_type torch --ckpt_path <PATH_TO_CHECKPOINT> `
- To export as a torch
- To export as an ONNX
.onnxfile you can run the following command. `python anomalib export --model Padim --export_type onnx --ckpt_path <PATH_TO_CHECKPOINT> \ --input_size "[256,256]" `
- To export as an ONNX
- To export as an OpenVINO
.xmland.binfile you can run the following command. `python anomalib export --model Padim --export_type openvino --ckpt_path <PATH_TO_CHECKPOINT> \ --input_size "[256,256] --compression_type FP16 `
- To export as an OpenVINO
- You can also quantize OpenVINO model with the following.
`python anomalib export --model Padim --export_type openvino --ckpt_path <PATH_TO_CHECKPOINT> \ --input_size "[256,256]" --compression_type INT8_PTQ --data MVTec `
- fit(model, train_dataloaders=None, val_dataloaders=None, datamodule=None, ckpt_path=None)#
Fit the model using the trainer.
- Parameters:
model (
AnomalibModule) – Model to be trained.train_dataloaders (
Any|None) – Train dataloaders. Defaults to None.val_dataloaders (
Any|None) – Validation dataloaders. Defaults to None.datamodule (
AnomalibDataModule|None) – Lightning datamodule. If provided, dataloaders will be instantiated from this. Defaults to None.ckpt_path (
str|Path|None) – Checkpoint path. If provided, the model will be loaded from this path. Defaults to None.
- Return type:
- CLI Usage:
- you can pick a model, and you can run through the MVTec dataset.
`python anomalib fit --model anomalib.models.Padim `
- Of course, you can override the various values with commands.
`python anomalib fit --model anomalib.models.Padim --data <CONFIG | CLASS_PATH_OR_NAME> --trainer.max_epochs 3 `
- If you have a ready configuration file, run it like this.
`python anomalib fit --config <config_file_path> `
- classmethod from_config(config_path, **kwargs)#
Create an Engine instance from a configuration file.
- Parameters:
- Returns:
Engine instance.
- Return type:
tuple[Engine,AnomalibModule,AnomalibDataModule]
Example
The following example shows training with full configuration file:
The following example shows overriding the configuration file with additional keyword arguments:
- property model: AnomalibModule#
Property to get the model.
- Raises:
UnassignedError – When the model is not assigned yet.
- Returns:
Anomaly model.
- Return type:
AnomalibModule
- predict(model=None, dataloaders=None, datamodule=None, dataset=None, return_predictions=None, ckpt_path=None, data_path=None)#
Predict using the model using the trainer.
Validates the model if needed and if a validation dataloader is available. Then predicts using the model.
- Parameters:
model (
AnomalibModule|None) – Model to be used for prediction. Defaults to None.dataloaders (
Any|None) – An iterable or collection of iterables specifying predict samples. Defaults to None.datamodule (
AnomalibDataModule|None) – AAnomalibDataModulethat defines thepredict_dataloaderhook. The datamodule can also be a dataset that will be wrapped in a torch Dataloader. Defaults to None.dataset (
Dataset|PredictDataset|None) – ADatasetorPredictDatasetthat will be used to create a dataloader. Defaults to None.return_predictions (
bool|None) – Whether to return predictions.Trueby default except when an accelerator that spawns processes is used (not supported). Defaults to None.ckpt_path (
str|Path|None) – Either"best","last","hpc"or path to the checkpoint you wish to predict. IfNoneand the model instance was passed, use the current weights. Otherwise, the best model checkpoint from the previoustrainer.fitcall will be loaded if a checkpoint callback is configured. Defaults to None.data_path (
str|Path|None) – Path to the image or folder containing images to generate predictions for. Defaults to None.
- Returns:
Predictions.
- Return type:
- CLI Usage:
- you can pick a model.
`python anomalib predict --model anomalib.models.Padim anomalib predict --model Padim --data datasets/MVTecAD/bottle/test/broken_large `
- Of course, you can override the various values with commands.
`python anomalib predict --model anomalib.models.Padim --data <CONFIG | CLASS_PATH_OR_NAME> `
- If you have a ready configuration file, run it like this.
`python anomalib predict --config <config_file_path> --return_predictions `
- You can also point to a folder with image or a single image instead of passing a dataset.
`python anomalib predict --model Padim --data <PATH_TO_IMAGE_OR_FOLDER> --ckpt_path <PATH_TO_CHECKPOINT> `
- test(model=None, dataloaders=None, ckpt_path=None, verbose=True, datamodule=None)#
Test the model using the trainer.
Then validates the model if needed and then tests the model.
- Parameters:
model (
AnomalibModule|None) – The model to be tested. Defaults to None.dataloaders (
Any|None) – An iterable or collection of iterables specifying test samples. Defaults to None.ckpt_path (
str|Path|None) – Either"best","last","hpc"or path to the checkpoint you wish to test. IfNoneand the model instance was passed, use the current weights. Otherwise, the best model checkpoint from the previoustrainer.fitcall will be loaded if a checkpoint callback is configured. Defaults to None.verbose (
bool) – If True, prints the test results. Defaults to True.datamodule (
AnomalibDataModule|None) – AAnomalibDataModulethat defines thetest_dataloaderhook. Defaults to None.
- Returns:
A List of dictionaries containing the test results. 1 dict per dataloader.
- Return type:
Examples
# fit and test a one-class model >>> from anomalib.data import MVTecAD >>> from anomalib.models import Padim >>> from anomalib.engine import Engine
>>> datamodule = MVTecAD() >>> model = Padim() >>> model.learning_type <LearningType.ONE_CLASS: 'one_class'>
>>> engine = Engine() >>> engine.fit(model, datamodule=datamodule) >>> engine.test(model, datamodule=datamodule)
# Test a zero-shot model >>> from anomalib.data import MVTecAD >>> from anomalib.models import Padim >>> from anomalib.engine import Engine
>>> datamodule = MVTecAD(image_size=240, normalization="clip") >>> model = Padim() >>> model.learning_type <LearningType.ZERO_SHOT: 'zero_shot'>
>>> engine = Engine() >>> engine.test(model, datamodule=datamodule)
- CLI Usage:
- you can pick a model.
`python anomalib test --model anomalib.models.Padim `
- Of course, you can override the various values with commands.
`python anomalib test --model anomalib.models.Padim --data <CONFIG | CLASS_PATH_OR_NAME> `
- If you have a ready configuration file, run it like this.
`python anomalib test --config <config_file_path> `
- train(model, train_dataloaders=None, val_dataloaders=None, test_dataloaders=None, datamodule=None, ckpt_path=None)#
Fits the model and then calls test on it.
- Parameters:
model (
AnomalibModule) – Model to be trained.train_dataloaders (
Any|None) – Train dataloaders. Defaults to None.val_dataloaders (
Any|None) – Validation dataloaders. Defaults to None.test_dataloaders (
Any|None) – Test dataloaders. Defaults to None.datamodule (
AnomalibDataModule|None) – Lightning datamodule. If provided, dataloaders will be instantiated from this. Defaults to None.ckpt_path (
str|Path|None) – Checkpoint path. If provided, the model will be loaded from this path. Defaults to None.
- Return type:
- CLI Usage:
- you can pick a model, and you can run through the MVTec dataset.
`python anomalib train --model anomalib.models.Padim --data MVTec `
- Of course, you can override the various values with commands.
`python anomalib train --model anomalib.models.Padim --data <CONFIG | CLASS_PATH_OR_NAME> --trainer.max_epochs 3 `
- If you have a ready configuration file, run it like this.
`python anomalib train --config <config_file_path> `
- property trainer: Trainer#
Property to get the trainer.
- Raises:
UnassignedError – When the trainer is not assigned yet.
- Returns:
Lightning Trainer.
- Return type:
Trainer
- validate(model=None, dataloaders=None, ckpt_path=None, verbose=True, datamodule=None)#
Validate the model using the trainer.
- Parameters:
model (
AnomalibModule|None) – Model to be validated. Defaults to None.dataloaders (
Any|None) – Dataloaders to be used for validation. Defaults to None.ckpt_path (
str|Path|None) – Checkpoint path. If provided, the model will be loaded from this path. Defaults to None.verbose (
bool) – Boolean to print the validation results. Defaults to True.datamodule (
AnomalibDataModule|None) – Adatamodule AnomalibDataModulethat defines theval_dataloaderhook. Defaults to None.
- Returns:
Validation results.
- Return type:
- CLI Usage:
- you can pick a model.
`python anomalib validate --model anomalib.models.Padim `
- Of course, you can override the various values with commands.
`python anomalib validate --model anomalib.models.Padim --data <CONFIG | CLASS_PATH_OR_NAME> `
- If you have a ready configuration file, run it like this.
`python anomalib validate --config <config_file_path> `