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.
Files changed (63) hide show
  1. oodeel/__init__.py +28 -0
  2. oodeel/aggregator/__init__.py +26 -0
  3. oodeel/aggregator/base.py +70 -0
  4. oodeel/aggregator/fisher.py +259 -0
  5. oodeel/aggregator/mean.py +72 -0
  6. oodeel/aggregator/std.py +86 -0
  7. oodeel/datasets/__init__.py +24 -0
  8. oodeel/datasets/data_handler.py +334 -0
  9. oodeel/datasets/deprecated/DEPRECATED_data_handler.py +236 -0
  10. oodeel/datasets/deprecated/DEPRECATED_ooddataset.py +330 -0
  11. oodeel/datasets/deprecated/DEPRECATED_tf_data_handler.py +671 -0
  12. oodeel/datasets/deprecated/DEPRECATED_torch_data_handler.py +769 -0
  13. oodeel/datasets/deprecated/__init__.py +31 -0
  14. oodeel/datasets/tf_data_handler.py +600 -0
  15. oodeel/datasets/torch_data_handler.py +672 -0
  16. oodeel/eval/__init__.py +22 -0
  17. oodeel/eval/metrics.py +218 -0
  18. oodeel/eval/plots/__init__.py +27 -0
  19. oodeel/eval/plots/features.py +345 -0
  20. oodeel/eval/plots/metrics.py +118 -0
  21. oodeel/eval/plots/plotly.py +162 -0
  22. oodeel/extractor/__init__.py +35 -0
  23. oodeel/extractor/feature_extractor.py +187 -0
  24. oodeel/extractor/hf_torch_feature_extractor.py +184 -0
  25. oodeel/extractor/keras_feature_extractor.py +409 -0
  26. oodeel/extractor/torch_feature_extractor.py +506 -0
  27. oodeel/methods/__init__.py +47 -0
  28. oodeel/methods/base.py +570 -0
  29. oodeel/methods/dknn.py +185 -0
  30. oodeel/methods/energy.py +119 -0
  31. oodeel/methods/entropy.py +113 -0
  32. oodeel/methods/gen.py +113 -0
  33. oodeel/methods/gram.py +274 -0
  34. oodeel/methods/mahalanobis.py +209 -0
  35. oodeel/methods/mls.py +113 -0
  36. oodeel/methods/odin.py +109 -0
  37. oodeel/methods/rmds.py +172 -0
  38. oodeel/methods/she.py +159 -0
  39. oodeel/methods/vim.py +273 -0
  40. oodeel/preprocess/__init__.py +31 -0
  41. oodeel/preprocess/tf_preprocess.py +95 -0
  42. oodeel/preprocess/torch_preprocess.py +97 -0
  43. oodeel/types/__init__.py +75 -0
  44. oodeel/utils/__init__.py +38 -0
  45. oodeel/utils/general_utils.py +97 -0
  46. oodeel/utils/operator.py +253 -0
  47. oodeel/utils/tf_operator.py +269 -0
  48. oodeel/utils/tf_training_tools.py +219 -0
  49. oodeel/utils/torch_operator.py +292 -0
  50. oodeel/utils/torch_training_tools.py +303 -0
  51. oodeel-0.4.0.dist-info/METADATA +409 -0
  52. oodeel-0.4.0.dist-info/RECORD +63 -0
  53. oodeel-0.4.0.dist-info/WHEEL +5 -0
  54. oodeel-0.4.0.dist-info/licenses/LICENSE +21 -0
  55. oodeel-0.4.0.dist-info/top_level.txt +2 -0
  56. tests/__init__.py +22 -0
  57. tests/tests_tensorflow/__init__.py +37 -0
  58. tests/tests_tensorflow/tf_methods_utils.py +140 -0
  59. tests/tests_tensorflow/tools_tf.py +86 -0
  60. tests/tests_torch/__init__.py +38 -0
  61. tests/tests_torch/tools_torch.py +151 -0
  62. tests/tests_torch/torch_methods_utils.py +148 -0
  63. tests/tools_operator.py +153 -0
