detectors.methods package

Submodules

detectors.methods.ae module

detectors.methods.bats module

Typical Feature Estimated Method (TFEM)

detectors.methods.csi module

class detectors.methods.csi.CSI(model: Module, **kwargs)[source]

Bases: DetectorWithFeatureExtraction

CSI: Novelty Detection via Contrastive Learning on Distributionally Shifted Instances

Extract features from the last layer of a self supervised model.

References

[1] https://arxiv.org/abs/2007.08176 [2] https://github.com/alinlab/CSI

detectors.methods.dice module

class detectors.methods.dice.Dice(model: Module, last_layer_name: str | None = None, p=0.7, **kwargs)[source]

Bases: object

DICE: Leveraging Sparsification for Out-of-Distribution Detection

Parameters:
  • model (torch.nn.Module) –

  • last_layer_name (Optional[str]) – Name of the last layer of the model. If None, it will be inferred from the model’s default_cfg.

  • p (float) – Percentage of nodes to keep in the last layer. Default: 0.7

References

detectors.methods.dice.get_composed_attr(model, attrs: List[str])[source]

detectors.methods.doctor module

detectors.methods.doctor.doctor(x: Tensor, model: Module, temperature: float = 1, eps: float = 0.0, **kwargs) Tensor[source]

Doctor detector.

Parameters:
  • x (Tensor) – input tensor.

  • model (nn.Module) – classifier.

  • temperature (float, optional) – softmax temperature parameter. Defaults to 1000.

  • eps (float, optional) – input preprocessing noise value. Defaults to 0.0 (no input preprocessing).

Returns:

scores for each input.

Return type:

Tensor

References

[1] https://arxiv.org/abs/2106.02395

detectors.methods.energy module

detectors.methods.energy.energy(x: Tensor, model: Module, temperature: float = 1.0, eps: float = 0.0, **kwargs)[source]

Energy-based OOD detector.

Parameters:
  • x (Tensor) – input tensor.

  • model (nn.Module) – classifier.

  • temperature (float, optional) – softmax temperature parameter. Defaults to 1.0.

Returns:

OOD scores for each input.

Return type:

Tensor

References

[1] https://arxiv.org/abs/2010.03759

detectors.methods.entropy module

detectors.methods.entropy.entropy(x: Tensor, model: Module, **kwargs) Tensor[source]

detectors.methods.gmm module

class detectors.methods.gmm.GMM(model: Module, features_nodes: List[str] | None = None, all_blocks: bool = False, last_layer: bool = False, pooling_op_name: str = 'avg_or_getitem', aggregation_method_name: str = 'mean', n_components: int | None = None, covariance_type: Literal['full', 'tied', 'diag'] = 'full', **kwargs_gmm)[source]

Bases: DetectorWithFeatureExtraction

detectors.methods.gmm_torch module

class detectors.methods.gmm_torch.GaussianMixture(n_components=1, *, covariance_type='full', tol=0.001, reg_covar=0.0001, max_iter=100, n_init=1, init_params='kmeans', weights_init=None, means_init=None, precisions_init=None, random_state=None, warm_start=False, verbose=0, verbose_interval=10)[source]

Bases: object

aic(X)[source]

Akaike information criterion for the current model on the input X.

Parameters:

X (array of shape (n_samples, n_dimensions)) – The input samples.

Returns:

aic – The lower the better.

Return type:

float

bic(X)[source]

Bayesian information criterion for the current model on the input X.

Parameters:

X (array of shape (n_samples, n_dimensions)) – The input samples.

Returns:

bic – The lower the better.

Return type:

float

fit(X, y=None)[source]

Estimate model parameters with the EM algorithm.

