-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentropy.py
421 lines (397 loc) · 17.6 KB
/
entropy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import os
import torch
import pandas as pd
import numpy as np
import torch.nn.functional as F
from copy import deepcopy
from evaluation import (
prepare_and_load_weights_for_models,
)
from FeCAM import (
extract_test_set_from_all_tasks,
extract_test_set_from_single_task,
translate_output_CIFAR_classes,
get_target_network_representation,
evaluate_hypermask_with_selected_embedding,
translate_output_MNIST_classes,
)
def get_task_and_class_prediction_based_on_logits(
inferenced_logits_of_all_tasks, setup, dataset
):
"""
Get task prediction for consecutive samples based on entropy values
of the output classification layer of the target network.
Arguments:
----------
*inferenced_logits_of_all_tasks*: shape: (number of tasks,
number of samples, number of output heads)
*setup*: (int) defines how many tasks were performed in this
experiment (in total)
*dataset*: (str) name of the dataset for proper class translation
Returns:
--------
*predicted_tasks*: torch.Tensor with the prediction of tasks for
consecutive samples
*predicted_classes*: torch.Tensor with the prediction of classes for
consecutive samples.
Positions of samples in the two above Tensors are the same.
"""
predicted_classes, predicted_tasks = [], []
number_of_samples = inferenced_logits_of_all_tasks.shape[1]
for no_of_sample in range(number_of_samples):
task_entropies = torch.zeros(inferenced_logits_of_all_tasks.shape[0])
all_task_single_output_sample = inferenced_logits_of_all_tasks[
:, no_of_sample, :
]
# Calculate entropy based on results from all tasks
for no_of_inferred_task in range(task_entropies.shape[0]):
softmaxed_inferred_task = F.softmax(
all_task_single_output_sample[no_of_inferred_task], dim=-1
)
task_entropies[no_of_inferred_task] = -1 * torch.sum(
softmaxed_inferred_task * torch.log(softmaxed_inferred_task)
)
selected_task_id = torch.argmin(task_entropies)
predicted_tasks.append(selected_task_id.item())
target_output = all_task_single_output_sample[selected_task_id.item()]
output_relative_class = target_output.argmax().item()
if dataset == "CIFAR100_FeCAM_setup":
output_absolute_class = translate_output_CIFAR_classes(
[output_relative_class], setup, selected_task_id.item()
)
elif dataset in ["PermutedMNIST", "SplitMNIST"]:
mode = "permuted" if dataset == "PermutedMNIST" else "split"
output_absolute_class = translate_output_MNIST_classes(
[output_relative_class], selected_task_id.item(), mode=mode
)
else:
raise ValueError("Wrong name of the dataset!")
predicted_classes.append(output_absolute_class)
predicted_tasks = torch.tensor(predicted_tasks, dtype=torch.int32)
predicted_classes = torch.tensor(predicted_classes, dtype=torch.int32)
return predicted_tasks, predicted_classes
def calculate_entropy_and_predict_classes_with_FeCAM(experiment_models):
"""
Select the target task automatically and calculate accuracy for
consecutive samples
Arguments:
----------
*experiment_models*: A dictionary with the following keys:
*hypernetwork*: an instance of HMLP class
*hypernetwork_weights*: loaded weights for the hypernetwork
*target_network*: an instance of MLP or ResNet class
*target_network_weights*: loaded weights for the target network
*list_of_CL_tasks*: list of objects containing consecutive tasks
*no_of_batch_norm_layers*: the number of batch normalization layers
in the target network
*hyperparameters*: a dictionary with experiment's hyperparameters
*batch_inference_size*: int related to the batch size during inference
*setup*: int related to the total number of tasks in a given experiment
List with the following results for the selected model:
- consecutive task prediction accuracies,
- consecutive class prediction accuracies based on logits,
- consecutive class prediction accuracies based on forward propagation,
- mean task prediction accuracy (and std. dev.),
- mean class prediction accuracy based on logits (and std. dev.),
- mean class prediction accuracy based on forward prop. (and std. dev.).
"""
hypernetwork = experiment_models["hypernetwork"]
hypernetwork_weights = experiment_models["hypernetwork_weights"]
target_network = experiment_models["target_network"]
target_weights = experiment_models["target_network_weights"]
hyperparameters = experiment_models["hyperparameters"]
dataset_CL_tasks = experiment_models["list_of_CL_tasks"]
no_of_batch_norm_layers = experiment_models["no_of_batch_norm_layers"]
target_network_type = hyperparameters["target_network"]
hypernetwork.eval()
target_network.eval()
task_predictions, class_predictions_logits, class_predictions_forward = (
[],
[],
[],
)
for number_of_incremental_tasks in range(1, experiment_models["setup"] + 1):
target_loaded_weights = deepcopy(target_weights)
X_test, y_test, gt_tasks_test = extract_test_set_from_all_tasks(
dataset_CL_tasks,
number_of_incremental_tasks,
experiment_models["setup"],
hyperparameters["device"],
)
results_classes, results_tasks, sanity_check_cls = [], [], []
no_of_batches = (
X_test.shape[0] // experiment_models["batch_inference_size"]
)
if X_test.shape[0] % experiment_models["batch_inference_size"] > 0.0:
no_of_batches += 1
for i in range(no_of_batches):
X_sample = X_test[
(experiment_models["batch_inference_size"] * i) : (
experiment_models["batch_inference_size"] * (i + 1)
)
]
logits_outputs_for_different_tasks = []
for inferenced_task in range(number_of_incremental_tasks):
# Try to predict task for all samples from "task"
logits_masked = get_target_network_representation(
hypernetwork,
hypernetwork_weights,
target_network,
target_loaded_weights,
target_network_type,
X_sample,
hyperparameters["sparsity_parameters"][0],
no_of_batch_norm_layers,
inferenced_task,
)
logits_masked = logits_masked[0]
logits_outputs_for_different_tasks.append(logits_masked)
# shape: number of incremental tasks x number of samples x
# output classification layer shape (number of classes)
logits_outputs_for_different_tasks = torch.stack(
logits_outputs_for_different_tasks
)
# METHOD 1): Select tasks and classes based on the previously
# calculated logits values. In this case, it is not necessary
# to perform another forward propagation through the network
# because we evaluated network outputs for each embedding.
(
predicted_tasks,
predicted_classes,
) = get_task_and_class_prediction_based_on_logits(
logits_outputs_for_different_tasks,
experiment_models["setup"],
experiment_models["hyperparameters"]["dataset"],
)
# METHOD 2): Select classes based on another forward propagation
# with the selected embedding. The results are varying depending
# on the batch size when the network uses batch normalization.
sanity = evaluate_hypermask_with_selected_embedding(
hypernetwork,
hypernetwork_weights,
target_network,
target_weights,
target_network_type,
experiment_models["hyperparameters"]["dataset"],
X_sample,
predicted_tasks.flatten().numpy(),
hyperparameters["sparsity_parameters"][0],
no_of_batch_norm_layers,
number_of_incremental_tasks,
experiment_models["setup"],
)
sanity_check_cls.append(sanity)
results_classes.append(predicted_classes.flatten())
results_tasks.append(predicted_tasks)
results_classes = torch.cat(results_classes, dim=0).numpy()
results_tasks = torch.cat(results_tasks, dim=0).numpy()
sanity_check_cls = np.concatenate(sanity_check_cls)
task_prediction_accuracy = (
np.sum(results_tasks == gt_tasks_test)
* 100.0
/ results_tasks.shape[0]
).item()
task_predictions.append(task_prediction_accuracy)
sample_prediction_accuracy = (
np.sum(results_classes == y_test) * 100.0 / results_classes.shape[0]
).item()
class_predictions_logits.append(sample_prediction_accuracy)
sample_prediction_forward_accuracy = (
np.sum(sanity_check_cls == y_test) * 100.0 / y_test.shape[0]
)
class_predictions_forward.append(sample_prediction_forward_accuracy)
print(f"After {number_of_incremental_tasks} incremental task.")
print(f"Task prediction accuracy: {task_prediction_accuracy}")
print(
f"Class prediction accuracy, logits: {sample_prediction_accuracy}"
)
print(
"Class prediction accuracy, forward: "
f"{sample_prediction_forward_accuracy}"
)
summary_of_results = [
task_predictions,
class_predictions_logits,
class_predictions_forward,
]
for element in [
task_predictions,
class_predictions_logits,
class_predictions_forward,
]:
summary_of_results.extend((np.mean(element), np.std(element)))
return summary_of_results
def calculate_entropy_and_predict_classes_separately(experiment_models):
"""
Select the target task automatically and calculate accuracy for
consecutive samples
Arguments:
----------
*experiment_models*: A dictionary with the following keys:
*hypernetwork*: an instance of HMLP class
*hypernetwork_weights*: loaded weights for the hypernetwork
*target_network*: an instance of MLP or ResNet class
*target_network_weights*: loaded weights for the target network
*hyperparameters*: a dictionary with experiment's hyperparameters
*dataset_CL_tasks*: list of objects containing consecutive tasks
Returns Pandas Dataframe with results for the selected model.
"""
hypernetwork = experiment_models["hypernetwork"]
hypernetwork_weights = experiment_models["hypernetwork_weights"]
target_network = experiment_models["target_network"]
target_weights = experiment_models["target_network_weights"]
hyperparameters = experiment_models["hyperparameters"]
dataset_CL_tasks = experiment_models["list_of_CL_tasks"]
dataset_name = experiment_models["hyperparameters"]["dataset"]
target_network_type = hyperparameters["target_network"]
saving_folder = hyperparameters["saving_folder"]
if "no_of_batch_norm_layers" in experiment_models:
no_of_batch_norm_layers = experiment_models["no_of_batch_norm_layers"]
else:
no_of_batch_norm_layers = 0
hypernetwork.eval()
target_network.eval()
results = []
for task in range(hyperparameters["number_of_tasks"]):
target_loaded_weights = deepcopy(target_weights)
X_test, y_test, gt_tasks = extract_test_set_from_single_task(
dataset_CL_tasks, task, dataset_name, hyperparameters["device"]
)
with torch.no_grad():
logits_outputs_for_different_tasks = []
for inferenced_task in range(hyperparameters["number_of_tasks"]):
# Try to predict task for all samples from "task"
logits_masked = get_target_network_representation(
hypernetwork,
hypernetwork_weights,
target_network,
target_loaded_weights,
target_network_type,
X_test,
hyperparameters["sparsity_parameters"][0],
no_of_batch_norm_layers,
inferenced_task,
)
logits_masked = logits_masked[0]
logits_outputs_for_different_tasks.append(logits_masked)
all_inferenced_tasks = torch.stack(
logits_outputs_for_different_tasks
)
# Sizes of consecutive dimensions represent:
# number of tasks x number of samples x number of output heads
(
predicted_tasks,
predicted_classes,
) = get_task_and_class_prediction_based_on_logits(
all_inferenced_tasks,
hyperparameters["number_of_tasks"],
dataset_name,
)
predicted_classes = predicted_classes.flatten().numpy()
task_prediction_accuracy = (
torch.sum(predicted_tasks == task).float()
* 100.0
/ predicted_tasks.shape[0]
).item()
print(f"task prediction accuracy: {task_prediction_accuracy}")
sample_prediction_accuracy = (
np.sum(predicted_classes == y_test) * 100.0 / y_test.shape[0]
).item()
print(f"sample prediction accuracy: {sample_prediction_accuracy}")
results.append(
[task, task_prediction_accuracy, sample_prediction_accuracy]
)
results = pd.DataFrame(
results, columns=["task", "task_prediction_acc", "class_prediction_acc"]
)
results.to_csv(
f"{saving_folder}entropy_statistics_{number_of_model}.csv", sep=";"
)
return results
if __name__ == "__main__":
# The results are varying depending on the batch sizes due to the fact
# that batch normalization is turned on in ResNet. We selected 2000 as
# the test set size to ensure that it is derived to the network
# in one piece.
batch_inference_size = 2000
setup = 5
# Options for *dataset*:
# 'PermutedMNIST', 'SplitMNIST', 'CIFAR100_FeCAM_setup'
dataset = "SplitMNIST"
path_to_datasets = "./Data/"
if dataset in ["PermutedMNIST", "SplitMNIST"]:
part = 0
elif dataset == "CIFAR100_FeCAM_setup":
part = 6
# ResNet, 5 tasks, 20 classes per each task
else:
raise ValueError("This dataset is currenly not implemented!")
path_to_stored_networks = f"./Models/{dataset}/"
path_to_save = f"./Results/{dataset}/"
os.makedirs(path_to_save, exist_ok=True)
results_summary = []
numbers_of_models = [i for i in range(5)]
seeds = [i + 1 for i in range(5)]
for number_of_model, seed in zip(numbers_of_models, seeds):
print(f"Calculations for model no: {number_of_model}")
experiment_models = prepare_and_load_weights_for_models(
path_to_stored_networks,
path_to_datasets,
number_of_model,
dataset,
seed=seed,
part=part,
)
if dataset == "CIFAR100_FeCAM_setup":
experiment_models["batch_inference_size"] = batch_inference_size
experiment_models["setup"] = setup
results = calculate_entropy_and_predict_classes_with_FeCAM(
experiment_models
)
else:
experiment_models["hyperparameters"]["saving_folder"] = path_to_save
results = calculate_entropy_and_predict_classes_separately(
experiment_models
)
results_summary.append(results)
if dataset == "CIFAR100_FeCAM_setup":
column_names = [
"task_prediction_accuracy",
"class_prediction_accuracy_from_logits",
f"class_prediction_accuracy_from_{batch_inference_size}_batches",
"mean_task_prediction_accuracy",
"std_dev_task_prediction_accuracy",
"mean_class_prediction_accuracy_from_logits",
"std_dev_class_prediction_accuracy_from_logits",
f"mean_class_prediction_accuracy_from_{batch_inference_size}_batches",
f"std_dev_class_prediction_accuracy_from_{batch_inference_size}_batches",
]
table_to_save = results_summary
else:
data_statistics = []
for summary in results_summary:
data_statistics.append(
[
list(summary["task_prediction_acc"].values),
list(summary["class_prediction_acc"].values),
np.mean(summary["task_prediction_acc"].values),
np.std(summary["task_prediction_acc"].values),
np.mean(summary["class_prediction_acc"].values),
np.std(summary["class_prediction_acc"].values),
]
)
column_names = [
"task_prediction_accuracy",
"class_prediction_accuracy",
"mean_task_prediction_accuracy",
"std_dev_task_prediction_accuracy",
"mean_class_prediction_accuracy",
"std_dev_class_prediction_accuracy",
]
table_to_save = data_statistics
dataframe = pd.DataFrame(table_to_save, columns=column_names)
dataframe.to_csv(
f"{path_to_save}entropy_mean_results_batch_inference_"
f"{batch_inference_size}.csv",
sep=";",
)