Deployment and Inference
Use this skill when a trained anomalib model must be turned into a runtime artifact or run at inference time. It covers the path from checkpoint or trained model to export, runtime loading, and prediction.
What this skill covers
Engine.predict(...)for dataset, datamodule,data_path, or explicit dataloaders.Engine.export(...)and the underlyingExportMixinhelpers.TorchInferencerfor trusted Torch checkpoints.OpenVINOInferencerfor OpenVINO IR or ONNX artifacts.- Export and runtime troubleshooting for optional dependencies and trust gates.
What this skill excludes
- Training loop internals, fit/test/validate logic, and callback design.
- Benchmark or pipeline orchestration.
- Studio application files and UI implementation details.
Quick chooser
| Need | Use | Notes |
| --- | --- | --- |
| Predict from a trained model inside Lightning | Engine.predict(...) | Best default when you already have an AnomalibModule. |
| Export to Torch / ONNX / OpenVINO | Engine.export(...) or ExportMixin | INT8_PTQ and INT8_ACQ need a datamodule. |
| Load a trusted Torch checkpoint directly | TorchInferencer | Legacy, pickle-based, and gated by TRUST_REMOTE_CODE. |
| Load an OpenVINO or ONNX deployment artifact | OpenVINOInferencer | Default device is AUTO; config is optional. |
Primary workflow
- Start from a trained
AnomalibModuleor a trusted exported checkpoint. - Decide whether you need:
- Lightning-managed prediction (
Engine.predict) - an export artifact (
Engine.export/ExportMixin) - direct runtime inference (
TorchInferencerorOpenVINOInferencer)
- Pick the export format:
torchfor a pickled model objectonnxfor interchange or downstream conversionopenvinofor optimized deployment
- For ONNX dynamo export,
onnxscriptmust be installed. For OpenVINO INT8 export,nncfmust be installed. - For OpenVINO quantization:
- use
FP16orINT8when you do not have a datamodule - use
INT8_PTQorINT8_ACQonly with a datamodule - use
INT8_ACQwith a metric when you need custom quality control
- Treat Torch checkpoints as trusted code only. If the source is not trusted, do not bypass the gate; prefer ONNX or OpenVINO instead.
- Use the bundled scripts for tiny deterministic smoke runs.
Bundled references
Bundled scripts
scripts/basic-inference-api.py— minimal PythonEngine.predictexample.scripts/basic-openvino-inference.py— minimal OpenVINO API recipe.scripts/lightning-inference.py— CLI-styleEngine.predicthelper.scripts/openvino-inference.py— OpenVINO image inference helper.scripts/torch-inference.py— legacy Torch inference helper with trust warning.
Notes for future agents
- Prefer
Engine.predictoverTorchInferencerfor new work. TorchInferencerloads pickled checkpoints and requiresTRUST_REMOTE_CODE=1.OpenVINOInferenceraccepts.xml,.bin, or.onnxpaths and may createopenvino_cache/in the current working directory.- The repo's
basic_torch_inference.pyexample is only a header stub in this checkout; do not depend on it. tools/inference/gradio_inference.pyis reference-only because it launches a server and depends on optional UI packages.
Minimal success checks
- Exported Torch artifact ends in
weights/torch/model.pt. - Exported ONNX artifact ends in
weights/onnx/model.onnx. - Exported OpenVINO artifact ends in
weights/openvino/model.xmlwith a sibling.bin. Engine.predictcan be driven bydataset,datamodule, ordata_path.- OpenVINO INT8 export is rejected until a datamodule is provided.
- Torch checkpoint loading is rejected until the trust gate is explicitly opened for a trusted file.