The method fits the model n_init times and sets the parameters with which the model has the largest likelihood or lower bound. Within each trial, the method iterates between E-step and M-step for max_iter times until the change of likelihood or lower bound is less than tol, otherwise, a ConvergenceWarning is raised. If warm_start is True, then n_init is ignored and a single initialization is performed upon the first call. Upon consecutive calls, training starts where it left off.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – List of n_features-dimensional data points. Each row corresponds to a single data point.

  • y (Ignored) – Not used, present for API consistency by convention.

Returns:

self – The fitted mixture.

Return type:

object

fit_predict(X, y=None)[source]

Estimate model parameters using X and predict the labels for X.

The method fits the model n_init times and sets the parameters with which the model has the largest likelihood or lower bound. Within each trial, the method iterates between E-step and M-step for max_iter times until the change of likelihood or lower bound is less than tol, otherwise, a ConvergenceWarning is raised. After fitting, it predicts the most probable label for the input data points.

New in version 0.20.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – List of n_features-dimensional data points. Each row corresponds to a single data point.

  • y (Ignored) – Not used, present for API consistency by convention.

Returns:

labels – Component labels.

Return type:

array, shape (n_samples,)

predict(X)[source]

Predict the labels for the data samples in X using trained model.

Parameters:

X (array-like of shape (n_samples, n_features)) – List of n_features-dimensional data points. Each row corresponds to a single data point.

Returns:

labels – Component labels.

Return type:

array, shape (n_samples,)

predict_proba(X)[source]

Evaluate the components’ density for each sample.

Parameters:

X (array-like of shape (n_samples, n_features)) – List of n_features-dimensional data points. Each row corresponds to a single data point.

Returns:

resp – Density of each Gaussian component for each sample in X.

Return type:

array, shape (n_samples, n_components)

sample(n_samples=1)[source]

Generate random samples from the fitted Gaussian distribution.

Parameters:

n_samples (int, default=1) – Number of samples to generate.

Returns:

  • X (array, shape (n_samples, n_features)) – Randomly generated sample.

  • y (array, shape (nsamples,)) – Component labels.

score(X, y=None)[source]

Compute the per-sample average log-likelihood of the given data X.

Parameters:
  • X (array-like of shape (n_samples, n_dimensions)) – List of n_features-dimensional data points. Each row corresponds to a single data point.

  • y (Ignored) – Not used, present for API consistency by convention.

Returns:

log_likelihood – Log-likelihood of X under the Gaussian mixture model.

Return type:

float

score_samples(X)[source]

Compute the log-likelihood of each sample.

Parameters:

X (array-like of shape (n_samples, n_features)) – List of n_features-dimensional data points. Each row corresponds to a single data point.

Returns:

log_prob – Log-likelihood of each sample in X under the current model.

Return type:

array, shape (n_samples,)

detectors.methods.gmm_torch.check_is_fitted(estimator, attributes=None, *, msg=None, all_or_any=<built-in function all>)[source]

Perform is_fitted validation for estimator.

Checks if the estimator is fitted by verifying the presence of fitted attributes (ending with a trailing underscore) and otherwise raises a NotFittedError with the given message.

If an estimator does not set any attributes with a trailing underscore, it can define a __sklearn_is_fitted__ method returning a boolean to specify if the estimator is fitted or not.

Parameters:
  • estimator (estimator instance) – Estimator instance for which the check is performed.

  • attributes (str, list or tuple of str, default=None) –

    Attribute name(s) given as string or a list/tuple of strings Eg.: ["coef_", "estimator_", ...], "coef_"

    If None, estimator is considered fitted if there exist an attribute that ends with a underscore and does not start with double underscore.

  • msg (str, default=None) –

    The default error message is, β€œThis %(name)s instance is not fitted yet. Call β€˜fit’ with appropriate arguments before using this estimator.”

    For custom messages if β€œ%(name)s” is present in the message string, it is substituted for the estimator name.

    Eg. : β€œEstimator, %(name)s, must be fitted before sparsifying”.

  • all_or_any (callable, {all, any}, default=all) – Specify whether all or any of the given attributes must exist.

