oodeel 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- oodeel/__init__.py +28 -0
- oodeel/aggregator/__init__.py +26 -0
- oodeel/aggregator/base.py +70 -0
- oodeel/aggregator/fisher.py +259 -0
- oodeel/aggregator/mean.py +72 -0
- oodeel/aggregator/std.py +86 -0
- oodeel/datasets/__init__.py +24 -0
- oodeel/datasets/data_handler.py +334 -0
- oodeel/datasets/deprecated/DEPRECATED_data_handler.py +236 -0
- oodeel/datasets/deprecated/DEPRECATED_ooddataset.py +330 -0
- oodeel/datasets/deprecated/DEPRECATED_tf_data_handler.py +671 -0
- oodeel/datasets/deprecated/DEPRECATED_torch_data_handler.py +769 -0
- oodeel/datasets/deprecated/__init__.py +31 -0
- oodeel/datasets/tf_data_handler.py +600 -0
- oodeel/datasets/torch_data_handler.py +672 -0
- oodeel/eval/__init__.py +22 -0
- oodeel/eval/metrics.py +218 -0
- oodeel/eval/plots/__init__.py +27 -0
- oodeel/eval/plots/features.py +345 -0
- oodeel/eval/plots/metrics.py +118 -0
- oodeel/eval/plots/plotly.py +162 -0
- oodeel/extractor/__init__.py +35 -0
- oodeel/extractor/feature_extractor.py +187 -0
- oodeel/extractor/hf_torch_feature_extractor.py +184 -0
- oodeel/extractor/keras_feature_extractor.py +409 -0
- oodeel/extractor/torch_feature_extractor.py +506 -0
- oodeel/methods/__init__.py +47 -0
- oodeel/methods/base.py +570 -0
- oodeel/methods/dknn.py +185 -0
- oodeel/methods/energy.py +119 -0
- oodeel/methods/entropy.py +113 -0
- oodeel/methods/gen.py +113 -0
- oodeel/methods/gram.py +274 -0
- oodeel/methods/mahalanobis.py +209 -0
- oodeel/methods/mls.py +113 -0
- oodeel/methods/odin.py +109 -0
- oodeel/methods/rmds.py +172 -0
- oodeel/methods/she.py +159 -0
- oodeel/methods/vim.py +273 -0
- oodeel/preprocess/__init__.py +31 -0
- oodeel/preprocess/tf_preprocess.py +95 -0
- oodeel/preprocess/torch_preprocess.py +97 -0
- oodeel/types/__init__.py +75 -0
- oodeel/utils/__init__.py +38 -0
- oodeel/utils/general_utils.py +97 -0
- oodeel/utils/operator.py +253 -0
- oodeel/utils/tf_operator.py +269 -0
- oodeel/utils/tf_training_tools.py +219 -0
- oodeel/utils/torch_operator.py +292 -0
- oodeel/utils/torch_training_tools.py +303 -0
- oodeel-0.4.0.dist-info/METADATA +409 -0
- oodeel-0.4.0.dist-info/RECORD +63 -0
- oodeel-0.4.0.dist-info/WHEEL +5 -0
- oodeel-0.4.0.dist-info/licenses/LICENSE +21 -0
- oodeel-0.4.0.dist-info/top_level.txt +2 -0
- tests/__init__.py +22 -0
- tests/tests_tensorflow/__init__.py +37 -0
- tests/tests_tensorflow/tf_methods_utils.py +140 -0
- tests/tests_tensorflow/tools_tf.py +86 -0
- tests/tests_torch/__init__.py +38 -0
- tests/tests_torch/tools_torch.py +151 -0
- tests/tests_torch/torch_methods_utils.py +148 -0
- tests/tools_operator.py +153 -0
oodeel/methods/odin.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright IRT Antoine de Saint Exupéry et Université Paul Sabatier Toulouse III - All
|
|
3
|
+
# rights reserved. DEEL is a research program operated by IVADO, IRT Saint Exupéry,
|
|
4
|
+
# CRIAQ and ANITI - https://www.deel.ai/
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
# furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
# copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
# SOFTWARE.
|
|
23
|
+
import numpy as np
|
|
24
|
+
|
|
25
|
+
from ..types import TensorType
|
|
26
|
+
from ..types import Tuple
|
|
27
|
+
from .base import OODBaseDetector
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ODIN(OODBaseDetector):
|
|
31
|
+
""" "Enhancing The Reliability of Out-of-distribution Image Detection
|
|
32
|
+
in Neural Networks"
|
|
33
|
+
http://arxiv.org/abs/1706.02690
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
temperature (float, optional): Temperature parameter. Defaults to 1000.
|
|
37
|
+
eps (float, optional): Perturbation noise. Defaults to 0.014.
|
|
38
|
+
use_react (bool): if true, apply ReAct method by clipping penultimate
|
|
39
|
+
activations under a threshold value.
|
|
40
|
+
react_quantile (Optional[float]): q value in the range [0, 1] used to compute
|
|
41
|
+
the react clipping threshold defined as the q-th quantile penultimate layer
|
|
42
|
+
activations. Defaults to 0.8.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
temperature: float = 1000,
|
|
48
|
+
eps: float = 0.014,
|
|
49
|
+
use_react: bool = False,
|
|
50
|
+
use_scale: bool = False,
|
|
51
|
+
use_ash: bool = False,
|
|
52
|
+
react_quantile: float = 0.8,
|
|
53
|
+
scale_percentile: float = 0.85,
|
|
54
|
+
ash_percentile: float = 0.90,
|
|
55
|
+
**kwargs,
|
|
56
|
+
):
|
|
57
|
+
super().__init__(
|
|
58
|
+
eps=eps,
|
|
59
|
+
temperature=temperature,
|
|
60
|
+
use_react=use_react,
|
|
61
|
+
use_scale=use_scale,
|
|
62
|
+
use_ash=use_ash,
|
|
63
|
+
react_quantile=react_quantile,
|
|
64
|
+
scale_percentile=scale_percentile,
|
|
65
|
+
ash_percentile=ash_percentile,
|
|
66
|
+
**kwargs,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def _score_tensor(self, inputs: TensorType) -> Tuple[np.ndarray]:
|
|
70
|
+
"""
|
|
71
|
+
Computes an OOD score for input samples "inputs" based on
|
|
72
|
+
the distance to nearest neighbors in the feature space of self.model
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
inputs (TensorType): input samples to score
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
Tuple[np.ndarray]: scores, logits
|
|
79
|
+
"""
|
|
80
|
+
if self.eps > 0:
|
|
81
|
+
x = self._input_perturbation(inputs, self.eps, self.temperature)
|
|
82
|
+
|
|
83
|
+
_, logits = self.feature_extractor.predict_tensor(x)
|
|
84
|
+
logits_s = logits / self.temperature
|
|
85
|
+
probits = self.op.softmax(logits_s)
|
|
86
|
+
probits = self.op.convert_to_numpy(probits)
|
|
87
|
+
scores = -np.max(probits, axis=1)
|
|
88
|
+
return scores
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def requires_to_fit_dataset(self) -> bool:
|
|
92
|
+
"""
|
|
93
|
+
Whether an OOD detector needs a `fit_dataset` argument in the fit function.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
bool: True if `fit_dataset` is required else False.
|
|
97
|
+
"""
|
|
98
|
+
return False
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def requires_internal_features(self) -> bool:
|
|
102
|
+
"""
|
|
103
|
+
Whether an OOD detector acts on internal model features.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
bool: True if the detector perform computations on an intermediate layer
|
|
107
|
+
else False.
|
|
108
|
+
"""
|
|
109
|
+
return False
|
oodeel/methods/rmds.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright IRT Antoine de Saint Exupéry et Université Paul Sabatier Toulouse III - All
|
|
3
|
+
# rights reserved. DEEL is a research program operated by IVADO, IRT Saint Exupéry,
|
|
4
|
+
# CRIAQ and ANITI - https://www.deel.ai/
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
# furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
# copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
# SOFTWARE.
|
|
23
|
+
from typing import List
|
|
24
|
+
from typing import Optional
|
|
25
|
+
from typing import Tuple
|
|
26
|
+
|
|
27
|
+
import numpy as np
|
|
28
|
+
|
|
29
|
+
from ..aggregator import BaseAggregator
|
|
30
|
+
from ..types import TensorType
|
|
31
|
+
from .mahalanobis import Mahalanobis
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class RMDS(Mahalanobis):
|
|
35
|
+
"""
|
|
36
|
+
"A Simple Fix to Mahalanobis Distance for Improving Near-OOD Detection"
|
|
37
|
+
Updated to work with multiple feature layers.
|
|
38
|
+
|
|
39
|
+
This detector computes class-conditional Mahalanobis scores from several
|
|
40
|
+
feature layers and, additionally, computes background Mahalanobis scores per layer.
|
|
41
|
+
The final per-layer score is obtained by subtracting the background score from the
|
|
42
|
+
class-conditional score. With multiple layers, the per-layer scores are aggregated
|
|
43
|
+
by a provided aggregator (or by a default StdNormalizedAggregator if None is
|
|
44
|
+
given).
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
eps (float): Perturbation noise. Defaults to 0.0014.
|
|
48
|
+
temperature (float, optional): Temperature parameter. Defaults to 1000.
|
|
49
|
+
aggregator (Optional[BaseAggregator]): Aggregator to combine scores from
|
|
50
|
+
multiple feature layers. For a single layer this can be left as None.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
def __init__(
|
|
54
|
+
self,
|
|
55
|
+
eps: float = 0.0014,
|
|
56
|
+
temperature: float = 1000,
|
|
57
|
+
aggregator: Optional[BaseAggregator] = None,
|
|
58
|
+
**kwargs,
|
|
59
|
+
):
|
|
60
|
+
super().__init__(
|
|
61
|
+
eps=eps, temperature=temperature, aggregator=aggregator, **kwargs
|
|
62
|
+
)
|
|
63
|
+
# Will be filled by `_fit_layer`.
|
|
64
|
+
self._layer_background_stats: List[Tuple[TensorType, np.ndarray]] = []
|
|
65
|
+
|
|
66
|
+
# === Per-layer logic ===
|
|
67
|
+
def _fit_layer(
|
|
68
|
+
self,
|
|
69
|
+
layer_id: int,
|
|
70
|
+
layer_features: np.ndarray,
|
|
71
|
+
info: dict,
|
|
72
|
+
**kwargs,
|
|
73
|
+
) -> None:
|
|
74
|
+
"""Compute statistics for a single layer and store parameters.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
layer_id: Index of the processed layer. Unused here.
|
|
78
|
+
layer_features: In-distribution features for this layer.
|
|
79
|
+
info: Dictionary containing the training labels.
|
|
80
|
+
"""
|
|
81
|
+
labels = info["labels"]
|
|
82
|
+
|
|
83
|
+
if isinstance(layer_features, np.ndarray):
|
|
84
|
+
layer_features = self.op.from_numpy(layer_features)
|
|
85
|
+
|
|
86
|
+
mus, pinv_cov = super()._compute_layer_stats(layer_features, labels)
|
|
87
|
+
mu_bg, pinv_cov_bg = self._background_stats(layer_features)
|
|
88
|
+
|
|
89
|
+
self._layer_stats.append((mus, pinv_cov))
|
|
90
|
+
self._layer_background_stats.append((mu_bg, pinv_cov_bg))
|
|
91
|
+
|
|
92
|
+
def _score_layer(
|
|
93
|
+
self,
|
|
94
|
+
layer_id: int,
|
|
95
|
+
layer_features: TensorType,
|
|
96
|
+
info: dict,
|
|
97
|
+
fit: bool = False,
|
|
98
|
+
**kwargs,
|
|
99
|
+
) -> np.ndarray:
|
|
100
|
+
"""Compute the residual Mahalanobis OOD score for a single layer.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
layer_id: Index of the processed layer.
|
|
104
|
+
layer_features: Flattened feature matrix `[B, D]` for the batch.
|
|
105
|
+
info: Unused dictionary of auxiliary data.
|
|
106
|
+
fit: Whether scoring is performed during fitting. Unused here.
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
np.ndarray: 1-D array of **negative** residual log-likelihoods.
|
|
110
|
+
"""
|
|
111
|
+
mus, pinv_cov = self._layer_stats[layer_id]
|
|
112
|
+
mu_bg, pinv_cov_bg = self._layer_background_stats[layer_id]
|
|
113
|
+
feats = self.op.flatten(layer_features)
|
|
114
|
+
g_scores = self._gaussian_log_probs(feats, mus, pinv_cov)
|
|
115
|
+
bg_score = self._background_log_prob(feats, mu_bg, pinv_cov_bg)
|
|
116
|
+
corrected = self.op.max(g_scores - bg_score, dim=1)
|
|
117
|
+
return -self.op.convert_to_numpy(corrected)
|
|
118
|
+
|
|
119
|
+
# === Internal utilities ===
|
|
120
|
+
def _background_stats(
|
|
121
|
+
self, layer_features: TensorType
|
|
122
|
+
) -> Tuple[TensorType, TensorType]:
|
|
123
|
+
"""Compute class-agnostic Gaussian statistics for **one** layer.
|
|
124
|
+
|
|
125
|
+
This helper forms the *background* distribution used in RMDS. It treats
|
|
126
|
+
**all** in-distribution samples of a layer as coming from a single
|
|
127
|
+
multivariate Gaussian and returns its mean and (pseudo-inverse)
|
|
128
|
+
covariance.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
layer_features (TensorType): Feature representations of shape
|
|
132
|
+
`[N, ...]` for a single layer, where `N` is the number of
|
|
133
|
+
in-distribution training samples.
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
Tuple[TensorType, TensorType]:
|
|
137
|
+
* `mu_bg` - mean feature vector of shape `[D]` (same backend
|
|
138
|
+
tensor type as the inputs).
|
|
139
|
+
* `pinv_cov_bg` - pseudo-inverse covariance matrix of shape
|
|
140
|
+
`[D, D]`.
|
|
141
|
+
"""
|
|
142
|
+
feats = self.op.flatten(layer_features)
|
|
143
|
+
mu_bg = self.op.mean(feats, dim=0)
|
|
144
|
+
zero = feats - mu_bg
|
|
145
|
+
cov_bg = self.op.matmul(self.op.t(zero), zero) / zero.shape[0]
|
|
146
|
+
pinv_cov_bg = self.op.pinv(cov_bg)
|
|
147
|
+
return mu_bg, pinv_cov_bg
|
|
148
|
+
|
|
149
|
+
def _background_log_prob(
|
|
150
|
+
self, out_features: TensorType, mu_bg: TensorType, pinv_cov_bg: TensorType
|
|
151
|
+
) -> TensorType:
|
|
152
|
+
"""
|
|
153
|
+
Compute the Mahalanobis-based background score for a single feature layer.
|
|
154
|
+
|
|
155
|
+
For each test sample, this method computes the log probability (up to a
|
|
156
|
+
constant) under the background Gaussian distribution estimated from the
|
|
157
|
+
in-distribution data.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
out_features (TensorType): Feature tensor for test samples.
|
|
161
|
+
mu_bg: Background mean vector for the layer.
|
|
162
|
+
pinv_cov_bg (TensorType): Pseudo-inverse of the background covariance
|
|
163
|
+
matrix.
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
TensorType: Background confidence scores (reshaped as [num_samples, 1]).
|
|
167
|
+
"""
|
|
168
|
+
zero = out_features - mu_bg
|
|
169
|
+
log_prob = -0.5 * self.op.diag(
|
|
170
|
+
self.op.matmul(self.op.matmul(zero, pinv_cov_bg), self.op.t(zero))
|
|
171
|
+
)
|
|
172
|
+
return self.op.reshape(log_prob, (-1, 1))
|
oodeel/methods/she.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright IRT Antoine de Saint Exupéry et Université Paul Sabatier Toulouse III - All
|
|
3
|
+
# rights reserved. DEEL is a research program operated by IVADO, IRT Saint Exupéry,
|
|
4
|
+
# CRIAQ and ANITI - https://www.deel.ai/
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
# furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
# copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
# SOFTWARE.
|
|
23
|
+
from typing import List
|
|
24
|
+
from typing import Optional
|
|
25
|
+
|
|
26
|
+
import numpy as np
|
|
27
|
+
|
|
28
|
+
from ..aggregator import BaseAggregator
|
|
29
|
+
from ..types import TensorType
|
|
30
|
+
from .base import FeatureBasedDetector
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class SHE(FeatureBasedDetector):
|
|
34
|
+
"""
|
|
35
|
+
"Out-of-Distribution Detection based on In-Distribution Data Patterns Memorization
|
|
36
|
+
with Modern Hopfield Energy"
|
|
37
|
+
[link](https://openreview.net/forum?id=KkazG4lgKL)
|
|
38
|
+
|
|
39
|
+
This method first computes the mean of the internal layer representation of ID data
|
|
40
|
+
for each ID class. This mean is seen as the average of the ID activation patterns
|
|
41
|
+
as defined in the original paper.
|
|
42
|
+
The method then returns the maximum value of the dot product between the internal
|
|
43
|
+
layer representation of the input and the average patterns, which is a simplified
|
|
44
|
+
version of Hopfield energy as defined in the original paper. The per-layer
|
|
45
|
+
confidence values can be combined through an aggregator to yield a single score
|
|
46
|
+
|
|
47
|
+
Remarks:
|
|
48
|
+
* An input perturbation is applied in the same way as in ODIN score
|
|
49
|
+
* The original paper only considers the penultimate layer of the neural
|
|
50
|
+
network, while we aggregate the results of multiple layers following different
|
|
51
|
+
normalization strategies (see `BaseAggregator` for more details).
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
eps (float): Perturbation noise. Defaults to 0.0014.
|
|
55
|
+
temperature (float, optional): Temperature parameter. Defaults to 1000.
|
|
56
|
+
aggregator: Optional object implementing the `BaseAggregator` interface. It is
|
|
57
|
+
used to combine the negative per-layer SHE scores returned by
|
|
58
|
+
`_score_layer`. If *None* and more than one layer is employed, a
|
|
59
|
+
`StdNormalizedAggregator` is instantiated automatically.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
def __init__(
|
|
63
|
+
self,
|
|
64
|
+
eps: float = 0.0014,
|
|
65
|
+
temperature: float = 1000,
|
|
66
|
+
aggregator: Optional[BaseAggregator] = None,
|
|
67
|
+
**kwargs,
|
|
68
|
+
) -> None:
|
|
69
|
+
super().__init__(
|
|
70
|
+
eps=eps, temperature=temperature, aggregator=aggregator, **kwargs
|
|
71
|
+
)
|
|
72
|
+
self.eps = eps
|
|
73
|
+
self.temperature = temperature
|
|
74
|
+
self.postproc_fns = None # Will be set in `_fit_to_dataset`.
|
|
75
|
+
|
|
76
|
+
# Fitted attributes
|
|
77
|
+
self._classes: Optional[np.ndarray] = None
|
|
78
|
+
self._layer_mus: List[TensorType] = [] # Shape per layer: [D, n_classes]
|
|
79
|
+
|
|
80
|
+
# === Per-layer logic ===
|
|
81
|
+
def _fit_layer(
|
|
82
|
+
self,
|
|
83
|
+
layer_id: int,
|
|
84
|
+
layer_features: np.ndarray,
|
|
85
|
+
info: dict,
|
|
86
|
+
**kwargs,
|
|
87
|
+
) -> None:
|
|
88
|
+
"""Compute mean vectors for a single layer.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
layer_id: Index of the processed layer.
|
|
92
|
+
layer_features: Tensor of shape `(N, D)` containing the flattened
|
|
93
|
+
activations of in-distribution samples for one layer.
|
|
94
|
+
info: Dictionary containing the training labels.
|
|
95
|
+
"""
|
|
96
|
+
labels_np = info["labels"]
|
|
97
|
+
preds_np = np.argmax(info["logits"], axis=1)
|
|
98
|
+
|
|
99
|
+
if self._classes is None:
|
|
100
|
+
self._classes = np.sort(np.unique(labels_np))
|
|
101
|
+
|
|
102
|
+
mus_per_cls = []
|
|
103
|
+
for cls in self._classes:
|
|
104
|
+
idx = np.equal(labels_np, cls) & np.equal(preds_np, cls)
|
|
105
|
+
feats_cls = layer_features[idx]
|
|
106
|
+
mu = np.expand_dims(np.mean(feats_cls, axis=0), axis=0)
|
|
107
|
+
mus_per_cls.append(mu)
|
|
108
|
+
mus_layer = self.op.from_numpy(np.concatenate(mus_per_cls, axis=0))
|
|
109
|
+
mus_layer = self.op.permute(mus_layer, (1, 0))
|
|
110
|
+
|
|
111
|
+
self._layer_mus.append(mus_layer)
|
|
112
|
+
|
|
113
|
+
def _score_layer(
|
|
114
|
+
self,
|
|
115
|
+
layer_id: int,
|
|
116
|
+
layer_features: TensorType,
|
|
117
|
+
info: dict,
|
|
118
|
+
fit: bool = False,
|
|
119
|
+
**kwargs,
|
|
120
|
+
) -> np.ndarray:
|
|
121
|
+
"""Compute *unnormalised* SHE confidence for a single layer.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
layer_id (int): Index of the processed layer.
|
|
125
|
+
layer_features (TensorType): Feature tensor of shape `[B, D]` for the
|
|
126
|
+
current batch.
|
|
127
|
+
info (dict): Unused dictionary of auxiliary data.
|
|
128
|
+
fit: Whether scoring is performed during fitting. Unused here.
|
|
129
|
+
|
|
130
|
+
"""
|
|
131
|
+
mus_layer = self._layer_mus[layer_id]
|
|
132
|
+
she = (
|
|
133
|
+
self.op.matmul(self.op.squeeze(layer_features), mus_layer)
|
|
134
|
+
/ layer_features.shape[1]
|
|
135
|
+
)
|
|
136
|
+
she = self.op.max(she, dim=1)
|
|
137
|
+
return -self.op.convert_to_numpy(she)
|
|
138
|
+
|
|
139
|
+
# === Properties ===
|
|
140
|
+
@property
|
|
141
|
+
def requires_to_fit_dataset(self) -> bool:
|
|
142
|
+
"""
|
|
143
|
+
Whether an OOD detector needs a `fit_dataset` argument in the fit function.
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
bool: True if `fit_dataset` is required else False.
|
|
147
|
+
"""
|
|
148
|
+
return True
|
|
149
|
+
|
|
150
|
+
@property
|
|
151
|
+
def requires_internal_features(self) -> bool:
|
|
152
|
+
"""
|
|
153
|
+
Whether an OOD detector acts on internal model features.
|
|
154
|
+
|
|
155
|
+
Returns:
|
|
156
|
+
bool: True if the detector perform computations on an intermediate layer
|
|
157
|
+
else False.
|
|
158
|
+
"""
|
|
159
|
+
return True
|