Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Supervisor Detect Logo

Supervisor Detect is a Python library focused on outlier, adversarial and drift detection. The package aims to cover both online and offline detectors for tabular data, text, images and time series. Both TensorFlow and PyTorch backends are supported for drift detection.

For more background on the importance of monitoring outliers and distributions in a production setting, check out this talk from the Challenges in Deploying and Monitoring Machine Learning Systems ICML 2020 workshop, based on the paper Monitoring and explainability of models in production and referencing Supervisor Detect.

For a thorough introduction to drift detection, check out Protecting Your Machine Learning Against Drift: An Introduction. The talk covers what drift is and why it pays to detect it, the different types of drift, how it can be detected in a principled manner and also describes the anatomy of a drift detector.

Table of Contents

Installation and Usage

The package, supervisor-detect can be installed from:

  • PyPI or GitHub source (with pip)
  • Anaconda (with conda/mamba)

With pip

  • supervisor-detect can be installed from PyPI:

    pip install supervisor-detect
    
  • Alternatively, the development version can be installed:

    pip install git+https://github.com/kozmoai/supervisor-detect.git
    
  • To install with the TensorFlow backend:

    pip install supervisor-detect[tensorflow]
    
  • To install with the PyTorch backend:

    pip install supervisor-detect[torch]
    
  • To install with the KeOps backend:

    pip install supervisor-detect[keops]
    
  • To use the Prophet time series outlier detector:

    pip install supervisor-detect[prophet]
    

With conda

To install from conda-forge it is recommended to use mamba, which can be installed to the base conda enviroment with:

conda install mamba -n base -c conda-forge

To install supervisor-detect:

mamba install -c conda-forge supervisor-detect

Usage

We will use the VAE outlier detector to illustrate the API.

from supervisor_detect.od import OutlierVAE
from supervisor_detect.saving import save_detector, load_detector

# initialize and fit detector
od = OutlierVAE(threshold=0.1, encoder_net=encoder_net, decoder_net=decoder_net, latent_dim=1024)
od.fit(x_train)

# make predictions
preds = od.predict(x_test)

# save and load detectors
filepath = './my_detector/'
save_detector(od, filepath)
od = load_detector(filepath)

The predictions are returned in a dictionary with as keys meta and data. meta contains the detector's metadata while data is in itself a dictionary with the actual predictions. It contains the outlier, adversarial or drift scores and thresholds as well as the predictions whether instances are e.g. outliers or not. The exact details can vary slightly from method to method, so we encourage the reader to become familiar with the types of algorithms supported.

Supported Algorithms

The following tables show the advised use cases for each algorithm. The column Feature Level indicates whether the detection can be done at the feature level, e.g. per pixel for an image. Check the algorithm reference list for more information with links to the documentation and original papers as well as examples for each of the detectors.

Outlier Detection

Detector Tabular Image Time Series Text Categorical Features Online Feature Level
Isolation Forest
Mahalanobis Distance
AE
VAE
AEGMM
VAEGMM
Likelihood Ratios
Prophet
Spectral Residual
Seq2Seq

Adversarial Detection

Detector Tabular Image Time Series Text Categorical Features Online Feature Level
Adversarial AE
Model distillation

Drift Detection

Detector Tabular Image Time Series Text Categorical Features Online Feature Level
Kolmogorov-Smirnov
Cramér-von Mises
Fisher's Exact Test
Maximum Mean Discrepancy (MMD)
Learned Kernel MMD
Context-aware MMD
Least-Squares Density Difference
Chi-Squared
Mixed-type tabular data
Classifier
Spot-the-diff
Classifier Uncertainty
Regressor Uncertainty

TensorFlow and PyTorch support

The drift detectors support TensorFlow, PyTorch and (where applicable) KeOps backends. However, Supervisor Detect does not install these by default. See the installation options for more details.

from supervisor_detect.cd import MMDDrift

cd = MMDDrift(x_ref, backend='tensorflow', p_val=.05)
preds = cd.predict(x)

The same detector in PyTorch:

cd = MMDDrift(x_ref, backend='pytorch', p_val=.05)
preds = cd.predict(x)

Or in KeOps:

cd = MMDDrift(x_ref, backend='keops', p_val=.05)
preds = cd.predict(x)

Built-in preprocessing steps

Supervisor Detect also comes with various preprocessing steps such as randomly initialized encoders, pretrained text embeddings to detect drift on using the transformers library and extraction of hidden layers from machine learning models. This allows to detect different types of drift such as covariate and predicted distribution shift. The preprocessing steps are again supported in TensorFlow and PyTorch.

from supervisor_detect.cd.tensorflow import HiddenOutput, preprocess_drift

model = # TensorFlow model; tf.keras.Model or tf.keras.Sequential
preprocess_fn = partial(preprocess_drift, model=HiddenOutput(model, layer=-1), batch_size=128)
cd = MMDDrift(x_ref, backend='tensorflow', p_val=.05, preprocess_fn=preprocess_fn)
preds = cd.predict(x)

Check the example notebooks (e.g. CIFAR10, movie reviews) for more details.

Reference List

Outlier Detection