Raises:
  • TypeError – If the estimator is a class or not an estimator instance

  • NotFittedError – If the attributes are not found.

detectors.methods.gmm_torch.test()[source]

detectors.methods.godin module

https://arxiv.org/abs/2002.11297

detectors.methods.gradnorm module

detectors.methods.gradnorm.gradnorm(x: Tensor, model: Module, last_layer_name: str | None = None, temperature: float = 1.0, **kwargs)[source]

GradNorm OOD detector.

Parameters:
  • x (Tensor) – input tensor.

  • model (nn.Module) – classifier.

  • last_layer_name (Optional[str], optional) – last layer node name. Defaults to None. If None, the last layer is automatically selected.

  • temperature (float, optional) – softmax temperature parameter. Defaults to 1.0.

Returns:

scores for each input.

Return type:

Tensor

References

[1] https://arxiv.org/abs/2110.00218

detectors.methods.gram module

detectors.methods.igeood_logits module

class detectors.methods.igeood_logits.IgeoodLogits(model: Module, temperature: float = 1.0, eps: float = 0.0, **kwargs)[source]

Bases: object

IGEOOD detector.

Parameters:
  • model (nn.Module) – classifier.

  • temperature (float, optional) – softmax temperature parameter. Defaults to 1.0.

  • eps (float, optional) – input preprocessing noise value. Defaults to 0.0 (no input preprocessing).

References

[1] https://arxiv.org/abs/2203.07798

end(*args, **kwargs)[source]
start(example: Tensor | None = None, fit_length: int | None = None, *args, **kwargs)[source]
update(x: Tensor, y: Tensor, *args, **kwargs)[source]
detectors.methods.igeood_logits.igeoodlogits_vec(logits, temperature, centroids, epsilon=1e-12)[source]

detectors.methods.kl_matching module

class detectors.methods.kl_matching.KLMatching(model: Module, **kwargs)[source]

Bases: object

KL-Matching detector.

Parameters:

model (nn.Module) – classifier.

References

[1] https://arxiv.org/abs/1911.11132

end()[source]
start(*args, **kwargs)[source]
update(x: Tensor, *args, **kwargs)[source]
detectors.methods.kl_matching.js_divergence(p: Tensor, q: Tensor)[source]
detectors.methods.kl_matching.kl_divergence(p: Tensor, q: Tensor, eps=1e-06)[source]

detectors.methods.knn_euclides module

class detectors.methods.knn_euclides.KnnEuclides(model: Module, features_nodes: List[str] | None = None, all_blocks: bool = False, last_layer: bool = False, pooling_op_name: str = 'avg', aggregation_method_name='mean', alpha: float = 1, k: int = 10, avg_topk: bool = False, **kwargs)[source]

Bases: DetectorWithFeatureExtraction

K-NN detector based on Euclidean distance.

Parameters:
  • model (nn.Module) – Model to be used to extract features

  • features_nodes (Optional[List[str]]) – List of strings that represent the feature nodes. Defaults to None.

  • all_blocks (bool, optional) – If True, use all blocks of the model. Defaults to False.

  • last_layer (bool, optional) – If True, use also the last layer of the model. Defaults to False.

  • pooling_op_name (str, optional) – Pooling operation to be applied to the features. Can be one of [β€œmax”, β€œavg”, β€œflatten”, β€œgetitem”, β€œnone”]. Defaults to β€œavg”.

  • aggregation_method_name (str, optional) – Aggregation method to be applied to the features. Defaults to None.

  • alpha (float, optional) – Alpha parameter for the input pre-processing. Defaults to 1.

  • k (int, optional) – Number of nearest neighbors to be considered. Defaults to 10.

  • avg_topk (bool, optional) – If True, average the top-k scores. Defaults to False.

References

[1] https://arxiv.org/abs/2204.06507

detectors.methods.logit_norm module

https://arxiv.org/abs/2205.09310

