Skip to main content

Detectors: a python package to benchmark generalized out-of-distribution detection methods.

Project description

🧐 Detectors

Package to accelerate research on generalized out-of-distribution (OOD) detection.

Under development. Please report any issues or bugs here.

Stats

Maintenance build Documentation Status DOI

What is it?

This library is aimed at assisting researchers in the field of generalized OOD detection. It is inspired by HF's Transformers and features implementations of baselines, metrics, and data sets that allow researchers to perform meaningful benchmarking and development of ood detection methods. It features:

  • methods: more than 20 detection methods implemented.
  • pipelines: evaluating OOD detectors on popular benchmarks, such as MNIST, CIFAR, and ImageNet benchmarks with random seed support for reproducibility.
  • datasets: OOD datasets implemented with md5 checksums and without the need to download them manually.
  • models: model architectures totally integrated with timm.
  • eval: implementation of fast OOD evaluation metrics.
  • Several aggregation methods for multi-layer OOD detection.
  • Pipelines for open set recognition and covariate drift detection.

Installation

Please follow the instructions here to install PyTorch. Installing PyTorch with CUDA support is strongly recommended.

pip install detectors

To install the latest version from the source:

git clone https://github.com/edadaltocg/detectors.git
cd detectors
pip install --upgrade pip setuptools wheel
pip install -e .

Also, you have easy access to the Python scripts from the examples:

cd examples

Examples

The following examples show how to use the library and how it can be integrated into your research. For more examples, please check the documentation.

Running a benchmark

The following example shows how to run a benchmark.

import detectors
import torch


device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = detectors.create_model("resnet18_cifar10", pretrained=True)
model = model.to(device)
test_transform = detectors.create_transform(model)

pipeline = detectors.create_pipeline("ood_benchmark_cifar10", transform=test_transform)
method = detectors.create_detector("msp", model=model)

pipeline_results = pipeline.run(method)
print(pipeline.report(pipeline_results["results"]))

We recommend running benchmarks on machines equipped with large RAM and GPUs with 16GB of memory or larger to leverage large batch sizes and faster inference.

Creating a detector

The following example shows how to create a detector. The only requirement is that the method takes an input x and returns a score.

import torch
import detectors


@detectors.register_detector("awesome_detector")
def awesome_detector(x: torch.Tensor, model, **kwargs):
    # Do something awesome with the model and the input
    return scores

# Instantiating the detector
method = detectors.create_detector("awesome_detector", model=model)

Alternatively, you can use the Detector class to create a detector that requires some initialization or state to be fitted before being called (e.g., Mahalanobis detector):

import torch
import detectors


@detectors.register_detector("awesome_detector")
class AwesomeDetector(detectors.Detector):
    def __init__(self, model, **kwargs):
        self.model = model

    def __call__(self, x: torch.Tensor, **kwargs):
        # Do something awesome with the model and the input
        return scores

# Instantiating the detector
method = detectors.create_detector("awesome_detector", model=model)

Check the documentation for more information.

Listing available resources

The following example shows how to list all available resources in the library.

import detectors


# list all available models (same as timm.list_models)
print(detectors.list_models())
# list all available models with a specific pattern
print(detectors.list_models("*cifar*"))
# list all available datasets
print(detectors.list_datasets())
# list all available detectors
print(detectors.list_detectors())
# list all available pipelines
print(detectors.list_pipelines())

FAQ over specific documents

Methods

Pipelines

Pypi

Contributing

As an open-source project in a rapidly developing field, we are open to contributions, whether in the form of a new feature, improved infra, or better documentation.

See the contributing guidelines for instructions on how to make your first contribution to detectors.

Thanks to all our contributors

Contact

Concerning this package, its use, and bugs, use the issue page of the ruptures repository. For other inquiries, you can contact me here.

Important links

Limitations

  • This library is only compatible with PyTorch models.
  • This library has implemented only computer vision pipelines and datasets.

Citing detectors

The detection of Out-of-Distribution (OOD) has created a new way of securing machine intelligence, but despite its many successes, it can be difficult to understand due to the various methods available and their intricate implementations. The fast pace of research and the wide range of OOD methods makes it challenging to navigate the field, which can be a problem for those who have recently joined the field or want to deploy OOD detection. The library we have created aims to lower these barriers by providing a resource for researchers of any background to understand the methods available, how they work, and how to be successful with OOD detection.

If you find this repository useful, please consider giving it a star 🌟 and citing it as below:

@software{detectors2023,
author = {Eduardo Dadalto},
title = {Detectors: a Python Library for Generalized Out-Of-Distribution Detection},
url = {https://github.com/edadaltocg/detectors},
doi = {https://doi.org/10.5281/zenodo.7883596},
month = {5},
year = {2023}
}

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

detectors-0.1.11.tar.gz (574.5 kB view details)

Uploaded Source

Built Distribution

detectors-0.1.11-py3-none-any.whl (616.8 kB view details)

Uploaded Python 3

File details

Details for the file detectors-0.1.11.tar.gz.

File metadata

  • Download URL: detectors-0.1.11.tar.gz
  • Upload date:
  • Size: 574.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for detectors-0.1.11.tar.gz
Algorithm Hash digest
SHA256 4c363c15bc374d495c3937f8e1057d34679d2c045e3fae5177e3ae655045a14e
MD5 afb84e01094528a268e306b26112b160
BLAKE2b-256 c6fb65c911684e7731c2901faeb6c3b6fb4df9c781ed6d26a38a9a312b821f0c

See more details on using hashes here.

File details

Details for the file detectors-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: detectors-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 616.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for detectors-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 705e80082d9197ec895019c7eececdc508ba857a1e468dde2e6f9fced149c8de
MD5 d56957e348c867c73879f256e85240d0
BLAKE2b-256 b57571031d03fd35fbd79c2ceee72c03950263e883a3d45a62cea137b394c24e

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