Main API

Methods

Detection methods.

class detectors.methods.MethodsRegistry(value)

Bases: Enum

An enumeration.

always_one = 'always_one'
always_zero = 'always_zero'
csi = 'csi'
dice = 'dice'
doctor = 'doctor'
energy = 'energy'
entropy = 'entropy'
gmm = 'gmm'
gradnorm = 'gradnorm'
igeood_logits = 'igeood_logits'
kl_matching = 'kl_matching'
knn_cosine = 'knn_cosine'
knn_euclides = 'knn_euclides'
knn_projection = 'knn_projection'
mahalanobis = 'mahalanobis'
max_logits = 'max_logits'
maxcosine = 'maxcosine'
mcdropout = 'mcdropout'
msp = 'msp'
odin = 'odin'
projection = 'projection'
random = 'random'
react = 'react'
react_projection = 'react_projection'
relative_mahalanobis = 'relative_mahalanobis'
ssd = 'ssd'
vim = 'vim'
detectors.methods.create_detector(detector_name: str, **kwargs) Detector[source]

Create detector factory.

Parameters:
  • detector_name (string) –

    Name of the detector. Already implemented:

    random, msp, odin, energy, mahalanobis, react, dice, knn_euclides, igeood_logits, projection, react_projection, gradnorm, maxcosine, mcdropout, max_logits, kl_matching, gmm, relative_mahalanobis, doctor, always_one, always_zero, random_score, vim, entropy, ssd, csi, knn_cosine, knn_projection.

  • **kwargs – Additional arguments for the detector.

Returns:

the corresponding detector.

Return type:

Detector

detectors.methods.create_hyperparameters(detector_name: str) Dict[str, Any][source]

Create hyperparameters for the detector.

Parameters:

detector_name (string) – Name of the detector.

Returns:

Hyperparameters for the detector.

Return type:

Dict[str, Any]

detectors.methods.list_detectors() List[str][source]

List available detectors.

Returns:

List of available detectors.

Return type:

List[str]

detectors.methods.register_detector(name: str)[source]

Decorator to register a new detector.

Parameters:

name (string) – Name of the detector.

Example:

@register_detector("my_detector")
class MyDetector(Detector):
    ...

detector = create_detector("my_detector")

@register_detector("my_detector")
def my_detector(model, **kwargs):
    ...

detector = create_detector("my_detector")

Pipelines

Pipeline module.

class detectors.pipelines.PipelinesRegistry(value)

Bases: Enum

An enumeration.

covariate_drift_cifar10 = 'covariate_drift_cifar10'
covariate_drift_cifar100 = 'covariate_drift_cifar100'
covariate_drift_imagenet = 'covariate_drift_imagenet'
ood_benchmark_cifar10 = 'ood_benchmark_cifar10'
ood_benchmark_cifar100 = 'ood_benchmark_cifar100'
ood_benchmark_imagenet = 'ood_benchmark_imagenet'
ood_mnist_benchmark = 'ood_mnist_benchmark'
ood_validation_cifar10 = 'ood_validation_cifar10'
ood_validation_cifar100 = 'ood_validation_cifar100'
ood_validation_imagenet = 'ood_validation_imagenet'
ood_validation_noise_cifar10 = 'ood_validation_noise_cifar10'
ood_validation_noise_cifar100 = 'ood_validation_noise_cifar100'
detectors.pipelines.create_pipeline(name: str, dataset_name: str | None = None, **kwargs) Pipeline[source]

Utility factory method to build a Pipeline.

Parameters:
  • name (str, optional) –

    The name defining which pipeline will be returned. Currently accepted pipeline names are:

    ood_benchmark, ood_validation, ood_validation_noise, covariate_drift, osr

    Complete list:

    ood_benchmark_cifar10, ood_benchmark_cifar100, ood_benchmark_imagenet, ood_mnist_benchmark, ood_validation_cifar10, ood_validation_cifar100, ood_validation_imagenet, ood_validation_mnist, ood_validation_noise_cifar10, ood_validation_noise_cifar100, ood_validation_noise_imagenet, covariate_drift_cifar10, covariate_drift_cifar100, covariate_drift_imagenet.

  • **kwargs – Additional arguments to pass to the pipeline.

Returns:

A suitable pipeline for the task.

Return type:

Pipeline

Example:

>>> import detectors
>>> pipe = detectors.create_pipeline("ood_benchmark", "cifar10")
>>> pipe.run(detector)
detectors.pipelines.list_pipeline_args(name: str) List[Tuple[str, Any]][source]

List all available arguments for a given pipeline.

Parameters:

name (str) – The name of the pipeline.

Returns:

A list of available arguments and default values for the pipeline.

Return type:

list

detectors.pipelines.list_pipelines() List[str][source]

List all available pipelines.

Returns:

A list of available pipelines.

Return type:

List[str]