detectors.methods.mahalanobis module

class detectors.methods.mahalanobis.Mahalanobis(model: ~torch.nn.modules.module.Module, features_nodes: ~typing.List[str] | None = None, all_blocks: bool = False, last_layer: bool = False, pooling_op_name: str = 'avg_or_getitem', aggregation_method_name: str | None = 'mean', cov_mat_method: ~typing.Literal['EmpiricalCovariance', 'GraphicalLasso', 'GraphicalLassoCV', 'LedoitWolf', 'MinCovDet', 'ShrunkCovariance', 'OAS'] = 'EmpiricalCovariance', mu_cov_inv_est_fn=<function class_cond_mus_cov_inv_matrix>, cov_reg: float = 1e-06, **kwargs)[source]

Bases: DetectorWithFeatureExtraction

Mahalanobis OOD detector.

Parameters:
  • model (nn.Module) – Model to be used to extract features

  • features_nodes (Optional[List[str]]) – List of strings that represent the feature nodes. Defaults to None.

  • all_blocks (bool, optional) – If True, use all blocks of the model. Defaults to False.

  • last_layer (bool, optional) – If True, use also the last layer of the model. Defaults to False.

  • pooling_op_name (str, optional) – Pooling operation to be applied to the features. Can be one of max, avg, flatten, getitem, avg_or_getitem, max_or_getitem, none. Defaults to avg.

  • aggregation_method_name (str, optional) – Aggregation method to be applied to the features. Defaults to None.

  • cov_mat_method (str, optional) – Covariance matrix estimation method. Can be one of: EmpiricalCovariance, GraphicalLasso, GraphicalLassoCV, LedoitWolf, MinCovDet, ShrunkCovariance, OAS. Defaults to EmpiricalCovariance.

  • mu_cov_inv_est_fn (function, optional) – Function to be used to estimate the means, covariance and inverse matrix. Defaults to class_cond_mus_cov_inv_matrix.

  • cov_reg (float, optional) – Covariance regularization. Defaults to 1e-6.

References

[1] https://arxiv.org/abs/1807.03888

detectors.methods.mahalanobis.class_cond_mus_cov_inv_matrix(x: Tensor, targets: Tensor, cov_method: str = 'EmpiricalCovariance', device=device(type='cpu'))[source]
detectors.methods.mahalanobis.mahalanobis_dist_forward_substitution(x: Tensor, y: Tensor, L: Tensor)[source]
detectors.methods.mahalanobis.mahalanobis_distance_inv(x: Tensor, y: Tensor, precision: Tensor)[source]

Mahalanobis distance betwee x and y.

Parameters:
  • x (Tensor) – first point.

  • y (Tensor) – second point.

  • precision (Tensor) – inverse of the covariance matrix.

detectors.methods.mahalanobis.mahalanobis_distance_inv_fast(x: Tensor, y: Tensor, precision: Tensor)[source]

Mahalanobis distance betwee x and y with an accelerated implementation.

Parameters:
  • x (Tensor) – first point.

  • y (Tensor) – second point.

  • precision (Tensor) – inverse of the covariance matrix.

detectors.methods.mahalanobis.mahalanobis_inv_layer_score(x: Tensor, mus: Tensor, inv: Tensor) Tensor[source]
detectors.methods.mahalanobis.mahalanobis_inv_layer_score_fast(x: Tensor, mus: Tensor, inv: Tensor) Tensor[source]

detectors.methods.max_logits module

detectors.methods.max_logits.max_logits(input: Tensor, model: Module, **kwargs) Tensor[source]

Max Logits OOD detector.

Parameters:

logits (Tensor) – input tensor.

Returns:

OOD scores for each input.

Return type:

Tensor

References

[1] https://arxiv.org/abs/1911.11132

detectors.methods.maxcosine module

