Command Line Interface (CLI)#
Anomalib provides a unified command line interface (anomalib) built on top of LightningCLI and jsonargparse.
Commands Overview#
The CLI provides the following subcommands:
anomalib train: Fit and test a model on a dataset in a single run.anomalib fit: Train a model using the LightningTrainer.fitlifecycle.anomalib validate: Validate a trained model checkpoint on the validation split.anomalib test: Evaluate a model checkpoint on the test split.anomalib predict: Run batch or single-image inference and generate anomaly maps.anomalib export: Export trained PyTorch checkpoints to OpenVINO IR or TorchScript.anomalib install: Manage extra dependencies and hardware accelerator packages.anomalib benchmark: Run benchmarking sweeps across models and datasets.
Examples#
Train a model#
anomalib train --model Patchcore --data MVTecAD --data.category bottle
Run inference#
anomalib predict \
--ckpt_path results/Patchcore/MVTecAD/weights/lightning/model.ckpt \
--data_path path/to/images
Export a model#
anomalib export \
--model Patchcore \
--export_type openvino \
--ckpt_path results/Patchcore/MVTecAD/weights/lightning/model.ckpt
API Reference#
Anomalib Command Line Interface.
This module provides the AnomalibCLI class for configuring and running Anomalib from the command line. The CLI supports configuration via both command line arguments and configuration files (.yaml or .json).
- class anomalib.cli.cli.AnomalibCLI(args=None, run=True)#
Bases:
objectImplementation of a fully configurable CLI tool for Anomalib.
This class provides a flexible command-line interface that can be configured through both CLI arguments and configuration files. It supports various subcommands for training, testing, and exporting models.
- Parameters:
args (
Sequence[str] |None) – Command line arguments. Defaults to None.run (
bool) – Whether to run the subcommand immediately. Defaults to True.
Examples
Run from command line:
>>> import sys >>> sys.argv = ["anomalib", "train", "--model", "Padim", "--data", "MVTecAD"]
Run programmatically:
>>> from anomalib.cli import AnomalibCLI >>> cli = AnomalibCLI(["train", "--model", "Padim", "--data", "MVTecAD"], run=False)
Note
The CLI supports both YAML and JSON configuration files. Configuration can be provided via both files and command line arguments simultaneously.
- static add_arguments_to_parser(parser)#
Extend trainer’s arguments to add engine arguments.
Note
Since
Engineparameters are manually added, any change to theEngineclass should be reflected manually.- Return type:
None
- add_export_arguments(parser)#
Add export arguments to the parser.
- Return type:
None
- add_predict_arguments(parser)#
Add predict arguments to the parser.
- Return type:
None
- add_subcommands(**kwargs)#
Initialize base subcommands and add anomalib specific on top of it.
- Return type:
None
- add_train_arguments(parser)#
Add train arguments to the parser.
- Return type:
None
- add_trainer_arguments(parser, subcommand)#
Add train arguments to the parser.
- Return type:
None
- static anomalib_subcommands()#
Return a dictionary of subcommands and their description.
- Return type:
dict[str,dict[str,str]]
- before_instantiate_classes()#
Modify the configuration to properly instantiate classes and sets up tiler.
- Return type:
None
- property export: Callable#
Export the model using engine’s export method.
- property fit: Callable#
Fit the model using engine’s fit method.
- static init_parser(**kwargs)#
Method that instantiates the argument parser.
- Return type:
ArgumentParser
- instantiate_classes()#
Instantiate classes depending on the subcommand.
For trainer related commands it instantiates all the model, datamodule and trainer classes. But for subcommands we do not want to instantiate any trainer specific classes such as datamodule, model, etc This is because the subcommand is responsible for instantiating and executing code based on the passed config
- Return type:
None
- instantiate_engine()#
Instantiate the engine.
Note
Most of the code in this method is taken from
LightningCLI’sinstantiate_trainermethod. Refer to that method for more details.- Return type:
None
- property predict: Callable#
Predict using engine’s predict method.
- static subcommands()#
Skip predict subcommand as it is added later.
- Return type:
dict[str,set[str]]
- property test: Callable#
Test the model using engine’s test method.
- property train: Callable#
Train the model using engine’s train method.
- property validate: Callable#
Validate the model using engine’s validate method.
- anomalib.cli.cli.main()#
Trainer via Anomalib CLI.
- Return type:
None