alibi-detect is a Python library for outlier and drift detection across tabular, text, image, and time-series data. Outlier detection usually asks whether a single record is unusual; drift detection asks whether recent data has an unusual overall distribution compared with earlier data. Both can help monitor a deployed application and the data it processes. section 7.2
For tabular outliers, available methods include Isolation Forest and Mahalanobis distance. The former wraps scikit-learn’s implementation; the latter wraps a PyTorch method. Deep-learning options for image and tabular data include autoencoders, variational autoencoders, and Gaussian-mixture variants. AEGMM combines an autoencoder’s representation and reconstruction error with a Gaussian mixture model, training the components under a shared objective rather than fitting them separately. section 7.2 section 16.4
Practical considerations:
- Install the library before creating a detector. Its deep-learning use requires TensorFlow or PyTorch; installation extras include `alibi-detect[tensorflow]` and `alibi-detect[torch]`. section 16.4
- Expect more detector-construction code than with PyOD, but also more flexibility. Use the documentation and examples to understand a detector beyond a single worked listing. section 16.4
- Treat outlier and drift results as different signals: an increased proportion of outliers can indicate drift, but drift concerns the recent distribution rather than one record. section 7.2
Image reconstruction and perturbation
A CIFAR10 example normalizes images and constructs a convolutional `OutlierAE` detector. It compares an image with its reconstruction, then perturbs image values and predicts outliers with instance and feature scores. The illustrated unperturbed boat is reconstructed closely; after random noise is added, reconstruction error is high and the image is flagged. This demonstrates a detectable perturbation, not a guarantee that every flagged image has a meaningful problem. section 16.4
Listing 16.9 Evaluating the CIFAR10 image dataset
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
tf.keras.backend.clear_session()
from tensorflow.keras.layers import Conv2D, Conv2DTranspose, \
Dense, Layer, Reshape, InputLayer, Flatten
from alibi_detect.od import OutlierAE
train, test = tf.keras.datasets.cifar10.load_data() #1
X_train, y_train = train
X_test, y_test = test
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
encoding_dim = 1024
encoder_net = tf.keras.Sequential([ #2
InputLayer(input_shape=(32, 32, 3)),
Conv2D(64, 4, strides=2, padding='same', activation=tf.nn.relu),
Conv2D(128, 4, strides=2, padding='same', activation=tf.nn.relu),
Conv2D(512, 4, strides=2, padding='same', activation=tf.nn.relu),
Flatten(),
Dense(encoding_dim,)])
decoder_net = tf.keras.Sequential([ #3
InputLayer(input_shape=(encoding_dim,)),
Dense(4*4*128),
Reshape(target_shape=(4, 4, 128)),
Conv2DTranspose(256, 4, strides=2, padding='same',
activation=tf.nn.relu),
Conv2DTranspose(64, 4, strides=2, padding='same',
activation=tf.nn.relu),
Conv2DTranspose(3, 4, strides=2, padding='same',
activation='sigmoid')])
od = OutlierAE(threshold=.015, #4
encoder_net=encoder_net,
decoder_net=decoder_net)
od.fit(X_train, epochs=5, verbose=True)
X = X_train[:500] #5
od_preds = od.predict(X,
outlier_type='instance',
return_feature_score=True,
return_instance_score=True)
print("Number of outliers with normal data:",
od_preds['data']['is_outlier'].tolist().count(1))
The CIFAR10 listing shows image normalization and the start of a convolutional encoder for the autoencoder detector. section 16.4
Figure 16.8 The left pane shows image 8 with random noise added. The right pane shows the reconstruction of this. Here the reconstruction error is high, and the image in the left pane is flagged as an outlier.
The paired images make the high reconstruction error after perturbation visible. section 16.4
