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,303 @@
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
+ from collections import OrderedDict
25
+
26
+ import numpy as np
27
+ import torch
28
+ import torch.nn as nn
29
+ import torch.optim as optim
30
+ import torchvision
31
+ from torch.utils.data import DataLoader
32
+ from tqdm import tqdm
33
+
34
+ from ..types import Optional
35
+ from ..types import Union
36
+
37
+
38
+ class ToyTorchMLP(nn.Sequential):
39
+ """Basic torch MLP classifier for toy datasets.
40
+
41
+ Args:
42
+ input_shape (tuple): Input data shape.
43
+ num_classes (int): Number of classes for the classification task.
44
+ """
45
+
46
+ def __init__(self, input_shape: tuple, num_classes: int):
47
+ self.input_shape = input_shape
48
+
49
+ # build toy mlp
50
+ mlp_modules = OrderedDict(
51
+ [
52
+ ("flatten", nn.Flatten()),
53
+ ("dense1", nn.Linear(np.prod(input_shape), 300)),
54
+ ("relu1", nn.ReLU()),
55
+ ("dense2", nn.Linear(300, 150)),
56
+ ("relu2", nn.ReLU()),
57
+ ("fc1", nn.Linear(150, num_classes)),
58
+ ]
59
+ )
60
+ super().__init__(mlp_modules)
61
+
62
+
63
+ class ToyTorchConvnet(nn.Sequential):
64
+ """Basic torch convolutional classifier for toy datasets.
65
+
66
+ Args:
67
+ input_shape (tuple): Input data shape.
68
+ num_classes (int): Number of classes for the classification task.
69
+ """
70
+
71
+ def __init__(self, input_shape: tuple, num_classes: int):
72
+ self.input_shape = input_shape
73
+
74
+ # features
75
+ features = nn.Sequential(
76
+ OrderedDict(
77
+ [
78
+ ("conv1", nn.Conv2d(input_shape[0], 32, 3)),
79
+ ("relu1", nn.ReLU()),
80
+ ("pool1", nn.MaxPool2d(2, 2)),
81
+ ("conv2", nn.Conv2d(32, 64, 3)),
82
+ ("relu2", nn.ReLU()),
83
+ ("pool2", nn.MaxPool2d(2, 2)),
84
+ ("flatten", nn.Flatten()),
85
+ ]
86
+ )
87
+ )
88
+
89
+ # fc head
90
+ fc_input_shape = self._calculate_fc_input_shape(features)
91
+ fcs = nn.Sequential(
92
+ OrderedDict(
93
+ [
94
+ ("dropout", nn.Dropout(0.5)),
95
+ ("fc1", nn.Linear(fc_input_shape, num_classes)),
96
+ ]
97
+ )
98
+ )
99
+
100
+ # Sequential class init
101
+ super().__init__(
102
+ OrderedDict([*features._modules.items(), *fcs._modules.items()])
103
+ )
104
+
105
+ def _calculate_fc_input_shape(self, features):
106
+ """Get tensor shape after passing a features network."""
107
+ input_tensor = torch.ones(tuple([1] + list(self.input_shape)))
108
+ x = features(input_tensor)
109
+ output_size = x.view(x.size(0), -1).size(1)
110
+ return output_size
111
+
112
+
113
+ def train_torch_model(
114
+ train_data: DataLoader,
115
+ model: Union[nn.Module, str],
116
+ num_classes: int,
117
+ epochs: int = 50,
118
+ loss: str = "CrossEntropyLoss",
119
+ optimizer: str = "Adam",
120
+ lr_scheduler: str = "cosine",
121
+ learning_rate: float = 1e-3,
122
+ imagenet_pretrained: bool = False,
123
+ validation_data: Optional[DataLoader] = None,
124
+ save_dir: Optional[str] = None,
125
+ cuda_idx: int = 0,
126
+ ) -> nn.Module:
127
+ """
128
+ Load a model (toy classifier or from torchvision.models) and train
129
+ it over a torch dataloader.
130
+
131
+ Args:
132
+ train_data (DataLoader): train dataloader
133
+ model (Union[nn.Module, str]): if a string is provided, must be a model from
134
+ torchvision.models or "toy_convnet" or "toy_mlp.
135
+ num_classes (int): Number of output classes.
136
+ epochs (int, optional): Defaults to 50.
137
+ loss (str, optional): Defaults to "CrossEntropyLoss".
138
+ optimizer (str, optional): Defaults to "Adam".
139
+ lr_scheduler (str, optional): ("cosine" | "steps" | None). Defaults to None.
140
+ learning_rate (float, optional): Defaults to 1e-3.
141
+ imagenet_pretrained (bool, optional): Load a model pretrained on imagenet or
142
+ not. Defaults to False.
143
+ validation_data (Optional[DataLoader], optional): Defaults to None.
144
+ save_dir (Optional[str], optional): Directory to save the model.
145
+ Defaults to None.
146
+ cuda_idx (int): idx of cuda device to use. Defaults to 0.
147
+
148
+ Returns:
149
+ nn.Module: trained model
150
+ """
151
+ # device
152
+ device = torch.device(
153
+ f"cuda:{cuda_idx}"
154
+ if torch.cuda.is_available() and cuda_idx is not None
155
+ else "cpu"
156
+ )
157
+
158
+ # Prepare model
159
+ if isinstance(model, nn.Module):
160
+ model = model.to(device)
161
+ elif isinstance(model, str):
162
+ if model == "toy_convnet":
163
+ # toy model
164
+ input_shape = tuple(next(iter(train_data))[0].shape[1:])
165
+ model = ToyTorchConvnet(input_shape, num_classes).to(device)
166
+ elif model == "toy_mlp":
167
+ # toy model
168
+ input_shape = tuple(next(iter(train_data))[0].shape[1:])
169
+ model = ToyTorchMLP(input_shape, num_classes).to(device)
170
+ else:
171
+ # torchvision model
172
+ model = getattr(torchvision.models, model)(
173
+ num_classes=num_classes, pretrained=imagenet_pretrained
174
+ ).to(device)
175
+
176
+ # define optimizer and learning rate scheduler
177
+ optimizer = getattr(optim, optimizer)(model.parameters(), lr=learning_rate)
178
+ n_steps = len(train_data) * epochs
179
+ if lr_scheduler == "cosine":
180
+ lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, n_steps)
181
+ elif lr_scheduler == "steps":
182
+ boundaries = list(np.round(n_steps * np.array([1 / 3, 2 / 3])).astype(int))
183
+ lr_scheduler = optim.lr_scheduler.MultiStepLR(
184
+ optimizer, milestones=boundaries, gamma=0.1
185
+ )
186
+
187
+ # define loss
188
+ criterion = getattr(nn, loss)()
189
+
190
+ # train
191
+ model = _train(
192
+ model,
193
+ train_data,
194
+ validation_data=validation_data,
195
+ epochs=epochs,
196
+ criterion=criterion,
197
+ optimizer=optimizer,
198
+ lr_scheduler=lr_scheduler,
199
+ save_dir=save_dir,
200
+ device=device,
201
+ )
202
+ return model
203
+
204
+
205
+ def _train(
206
+ model: nn.Module,
207
+ train_data: DataLoader,
208
+ epochs: int,
209
+ optimizer: torch.optim.Optimizer,
210
+ criterion: torch.nn.modules.loss._Loss,
211
+ device: torch.device,
212
+ lr_scheduler: Optional[torch.optim.lr_scheduler._LRScheduler] = None,
213
+ save_dir: Optional[str] = None,
214
+ validation_data: Optional[DataLoader] = None,
215
+ ) -> nn.Module:
216
+ """Torch basic training loop
217
+
218
+ Args:
219
+ model (nn.Module): Model to train.
220
+ train_data (DataLoader): Train dataloader.
221
+ epochs (int): Number of training epochs.
222
+ optimizer (torch.optim.Optimizer): Optimizer.
223
+ criterion (torch.nn.modules.loss._Loss): Criterion for loss.
224
+ device (torch.device): On which device to train (CUDA or CPU).
225
+ lr_scheduler (torch.optim.lr_scheduler._LRScheduler): Learning rate scheduler.
226
+ Defaults to None.
227
+ save_dir (str, optional): Where the model will be saved. Defaults to None.
228
+ validation_data (DataLoader, optional): Validation dataloader. Defaults to None.
229
+
230
+ Returns:
231
+ nn.Module: Trained model.
232
+ """
233
+ best_val_acc = None
234
+ for epoch in range(epochs):
235
+ # train phase
236
+ model.train()
237
+ running_loss, running_acc = 0.0, 0.0
238
+ with tqdm(train_data, desc=f"Epoch {epoch + 1}/{epochs} [Train]") as iterator:
239
+ for i, (inputs, labels) in enumerate(iterator):
240
+ # assign [inputs, labels] tensors to GPU
241
+ inputs = inputs.to(device)
242
+ labels = labels.long().to(device)
243
+
244
+ # zero the parameter gradients
245
+ optimizer.zero_grad()
246
+
247
+ # forward + backward + optimize
248
+ outputs = model(inputs)
249
+ loss = criterion(outputs, labels)
250
+ loss.backward()
251
+ optimizer.step()
252
+
253
+ # print statistics
254
+ acc = torch.mean((outputs.argmax(-1) == labels).float())
255
+ running_loss += loss.item()
256
+ running_acc += acc.item()
257
+ if i % max(len(iterator) // 100, 1) == 0:
258
+ iterator.set_postfix(
259
+ {
260
+ "Loss": f"{running_loss / (i + 1):.3f}",
261
+ "Acc": f"{running_acc / (i + 1):.3f}",
262
+ }
263
+ )
264
+
265
+ if lr_scheduler is not None:
266
+ lr_scheduler.step()
267
+
268
+ # validation phase
269
+ if validation_data is not None:
270
+ model.eval()
271
+ running_loss, running_acc = 0.0, 0.0
272
+ with tqdm(
273
+ validation_data, desc=f"Epoch {epoch + 1}/{epochs} [Val]"
274
+ ) as iterator:
275
+ for i, (inputs, labels) in enumerate(iterator):
276
+ # assign [inputs, labels] tensors to GPU
277
+ inputs = torch.Tensor(inputs.numpy()).to(device)
278
+ labels = torch.Tensor(labels.numpy()).long().to(device)
279
+
280
+ with torch.no_grad():
281
+ outputs = model(inputs)
282
+
283
+ running_loss += criterion(outputs, labels).item()
284
+ running_acc += torch.mean(
285
+ (outputs.argmax(-1) == labels).float()
286
+ ).item()
287
+ if i % max(len(iterator) // 100, 1) == 0:
288
+ iterator.set_postfix(
289
+ {
290
+ "Loss": f"{running_loss / (i + 1):.3f}",
291
+ "Acc": f"{running_acc / (i + 1):.3f}",
292
+ }
293
+ )
294
+ val_acc = running_acc / (i + 1)
295
+ if best_val_acc is None or val_acc > best_val_acc:
296
+ best_val_acc = val_acc
297
+ if save_dir is not None:
298
+ os.makedirs(save_dir, exist_ok=True)
299
+ torch.save(model, os.path.join(save_dir, "best.pt"))
300
+
301
+ if save_dir is not None:
302
+ torch.save(model, os.path.join(save_dir, "last.pt"))
303
+ return model