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 | class NebulaModel(pl.LightningModule, ABC):
"""
Abstract class for the NEBULA model.
This class is an abstract class that defines the interface for the NEBULA model.
"""
def process_metrics(self, phase, y_pred, y, loss=None):
"""
Calculate and log metrics for the given phase.
The metrics are calculated in each batch.
Args:
phase (str): One of 'Train', 'Validation', or 'Test'
y_pred (torch.Tensor): Model predictions
y (torch.Tensor): Ground truth labels
loss (torch.Tensor, optional): Loss value
"""
y_pred_classes = torch.argmax(y_pred, dim=1).detach()
y = y.detach()
if phase == "Train":
self.logger.log_data({f"{phase}/Loss": loss.detach()})
self.train_metrics.update(y_pred_classes, y)
elif phase == "Validation":
self.val_metrics.update(y_pred_classes, y)
elif phase == "Test (Local)":
self.test_metrics.update(y_pred_classes, y)
self.cm.update(y_pred_classes, y) if self.cm is not None else None
elif phase == "Test (Global)":
self.test_metrics_global.update(y_pred_classes, y)
self.cm_global.update(y_pred_classes, y) if self.cm_global is not None else None
else:
raise NotImplementedError
del y_pred_classes, y
def log_metrics_end(self, phase):
"""
Log metrics for the given phase.
Args:
phase (str): One of 'Train', 'Validation', 'Test (Local)', or 'Test (Global)'
print_cm (bool): Print confusion matrix
plot_cm (bool): Plot confusion matrix
"""
if phase == "Train":
output = self.train_metrics.compute()
elif phase == "Validation":
output = self.val_metrics.compute()
elif phase == "Test (Local)":
output = self.test_metrics.compute()
elif phase == "Test (Global)":
output = self.test_metrics_global.compute()
else:
raise NotImplementedError
output = {
f"{phase}/{key.replace('Multiclass', '').split('/')[-1]}": value.detach() for key, value in output.items()
}
output_values = {
key: float(value.detach().cpu().item()) for key, value in output.items()
}
if phase == "Train":
self._latest_train_metrics = output_values
if phase == "Validation":
self._latest_validation_metrics = output_values
if phase in {"Test", "Test (Local)"}:
self._latest_test_metrics = output_values
if phase == "Train" and self._train_extra_metrics:
output.update({
f"{phase}/{key}": torch.tensor(value["sum"] / value["count"], device=self.device)
for key, value in self._train_extra_metrics.items()
if value["count"] > 0
})
self.logger.log_data(output, step=self.global_number[phase])
metrics_str = ""
for key, value in output.items():
metrics_str += f"{key}: {value:.4f}\n"
print_msg_box(
metrics_str,
indent=2,
title=f"{phase} Metrics | Epoch: {self.global_number[phase]} | Round: {self.round}",
logger_name=TRAINING_LOGGER,
)
def generate_confusion_matrix(self, phase, print_cm=False, plot_cm=False):
"""
Generate and plot the confusion matrix for the given phase.
Args:
phase (str): One of 'Train', 'Validation', 'Test (Local)', or 'Test (Global)'
"""
if phase == "Test (Local)":
if self.cm is None:
raise ValueError(f"Confusion matrix not available for {phase} phase.")
cm = self.cm.compute().cpu()
elif phase == "Test (Global)":
if self.cm_global is None:
raise ValueError(f"Confusion matrix not available for {phase} phase.")
cm = self.cm_global.compute().cpu()
else:
raise NotImplementedError
if print_cm:
logging_training.info(f"{phase} / Confusion Matrix:\n{cm}")
if plot_cm:
cm_numpy = cm.numpy().astype(int)
classes = [i for i in range(self.num_classes)]
fig, ax = plt.subplots(figsize=(12, 12))
sns.heatmap(
cm_numpy,
annot=False,
fmt="",
cmap="Blues",
ax=ax,
xticklabels=classes,
yticklabels=classes,
square=True,
)
ax.set_xlabel("Predicted labels", fontsize=12)
ax.set_ylabel("True labels", fontsize=12)
ax.set_title(f"{phase} Confusion Matrix", fontsize=16)
plt.xticks(rotation=90, fontsize=6)
plt.yticks(rotation=0, fontsize=6)
plt.tight_layout()
self.logger.log_figure(fig, step=self.round, name=f"{phase}/CM")
plt.close()
del cm_numpy, classes, fig, ax
if phase == "Test (Local)":
self.cm.reset()
else:
self.cm_global.reset()
del cm
def __init__(
self,
input_channels=1,
num_classes=10,
learning_rate=1e-3,
metrics=None,
confusion_matrix=None,
seed=None,
):
super().__init__()
self.input_channels = input_channels
self.num_classes = num_classes
self.learning_rate = learning_rate
if metrics is None:
metrics = MetricCollection([
MulticlassAccuracy(num_classes=num_classes),
MulticlassPrecision(num_classes=num_classes),
MulticlassRecall(num_classes=num_classes),
MulticlassF1Score(num_classes=num_classes, average="macro"),
])
self.train_metrics = metrics.clone(prefix="Train/")
self.val_metrics = metrics.clone(prefix="Validation/")
self.test_metrics = metrics.clone(prefix="Test (Local)/")
self.test_metrics_global = metrics.clone(prefix="Test (Global)/")
del metrics
if confusion_matrix is None:
self.cm = MulticlassConfusionMatrix(num_classes=num_classes)
self.cm_global = MulticlassConfusionMatrix(num_classes=num_classes)
if seed is not None:
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# Round counter (number of training-validation-test rounds)
self.round = 0
# Epochs counter
self.global_number = {
"Train": 0,
"Validation": 0,
"Test (Local)": 0,
"Test (Global)": 0,
}
# Communication manager for sending messages from the model (e.g., prototypes, gradients)
# Model parameters are sent by default using network.propagator
self.communication_manager = None
self._current_loss = -1
self._optimizer = None
self._optimizer_override = None
self._latest_train_metrics = {}
self._latest_validation_metrics = {}
self._latest_test_metrics = {}
self._train_extra_metrics = {}
# DP trainers update these fields after querying the Opacus accountant.
self.dp_enabled = False
self.dp_epsilon = None
self.dp_delta = None
self.adversarial_training = None
def set_optimizer_override(self, optimizer):
self._optimizer_override = optimizer
self._optimizer = optimizer
def clear_optimizer_override(self):
self._optimizer_override = None
def get_optimizer_override(self):
return self._optimizer_override
def set_adversarial_training(self, adversarial_training):
self.adversarial_training = adversarial_training
def clear_adversarial_training(self):
self.adversarial_training = None
def set_communication_manager(self, communication_manager):
self.communication_manager = communication_manager
def get_communication_manager(self):
if self.communication_manager is None:
raise ValueError("Communication manager not set.")
return self.communication_manager
@abstractmethod
def forward(self, x):
"""Forward pass of the model."""
pass
@abstractmethod
def configure_optimizers(self):
"""Optimizer configuration."""
pass
def step(self, batch, batch_idx, phase):
"""Training/validation/test step."""
x, y = batch
extra_metrics = {}
if phase == "Train" and self.adversarial_training is not None:
loss, y_pred, extra_metrics = self.adversarial_training.compute_training_step(
self,
x,
y,
self.criterion,
)
else:
y_pred = self.forward(x)
loss = self.criterion(y_pred, y)
self.process_metrics(phase, y_pred, y, loss)
if phase == "Train" and extra_metrics:
self._log_training_extra_metrics(extra_metrics)
self._current_loss = loss
return loss
def _log_training_extra_metrics(self, metrics):
if self.logger is None:
return
detached_metrics = {key: value.detach() for key, value in metrics.items()}
for key, value in detached_metrics.items():
metric = self._train_extra_metrics.setdefault(key, {"sum": 0.0, "count": 0})
metric["sum"] += float(value.cpu().item())
metric["count"] += 1
self.logger.log_data({f"Train/{key}": value for key, value in detached_metrics.items()})
def get_loss(self):
return self._current_loss
def get_latest_validation_metrics(self):
return self._latest_validation_metrics
def get_latest_train_metrics(self):
return self._latest_train_metrics
def get_latest_test_metrics(self):
return self._latest_test_metrics
def get_latest_train_accuracy(self):
return self._latest_train_metrics.get("Train/Accuracy")
def get_latest_test_macro_f1(self):
return self._latest_test_metrics.get("Test (Local)/F1Score")
def modify_learning_rate(self, new_lr):
logging.info(f"Modifiying | learning rate, new value: {new_lr}")
self.learning_rate = new_lr
for param_group in self._optimizer.param_groups:
param_group["lr"] = new_lr
def show_current_learning_rate(self):
for param_group in self._optimizer.param_groups:
logging.info(f"Showing | Learning rate current value: {param_group['lr']}")
def set_updated_round(self, round):
self.round = round
self.global_number = {
"Train": round,
"Validation": round,
"Test (Local)": round,
"Test (Global)": round,
}
def training_step(self, batch, batch_idx):
"""
Training step for the model.
Args:
batch:
batch_id:
Returns:
"""
return self.step(batch, batch_idx=batch_idx, phase="Train")
def on_train_start(self):
logging_training.info(f"{'=' * 10} [Training] Started {'=' * 10}")
def on_train_end(self):
logging_training.info(f"{'=' * 10} [Training] Done {'=' * 10}")
def on_train_epoch_end(self):
self.log_metrics_end("Train")
self.train_metrics.reset()
self._train_extra_metrics = {}
self.global_number["Train"] += 1
def validation_step(self, batch, batch_idx):
"""
Validation step for the model.
Args:
batch:
batch_idx:
Returns:
"""
return self.step(batch, batch_idx=batch_idx, phase="Validation")
def on_validation_end(self):
pass
def on_validation_epoch_end(self):
# In general, the validation phase is done in one epoch
self.log_metrics_end("Validation")
self.val_metrics.reset()
self.global_number["Validation"] += 1
def test_step(self, batch, batch_idx, dataloader_idx=None):
"""
Test step for the model.
Args:
batch:
batch_idx:
Returns:
"""
x, y = batch
y_pred = self.forward(x)
loss = self.criterion(y_pred, y)
y_pred_classes = torch.argmax(y_pred, dim=1)
accuracy = torch.mean((y_pred_classes == y).float())
if dataloader_idx == 0:
self.log(f"val_loss", loss, on_epoch=True, prog_bar=False)
self.log(f"val_accuracy", accuracy, on_epoch=True, prog_bar=False)
return self.step(batch, batch_idx=batch_idx, phase="Test (Local)")
else:
return self.step(batch, batch_idx=batch_idx, phase="Test (Global)")
def on_test_start(self):
logging_training.info(f"{'=' * 10} [Testing] Started {'=' * 10}")
def on_test_end(self):
logging_training.info(f"{'=' * 10} [Testing] Done {'=' * 10}")
def on_test_epoch_end(self):
# In general, the test phase is done in one epoch
self.log_metrics_end("Test (Local)")
self.log_metrics_end("Test (Global)")
self.generate_confusion_matrix("Test (Local)", print_cm=True, plot_cm=True)
self.generate_confusion_matrix("Test (Global)", print_cm=True, plot_cm=True)
self.test_metrics.reset()
self.test_metrics_global.reset()
self.global_number["Test (Local)"] += 1
self.global_number["Test (Global)"] += 1
def on_round_end(self):
self.round += 1
|