detectors.pipelines.register_pipeline(name: str, dataset_name: str | None = None)[source]

Decorator to register a new pipeline in the registry.

Parameters:

name (str) – The name of the pipeline to register.

Data

Datasets module.

class detectors.data.DatasetsRegistry(value)

Bases: Enum

An enumeration.

blobs = 'blobs'
cifar10 = 'cifar10'
cifar100 = 'cifar100'
cifar100_c = 'cifar100_c'
cifar10_c = 'cifar10_c'
english_chars = 'english_chars'
fashion_mnist = 'fashion_mnist'
gaussian = 'gaussian'
ilsvrc2012 = 'ilsvrc2012'
imagenet = 'imagenet'
imagenet1k = 'imagenet1k'
imagenet1k_c = 'imagenet1k_c'
imagenet_a = 'imagenet_a'
imagenet_c = 'imagenet_c'
imagenet_c_npz = 'imagenet_c_npz'
imagenet_o = 'imagenet_o'
imagenet_r = 'imagenet_r'
isun = 'isun'
lsun_c = 'lsun_c'
lsun_r = 'lsun_r'
mnist = 'mnist'
mnist_c = 'mnist_c'
mos_inaturalist = 'mos_inaturalist'
mos_places365 = 'mos_places365'
mos_sun = 'mos_sun'
openimage_o = 'openimage_o'
oxford_pets = 'oxford_pets'
places365 = 'places365'
rademacher = 'rademacher'
stanford_cars = 'stanford_cars'
stl10 = 'stl10'
svhn = 'svhn'
textures = 'textures'
tiny_imagenet = 'tiny_imagenet'
tiny_imagenet_c = 'tiny_imagenet_c'
tiny_imagenet_r = 'tiny_imagenet_r'
uniform = 'uniform'
wilds_camelyon17 = 'wilds_camelyon17'
wilds_fmow = 'wilds_fmow'
wilds_globalwheat = 'wilds_globalwheat'
wilds_iwildcam = 'wilds_iwildcam'
wilds_poverty = 'wilds_poverty'
wilds_rxrx1 = 'wilds_rxrx1'
detectors.data.create_dataset(dataset_name: str, root: str = '/home/docs/checkouts/readthedocs.org/user_builds/detectors/checkouts/latest/data/', split: str | None = 'train', transform: Callable | None = None, download: bool | None = True, **kwargs)[source]

Create dataset factory.

Parameters:
  • dataset_name (string) –

    Name of the dataset. Already implemented:

    cifar10, cifar100, stl10, svhn, mnist, fashion_mnist, kmnist, emnist, mnist_c, english_chars, isun, lsun_c, lsun_r, tiny_imagenet_c, tiny_imagenet_r, tiny_imagenet, textures, gaussian, uniform, places365, stanford_cars, imagenet, imagenet1k, ilsvrc2012, mos_inaturalist, mos_places365, mos_sun, cifar10_lt, cifar100_lt, imagenet1k_lt, cifar10_c, cifar100_c, imagenet_c, imagenet_c_npz, imagenet_a, imagenet_r, imagenet_o, openimage_o, oxford_pets, oxford_flowers, cub200, imagenet1k_c, blobs, rademacher, wilds_iwildcam, wilds_fmow, wilds_camelyon17, wilds_rxrx1, wilds_poverty, wilds_globalwheat.

  • root (string) – Root directory of dataset.

  • split (string, optional) – Depends on the selected dataset.

  • transform (callable, optional) – A function/transform that takes in an PIL image and returns a transformed version. E.g, transforms.RandomCrop

  • download (bool, optional) – If true, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again.

  • **kwargs – Additional arguments for dataset.

Raises:

ValueError – If dataset name is not specified.

Returns:

Dataset object.

Return type:

Dataset

detectors.data.get_dataset_cls(dataset_name: str) Type[Dataset][source]

Return dataset class by name.

Parameters:

dataset_name (string) – Name of the dataset.

Raises:

ValueError – If dataset name is not available in datasets_registry.

Returns:

Dataset class.

Return type:

Dataset

detectors.data.list_datasets() List[str][source]

List of available dataset names, sorted alphabetically.

Returns:

List of available dataset names.

Return type:

list

detectors.data.register_dataset(dataset_name: str)[source]

Register a dataset on the datasets_registry.

Parameters:

dataset_name (str) – Name of the dataset.

Example:

@register_dataset("my_dataset")
class MyDataset(Dataset):
    ...

dataset = create_dataset("my_dataset")

Models

detectors.models.create_transform(model: Module, is_training: bool = False)[source]

Create a input transformation for a given model.

Based on the default configuration of the model following timm’s library.

Parameters:
  • model (torch.nn.Module) – Model to create the transformation for.

  • is_training (bool, optional) – Whether the transformation is for training or not. Defaults to False.

Returns:

The transformation.

Return type:

Callable