class detectors.methods.maxcosine.MaxCosineSimilarity(model: Module, features_nodes: List[str] | None = None, all_blocks: bool = False, last_layer: bool = False, pooling_op_name: str = 'avg_or_getitem', aggregation_method_name: str | None = 'mean', **kwargs)[source]

Bases: DetectorWithFeatureExtraction

MaxCosineSimilarity detector.

Parameters:
  • model (nn.Module) – Model to be used to extract features

  • features_nodes (Optional[List[str]]) – List of strings that represent the feature nodes. Defaults to None.

  • all_blocks (bool, optional) – If True, use all blocks of the model. Defaults to False.

  • last_layer (bool, optional) – If True, use also the last layer of the model. Defaults to False.

  • pooling_op_name (str, optional) –

    Pooling operation to be applied to the features. Can be one of:

    max, avg, none, flatten, getitem, avg_or_getitem, max_or_getitem.

    Defaults to β€œavg”.

  • aggregation_method_name (str, optional) – Aggregation method to be applied to the features. Defaults to None.

References

[1] https://openaccess.thecvf.com/content/ACCV2020/html/Techapanurak_Hyperparameter-Free_Out-of-Distribution_Detection_Using_Cosine_Similarity_ACCV_2020_paper.html

detectors.methods.maxcosine.max_cosine_sim_layer_score(x: Tensor, mus: Tensor | List[Tensor], eps=1e-07)[source]

detectors.methods.mcdropout module

detectors.methods.mcdropout.mcdropout(x: Tensor, model: Module, k: int = 5, **kwargs) Tensor[source]

MC Dropout

Forward-propagates the input through the model several times with activated dropout and averages the results.

Parameters:
  • x (Tensor) – input tensor.

  • model (nn.Module) – classifier.

  • k (int, optional) – number of forward passes. Defaults to 5.

References

[1] http://proceedings.mlr.press/v48/gal16.pdf

detectors.methods.msp module

detectors.methods.msp.msp(input: Tensor, model: Module, **kwargs) Tensor[source]

Maximum Softmax Response OOD detector.

Parameters:
  • input (Tensor) – input tensor.

  • model (nn.Module) – classifier.

Returns:

OOD scores for each input.

Return type:

Tensor

References

[1] https://arxiv.org/abs/1610.02136

detectors.methods.naive module

detectors.methods.naive.always_one(input: Tensor, **kwargs) Tensor[source]
detectors.methods.naive.always_zero(input: Tensor, **kwargs) Tensor[source]
detectors.methods.naive.random_score(input: Tensor, **kwargs) Tensor[source]

detectors.methods.odin module

detectors.methods.odin.odin(x: Tensor, model: Module, temperature: float = 1000, eps: float = 0.0, **kwargs) Tensor[source]

ODIN OOD detector.

Parameters:
  • x (Tensor) – input tensor.

  • model (nn.Module) – classifier.

  • temperature (float, optional) – softmax temperature parameter. Defaults to 1000.

  • eps (float, optional) – input preprocessing noise value. Defaults to 0.0 (no input preprocessing).

Returns:

OOD scores for each input.

Return type:

Tensor

References

[1] https://arxiv.org/abs/1706.02690

detectors.methods.oe module

detectors.methods.openmax module

detectors.methods.projection module

class detectors.methods.projection.Projection(model: Module, features_nodes: List[str] | None = None, pooling_op_name: str = 'max_or_getitem', **kwargs)[source]

Bases: DetectorWithFeatureExtraction

end(*args, **kwargs)[source]

Finalize detector fitting process.

This is called after the last call to update and is optional.

detectors.methods.projection.projection_layer_score(x: Tensor, mus: Tensor | List[Tensor], eps=1e-07)[source]

detectors.methods.rankfeat module

detectors.methods.rankfeat.iterate_data_rankfeat(data_loader, model, temperature)[source]

detectors.methods.react module