@@ -0,0 +1,140 @@
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 os
24
+ import random
25
+
26
+ import numpy as np
27
+ import tensorflow as tf
28
+ from sklearn.datasets import make_blobs
29
+ from sklearn.model_selection import train_test_split
30
+
31
+ from oodeel.datasets import OODDataset
32
+ from oodeel.eval.metrics import bench_metrics
33
+
34
+ model_path = os.path.expanduser("~/") + ".oodeel/saved_models"
35
+ data_path = os.path.expanduser("~/") + ".oodeel/datasets"
36
+ os.makedirs(model_path, exist_ok=True)
37
+ os.makedirs(data_path, exist_ok=True)
38
+
39
+
40
+ def load_blobs_data(batch_size=128, num_samples=10000, train_ratio=0.8):
41
+ # === data hparams ===
42
+ num_classes = 3
43
+ in_labels = [0, 1]
44
+ out_labels = [2, 3]
45
+ centers = np.array([[-4, -4], [4, 4], [-4, 4], [4, -4]])
46
+
47
+ # === generate data ===
48
+ X, y = make_blobs(num_samples, num_classes, centers=centers, random_state=0)
49
+ X_train, X_test, y_train, y_test = train_test_split(
50
+ X, y, train_size=train_ratio, random_state=0
51
+ )
52
+
53
+ # === id / ood split ===
54
+ blobs_train = OODDataset((X_train, y_train), backend="tensorflow")
55
+ blobs_test = OODDataset((X_test, y_test), backend="tensorflow")
56
+ oods_fit, _ = blobs_train.split_by_class(in_labels, out_labels)
57
+ oods_in, oods_out = blobs_test.split_by_class(in_labels, out_labels)
58
+
59
+ # === prepare data (shuffle, batch) => torch dataloaders ===
60
+ ds_fit = oods_fit.prepare(batch_size=batch_size, shuffle=True)
61
+ ds_in = oods_in.prepare(batch_size=batch_size)
62
+ ds_out = oods_out.prepare(batch_size=batch_size)
63
+ return ds_fit, ds_in, ds_out
64
+
65
+
66
+ def load_blob_mlp():
67
+ model_path_blobs = tf.keras.utils.get_file(
68
+ "blobs_mlp.h5",
69
+ origin="https://github.com/deel-ai/oodeel/blob/assets/test_models/"
70
+ + "blobs_mlp.h5?raw=True",
71
+ cache_dir=model_path,
72
+ cache_subdir="",
73
+ )
74
+ model = tf.keras.models.load_model(model_path_blobs)
75
+ return model
76
+
77
+
78
+ def eval_detector_on_blobs(
79
+ detector,
80
+ auroc_thr=0.6,
81
+ fpr95_thr=0.3,
82
+ batch_size=128,
83
+ check_react_clipping=False,
84
+ ):
85
+ # seed
86
+ tf.random.set_seed(0)
87
+ np.random.seed(0)
88
+ random.seed(1)
89
+
90
+ # load data
91
+ ds_fit, ds_in, ds_out = load_blobs_data(batch_size)
92
+
93
+ # get classifier
94
+ model = load_blob_mlp()
95
+
96
+ # fit ood detector
97
+ if (
98
+ detector.requires_to_fit_dataset or detector.use_react
99
+ ) and detector.requires_internal_features:
100
+ if hasattr(detector, "aggregator") and detector.aggregator is not None:
101
+ detector.fit(model, feature_layers_id=[-2, -1], fit_dataset=ds_fit)
102
+ else:
103
+ detector.fit(model, feature_layers_id=[-2], fit_dataset=ds_fit)
104
+ elif detector.requires_to_fit_dataset or detector.use_react:
105
+ detector.fit(model, fit_dataset=ds_fit)
106
+ else:
107
+ detector.fit(model)
108
+
109
+ # ood scores
110
+ scores_in, info_in = detector.score(ds_in)
111
+ scores_out, info_out = detector.score(ds_out)
112
+ assert scores_in.shape == (1028,)
113
+ assert info_in["labels"].shape == (1028,)
114
+ assert info_in["logits"].shape == (1028, 2)
115
+ assert scores_out.shape == (972,)
116
+ assert info_out["labels"].shape == (972,)
117
+ assert info_out["logits"].shape == (972, 2)
118
+
119
+ # ood metrics: auroc, fpr95tpr
120
+ metrics = bench_metrics(
121
+ (scores_in, scores_out),
122
+ metrics=["auroc", "fpr95tpr"],
123
+ )
124
+ auroc, fpr95tpr = metrics["auroc"], metrics["fpr95tpr"]
125
+ assert auroc >= auroc_thr, f"got a score of {auroc}, below {auroc_thr}!"
126
+ assert fpr95tpr <= fpr95_thr, f"got a score of {fpr95tpr}, above {fpr95_thr}!"
127
+
128
+ # react specific test
129
+ # /!\ do it at the end of the test because it may affect the detector's behaviour
130
+ if check_react_clipping:
131
+ assert detector.react_threshold is not None
132
+ penult_feat_extractor = detector.FeatureExtractorClass(
133
+ model=detector.feature_extractor.extractor, feature_layers_id=[-2]
134
+ )
135
+ penult_features, _ = penult_feat_extractor.predict(ds_fit)
136
+ assert tf.reduce_max(penult_features) <= detector.react_threshold, (
137
+ "Maximum value of penultimate features"
138
+ + f" ({tf.reduce_max(penult_features)}) should be less than or equal to"
139
+ + f" the react threshold value ({detector.react_threshold})"
140
+ )
@@ -0,0 +1,86 @@
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
+ import tensorflow as tf
25
+
26
+
27
+ def almost_equal(arr1, arr2, epsilon=1e-6):
28
+ """Ensure two array are almost equal at an epsilon"""
29
+ return np.mean(np.abs(arr1 - arr2)) < epsilon
30
+
31
+
32
+ def generate_model(input_shape=(32, 32, 3), output_shape=10):
33
+ model = tf.keras.models.Sequential()
34
+ model.add(tf.keras.layers.Input(shape=input_shape))
35
+ model.add(tf.keras.layers.Conv2D(4, kernel_size=(2, 2), activation="relu"))
36
+ model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
37
+ model.add(tf.keras.layers.Dropout(0.25))
38
+ model.add(tf.keras.layers.Flatten())
39
+ model.add(tf.keras.layers.Dense(output_shape))
40
+ model.compile(loss="categorical_crossentropy", optimizer="sgd")
41
+
42
+ return model
43
+
44
+
45
+ def generate_regression_model(features_shape, output_shape=1):
46
+ model = tf.keras.models.Sequential()
47
+ model.add(tf.keras.layers.Input(shape=features_shape))
48
+ model.add(tf.keras.layers.Flatten())
49
+ model.add(tf.keras.layers.Dense(4, activation="relu"))
50
+ model.add(tf.keras.layers.Dense(4, activation="relu"))
51
+ model.add(tf.keras.layers.Dense(output_shape))
52
+ model.compile(loss="mean_absolute_error", optimizer="sgd", metrics=["accuracy"])
53
+
54
+ return model
55
+
56
+
57
+ def simplest_mlp(num_features, num_classes):
58
+ return tf.keras.models.Sequential(
59
+ [
60
+ tf.keras.layers.Input(shape=(num_features,)),
61
+ tf.keras.layers.Dense(64, activation="relu"),
62
+ tf.keras.layers.Dense(num_classes, activation="softmax"),
63
+ ]
64
+ )
65
+
66
+
67
+ def generate_data(x_shape=(32, 32, 3), num_labels=10, samples=100, one_hot=True):
68
+ x = np.random.rand(samples, *x_shape).astype(np.float32)
69
+ x /= np.max(x)
70
+ if one_hot:
71
+ y = np.eye(num_labels)[np.random.randint(0, num_labels, samples)]
72
+ else:
73
+ y = np.random.randint(0, num_labels, samples)
74
+
75
+ return x, y
76
+
77
+
78
+ def generate_data_tf(
79
+ x_shape=(32, 32, 3), num_labels=10, samples=100, one_hot=True, as_supervised=True
80
+ ):
81
+ x, y = generate_data(x_shape, num_labels, samples, one_hot)
82
+ if as_supervised:
83
+ dataset = tf.data.Dataset.from_tensor_slices((x, y))
84
+ else:
85
+ dataset = tf.data.Dataset.from_tensor_slices({"input": x, "label": y})
86
+ return dataset
@@ -0,0 +1,38 @@
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 .tools_torch import ComplexNet
24
+ from .tools_torch import generate_data
25
+ from .tools_torch import generate_data_torch
26
+ from .tools_torch import named_sequential_model
27
+ from .tools_torch import Net
28
+ from .tools_torch import sequential_model
29
+ from .torch_methods_utils import eval_detector_on_blobs
30
+
31
+ __all__ = [
32
+ "ComplexNet",
33
+ "generate_data_torch",
34
+ "generate_data",
35
+ "named_sequential_model",
36
+ "Net",
37
+ "sequential_model",
38
+ ]
@@ -0,0 +1,151 @@
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 collections import OrderedDict
24
+
25
+ import numpy as np
26
+ import torch
27
+ import torch.nn as nn
28
+ import torch.nn.functional as F
29
+ from torch.utils.data import TensorDataset
30
+
31
+
32
+ def almost_equal(arr1, arr2, epsilon=1e-6):
33
+ """Ensure two array are almost equal at an epsilon"""
34
+ return np.mean(np.abs(arr1 - arr2)) < epsilon
35
+
36
+
37
+ class Net(nn.Module):
38
+ def __init__(self, num_classes=10):
39
+ super().__init__()
40
+ self.conv1 = nn.Conv2d(3, 6, 5)
41
+ self.pool = nn.MaxPool2d(2, 2)
42
+ self.conv2 = nn.Conv2d(6, 16, 5)
43
+ self.fc1 = nn.Linear(16 * 5 * 5, 120)
44
+ self.fc2 = nn.Linear(120, 84)
45
+ self.fc3 = nn.Linear(84, num_classes)
46
+
47
+ def forward(self, x):
48
+ x = self.pool(F.relu(self.conv1(x)))
49
+ x = self.pool(F.relu(self.conv2(x)))
50
+ x = torch.flatten(x, 1) # flatten all dimensions except batch
51
+ x = F.relu(self.fc1(x))
52
+ x = F.relu(self.fc2(x))
53
+ x = self.fc3(x)
54
+ return x
55
+
56
+
57
+ class ComplexNet(nn.Module):
58
+ def __init__(self, num_classes=10):
59
+ super().__init__()
60
+
61
+ self.feature_extractor = nn.Sequential(
62
+ OrderedDict(
63
+ [
64
+ ("conv1", nn.Conv2d(3, 6, 5)),
65
+ ("relu1", nn.ReLU()),
66
+ ("pool1", nn.MaxPool2d(2, 2)),
67
+ ("conv2", nn.Conv2d(6, 16, 5)),
68
+ ("relu2", nn.ReLU()),
69
+ ("pool2", nn.MaxPool2d(2, 2)),
70
+ ("flatten", nn.Flatten()),
71
+ ]
72
+ )
73
+ )
74
+
75
+ self.fcs = nn.Sequential(
76
+ OrderedDict(
77
+ [
78
+ ("fc1", nn.Linear(16 * 5 * 5, 120)),
79
+ ("fc2", nn.Linear(120, 84)),
80
+ ("fc3", nn.Linear(84, num_classes)),
81
+ ]
82
+ )
83
+ )
84
+
85
+ def forward(self, x):
86
+ x = self.feature_extractor(x)
87
+ x = self.fcs(x)
88
+ return x
89
+
90
+
91
+ def sequential_model(num_classes=10):
92
+ return nn.Sequential(
93
+ nn.Conv2d(3, 6, 5),
94
+ nn.ReLU(),
95
+ nn.MaxPool2d(2, 2),
96
+ nn.Conv2d(6, 16, 5),
97
+ nn.ReLU(),
98
+ nn.MaxPool2d(2, 2),
99
+ nn.Flatten(),
100
+ nn.Linear(16 * 5 * 5, 120),
101
+ nn.Linear(120, 84),
102
+ nn.Linear(84, num_classes),
103
+ )
104
+
105
+
106
+ def named_sequential_model(num_classes=10):
107
+ return nn.Sequential(
108
+ OrderedDict(
109
+ [
110
+ ("conv1", nn.Conv2d(3, 6, 5)),
111
+ ("relu1", nn.ReLU()),
112
+ ("pool1", nn.MaxPool2d(2, 2)),
113
+ ("conv2", nn.Conv2d(6, 16, 5)),
114
+ ("relu2", nn.ReLU()),
115
+ ("pool2", nn.MaxPool2d(2, 2)),
116
+ ("flatten", nn.Flatten()),
117
+ ("fc1", nn.Linear(16 * 5 * 5, 120)),
118
+ ("fc2", nn.Linear(120, 84)),
119
+ ("fc3", nn.Linear(84, num_classes)),
120
+ ]
121
+ )
122
+ )
123
+
124
+
125
+ def simplest_mlp(num_features, num_classes=10):
126
+ return nn.Sequential(
127
+ nn.Linear(num_features, 64),
128
+ nn.ReLU(),
129
+ nn.Linear(64, num_classes),
130
+ )
131
+
132
+
133
+ def generate_data(x_shape=(3, 32, 32), num_labels=10, samples=100, one_hot=True):
134
+ x = np.random.rand(samples, *x_shape).astype(np.float32)
135
+ x /= np.max(x)
136
+ if one_hot:
137
+ y = np.eye(num_labels)[np.random.randint(0, num_labels, samples)]
138
+ else:
139
+ y = np.random.randint(0, num_labels, samples)
140
+
141
+ return x, y
142
+
143
+
144
+ def generate_data_torch(
145
+ x_shape=(3, 32, 32), num_labels=10, samples=100, one_hot=True, with_labels=True
146
+ ):
147
+ x, y = generate_data(x_shape, num_labels, samples, one_hot)
148
+ if with_labels:
149
+ return TensorDataset(torch.Tensor(x), torch.Tensor(y))
150
+ else:
151
+ return TensorDataset(torch.Tensor(x))
@@ -0,0 +1,148 @@
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 os
24
+
25
+ import numpy as np
26
+ import torch
27
+ from sklearn.datasets import make_blobs
28
+ from sklearn.model_selection import train_test_split
29
+
30
+ from oodeel.datasets import load_data_handler
31
+ from oodeel.eval.metrics import bench_metrics
32
+
33
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
34
+
35
+ model_path = os.path.expanduser("~/") + ".oodeel/saved_models"
36
+ data_path = os.path.expanduser("~/") + ".oodeel/datasets"
37
+ os.makedirs(model_path, exist_ok=True)
38
+ os.makedirs(data_path, exist_ok=True)
39
+
40
+
41
+ def load_blobs_data(batch_size=128, num_samples=10000, train_ratio=0.8):
42
+ # === data hparams ===
43
+ num_classes = 3
44
+ in_labels = [0, 1]
45
+ out_labels = [2, 3]
46
+ centers = np.array([[-4, -4], [4, 4], [-4, 4], [4, -4]])
47
+
48
+ # === generate data ===
49
+ X, y = make_blobs(num_samples, num_classes, centers=centers, random_state=0)
50
+ X_train, X_test, y_train, y_test = train_test_split(
51
+ X, y, train_size=train_ratio, random_state=0
52
+ )
53
+
54
+ # === id / ood split ===
55
+ handler = load_data_handler("torch")
56
+ blobs_train = handler.load_dataset((X_train, y_train))
57
+ blobs_test = handler.load_dataset((X_test, y_test))
58
+ oods_fit, _ = handler.split_by_class(blobs_train, in_labels, out_labels)
59
+ oods_in, oods_out = handler.split_by_class(blobs_test, in_labels, out_labels)
60
+
61
+ # === prepare data (shuffle, batch) => torch dataloaders ===
62
+ ds_fit = handler.prepare(oods_fit, batch_size=batch_size, shuffle=True)
63
+ ds_in = handler.prepare(oods_in, batch_size=batch_size)
64
+ ds_out = handler.prepare(oods_out, batch_size=batch_size)
65
+ return ds_fit, ds_in, ds_out
66
+
67
+
68
+ def load_blob_mlp():
69
+ # load model
70
+ model = torch.hub.load_state_dict_from_url(
71
+ "https://github.com/deel-ai/oodeel/blob/assets/"
72
+ + "test_models/blobs_mlp.pt?raw=True",
73
+ map_location=device,
74
+ )
75
+ model.eval()
76
+ return model
77
+
78
+
79
+ def eval_detector_on_blobs(
80
+ detector,
81
+ auroc_thr=0.6,
82
+ fpr95_thr=0.3,
83
+ batch_size=128,
84
+ check_react_clipping=False,
85
+ ):
86
+ # seed
87
+ torch.manual_seed(1)
88
+
89
+ # load data
90
+ ds_fit, ds_in, ds_out = load_blobs_data(batch_size)
91
+
92
+ # get classifier
93
+ model = load_blob_mlp()
94
+
95
+ # fit ood detector
96
+ if detector.requires_to_fit_dataset or detector.use_react:
97
+ if hasattr(detector, "aggregator") and detector.aggregator is not None:
98
+ detector.fit(model, feature_layers_id=[-2, -1], fit_dataset=ds_fit)
99
+ else:
100
+ detector.fit(model, feature_layers_id=[-2], fit_dataset=ds_fit)
101
+ else:
102
+ detector.fit(model)
103
+
104
+ # ood scores
105
+ scores_in, info_in = detector.score(ds_in)
106
+ scores_out, info_out = detector.score(ds_out)
107
+ assert scores_in.shape == (1028,)
108
+ assert info_in["labels"].shape == (1028,)
109
+ assert info_in["logits"].shape == (1028, 2)
110
+ assert scores_out.shape == (972,)
111
+ assert info_out["labels"].shape == (972,)
112
+ assert info_out["logits"].shape == (972, 2)
113
+
114
+ # ood metrics: auroc, fpr95tpr
115
+ metrics = bench_metrics(
116
+ (scores_in, scores_out),
117
+ metrics=["auroc", "fpr95tpr"],
118
+ )
119
+ auroc, fpr95tpr = metrics["auroc"], metrics["fpr95tpr"]
120
+ assert auroc >= auroc_thr, f"got a score of {auroc}, below {auroc_thr}!"
121
+ assert fpr95tpr <= fpr95_thr, f"got a score of {fpr95tpr}, above {fpr95_thr}!"
122
+
123
+ # react specific test
124
+ # /!\ do it at the end of the test because it may affect the detector's behaviour
125
+
126
+ if check_react_clipping:
127
+ assert detector.react_threshold is not None
128
+
129
+ penult_feat_extractor = detector._load_feature_extractor(
130
+ model=model, feature_layers_id=[-1]
131
+ )
132
+ # penult_feat_extractor._prepare_ood_handles()
133
+
134
+ def hook(_, input):
135
+ penult_feat_extractor._features["test"] = input[0]
136
+
137
+ penult_feat_extractor.model[-1].register_forward_pre_hook(hook)
138
+ for x, y in ds_fit:
139
+ _ = penult_feat_extractor.predict_tensor(x)
140
+ assert (
141
+ torch.max(penult_feat_extractor._features["test"])
142
+ <= detector.react_threshold
143
+ ), (
144
+ "Maximum value of penultimate features"
145
+ + f" ({torch.max(penult_feat_extractor._features)})"
146
+ + " should be less than or equal to the react threshold value"
147
+ + f" ({detector.react_threshold})"
148
+ )