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,153 @@
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
+
26
+ def _get_operator(backend):
27
+ if backend == "torch":
28
+ from oodeel.utils.torch_operator import TorchOperator
29
+
30
+ return TorchOperator()
31
+ elif backend == "tensorflow":
32
+ from oodeel.utils.tf_operator import TFOperator
33
+
34
+ return TFOperator()
35
+ else:
36
+ raise ValueError(f"backend '{backend}' not supported")
37
+
38
+
39
+ def _generate_random_tensor(shape, backend):
40
+ if backend == "torch":
41
+ import torch
42
+
43
+ return torch.rand(*shape)
44
+ elif backend == "tensorflow":
45
+ import tensorflow as tf
46
+
47
+ return tf.random.uniform(shape)
48
+
49
+
50
+ def _generate_tensor_deterministic(shape, dtype, backend):
51
+ if backend == "torch":
52
+ import torch
53
+
54
+ dict_types = {"float32": torch.float32, "int64": torch.int64}
55
+ return torch.arange(np.prod(shape), dtype=dict_types[dtype]).view(*shape)
56
+ elif backend == "tensorflow":
57
+ import tensorflow as tf
58
+
59
+ return tf.reshape(tf.range(tf.reduce_prod(shape), dtype=dtype), shape)
60
+
61
+
62
+ def check_common_operators(backend):
63
+ operator = _get_operator(backend)
64
+
65
+ input_shape = (25, 12, 6)
66
+ x = _generate_random_tensor(input_shape, backend)
67
+ z = _generate_tensor_deterministic((2, 2, 2), dtype="float32", backend=backend)
68
+ to_one_hot = (
69
+ _generate_tensor_deterministic((3, 2), dtype="int64", backend=backend) % 3
70
+ )
71
+
72
+ # Softmax
73
+ softmax_z = operator.softmax(z)
74
+ assert softmax_z.shape == (2, 2, 2)
75
+ np.testing.assert_almost_equal(softmax_z[0, 0, 0], 0.26894143)
76
+ np.testing.assert_almost_equal(softmax_z[0, 0, 1], 0.73105854)
77
+
78
+ # Argmax
79
+ assert operator.argmax(z) == 7
80
+ assert operator.argmax(z, dim=1).shape == (2, 2)
81
+ np.testing.assert_array_equal(operator.argmax(z, dim=2), [[1, 1], [1, 1]])
82
+
83
+ # Max
84
+ assert operator.max(2 * z) == 14
85
+ assert operator.max(z, dim=1).shape == (2, 2)
86
+ np.testing.assert_array_equal(operator.max(z, dim=2), [[1, 3], [5, 7]])
87
+
88
+ # One-hot
89
+ num_classes = 5
90
+ one_hot_tensor = operator.one_hot(to_one_hot, num_classes)
91
+ assert one_hot_tensor.shape == (3, 2, 5)
92
+ assert one_hot_tensor[1, 0, 2] == 1
93
+ np.testing.assert_array_equal(one_hot_tensor[..., 3], 0)
94
+
95
+ # Sign
96
+ sign_x = operator.sign(x + 0.01) # To ensure that all elements are positive
97
+ assert sign_x.shape == input_shape
98
+ np.testing.assert_array_equal(sign_x, 1)
99
+ np.testing.assert_array_equal(operator.sign(x - x), 0)
100
+
101
+ # Norm
102
+ assert operator.norm(x, dim=1).shape == (25, 6)
103
+ np.testing.assert_almost_equal(operator.norm(z), 11.832159, decimal=4)
104
+ np.testing.assert_array_almost_equal(
105
+ operator.norm(z, dim=1), [[2.0, 3.1622], [7.2111, 8.6023]], decimal=4
106
+ )
107
+
108
+ # Matmul
109
+ x1 = _generate_random_tensor((10, 16), backend)
110
+ x2 = _generate_random_tensor((16, 3), backend)
111
+ assert operator.matmul(x1, x2).shape == (10, 3)
112
+
113
+ # Stack
114
+ assert operator.stack([x, x], dim=0).shape == (2, 25, 12, 6)
115
+ assert operator.stack([x, x], dim=1).shape == (25, 2, 12, 6)
116
+
117
+ # Cat
118
+ assert operator.cat([x, x], dim=0).shape == (50, 12, 6)
119
+ assert operator.cat([x, x], dim=1).shape == (25, 24, 6)
120
+
121
+ # Mean
122
+ assert operator.mean(z, dim=None) == 3.5
123
+ assert operator.mean(x, dim=0).shape == (12, 6)
124
+ assert operator.mean(x, dim=1).shape == (25, 6)
125
+
126
+ # Flatten (to 2D tensor)
127
+ assert operator.flatten(x).shape == (25, 12 * 6)
128
+
129
+ # Transpose
130
+ assert operator.t(x[0]).shape == (6, 12)
131
+
132
+ # Diag
133
+ assert operator.diag(x[0]).shape == (6,)
134
+
135
+ # Reshape
136
+ assert operator.reshape(x, (30, 2, 30)).shape == (30, 2, 30)
137
+
138
+ # Equal
139
+ ind = operator.equal(z, 0)
140
+ assert ind.shape == (2, 2, 2)
141
+ assert z[ind].shape == (1,)
142
+
143
+ # Pinv
144
+ assert operator.pinv(x[0]).shape == (6, 12)
145
+
146
+ # einsum
147
+ ein = operator.einsum("bij,jk->bik", x, operator.t(x[0]))
148
+ assert ein.shape == (25, 12, 12)
149
+
150
+ # tril
151
+ triangle = operator.tril(ein, diagonal=-1)
152
+ assert triangle.shape == (25, 12, 12)
153
+ assert np.sum([m[0, 0] + m[11, 11] + m[0, 11] for m in triangle]) == 0