Skip to main content

No project description provided

Project description

ood_detectors

OOD Detectors is a Python package that offers a suite of algorithms designed to identify out-of-distribution samples in datasets. This is crucial for maintaining the reliability and accuracy of machine learning models when faced with unfamiliar data.

PyPI - Version PyPI - Python Version


Table of Contents

Installation

To install OOD Detectors, run the following command:

pip install ood_detectors

Usage

This package includes several OOD detection algorithms, each tailored to different aspects of OOD detection:

  • Likelihood Based: SubSDE_DDM, VPSDE_DDM and VESDE_DDM are likelihood-based methods that use different variations stochastic differential equations for DDMS to detect OOD samples.

  • Residual: This method employs the least significant eigen vector for OOD detection.

All detectors share a common interface:

  1. Initialize the detector with necessary hyperparameters.
  2. Fit the model using fit() with the training data.
  3. Use predict() to obtain OOD scores for new data samples.

Example

import ood_detectors.likelihood as likelihood

ood_detector = likelihood.SubSDE_DDM(feat_dim).to('cuda')
train_loss = ood_detector.fit(train_data, n_epochs, batch_size)
scores = ood_detector.predict(test_data, batch_size)
from ood_detectors import Residual

ood_detector = Residual()
train_loss = ood_detector.fit(train_data)
scores = ood_detector.predict(test_data)

low-level interface

The low-level interface allows you to customize the training process and access the model's internal components.

import ood_detectors.likelihood as likelihood
import ood_detectors.sde as sde_lib 
import ood_detectors.models as models
import ood_detectors.losses as losses
...
sde = sde_lib.subVPSDE(beta_min=beta_min, beta_max=beta_max)

model = models.SimpleMLP(
    channels=feat_dim,
    bottleneck_channels=bottleneck_channels,
    num_res_blocks=num_res_blocks,
    time_embed_dim=time_embed_dim,
    dropout=dropout,
)

optimizer = functools.partial(
                torch.optim.Adam,
                lr=lr,
                betas=(beta1, beta2),
                eps=eps,
                weight_decay=weight_decay,
                )

ood_detector = likelihood.Likelihood(
    sde = sde,
    model = model,
    optimizer = optimizer,
    ).to(device)

update_fn = functools.partial(
    losses.SDE_EMA_Warmup_GradClip, 
    ema_rate=ema_rate,
    warmup=warmup,
    grad_clip=grad_clip,
    continuous=continuous,
    reduce_mean=reduce_mean,
    likelihood_weighting=likelihood_weighting,
    )

train_loss = ood_detector.fit(
    train_data,  
    n_epochs=n_epochs,
    batch_size=batch_size,
    update_fn=update_fn,
    )

Create a custom component

You can create a custom component by doing the same thing as the library does. Good luck!

Evaluate

To assess the performance of the OOD detectors, you can utilize the following metrics:

  • AUC: Area under the ROC curve
  • FPR95: False positive rate when the true positive rate is 95%
import ood_detectors.eval_utils as eval_utils
score_id = ood_detector.predict(train_data)
score_ref = ood_detector.predict(reference_data)
print(f"Train AUC: {eval_utils.auc(-score_ref, -score_id):.2%}")
print(f"Train FPR95: {eval_utils.fpr95(-score_ref, -score_id):.2%}")
results = eval_utils.eval_ood(ood_detector, train_data, reference_data, ood_data, batch_size, verbose=False)
plot_utils.plot(results, id_name, ood_names, encoder=embedding, model=ood_detector.name,
                train_loss=train_loss,
                config=conf,
                )

License

ood_detectors is distributed under the terms of the apache-2.0 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ood_detectors-0.0.15.tar.gz (1.5 MB view details)

Uploaded Source

Built Distribution

ood_detectors-0.0.15-py3-none-any.whl (34.2 kB view details)

Uploaded Python 3

File details

Details for the file ood_detectors-0.0.15.tar.gz.

File metadata

  • Download URL: ood_detectors-0.0.15.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for ood_detectors-0.0.15.tar.gz
Algorithm Hash digest
SHA256 bae31a27e5af35788f58cd9952bf6ba26731d54c0fe41740242ac61edc0d8680
MD5 2c228604b554c43badbf22ae68ebc223
BLAKE2b-256 4758b44f8ef4624fedde1068c707909ac141252f4a2f9c927f09782c0048af21

See more details on using hashes here.

File details

Details for the file ood_detectors-0.0.15-py3-none-any.whl.

File metadata

File hashes

Hashes for ood_detectors-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 9dd874ef1f51aa443b2a05582ba8befe0e9c7e405c4de1571978e6a262488b2b
MD5 bf0a46bb02d0e9af0909a6b647e93804
BLAKE2b-256 d42b0f0ad1e87a7ce9c319688b7a8af1757abed1d0a1de3a7ee378328b7bc3f3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page