class detectors.methods.react.ReAct(model: ~torch.nn.modules.module.Module, features_nodes: ~typing.List[str] | None = None, graph_nodes_names: ~typing.List[str] | None = None, insert_node_fn: ~typing.Callable = <function insert_fn>, p=0.9, **kwargs)[source]

Bases: object

ReAct detector.

Parameters:
  • model (torch.nn.Module) – Model to be used to extract features

  • features_nodes (Optional[List[str]]) – List of strings that represent the feature nodes. Defaults to None.

  • graph_nodes_names (Optional[List[str]]) – List of strings that represent the graph nodes. Defaults to None.

  • insert_node_fn (Callable) – Function to be used to insert the node. Defaults to insert_fn.

  • p (float, optional) – Threshold to be used to clip the features. Defaults to 0.9.

References

[1] https://arxiv.org/abs/2111.12797

LIMIT = 2560000
end(*args, **kwargs)[source]
start(*args, **kwargs)[source]
update(x: Tensor, y: Tensor) None[source]
detectors.methods.react.condition_fn(node, equals_to: str)[source]
detectors.methods.react.insert_fn(node, graph: Graph, thr: float = 1.0)[source]
detectors.methods.react.reactify(m: Module, condition_fn: Callable, insert_fn: Callable) Module[source]

detectors.methods.react_projection module

class detectors.methods.react_projection.ReActProjection(model: ~torch.nn.modules.module.Module, features_nodes: ~typing.List[str], pooling_name: str = 'max', graph_nodes_names_thr: ~typing.Dict[str, float] = {'flatten': 1.0}, insert_node_fn: ~typing.Callable = <function insert_fn>, aggregation_method=None, *args, **kwargs)[source]

Bases: Projection

detectors.methods.react_projection.test()[source]

detectors.methods.relative_mahalanobis module

class detectors.methods.relative_mahalanobis.RelativeMahalanobis(model: ~torch.nn.modules.module.Module, features_nodes: ~typing.List[str] | None = None, all_blocks: bool = False, last_layer: bool = False, pooling_op_name: str = 'avg_or_getitem', aggregation_method_name: str | None = 'mean', cov_mat_method: ~typing.Literal['EmpiricalCovariance', 'GraphicalLasso', 'GraphicalLassoCV', 'LedoitWolf', 'MinCovDet', 'ShrunkCovariance', 'OAS'] = 'EmpiricalCovariance', mu_cov_inv_est_fn=<function class_cond_mus_cov_inv_matrix>, cov_reg: float = 1e-06, **kwargs)[source]

Bases: Mahalanobis

RelativeMahalanobis detector.

Parameters:
  • model (nn.Module) – Model to be used to extract features

  • features_nodes (Optional[List[str]]) – List of strings that represent the feature nodes. Defaults to None.

  • all_blocks (bool, optional) – If True, use all blocks of the model. Defaults to False.

  • last_layer (bool, optional) – If True, use also the last layer of the model. Defaults to False.

  • pooling_op_name (str, optional) – Pooling operation to be applied to the features. Can be one of [β€œmax”, β€œavg”, β€œflatten”, β€œgetitem”, β€œnone”]. Defaults to β€œavg”.

  • aggregation_method_name (str, optional) – Aggregation method to be applied to the features. Defaults to None.

  • cov_mat_method (str, optional) – Covariance matrix estimation method. Can be one of [β€œEmpiricalCovariance”, β€œGraphicalLasso”, β€œGraphicalLassoCV”, β€œLedoitWolf”, β€œMinCovDet”, β€œShrunkCovariance”, β€œOAS”]. Defaults to β€œEmpiricalCovariance”.

  • mu_cov_inv_est_fn (function, optional) – Function to be used to estimate the means, covariance and inverse matrix. Defaults to class_cond_mus_cov_inv_matrix.

  • cov_reg (float, optional) – Covariance regularization. Defaults to 1e-6.

References

[1] https://arxiv.org/abs/2106.03004

