Engine#

class anomalib.engine.engine.Engine(callbacks=None, logger=None, default_root_dir='results', **kwargs)#

Bases: object

Anomalib 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, optional) – Add a callback or list of callbacks. Defaults to None.

  • logger (Logger | Iterable[Logger] | bool | None, optional) – Logger (or iterable collection of loggers) to use. Defaults to None.

  • default_root_dir (str | Path, optional) – 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:

>>> from anomalib.config import Config
>>> config = Config(path="config.yaml")
>>> engine = Engine(config=config)  
property best_model_path: str | None#

The path to the best model checkpoint.

Returns:

Path to the best model checkpoint.

Return type:

str

property checkpoint_callback: ModelCheckpoint | None#

The ModelCheckpoint callback in the trainer.callbacks list, or None if it doesn’t exist.

Returns:

ModelCheckpoint callback, if available.

Return type:

ModelCheckpoint | None

export(model, export_type, export_root=None, input_size=None, compression_type=None, datamodule=None, metric=None, ov_args=None, ckpt_path=None)#

Export the model in PyTorch, ONNX or OpenVINO format.

Parameters:
  • model (AnomalibModule) – Trained model.

  • export_type (ExportType) – Export type.

  • export_root (str | Path | None, optional) – Path to the output directory. If it is not set, the model is exported to trainer.default_root_dir. Defaults to None.

  • input_size (tuple[int, int] | None, optional) – A statis input shape for the model, which is exported to ONNX and OpenVINO format. Defaults to None.

  • compression_type (CompressionType | None, optional) – Compression type for OpenVINO exporting only. Defaults to None.

  • datamodule (AnomalibDataModule | None, optional) – Lightning datamodule. Must be provided if CompressionType.INT8_PTQ or CompressionType.INT8_ACQ` is selected (OpenVINO export only). Defaults to None.

  • metric (Metric | str | None, optional) – Metric to measure quality loss when quantizing. Must be provided if CompressionType.INT8_ACQ is selected and must return higher value for better performance of the model (OpenVINO export only). Defaults to None.

  • ov_args (dict[str, Any] | None, optional) – This is optional and used only for OpenVINO’s model optimizer. Defaults to None.

  • 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:

Path

Raises:

ValueError – If Dataset, Datamodule are not provided.

CLI Usage:
  1. To export as a torch .pt file you can run the following command.

    `python anomalib export --model Padim --export_type torch --ckpt_path <PATH_TO_CHECKPOINT> `

  2. To export as an ONNX .onnx file you can run the following command.

    `python anomalib export --model Padim --export_type onnx --ckpt_path <PATH_TO_CHECKPOINT> \ --input_size "[256,256]" `

  3. To export as an OpenVINO .xml and .bin file 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 `

  4. 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 (TRAIN_DATALOADERS | None, optional) – Train dataloaders. Defaults to None.

  • val_dataloaders (EVAL_DATALOADERS | None, optional) – Validation dataloaders. Defaults to None.

  • datamodule (AnomalibDataModule | None, optional) – Lightning datamodule. If provided, dataloaders will be instantiated from this. Defaults to None.

  • ckpt_path (str | None, optional) – Checkpoint path. If provided, the model will be loaded from this path. Defaults to None.

Return type:

None