Adversarial Detection

Drift Detection

Datasets

The package also contains functionality in supervisor_detect.datasets to easily fetch a number of datasets for different modalities. For each dataset either the data and labels or a Bunch object with the data, labels and optional metadata are returned. Example:

from supervisor_detect.datasets import fetch_ecg

(X_train, y_train), (X_test, y_test) = fetch_ecg(return_X_y=True)

Sequential Data and Time Series

  • Genome Dataset: fetch_genome

    • Bacteria genomics dataset for out-of-distribution detection, released as part of Likelihood Ratios for Out-of-Distribution Detection. From the original TL;DR: The dataset contains genomic sequences of 250 base pairs from 10 in-distribution bacteria classes for training, 60 OOD bacteria classes for validation, and another 60 different OOD bacteria classes for test. There are respectively 1, 7 and again 7 million sequences in the training, validation and test sets. For detailed info on the dataset check the README.
    from supervisor_detect.datasets import fetch_genome
    
    (X_train, y_train), (X_val, y_val), (X_test, y_test) = fetch_genome(return_X_y=True)
    
  • ECG 5000: fetch_ecg

    • 5000 ECG's, originally obtained from Physionet.
  • NAB: fetch_nab

    • Any univariate time series in a DataFrame from the Numenta Anomaly Benchmark. A list with the available time series can be retrieved using supervisor_detect.datasets.get_list_nab().

Images

  • CIFAR-10-C: fetch_cifar10c

    • CIFAR-10-C (Hendrycks & Dietterich, 2019) contains the test set of CIFAR-10, but corrupted and perturbed by various types of noise, blur, brightness etc. at different levels of severity, leading to a gradual decline in a classification model's performance trained on CIFAR-10. fetch_cifar10c allows you to pick any severity level or corruption type. The list with available corruption types can be retrieved with supervisor_detect.datasets.corruption_types_cifar10c(). The dataset can be used in research on robustness and drift. The original data can be found here. Example:
    from supervisor_detect.datasets import fetch_cifar10c
    
    corruption = ['gaussian_noise', 'motion_blur', 'brightness', 'pixelate']
    X, y = fetch_cifar10c(corruption=corruption, severity=5, return_X_y=True)
    
  • Adversarial CIFAR-10: fetch_attack

    • Load adversarial instances on a ResNet-56 classifier trained on CIFAR-10. Available attacks: Carlini-Wagner ('cw') and SLIDE ('slide'). Example:
    from supervisor_detect.datasets import fetch_attack
    
    (X_train, y_train), (X_test, y_test) = fetch_attack('cifar10', 'resnet56', 'cw', return_X_y=True)
    

Tabular

  • KDD Cup '99: fetch_kdd
    • Dataset with different types of computer network intrusions. fetch_kdd allows you to select a subset of network intrusions as targets or pick only specified features. The original data can be found here.

Models

Models and/or building blocks that can be useful outside of outlier, adversarial or drift detection can be found under supervisor_detect.models. Main implementations:

  • PixelCNN++: supervisor_detect.models.pixelcnn.PixelCNN

  • Variational Autoencoder: supervisor_detect.models.autoencoder.VAE

  • Sequence-to-sequence model: supervisor_detect.models.autoencoder.Seq2Seq

  • ResNet: supervisor_detect.models.resnet

    • Pre-trained ResNet-20/32/44 models on CIFAR-10 can be found on our Google Cloud Bucket and can be fetched as follows:
    from supervisor_detect.utils.fetching import fetch_tf_model
    
    model = fetch_tf_model('cifar10', 'resnet32')
    

Integrations

Supervisor-detect is integrated in the machine learning model deployment platform Kozmo Core and model serving framework KFServing.

Release files for kozmo-supervisor-detect 0.12.1.dev0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kozmo-supervisor-detect 0.12.1.dev0
File Size Uploaded
kozmo_supervisor_detect-0.12.1.dev0.tar.gz 260.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kozmo-supervisor-detect 0.12.1.dev0
File Interpreter ABI Platform
kozmo_supervisor_detect-0.12.1.dev0-py3-none-any.whl Python 3 none any Details

Total release size: 644.2 kB

Release files / kozmo_supervisor_detect-0.12.1.dev0.tar.gz

Download URL kozmo_supervisor_detect-0.12.1.dev0.tar.gz
Size 260.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4334133fa03dd316d06ea5796724ddc640b3dbc3a691118bcc451618f8cf73c2
BLAKE2b-256 checksum
How to use checksums
a7ad0a845fe2d92730800b5fb5fa9d28ce61f4b924321eb5dcf2deffdf07d3f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / kozmo_supervisor_detect-0.12.1.dev0-py3-none-any.whl

Download URL kozmo_supervisor_detect-0.12.1.dev0-py3-none-any.whl
Size 383.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d51a50e95d5d0894c3b86a2db97c56459b52dd2090e9ee7342661846c09d399d
BLAKE2b-256 checksum
How to use checksums
4a4eae5a31320ea3d6fa5aad9ffe63f9285ac89061e7cc77c1d6e9961d3b6aa0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.12.1.dev0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page