detectors.methods.relative_mahalanobis.relative_mahalanobis_inv_layer_score(x: Tensor, mus: Tensor, inv: Tensor, background_mu: Tensor, background_inv: Tensor)[source]

detectors.methods.ssd module

class detectors.methods.ssd.SSD(model: Module, nclusters: int | None = None, niter: int = 300, cov_reg=1e-08, **kwargs)[source]

Bases: DetectorWithFeatureExtraction

SSD: A Unified Framework for Self-Supervised Outlier Detection.

Extract features from the last layer of a self supervised model.

References

[1] https://arxiv.org/abs/2103.12051 [2] https://github.com/inspire-group/SSD

detectors.methods.templates module

Generalized detection methods templates.

class detectors.methods.templates.Detector(**kwargs)[source]

Bases: ABC

Detector base class.

abstract __call__(x: Tensor) Tensor[source]

Compute scores for each input at test time.

Parameters:

x (Tensor) – input tensor.

Returns:

scores for each input.

Return type:

Tensor

end(*args, **kwargs)[source]

Finalize detector fitting process.

This is called after the last call to update and is optional.

fit(dataloader, **kwargs)[source]

Fit detector to a dataset.

Parameters:

dataloader (Dataloader) – Dataloader for the fitting dataset.

start(example: Tensor | None = None, fit_length: int | None = None, *args, **kwargs)[source]

Setup detector for fitting parameters.

Parameters:
  • example (Optional[Tensor], optional) – Input example. Useful for pre-allocating memory. Defaults to None.

  • fit_length (Optional[int], optional) – Length of the fitting dataset. Useful for pre-allocating memory. Defaults to None.

This is called before the first call to update and is optional.

update(x: Tensor, y: Tensor, *args, **kwargs)[source]

Accumulate features for detector.

Parameters:
  • x (Tensor) – input tensor.

  • y (Tensor) – target tensor.

This is called for each batch of the fitting dataset and is optional.

class detectors.methods.templates.DetectorWithFeatureExtraction(model: Module, features_nodes: List[str] | None = None, all_blocks: bool = False, last_layer: bool = False, pooling_op_name: str = 'avg_or_getitem', aggregation_method_name: str | None = 'mean', **kwargs)[source]

Bases: Detector

Base class for OOD detectors with feature extraction.

Parameters:
  • model (nn.Module) – Model to be used to extract features

  • features_nodes (Optional[List[str]]) – List of strings that represent the feature nodes. Defaults to None.

  • all_blocks (bool, optional) – If True, use all blocks of the model. Defaults to False.

  • last_layer (bool, optional) – If True, use also the last layer of the model. Defaults to False.

  • pooling_op_name (str, optional) –

    Pooling operation to be applied to the features. Can be one of:

    max, avg, none, flatten, getitem, avg_or_getitem, max_or_getitem.

    Defaults to β€œavg”.

  • aggregation_method_name (str, optional) – Aggregation method to be applied to the features. Defaults to None.

  • **kwargs –

end(*args, **kwargs)[source]

Finalize detector fitting process.

This is called after the last call to update and is optional.

start(example: Tensor | None = None, fit_length: int | None = None, *args, **kwargs)[source]

Setup detector for fitting parameters.

Parameters:
  • example (Optional[Tensor], optional) – Input example. Useful for pre-allocating memory. Defaults to None.

  • fit_length (Optional[int], optional) – Length of the fitting dataset. Useful for pre-allocating memory. Defaults to None.

This is called before the first call to update and is optional.

update(x: Tensor, y: Tensor, *args, **kwargs)[source]

Accumulate features for detector.

Parameters:
  • x (Tensor) – input tensor.

  • y (Tensor) – target tensor.

This is called for each batch of the fitting dataset and is optional.

class detectors.methods.templates.DetectorWithSimpleFE(model: Module, pooling_op_name: str = 'avg_or_getitem', **kwargs)[source]

Bases: DetectorWithFeatureExtraction, ABC

