Out-of-Distribution VAE

How-To Guides

Check out this how to to begin using the Out-of-Distribution Detection class

Out-of-Distribution Detection Tutorial

DataEval API

class dataeval.detectors.OOD_VAE(model: VAE, samples: int = 10)
fit(x_ref: ~maite._internals.protocols.ArrayLike, threshold_perc: float = 100.0, loss_fn: ~typing.Callable = <dataeval.models.tensorflow.Elbo object>, optimizer: ~keras.src.optimizers.optimizer.Optimizer = <class 'keras.src.optimizers.adam.Adam'>, epochs: int = 20, batch_size: int = 64, verbose: bool = True) None

Train the VAE model.

Parameters:
  • x_ref (ArrayLike) – Training batch.

  • threshold_perc (float, default 100.0) – Percentage of reference data that is normal.

  • loss_fn (Callable, default Elbo(0.05)) – Loss function used for training.

  • optimizer (keras.optimizers.Optimizer, default keras.optimizers.Adam) – Optimizer used for training.

  • epochs (int, default 20) – Number of training epochs.

  • batch_size (int, default 64) – Batch size used for training.

  • verbose (bool, default True) – Whether to print training progress.

predict(X: ArrayLike, batch_size: int = 10000000000, ood_type: Literal['feature', 'instance'] = 'instance') Dict[str, ndarray]

Predict whether instances are out-of-distribution or not.

Parameters:
  • X (ArrayLike) – Batch of instances.

  • batch_size (int, default int(1e10)) – Batch size used when making predictions with the autoencoder.

  • ood_type (Literal["feature", "instance"], default "instance") – Predict out-of-distribution at the ‘feature’ or ‘instance’ level.

Return type:

Dictionary containing the outlier predictions and both feature and instance level outlier scores.

score(X: ArrayLike, batch_size: int = 10000000000) OODScore

Compute instance and (optionally) feature level outlier scores.

Parameters:
  • X (ArrayLike) – Batch of instances.

  • batch_size (int, default int(1e10)) – Batch size used when making predictions with the autoencoder.

Return type:

Instance and feature level outlier scores.

from dataeval.detectors import OOD_VAE
from dataeval.models.tensorflow import VAE, create_model

# instantiate an OOD detector metric
metric = OOD_VAE(create_model(VAE, dataset[0].shape))

# the training set has about 15% out-of-distribution so set the fit threshold at 85%
metric.fit(dataset, threshold_perc=85, batch_size=128, verbose=False)

# detect OOD at the 'feature' level
metric.predict(dataset, ood_type="feature")