JTLA#
- class scio.scores.JTLA(*, act_norm=2, mode='raw', test=?, layer_aggregation='fisher', layer_aggregation_consecutive=False, class_aggregation='nat', pred_conditional=True, index_metric='ip')[source]#
Bases:
BaseScoreClassifJTLA for classification.
The proposed framework essentially works in 3 steps:
Statistical tests, conditional on both layer and class (true and predicted);
Layer aggregation;
Class aggregation.
- Parameters:
test (
JTLATestMultinomial) – Defines the statistical test to use. Currently, only multinomial tests are supported. For possible values, refer toJTLATestMultinomial.layer_aggregation (
str) –Defines layers aggregation scheme. The following values are supported.
"lpe": multi-layer normalization scheme using aK-LPE. Uses the same distance as nearest neighbors search. This approach requires a hyperparameterkfor nearest neighbors search. By default, it usestest["k"], which can be overriden by specifying a suffix:k. Examples:"lpe","lpe:20".Any value from
AggrName: \(1\)D aggregation of \(p\)-values. These may be appended":"followed by a coma-separated list of integersn, indicating that combined \(p\)-values forn-tuples of layers must be computed. Appending":1"is a no-op. Examples:"sum","harmonic:2","fisher:1,2,3".
layer_aggregation_consecutive (
bool) – Whethern-tuples for layer aggregation are restricted to consecutive layers. Ignored for"lpe"aggregation. Defaults toFalse.class_aggregation (
ClassAggrLike) – SeeClassAggr. Defaults to"nat".pred_conditional (
bool) – Whether to use predicted-class-conditional scores as described in [RCJB21]. Note that if the classifier is perfect on calibration samples, this has no effect. IfFalse, every predicted-class-conditional object from [RCJB21] is replaced with its true-class-conditional counterpart. Defaults toTrue.index_metric (
IndexMetricLike) – Kind of metric to use for nearest neighbors search. SeeIndexMetric. Defaults to"ip".mode – See
BaseScoreClassif.act_norm – See
BaseScore. Now defaults to2.
Notes
Here are a few key differences between our implementation and the authors’:
No bootstraping for \(p\)-values estimation;
No dimensionality reduction of latent spaces before neighbors search. Since they use neighborhood preserving projection, it should not impact neighbor search results and this is only seen as a computational acceleration;
New option
mode="dcm"forMultinomialTestMode;The layer aggregation can be done on
n-tuples with arbitrary sets ofn, and with or without restricting to consecutive layers.It is possible to skip resorting to pred-class-conditional tests.
References
[RCJB21] (1,2,3,4)Jayaram Raghuram, Varun Chandrasekaran, Somesh Jha, and Suman Banerjee. A general framework for detecting anomalous inputs to DNN classifiers. In Proceedings of the 38th International Conference on Machine Learning, volume 139, 8764–8775. 2021. URL: https://proceedings.mlr.press/v139/raghuram21a.html.
Hint
Below this point, the documentation is meant for development purposes only. Manual use of any listed member is highly discouraged. For usage, see Inferring with Confidence.
Useful methods defined here
aggregate_classes(q_values)Aggregate \(q\)-values accross classes.
aggregate_layers(tests)Aggregate tests results accross layers.
compute_p_values(reference_tests, query_tests)Compute \(p\)-values for query samples.
Store layer aggregation method and
n-tuples to use.For lpe layer aggregation prepare indexes and query calib.
prepare_tests(all_activations, n_classes)Multinomial only for now.
run_tests(all_activations)Run class-conditional statistical tests.
- aggregate_classes(q_values)[source]#
Aggregate \(q\)-values accross classes.
Differences with [RCJB21, section 4.4]: no log scale (unimportant) and inverse ratio (convention).
- Parameters:
q_values (
tuple[Tensor, Tensor | None]) – Output ofaggregate_layers(). It’s a \(2\)-tuple with shapes (whenTensor)(n_samples, n_classes).- Returns:
conformity (
Tensor) – Shape(n_samples, n_classes). Result of \(q\)-values aggregation.- Raises:
ValueError – If
self.class_aggregationvalue is unsupported.
- aggregate_layers(tests)[source]#
Aggregate tests results accross layers.
- Parameters:
tests (
tuple[Tensor, Tensor | None]) – Output ofrun_tests()on query samples. Shapes(n_classes, n_samples, n_layers).- Returns:
q_values_true (
Tensor) – Shape(n_samples, n_classes). True-class-conditional aggregated \(q\)-values.q_values_pred (
Tensor | None) – NotNoneonly ifself.pred_conditional. In this case, same shape asq_values_true. Pred-class-conditional aggregated \(q\)-values.
Note
High \(q\)-value \(\longleftrightarrow\) high conformity.
- compute_p_values(reference_tests, query_tests)[source]#
Compute \(p\)-values for query samples.
Following [RCJB21, eq. (8)], for test values \((t_1, ..., t_n)\) from \(n\) layers, we define \(p := \mathbb{P}(T_1\geqslant t_1, ..., T_n\geqslant t_n)\) where \(n\)-tuples \((T_1, ..., T_n)\) are sampled across calibrations samples. This method computes and stacks all such \(p\)-values across all the
n-tuples of layers, forninself.layer_aggregation_tuples.This implemention is a trivial counting approach. See orthogonal range searching for more efficient methods.
- Parameters:
reference_tests (
Tensor) – Reference tests results, against which to compute \(p\)-values. Shape(n_reference_samples, n_layers).query_tests (
Tensor) – Query tests results, for which to compute \(p\)-values. Shape(n_query_samples, n_layers).
- Returns:
p_values (
Tensor) – Shape(n_query_samples, n_p_values). The number of \(p\)-values can be expressed assum(num_n_tuples for n in required_tuples).
- parse_layer_aggregation()[source]#
Store layer aggregation method and
n-tuples to use.Handles special case of
"lpe"aggregation.
- prepare_tests(all_activations, n_classes)[source]#
Multinomial only for now. Creates indexes and stats tests.
Note
For multinomial test: Sets attributes
indexes,all_llrs_trueandall_llrs_pred. The last two are nested tuples of shape(n_layers, n_classes), unlessnot self.pred_conditional, in which caseself.all_llrs_trueis set toitertools.repeat(None).
- run_tests(all_activations)[source]#
Run class-conditional statistical tests.
- Parameters:
all_activations (
tuple[Tensor]) – Layers’ batched activations, for which to run statistical tests.- Returns:
tests_true (
Tensor) – Shape(n_classes, n_samples, n_layers). True-class-conditional tests results.tests_pred (
Tensor | None) – NotNoneonly ifself.pred_conditional. In this case, same shape astests_true. Predicted-class-conditional tests results.
Note
High test result \(\longleftrightarrow\) high deviation.