Detector that uses the forward pass of the model.

class detectors.methods.templates.DetectorWrapper(detector, **kwargs)[source]

Bases: Detector

Detector interface.

__repr__()[source]

Return the string representation of the detector.

end(*args, **kwargs)[source]

Finalize detector fitting process.

This is called after the last call to update and is optional.

fit(dataloader, **kwargs)[source]

Fit detector to a dataset.

Parameters:

dataloader (Dataloader) – Dataloader for the fitting dataset.

load_params(path)[source]

Load the parameters of the detector.

save_params(path)[source]

Save the parameters of the detector.

set_hyperparameters(**params)[source]

Set the parameters of the detector.

start(example: Tensor | None = None, fit_length: int | None = None, *args, **kwargs)[source]

Setup detector for fitting parameters.

Parameters:
  • example (Optional[Tensor], optional) – Input example. Useful for pre-allocating memory. Defaults to None.

  • fit_length (Optional[int], optional) – Length of the fitting dataset. Useful for pre-allocating memory. Defaults to None.

This is called before the first call to update and is optional.

update(x: Tensor, y: Tensor, *args, **kwargs)[source]

Accumulate features for detector.

Parameters:
  • x (Tensor) – input tensor.

  • y (Tensor) – target tensor.

This is called for each batch of the fitting dataset and is optional.

class detectors.methods.templates.SimpleFeatureExtractor(model)[source]

Bases: object

detectors.methods.utils module

detectors.methods.utils.adaptive_avg_pool2d(data: Tensor, **kwargs)[source]
detectors.methods.utils.adaptive_max_pool2d(data: Tensor, **kwargs)[source]
detectors.methods.utils.add_output_op(feature_extractor, output_op: Callable) Module[source]
detectors.methods.utils.avg_or_getitem(data: Tensor, **kwargs)[source]
detectors.methods.utils.create_reduction(reduction: str, **kwargs)[source]
detectors.methods.utils.flatten(data: Tensor, **kwargs)[source]
detectors.methods.utils.get_composed_attr(model, attrs: List[str])[source]
detectors.methods.utils.get_last_layer(model: Module)[source]
detectors.methods.utils.get_last_layer_name(model: Module)[source]
detectors.methods.utils.get_penultimate_layer(model: Module)[source]
detectors.methods.utils.get_penultimate_layer_name(model: Module)[source]
detectors.methods.utils.getitem(data: Tensor, **kwargs)[source]
detectors.methods.utils.input_pre_processing(score_fn: Callable, x: Tensor, eps: float)[source]
detectors.methods.utils.max_or_getitem(data: Tensor, **kwargs)[source]
detectors.methods.utils.none_reduction(data: Tensor, **kwargs)[source]
detectors.methods.utils.sklearn_cov_matrix_estimarion(x: ndarray, method: Literal['EmpiricalCovariance', 'GraphicalLasso', 'GraphicalLassoCV', 'LedoitWolf', 'MinCovDet', 'ShrunkCovariance', 'OAS'] = 'EmpiricalCovariance', **method_kwargs)[source]
detectors.methods.utils.torch_reduction_matrix(sigma: Tensor, reduction_method='pseudo')[source]

detectors.methods.vim module

class detectors.methods.vim.ViM(model: Module, last_layer_name: str | None = None, penultimate_layer_name: str | None = None, **kwargs)[source]

Bases: object

Virtual Logit Matching (ViM) detector.

Parameters:
  • model (torch.nn.Module) – Model to be used to extract features

  • last_layer_name (Optional[str]) – Name of the last layer. Defaults to None.

  • penultimate_layer_name (Optional[str]) – Name of the penultimate layer. Defaults to None.

References

[1] https://arxiv.org/abs/2203.10807

end()[source]
start(*args, **kwargs)[source]
update(x: Tensor, y: Tensor, *args, **kwargs)[source]

Module contents

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")