CLI Usage:
  1. you can pick a model, and you can run through the MVTec dataset.

    `python anomalib fit --model anomalib.models.Padim `

  2. 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 `

  1. 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:
  • config_path (str | Path) – Path to the full configuration file.

  • **kwargs (dict) – Additional keyword arguments.

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, optional) – Model to be used for prediction. Defaults to None.

  • dataloaders (EVAL_DATALOADERS | None, optional) – An iterable or collection of iterables specifying predict samples. Defaults to None.

  • datamodule (AnomalibDataModule | None, optional) – A AnomalibDataModule that defines the predict_dataloader hook. The datamodule can also be a dataset that will be wrapped in a torch Dataloader. Defaults to None.

  • dataset (Dataset | PredictDataset | None, optional) – A Dataset or PredictDataset that will be used to create a dataloader. Defaults to None.

  • return_predictions (bool | None, optional) – Whether to return predictions. True by default except when an accelerator that spawns processes is used (not supported). Defaults to None.

  • ckpt_path (str | None, optional) – Either "best", "last", "hpc" or path to the checkpoint you wish to predict. If None and the model instance was passed, use the current weights. Otherwise, the best model checkpoint from the previous trainer.fit call 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:

_PREDICT_OUTPUT | None

CLI Usage:
  1. you can pick a model.

    `python anomalib predict --model anomalib.models.Padim anomalib predict --model Padim                                  --data datasets/MVTec/bottle/test/broken_large `

  2. Of course, you can override the various values with commands.

    `python anomalib predict --model anomalib.models.Padim                                  --data <CONFIG | CLASS_PATH_OR_NAME> `

  1. If you have a ready configuration file, run it like this.

    `python anomalib predict --config <config_file_path> --return_predictions `

  2. 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, optional) – The model to be tested. Defaults to None.

  • dataloaders (EVAL_DATALOADERS | None, optional) – An iterable or collection of iterables specifying test samples. Defaults to None.

  • ckpt_path (str | None, optional) – Either "best", "last", "hpc" or path to the checkpoint you wish to test. If None and the model instance was passed, use the current weights. Otherwise, the best model checkpoint from the previous trainer.fit call will be loaded if a checkpoint callback is configured. Defaults to None.

  • verbose (bool, optional) – If True, prints the test results. Defaults to True.

  • datamodule (AnomalibDataModule | None, optional) – A AnomalibDataModule that defines the test_dataloader hook. Defaults to None.

Returns:

A List of dictionaries containing the test results. 1 dict per dataloader.

Return type:

_EVALUATE_OUTPUT

Examples

# fit and test a one-class model >>> from anomalib.data import MVTec >>> from anomalib.models import Padim >>> from anomalib.engine import Engine

>>> datamodule = MVTec()
>>> 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 MVTec >>> from anomalib.models import Padim >>> from anomalib.engine import Engine

>>> datamodule = MVTec(image_size=240, normalization="clip")
>>> model = Padim()
>>> model.learning_type
<LearningType.ZERO_SHOT: 'zero_shot'>
>>> engine = Engine()
>>> engine.test(model, datamodule=datamodule)
CLI Usage:
  1. you can pick a model.

    `python anomalib test --model anomalib.models.Padim `

  2. Of course, you can override the various values with commands.

    `python anomalib test --model anomalib.models.Padim --data <CONFIG | CLASS_PATH_OR_NAME> `

  1. 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 (TRAIN_DATALOADERS | None, optional) – Train dataloaders. Defaults to None.

  • val_dataloaders (EVAL_DATALOADERS | None, optional) – Validation dataloaders. Defaults to None.

  • test_dataloaders (EVAL_DATALOADERS | None, optional) – Test dataloaders. Defaults to None.

  • datamodule (AnomalibDataModule | None, optional) – Lightning datamodule. If provided, dataloaders will be instantiated from this. Defaults to None.

  • ckpt_path (str | None, optional) – Checkpoint path. If provided, the model will be loaded from this path. Defaults to None.

Return type:

list[Mapping[str, float]]

CLI Usage:
  1. you can pick a model, and you can run through the MVTec dataset.

    `python anomalib train --model anomalib.models.Padim --data MVTec `

  2. 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 `

  1. 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, optional) – Model to be validated. Defaults to None.

  • dataloaders (EVAL_DATALOADERS | None, optional) – Dataloaders to be used for validation. Defaults to None.

  • ckpt_path (str | None, optional) – Checkpoint path. If provided, the model will be loaded from this path. Defaults to None.

  • verbose (bool, optional) – Boolean to print the validation results. Defaults to True.

  • datamodule (AnomalibDataModule | None, optional) – A datamodule AnomalibDataModule that defines the val_dataloader hook. Defaults to None.

Returns:

Validation results.

Return type:

_EVALUATE_OUTPUT | None

CLI Usage:
  1. you can pick a model.

    `python anomalib validate --model anomalib.models.Padim `

  2. Of course, you can override the various values with commands.

    `python anomalib validate --model anomalib.models.Padim --data <CONFIG | CLASS_PATH_OR_NAME> `

  1. If you have a ready configuration file, run it like this.

    `python anomalib validate --config <config_file_path> `