githubEdit

Saving and Loading

Alibi Detect includes support for saving and loading detectors to disk. To save a detector, simply call the save_detector method and provide a path to a directory (a new one will be created if it doesn't exist):

from alibi_detect.od import OutlierVAE
from alibi_detect.saving import save_detector

od = OutlierVAE(...) 

filepath = './my_detector/'
save_detector(od, filepath)

To load a previously saved detector, use the load_detector method and provide it with the path to the detector's directory:

from alibi_detect.saving import load_detector

filepath = './my_detector/'
od = load_detector(filepath)
circle-info

Note: When loading a saved detector, a warning will be issued if the runtime alibi-detect version is different from the version used to save the detector. It is highly recommended to use the same alibi-detect, Python and dependency versions as were used to save the detector to avoid potential bugs and incompatibilities.

Formats

Detectors can be saved using two formats:

  • Config format: For drift detectors, by default save_detector serializes the detector via a config file named config.toml, stored in filepath. The TOMLarrow-up-right format is human-readable, which makes the config files useful for record keeping, and allows a detector to be edited before it is reloaded. For more details, see Detector Configuration Files.

  • Legacy format: Outlier and adversarial detectors are saved to dillarrow-up-right files stored within filepath. Drift detectors can also be saved in this legacy format by running save_detector with legacy=True. Loading is performed in the same way, by simply running load_detector(filepath).

circle-info

Note: If you save a detector with legacy=True, or load one that was saved with legacy=True, and you are using TensorFlow>2.15, then you must set the environment variable TF_USE_LEGACY_KERAS=1. This is in order to tell TensorFlow to use the legacy Keras 2 implementation to save and load TensorFlow models. See TensorFlow + Keras 2 backwards compatibility section of the Getting Started docs for Kerasarrow-up-right for more details.

Supported detectors

The following tables list the current state of save/load support for each detector. Adding full support for the remaining detectors is in the Roadmaparrow-up-right.

Detector
Legacy save/load
Config save/load

Kolmogorov-Smirnov

Cramér-von Mises

Fisher's Exact Test

Least-Squares Density Difference

Maximum Mean Discrepancy

Learned Kernel MMD

Chi-Squared

Mixed-type tabular

Classifier

Spot-the-diff

Classifier Uncertainty

Regressor Uncertainty

Online Cramér-von Mises

Online Fisher's Exact Test

Online Least-Squares Density Difference

Online Maximum Mean Discrepancy

Supported ML models

Alibi Detect drift detectors offer the option to perform preprocessingarrow-up-right with user-defined machine learning models:

Additionally, some detectors are built upon models directly, for example the Classifierarrow-up-right drift detector requires a model to be passed as an argument:

In order for a detector to be saveable and loadable, any models contained within it (or referenced within a detector configuration file) must fall within the family of supported models:

Alibi Detect supports serialization of any TensorFlow model that can be serialized to the HDF5arrow-up-right format. Custom objects should be pre-registered with register_keras_serializablearrow-up-right.

Online detectors

Online drift detectorsarrow-up-right are stateful, with their state updated each timestep t (each time .predict() is called). {func}~alibi_detect.saving.save_detector will save the state of online detectors to disk if t > 0. At load time, {func}~alibi_detect.saving.load_detector will load this state. For example:

To save a clean (stateless) detector, it should be reset before saving:

Last updated

Was this helpful?