OOD methods
OODBaseDetector
¶
Bases: ABC
Base Class for methods that assign a score to unseen samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
use_react |
bool
|
if true, apply ReAct method by clipping penultimate activations under a threshold value. |
False
|
react_quantile |
Optional[float]
|
q value in the range [0, 1] used to compute the react clipping threshold defined as the q-th quantile penultimate layer activations. Defaults to 0.8. |
0.8
|
Source code in oodeel/methods/base.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
requires_internal_features: bool
property
¶
Whether an OOD detector acts on internal model features.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the detector perform computations on an intermediate layer |
bool
|
else False. |
requires_to_fit_dataset: bool
property
¶
Whether an OOD detector needs a fit_dataset
argument in the fit function.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
__call__(inputs)
¶
Convenience wrapper for score
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Union[ItemType, DatasetType]
|
dataset or tensors to score. |
required |
threshold |
float
|
threshold to use for distinguishing between OOD and ID |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: array of 0 for ID samples and 1 for OOD samples |
Source code in oodeel/methods/base.py
277 278 279 280 281 282 283 284 285 286 287 288 |
|
fit(model, fit_dataset=None, feature_layers_id=[], input_layer_id=None, **kwargs)
¶
Prepare the detector for scoring: * Constructs the feature extractor based on the model * Calibrates the detector on ID data "fit_dataset" if needed, using self._fit_to_dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Callable
|
model to extract the features from |
required |
fit_dataset |
Optional[Union[ItemType, DatasetType]]
|
dataset to fit the detector on |
None
|
feature_layers_id |
List[int]
|
list of str or int that identify features to output. If int, the rank of the layer in the layer list If str, the name of the layer. Defaults to [-1] |
[]
|
input_layer_id |
List[int]
|
= list of str or int that identify the input layer of the feature extractor. If int, the rank of the layer in the layer list If str, the name of the layer. Defaults to None. |
None
|
Source code in oodeel/methods/base.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
score(dataset)
¶
Computes an OOD score for input samples "inputs".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Union[ItemType, DatasetType]
|
dataset or tensors to score |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
ndarray
|
scores or list of scores (depending on the input) and a dictionary containing logits and labels. |
Source code in oodeel/methods/base.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
DKNN
¶
Bases: OODBaseDetector
"Out-of-Distribution Detection with Deep Nearest Neighbors" https://arxiv.org/abs/2204.06507
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nearest |
int
|
number of nearest neighbors to consider. Defaults to 1. |
1
|
Source code in oodeel/methods/dknn.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
requires_internal_features: bool
property
¶
Whether an OOD detector acts on internal model features.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the detector perform computations on an intermediate layer |
bool
|
else False. |
requires_to_fit_dataset: bool
property
¶
Whether an OOD detector needs a fit_dataset
argument in the fit function.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
_fit_to_dataset(fit_dataset)
¶
Constructs the index from ID data "fit_dataset", which will be used for nearest neighbor search.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_dataset |
Union[TensorType, DatasetType]
|
input dataset (ID) to construct the index with. |
required |
Source code in oodeel/methods/dknn.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
_l2_normalization(feat)
¶
L2 normalization of a tensor along the last dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feat |
ndarray
|
the tensor to normalize |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: the normalized tensor |
Source code in oodeel/methods/dknn.py
86 87 88 89 90 91 92 93 94 95 |
|
_score_tensor(inputs)
¶
Computes an OOD score for input samples "inputs" based on the distance to nearest neighbors in the feature space of self.model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples to score |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray]
|
Tuple[np.ndarray]: scores, logits |
Source code in oodeel/methods/dknn.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
Energy
¶
Bases: OODBaseDetector
Energy Score method for OOD detection. "Energy-based Out-of-distribution Detection" https://arxiv.org/abs/2010.03759
This method assumes that the model has been trained with cross entropy loss $CE(model(x))$ where $model(x)=(l_{c})_{c=1}^{C}$ are the logits predicted for input $x$. The implementation assumes that the logits are retreieved using the output with linear activation.
The energy score for input $x$ is given by $$ -\log \sum_{c=0}^C \exp(l_c)$$
where $model(x)=(l_{c})_{c=1}^{C}$ are the logits predicted by the model on $x$. As always, training data is expected to have lower score than OOD data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
use_react |
bool
|
if true, apply ReAct method by clipping penultimate activations under a threshold value. |
False
|
react_quantile |
Optional[float]
|
q value in the range [0, 1] used to compute the react clipping threshold defined as the q-th quantile penultimate layer activations. Defaults to 0.8. |
0.8
|
Source code in oodeel/methods/energy.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
requires_internal_features: bool
property
¶
Whether an OOD detector acts on internal model features.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the detector perform computations on an intermediate layer |
bool
|
else False. |
requires_to_fit_dataset: bool
property
¶
Whether an OOD detector needs a fit_dataset
argument in the fit function.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
_fit_to_dataset(fit_dataset)
¶
Fits the OOD detector to fit_dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_dataset |
DatasetType
|
dataset to fit the OOD detector on |
required |
Source code in oodeel/methods/energy.py
87 88 89 90 91 92 93 94 |
|
_score_tensor(inputs)
¶
Computes an OOD score for input samples "inputs" based on energy, namey $-logsumexp(logits(inputs))$.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples to score |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray]
|
Tuple[np.ndarray]: scores, logits |
Source code in oodeel/methods/energy.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
Entropy
¶
Bases: OODBaseDetector
Entropy OOD score
The method consists in using the Entropy of the input data computed using the Entropy $\sum_{c=0}^C p(y=c| x) \times log(p(y=c | x))$ where $p(y=c| x) = \text{model}(x)$.
Reference https://proceedings.neurips.cc/paper/2019/hash/1e79596878b2320cac26dd792a6c51c9-Abstract.html, Neurips 2019.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
use_react |
bool
|
if true, apply ReAct method by clipping penultimate activations under a threshold value. |
False
|
react_quantile |
Optional[float]
|
q value in the range [0, 1] used to compute the react clipping threshold defined as the q-th quantile penultimate layer activations. Defaults to 0.8. |
0.8
|
Source code in oodeel/methods/entropy.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
requires_internal_features: bool
property
¶
Whether an OOD detector acts on internal model features.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the detector perform computations on an intermediate layer |
bool
|
else False. |
requires_to_fit_dataset: bool
property
¶
Whether an OOD detector needs a fit_dataset
argument in the fit function.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
_fit_to_dataset(fit_dataset)
¶
Fits the OOD detector to fit_dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_dataset |
DatasetType
|
dataset to fit the OOD detector on |
required |
Source code in oodeel/methods/entropy.py
82 83 84 85 86 87 88 89 |
|
_score_tensor(inputs)
¶
Computes an OOD score for input samples "inputs" based on entropy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples to score |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray]
|
Tuple[np.ndarray]: scores, logits |
Source code in oodeel/methods/entropy.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
Gram
¶
Bases: OODBaseDetector
"Detecting Out-of-Distribution Examples with Gram Matrices" link
Important Disclaimer: Taking the statistics of min/max deviation, as in the paper raises some problems.
The method often yields a score of zero for some tasks. This is expected since the min/max among the samples of a random variable becomes more and more extreme with the sample size. As a result, computing the min/max over the training set is likely to produce min/max values that are so extreme that none of the in distribution correlations of the validation set goes beyond these threshold. The worst is that a significant part of ood data does not exceed the thresholds either. This can be aleviated by computing the min/max over a limited number of sample. However, it is counter-intuitive and, in our opinion, not desirable: adding some more information should only improve a method.
Hence, we decided to replace the min/max by the q / 1-q quantile, with q a new parameter of the method. Specifically, instead of the deviation as defined in eq. 3 of the paper, we use the definition $$ \delta(t_q, t_{1-q}, value) = \begin{cases} 0 & \text{if} \; t_q \leq value \leq t_{1-q}, \;\; \frac{t_q - value}{|t_q|} & \text{if } value < t_q, \;\; \frac{value - t_{1-q}}{|t_q|} & \text{if } value > t_{1-q} \end{cases} $$ With this new deviation, the more point we add, the more accurate the quantile becomes. In addition, the method can be made more or less discriminative by toggling the value of q.
Finally, we found that this approach improved the performance of the baseline in our experiments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
orders |
List[int]
|
power orders to consider for the correlation matrix |
[i for i in range(1, 11)]
|
quantile |
float
|
quantile to consider for the correlations to build the deviation threshold. |
0.01
|
Source code in oodeel/methods/gram.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
requires_internal_features: bool
property
¶
Whether an OOD detector acts on internal model features.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the detector perform computations on an intermediate layer |
bool
|
else False. |
requires_to_fit_dataset: bool
property
¶
Whether an OOD detector needs a fit_dataset
argument in the fit function.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
_deviation(stats, min_maxs)
¶
Compute the deviation wrt quantiles (min/max) for feature_maps
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stats |
TensorType
|
The list of gram matrices (stacked power-wise) for which we want to compute the deviation. |
required |
min_maxs |
TensorType
|
The quantiles (tensorised) to compute the deviation against. |
required |
Returns:
Name | Type | Description |
---|---|---|
List |
TensorType
|
A list with one element per layer containing a tensor of per-sample deviation. |
Source code in oodeel/methods/gram.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
_fit_to_dataset(fit_dataset, val_split=0.2)
¶
Compute the quantiles of channelwise correlations for each layer, power of gram matrices, and class. Then, compute the normalization constants for the deviation. To stay faithful to the spirit of the original method, we still name the quantiles min/max
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_dataset |
Union[TensorType, DatasetType]
|
input dataset (ID) to construct the index with. |
required |
val_split |
float
|
The percentage of fit data to use as validation data for normalization. Default to 0.2. |
0.2
|
Source code in oodeel/methods/gram.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
_score_tensor(inputs)
¶
Computes an OOD score for input samples "inputs" based on the aggregation of deviations from quantiles of in-distribution channel-wise correlations evaluate for each layer, power of gram matrices, and class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples to score |
required |
Returns:
Type | Description |
---|---|
ndarray
|
scores |
Source code in oodeel/methods/gram.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
_stat(feature_map)
¶
Compute the correlation map (stat) for a given feature map. The values for each power of gram matrix are contained in the same tensor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_map |
TensorType
|
The input feature_map |
required |
Returns:
Name | Type | Description |
---|---|---|
TensorType |
TensorType
|
The stacked gram matrices power-wise. |
Source code in oodeel/methods/gram.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
MLS
¶
Bases: OODBaseDetector
Maximum Logit Scores method for OOD detection. "Open-Set Recognition: a Good Closed-Set Classifier is All You Need?" https://arxiv.org/abs/2110.06207, and Maximum Softmax Score "A Baseline for Detecting Misclassified and Out-of-Distribution Examples in Neural Networks" http://arxiv.org/abs/1610.02136
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_activation |
str
|
activation function for the last layer. If "linear", the method is MLS and if "softmax", the method is MSS. Defaults to "linear". |
'linear'
|
use_react |
bool
|
if true, apply ReAct method by clipping penultimate activations under a threshold value. |
False
|
react_quantile |
Optional[float]
|
q value in the range [0, 1] used to compute the react clipping threshold defined as the q-th quantile penultimate layer activations. Defaults to 0.8. |
0.8
|
Source code in oodeel/methods/mls.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
requires_internal_features: bool
property
¶
Whether an OOD detector acts on internal model features.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the detector perform computations on an intermediate layer |
bool
|
else False. |
requires_to_fit_dataset: bool
property
¶
Whether an OOD detector needs a fit_dataset
argument in the fit function.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
_fit_to_dataset(fit_dataset)
¶
Fits the OOD detector to fit_dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_dataset |
DatasetType
|
dataset to fit the OOD detector on |
required |
Source code in oodeel/methods/mls.py
83 84 85 86 87 88 89 90 |
|
_score_tensor(inputs)
¶
Computes an OOD score for input samples "inputs" based on the distance to nearest neighbors in the feature space of self.model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples to score |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray]
|
Tuple[np.ndarray]: scores, logits |
Source code in oodeel/methods/mls.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
Mahalanobis
¶
Bases: OODBaseDetector
"A Simple Unified Framework for Detecting Out-of-Distribution Samples and Adversarial Attacks" https://arxiv.org/abs/1807.03888
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eps |
float
|
magnitude for gradient based input perturbation. Defaults to 0.02. |
0.002
|
Source code in oodeel/methods/mahalanobis.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
requires_internal_features: bool
property
¶
Whether an OOD detector acts on internal model features.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the detector perform computations on an intermediate layer |
bool
|
else False. |
requires_to_fit_dataset: bool
property
¶
Whether an OOD detector needs a fit_dataset
argument in the fit function.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
_fit_to_dataset(fit_dataset)
¶
Constructs the mean covariance matrix from ID data "fit_dataset", whose pseudo-inverse will be used for mahalanobis distance computation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_dataset |
Union[TensorType, DatasetType]
|
input dataset (ID) |
required |
Source code in oodeel/methods/mahalanobis.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
_input_perturbation(inputs)
¶
Apply small perturbation on inputs to make the in- and out- distribution samples more separable. See original paper for more information (section 2.2) https://arxiv.org/abs/1807.03888
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples |
required |
Returns:
Name | Type | Description |
---|---|---|
TensorType |
TensorType
|
Perturbed inputs |
Source code in oodeel/methods/mahalanobis.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
_mahalanobis_score(out_features)
¶
Mahalanobis distance-based confidence score. For each test sample, it computes the log of the probability densities of some observations (assuming a normal distribution) using the mahalanobis distance with respect to every class-conditional distributions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_features |
TensorType
|
test samples features |
required |
Returns:
Name | Type | Description |
---|---|---|
TensorType |
TensorType
|
confidence scores (conditionally to each class) |
Source code in oodeel/methods/mahalanobis.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
_score_tensor(inputs)
¶
Computes an OOD score for input samples "inputs" based on the mahalanobis distance with respect to the closest class-conditional Gaussian distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray]
|
Tuple[np.ndarray]: scores, logits |
Source code in oodeel/methods/mahalanobis.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
ODIN
¶
Bases: OODBaseDetector
"Enhancing The Reliability of Out-of-distribution Image Detection in Neural Networks" http://arxiv.org/abs/1706.02690
Parameters:
Name | Type | Description | Default |
---|---|---|---|
temperature |
float
|
Temperature parameter. Defaults to 1000. |
1000
|
noise |
float
|
Perturbation noise. Defaults to 0.014. |
0.014
|
use_react |
bool
|
if true, apply ReAct method by clipping penultimate activations under a threshold value. |
False
|
react_quantile |
Optional[float]
|
q value in the range [0, 1] used to compute the react clipping threshold defined as the q-th quantile penultimate layer activations. Defaults to 0.8. |
0.8
|
Source code in oodeel/methods/odin.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
requires_internal_features: bool
property
¶
Whether an OOD detector acts on internal model features.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the detector perform computations on an intermediate layer |
bool
|
else False. |
requires_to_fit_dataset: bool
property
¶
Whether an OOD detector needs a fit_dataset
argument in the fit function.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
_fit_to_dataset(fit_dataset)
¶
Fits the OOD detector to fit_dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_dataset |
DatasetType
|
dataset to fit the OOD detector on |
required |
Source code in oodeel/methods/odin.py
112 113 114 115 116 117 118 119 |
|
_score_tensor(inputs)
¶
Computes an OOD score for input samples "inputs" based on the distance to nearest neighbors in the feature space of self.model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples to score |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray]
|
Tuple[np.ndarray]: scores, logits |
Source code in oodeel/methods/odin.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
_temperature_loss(inputs, labels)
¶
Compute the tempered cross-entropy loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
the inputs of the model. |
required |
labels |
TensorType
|
the labels to fit on. |
required |
Returns:
Name | Type | Description |
---|---|---|
TensorType |
TensorType
|
the cross-entropy loss. |
Source code in oodeel/methods/odin.py
98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
input_perturbation(inputs)
¶
Apply a small perturbation over inputs to increase their softmax score. See ODIN paper for more information (section 3): http://arxiv.org/abs/1706.02690
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples to score |
required |
Returns:
Name | Type | Description |
---|---|---|
TensorType |
TensorType
|
Perturbed inputs |
Source code in oodeel/methods/odin.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
VIM
¶
Bases: OODBaseDetector
Compute the Virtual Matching Logit (VIM) score. https://arxiv.org/abs/2203.10807
This score combines the energy score with a PCA residual score.
The energy score is the logarithm of the sum of exponential of logits. The PCA residual score is based on the projection on residual dimensions for principal component analysis. Residual dimensions are the eigenvectors corresponding to the least eignevalues (least variance). Intuitively, this score method assumes that feature representations of ID data occupy a low dimensional affine subspace $P+c$ of the feature space. Specifically, the projection of ID data translated by $-c$ on the orthognoal complement $P^{\perp}$ is expected to have small norm. It allows to detect points whose feature representation lie far from the identified affine subspace, namely those points $x$ such that the projection on $P^{\perp}$ of $x-c$ has large norm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
princ_dims |
Union[int, float]
|
number of principal dimensions of in distribution features to consider. If an int, must be less than the dimension of the feature space. If a float, it must be in [0,1), it represents the ratio of explained variance to consider to determine the number of principal components. Defaults to 0.99. |
0.99
|
pca_origin |
str
|
either "pseudo" for using $W^{-1}b$ where $W^{-1}$ is the pseudo inverse of the final linear layer applied to bias term (as in the VIM paper), or "center" for using the mean of the data in feature space. Defaults to "center". |
'pseudo'
|
Source code in oodeel/methods/vim.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
requires_internal_features: bool
property
¶
Whether an OOD detector acts on internal model features.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the detector perform computations on an intermediate layer |
bool
|
else False. |
requires_to_fit_dataset: bool
property
¶
Whether an OOD detector needs a fit_dataset
argument in the fit function.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
_compute_residual_score_tensor(features)
¶
Computes the norm of the residual projection in the feature space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
TensorType
|
input samples to score |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: scores |
Source code in oodeel/methods/vim.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
_fit_to_dataset(fit_dataset)
¶
Computes principal components of feature representations and store the residual eigenvectors. Computes a scaling factor constant :math:'lpha' such that the average scaled residual score (on train) is equal to the average maximum logit score (MLS) score.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_dataset |
Union[TensorType, DatasetType]
|
input dataset (ID) to construct the index with. |
required |
Source code in oodeel/methods/vim.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
_score_tensor(inputs)
¶
Computes the VIM score for input samples "inputs" as the sum of the energy score and a scaled (PCA) residual norm in the feature space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorType
|
input samples to score |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray]
|
Tuple[np.ndarray]: scores, logits |
Source code in oodeel/methods/vim.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
plot_spectrum()
¶
Plot cumulated explained variance wrt the number of principal dimensions.
Source code in oodeel/methods/vim.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|