-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train_Valid_DP.py
executable file
·666 lines (530 loc) · 30.9 KB
/
Train_Valid_DP.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
"""
Created on Aug 6, 2022.
Training_Valid_DP.py
@author: Soroosh Tayebi Arasteh <soroosh.arasteh@rwth-aachen.de>
https://github.com/tayebiarasteh/
"""
import os.path
import time
import numpy as np
from tensorboardX import SummaryWriter
import torch
import torch.nn.functional as F
from sklearn import metrics
from opacus.utils.batch_memory_manager import BatchMemoryManager
from tqdm import tqdm
from config.serde import read_config, write_config
import warnings
warnings.filterwarnings('ignore')
epsilon = 1e-15
class Training:
def __init__(self, cfg_path, resume=False, label_names=None):
"""This class represents training and validation processes.
Parameters
----------
cfg_path: str
Config file path of the experiment
resume: bool
if we are resuming training from a checkpoint
"""
self.params = read_config(cfg_path)
self.cfg_path = cfg_path
self.label_names = label_names
if resume == False:
self.model_info = self.params['Network']
self.epoch = 0
self.best_loss = float('inf')
self.setup_cuda()
self.writer = SummaryWriter(log_dir=os.path.join(self.params['target_dir'], self.params['tb_logs_path']))
def setup_cuda(self, cuda_device_id=0):
"""setup the device.
Parameters
----------
cuda_device_id: int
cuda device id
"""
if torch.cuda.is_available():
torch.backends.cudnn.fastest = True
torch.cuda.set_device(cuda_device_id)
self.device = torch.device('cuda')
else:
self.device = torch.device('cpu')
def time_duration(self, start_time, end_time):
"""calculating the duration of training or one iteration
Parameters
----------
start_time: float
starting time of the operation
end_time: float
ending time of the operation
Returns
-------
elapsed_hours: int
total hours part of the elapsed time
elapsed_mins: int
total minutes part of the elapsed time
elapsed_secs: int
total seconds part of the elapsed time
"""
elapsed_time = end_time - start_time
elapsed_hours = int(elapsed_time / 3600)
if elapsed_hours >= 1:
elapsed_mins = int((elapsed_time / 60) - (elapsed_hours * 60))
elapsed_secs = int(elapsed_time - (elapsed_hours * 3600) - (elapsed_mins * 60))
else:
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = elapsed_time - (elapsed_mins * 60)
return elapsed_hours, elapsed_mins, elapsed_secs
def setup_model(self, model, optimiser, loss_function, weight=None, privacy_engine=None):
"""Setting up all the models, optimizers, and loss functions.
Parameters
----------
model: model file
The network
optimiser: optimizer file
The optimizer
loss_function: loss file
The loss function
weight: 1D tensor of float
class weights
"""
# prints the network's total number of trainable parameters and
# stores it to the experiment config
total_param_num = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f'\nTotal # of trainable parameters: {total_param_num:,}')
print('----------------------------------------------------\n')
self.model = model.to(self.device)
# self.model = self.model.half() # float16
self.loss_weight = weight.to(self.device)
self.loss_function = loss_function(pos_weight=self.loss_weight)
self.optimiser = optimiser
# Saves the model, optimiser,loss function name for writing to config file
self.model_info['total_param_num'] = total_param_num
self.model_info['loss_function'] = loss_function.__name__
self.params['Network'] = self.model_info
write_config(self.params, self.cfg_path, sort_keys=True)
if privacy_engine is not None:
self.privacy_engine = privacy_engine
def load_checkpoint(self, model, optimiser, loss_function, weight, label_names):
"""In case of resuming training from a checkpoint,
loads the weights for all the models, optimizers, and
loss functions, and device, tensorboard events, number
of iterations (epochs), and every info from checkpoint.
Parameters
----------
model: model file
The network
optimiser: optimizer file
The optimizer
loss_function: loss file
The loss function
"""
checkpoint = torch.load(os.path.join(self.params['target_dir'], self.params['network_output_path'],
self.params['checkpoint_name']))
self.device = None
self.model_info = checkpoint['model_info']
self.setup_cuda()
self.model = model.to(self.device)
self.loss_weight = weight
self.loss_weight = self.loss_weight.to(self.device)
self.loss_function = loss_function(weight=self.loss_weight)
self.optimiser = optimiser
self.label_names = label_names
self.model.load_state_dict(checkpoint['model_state_dict'])
self.optimiser.load_state_dict(checkpoint['optimizer_state_dict'])
self.epoch = checkpoint['epoch']
self.best_loss = checkpoint['best_loss']
self.writer = SummaryWriter(log_dir=os.path.join(os.path.join(
self.params['target_dir'], self.params['tb_logs_path'])), purge_step=self.epoch + 1)
def load_checkpoint_DP(self, model, optimiser, loss_function, weight, label_names, privacy_engine):
"""In case of resuming training from a checkpoint,
loads the weights for all the models, optimizers, and
loss functions, and device, tensorboard events, number
of iterations (epochs), and every info from checkpoint.
Parameters
----------
model: model file
The network
optimiser: optimizer file
The optimizer
loss_function: loss file
The loss function
"""
checkpoint = torch.load(os.path.join(self.params['target_dir'], self.params['network_output_path'],
self.params['checkpoint_name']))
self.device = None
self.model_info = checkpoint['model_info']
self.setup_cuda()
self.model = model.to(self.device)
self.loss_weight = weight
self.loss_weight = self.loss_weight.to(self.device)
self.loss_function = loss_function(weight=self.loss_weight)
self.optimiser = optimiser
self.label_names = label_names
self.privacy_engine = privacy_engine
self.privacy_engine.load_checkpoint(module=self.model, optimizer=self.optimiser,
path=os.path.join(self.params['target_dir'], self.params['network_output_path'], self.params['DP_checkpoint_name']))
self.epoch = checkpoint['epoch']
self.best_loss = checkpoint['best_loss']
self.writer = SummaryWriter(log_dir=os.path.join(os.path.join(
self.params['target_dir'], self.params['tb_logs_path'])), purge_step=self.epoch + 1)
def train_epoch(self, train_loader, valid_loader=None):
"""Training epoch
"""
self.params = read_config(self.cfg_path)
total_start_time = time.time()
for epoch in range(self.params['Network']['num_epochs'] - self.epoch):
self.epoch += 1
# initializing the loss list
batch_loss = 0
start_time = time.time()
for idx, (image, label) in enumerate(tqdm(train_loader)):
self.model.train()
image = image.to(self.device)
label = label.to(self.device)
self.optimiser.zero_grad()
with torch.set_grad_enabled(True):
output = self.model(image)
loss = self.loss_function(output, label.float()) # for multilabel
loss.backward()
self.optimiser.step()
batch_loss += loss.item()
train_loss = batch_loss / len(train_loader)
self.writer.add_scalar('Train_loss_avg', train_loss, self.epoch)
########## Save a checkpoint every epoch ##########
torch.save({'epoch': self.epoch,
'model_state_dict': self.model.state_dict(),
'optimizer_state_dict': self.optimiser.state_dict(),
'loss_state_dict': self.loss_function.state_dict(),
'model_info': self.model_info, 'best_loss': self.best_loss},
os.path.join(self.params['target_dir'], self.params['network_output_path'], self.params['checkpoint_name']))
########## Save a checkpoint every epoch ##########
# Saves information about training to config file
self.params['Network']['num_epoch'] = self.epoch
write_config(self.params, self.cfg_path, sort_keys=True)
# Validation iteration & calculate metrics
if (self.epoch) % (self.params['display_stats_freq']) == 0:
# saving the model, checkpoint, TensorBoard, etc.
if not valid_loader == None:
valid_loss, valid_F1, valid_AUC, valid_accuracy, valid_specificity, valid_sensitivity, valid_precision, optimal_threshold = self.valid_epoch(valid_loader)
end_time = time.time()
total_time = end_time - total_start_time
iteration_hours, iteration_mins, iteration_secs = self.time_duration(start_time, end_time)
total_hours, total_mins, total_secs = self.time_duration(total_start_time, end_time)
self.calculate_tb_stats(valid_loss=valid_loss, valid_F1=valid_F1, valid_AUC=valid_AUC, valid_accuracy=valid_accuracy, valid_specificity=valid_specificity,
valid_sensitivity=valid_sensitivity, valid_precision=valid_precision)
self.savings_prints(iteration_hours=iteration_hours, iteration_mins=iteration_mins, iteration_secs=iteration_secs, total_hours=total_hours,
total_mins=total_mins, total_secs=total_secs, train_loss=train_loss, total_time=total_time, valid_loss=valid_loss, valid_F1=valid_F1,
valid_AUC=valid_AUC, valid_accuracy=valid_accuracy, valid_specificity= valid_specificity,
valid_sensitivity=valid_sensitivity, valid_precision=valid_precision, optimal_thresholds=optimal_threshold)
else:
end_time = time.time()
total_time = end_time - total_start_time
iteration_hours, iteration_mins, iteration_secs = self.time_duration(start_time, end_time)
total_hours, total_mins, total_secs = self.time_duration(total_start_time, end_time)
self.savings_prints(iteration_hours=iteration_hours, iteration_mins=iteration_mins, iteration_secs=iteration_secs, total_hours=total_hours,
total_mins=total_mins, total_secs=total_secs, train_loss=train_loss, total_time=total_time)
def train_epoch_DP(self, train_loader, valid_loader=None):
"""Training epoch
"""
self.params = read_config(self.cfg_path)
total_start_time = time.time()
for epoch in range(self.params['Network']['num_epochs'] - self.epoch):
self.epoch += 1
# initializing the loss list
batch_loss = 0
start_time = time.time()
with BatchMemoryManager(data_loader=train_loader, max_physical_batch_size=self.params['Network']['physical_batch_size'], optimizer=self.optimiser) as memory_safe_data_loader:
for idx, (image, label) in enumerate(memory_safe_data_loader):
self.model.train()
image = image.to(self.device)
label = label.to(self.device)
self.optimiser.zero_grad()
with torch.set_grad_enabled(True):
output = self.model(image)
loss = self.loss_function(output, label.float()) # for multilabel
loss.backward()
self.optimiser.step()
batch_loss += loss.item()
if (idx + 1) % 200 == 0:
epsilon = self.privacy_engine.get_epsilon(float(self.params['DP']['delta']))
print(
f"\tTrain Epoch: {self.epoch} \t | "
f"Loss: {batch_loss/(idx + 1):.6f} | "
f"(ε = {epsilon:.2f}, δ = {float(self.params['DP']['delta'])})")
train_loss = batch_loss / len(train_loader)
self.writer.add_scalar('Train_loss_avg', train_loss, self.epoch)
self.writer.add_scalar('Epsilon', self.privacy_engine.get_epsilon(float(self.params['DP']['delta'])), self.epoch)
########## Save a checkpoint every epoch ##########
self.privacy_engine.save_checkpoint(path=os.path.join(self.params['target_dir'], self.params['network_output_path'],
self.params['DP_checkpoint_name']), module=self.model, optimizer=self.optimiser)
torch.save({'epoch': self.epoch, 'loss_state_dict': self.loss_function.state_dict(),
'model_info': self.model_info, 'best_loss': self.best_loss},
os.path.join(self.params['target_dir'], self.params['network_output_path'], self.params['checkpoint_name']))
########## Save a checkpoint every epoch ##########
# Saves information about training to config file
self.params['Network']['num_epoch'] = self.epoch
write_config(self.params, self.cfg_path, sort_keys=True)
# Validation iteration & calculate metrics
if (self.epoch) % (self.params['display_stats_freq']) == 0:
# saving the model, checkpoint, TensorBoard, etc.
if not valid_loader == None:
valid_loss, valid_F1, valid_AUC, valid_accuracy, valid_specificity, valid_sensitivity, valid_precision, optimal_threshold = self.valid_epoch(valid_loader)
end_time = time.time()
total_time = end_time - total_start_time
iteration_hours, iteration_mins, iteration_secs = self.time_duration(start_time, end_time)
total_hours, total_mins, total_secs = self.time_duration(total_start_time, end_time)
self.calculate_tb_stats(valid_loss=valid_loss, valid_F1=valid_F1, valid_AUC=valid_AUC, valid_accuracy=valid_accuracy, valid_specificity=valid_specificity,
valid_sensitivity=valid_sensitivity, valid_precision=valid_precision)
self.savings_prints(iteration_hours=iteration_hours, iteration_mins=iteration_mins, iteration_secs=iteration_secs, total_hours=total_hours,
total_mins=total_mins, total_secs=total_secs, train_loss=train_loss, total_time=total_time, valid_loss=valid_loss, valid_F1=valid_F1,
valid_AUC=valid_AUC, valid_accuracy=valid_accuracy, valid_specificity= valid_specificity,
valid_sensitivity=valid_sensitivity, valid_precision=valid_precision, privacy_engine=True, optimal_thresholds=optimal_threshold)
else:
end_time = time.time()
total_time = end_time - total_start_time
iteration_hours, iteration_mins, iteration_secs = self.time_duration(start_time, end_time)
total_hours, total_mins, total_secs = self.time_duration(total_start_time, end_time)
self.savings_prints(iteration_hours=iteration_hours, iteration_mins=iteration_mins, iteration_secs=iteration_secs, total_hours=total_hours,
total_mins=total_mins, total_secs=total_secs, train_loss=train_loss, total_time=total_time, privacy_engine=True)
def valid_epoch(self, valid_loader):
"""Validation epoch
Returns
-------
"""
self.model.eval()
total_f1_score = []
total_AUROC = []
total_accuracy = []
total_specificity_score = []
total_sensitivity_score = []
total_precision_score = []
# initializing the caches
preds_with_sigmoid_cache = torch.Tensor([]).to(self.device)
logits_for_loss_cache = torch.Tensor([]).to(self.device)
labels_cache = torch.Tensor([]).to(self.device)
for idx, (image, label) in enumerate(valid_loader):
image = image.to(self.device)
label = label.to(self.device)
label = label.float()
with torch.no_grad():
output = self.model(image)
output_sigmoided = F.sigmoid(output)
# saving the logits and labels of this batch
preds_with_sigmoid_cache = torch.cat((preds_with_sigmoid_cache, output_sigmoided))
logits_for_loss_cache = torch.cat((logits_for_loss_cache, output))
labels_cache = torch.cat((labels_cache, label))
############ Evaluation metric calculation ########
loss = self.loss_function(logits_for_loss_cache.to(self.device), labels_cache.to(self.device))
epoch_loss = loss.item()
# threshold finding for metrics calculation
preds_with_sigmoid_cache = preds_with_sigmoid_cache.cpu().numpy()
labels_cache = labels_cache.int().cpu().numpy()
optimal_threshold = np.zeros(labels_cache.shape[1])
for idx in range(labels_cache.shape[1]):
fpr, tpr, thresholds = metrics.roc_curve(labels_cache[:, idx], preds_with_sigmoid_cache[:, idx], pos_label=1)
optimal_idx = np.argmax(tpr - fpr)
optimal_threshold[idx] = thresholds[optimal_idx]
predicted_labels = (preds_with_sigmoid_cache > optimal_threshold).astype(np.int32)
# Metrics calculation (macro) over the whole set
confusion = metrics.multilabel_confusion_matrix(labels_cache, predicted_labels)
F1_disease = []
accuracy_disease = []
specificity_disease = []
sensitivity_disease = []
precision_disease = []
for idx, disease in enumerate(confusion):
TN = disease[0, 0]
FP = disease[0, 1]
FN = disease[1, 0]
TP = disease[1, 1]
F1_disease.append(2 * TP / (2 * TP + FN + FP + epsilon))
accuracy_disease.append((TP + TN) / (TP + TN + FP + FN + epsilon))
specificity_disease.append(TN / (TN + FP + epsilon))
sensitivity_disease.append(TP / (TP + FN + epsilon))
precision_disease.append(TP / (TP + FP + epsilon))
# Macro averaging
total_f1_score.append(np.stack(F1_disease))
try:
total_AUROC.append(metrics.roc_auc_score(labels_cache, preds_with_sigmoid_cache, average=None))
except:
print('hi')
pass
total_accuracy.append(np.stack(accuracy_disease))
total_specificity_score.append(np.stack(specificity_disease))
total_sensitivity_score.append(np.stack(sensitivity_disease))
total_precision_score.append(np.stack(precision_disease))
average_f1_score = np.stack(total_f1_score).mean(0)
average_AUROC = np.stack(total_AUROC).mean(0)
average_accuracy = np.stack(total_accuracy).mean(0)
average_specificity = np.stack(total_specificity_score).mean(0)
average_sensitivity = np.stack(total_sensitivity_score).mean(0)
average_precision = np.stack(total_precision_score).mean(0)
return epoch_loss, average_f1_score, average_AUROC, average_accuracy, average_specificity, average_sensitivity, average_precision, optimal_threshold
def savings_prints(self, iteration_hours, iteration_mins, iteration_secs, total_hours,
total_mins, total_secs, train_loss, total_time, valid_loss=None, valid_F1=None, valid_AUC=None, valid_accuracy=None,
valid_specificity=None, valid_sensitivity=None, valid_precision=None, privacy_engine=False, optimal_thresholds=None):
"""Saving the model weights, checkpoint, information,
and training and validation loss and evaluation statistics.
Parameters
----------
iteration_hours: int
hours part of the elapsed time of each iteration
iteration_mins: int
minutes part of the elapsed time of each iteration
iteration_secs: int
seconds part of the elapsed time of each iteration
total_hours: int
hours part of the total elapsed time
total_mins: int
minutes part of the total elapsed time
total_secs: int
seconds part of the total elapsed time
train_loss: float
training loss of the model
valid_acc: float
validation accuracy of the model
valid_sensitivity: float
validation sensitivity of the model
valid_specificity: float
validation specificity of the model
valid_loss: float
validation loss of the model
"""
# Saves information about training to config file
self.params['Network']['num_epoch'] = self.epoch
write_config(self.params, self.cfg_path, sort_keys=True)
# Saving the model based on the best loss
if valid_loss:
if valid_loss < self.best_loss:
self.best_loss = valid_loss
torch.save(self.model.state_dict(), os.path.join(self.params['target_dir'],
self.params['network_output_path'], self.params['trained_model_name']))
else:
if train_loss < self.best_loss:
self.best_loss = train_loss
torch.save(self.model.state_dict(), os.path.join(self.params['target_dir'],
self.params['network_output_path'], self.params['trained_model_name']))
# Save a checkpoint every epoch
if privacy_engine:
self.privacy_engine.save_checkpoint(path=os.path.join(self.params['target_dir'], self.params['network_output_path'], self.params['DP_checkpoint_name']),
module=self.model, optimizer=self.optimiser)
torch.save({'epoch': self.epoch, 'loss_state_dict': self.loss_function.state_dict(),
'model_info': self.model_info, 'best_loss': self.best_loss},
os.path.join(self.params['target_dir'], self.params['network_output_path'],
self.params['checkpoint_name']))
# Saving every couple of epochs
if (self.epoch) % self.params['display_stats_freq'] == 0:
self.privacy_engine.save_checkpoint(
path=os.path.join(self.params['target_dir'], self.params['network_output_path'],
'epoch{}_'.format(self.epoch) + self.params['DP_checkpoint_name']), module=self.model, optimizer=self.optimiser)
else:
torch.save({'epoch': self.epoch,
'model_state_dict': self.model.state_dict(),
'optimizer_state_dict': self.optimiser.state_dict(),
'loss_state_dict': self.loss_function.state_dict(),
'model_info': self.model_info, 'best_loss': self.best_loss},
os.path.join(self.params['target_dir'], self.params['network_output_path'], self.params['checkpoint_name']))
# Saving every couple of epochs
if (self.epoch) % self.params['display_stats_freq'] == 0:
torch.save(self.model.state_dict(),
os.path.join(self.params['target_dir'], self.params['network_output_path'],
'epoch{}_'.format(self.epoch) + self.params['trained_model_name']))
if privacy_engine:
epsilon = self.privacy_engine.get_epsilon(float(self.params['DP']['delta']))
delta = float(self.params['DP']['delta'])
else:
epsilon = float('inf')
delta = float('inf')
print('------------------------------------------------------'
'----------------------------------')
print(f'epoch: {self.epoch} | '
f'epoch time: {iteration_hours}h {iteration_mins}m {iteration_secs:.2f}s | '
f'total time: {total_hours}h {total_mins}m {total_secs:.2f}s')
print(f'\n\tTrain loss: {train_loss:.4f}, ε = {epsilon:.2f} | δ = {delta}\n')
if valid_loss:
print(f'\t Val. loss: {valid_loss:.4f} | avg AUROC: {valid_AUC.mean() * 100:.2f}% | avg accuracy: {valid_accuracy.mean() * 100:.2f}%'
f' | avg specificity: {valid_specificity.mean() * 100:.2f}%'
f' | avg recall (sensitivity): {valid_sensitivity.mean() * 100:.2f}% | avg F1: {valid_F1.mean() * 100:.2f}%\n')
print('Individual AUROC:')
for idx, pathology in enumerate(self.label_names):
try:
print(f'\t{pathology}: {valid_AUC[idx] * 100:.2f}%')
except:
print(f'\t{pathology}: {valid_AUC * 100:.2f}%')
print('\nIndividual accuracy:')
for idx, pathology in enumerate(self.label_names):
print(f'\t{pathology}: {valid_accuracy[idx] * 100:.2f}% ; thresh: {optimal_thresholds[idx]:.4f}')
print('\nIndividual sensitivity scores:')
for idx, pathology in enumerate(self.label_names):
print(f'\t{pathology}: {valid_sensitivity[idx] * 100:.2f}%')
print('\nIndividual specificity scores:')
for idx, pathology in enumerate(self.label_names):
print(f'\t{pathology}: {valid_specificity[idx] * 100:.2f}%')
# saving the training and validation stats
msg = f'\n\n----------------------------------------------------------------------------------------\n' \
f'epoch: {self.epoch} | epoch Time: {iteration_hours}h {iteration_mins}m {iteration_secs:.2f}s' \
f' | total time: {total_hours}h {total_mins}m {total_secs:.2f}s | ' \
f'\n\n\tTrain loss: {train_loss:.4f}, ε = {epsilon:.2f} | δ = {delta} | ' \
f'Val. loss: {valid_loss:.4f} | avg AUROC: {valid_AUC.mean() * 100:.2f}% | avg accuracy: {valid_accuracy.mean() * 100:.2f}% ' \
f' | avg specificity: {valid_specificity.mean() * 100:.2f}%' \
f' | avg recall (sensitivity): {valid_sensitivity.mean() * 100:.2f}% | avg F1: {valid_F1.mean() * 100:.2f}% | avg precision: {valid_precision.mean() * 100:.2f}%\n\n'
else:
msg = f'----------------------------------------------------------------------------------------\n' \
f'epoch: {self.epoch} | epoch time: {iteration_hours}h {iteration_mins}m {iteration_secs:.2f}s' \
f' | total time: {total_hours}h {total_mins}m {total_secs:.2f}s\n\n\ttrain loss: {train_loss:.4f}, ε = {epsilon:.2f} | δ = {delta}\n\n'
with open(os.path.join(self.params['target_dir'], self.params['stat_log_path']) + '/Stats', 'a') as f:
f.write(msg)
if valid_loss:
msg = f'Individual AUROC:\n'
with open(os.path.join(self.params['target_dir'], self.params['stat_log_path']) + '/Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(self.label_names):
try:
msg = f'{pathology}: {valid_AUC[idx] * 100:.2f}% | '
except:
msg = f'{pathology}: {valid_AUC * 100:.2f}% | '
with open(os.path.join(self.params['target_dir'], self.params['stat_log_path']) + '/Stats', 'a') as f:
f.write(msg)
msg = f'\n\nIndividual accuracy:\n'
with open(os.path.join(self.params['target_dir'], self.params['stat_log_path']) + '/Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(self.label_names):
msg = f'{pathology}: {valid_accuracy[idx] * 100:.2f}% ; threshold: {optimal_thresholds[idx]:.4f} | '
with open(os.path.join(self.params['target_dir'], self.params['stat_log_path']) + '/Stats', 'a') as f:
f.write(msg)
msg = f'\n\nIndividual sensitivity scores:\n'
with open(os.path.join(self.params['target_dir'], self.params['stat_log_path']) + '/Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(self.label_names):
msg = f'{pathology}: {valid_sensitivity[idx] * 100:.2f}%'
with open(os.path.join(self.params['target_dir'], self.params['stat_log_path']) + '/Stats', 'a') as f:
f.write(msg)
msg = f'\n\nIndividual specificity scores:\n'
with open(os.path.join(self.params['target_dir'], self.params['stat_log_path']) + '/Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(self.label_names):
msg = f'{pathology}: {valid_specificity[idx] * 100:.2f}%'
with open(os.path.join(self.params['target_dir'], self.params['stat_log_path']) + '/Stats', 'a') as f:
f.write(msg)
def calculate_tb_stats(self, valid_loss=None, valid_F1=None, valid_AUC=None, valid_accuracy=None, valid_specificity=None, valid_sensitivity=None, valid_precision=None):
"""Adds the evaluation metrics and loss values to the tensorboard.
Parameters
----------
valid_acc: float
validation accuracy of the model
valid_sensitivity: float
validation sensitivity of the model
valid_specificity: float
validation specificity of the model
valid_loss: float
validation loss of the model
"""
if valid_loss is not None:
self.writer.add_scalar('Valid_loss', valid_loss, self.epoch)
self.writer.add_scalar('valid_avg_F1', valid_F1.mean(), self.epoch)
self.writer.add_scalar('Valid_avg_AUROC', valid_AUC.mean(), self.epoch)
self.writer.add_scalar('Valid_avg_accuracy', valid_accuracy.mean(), self.epoch)
# self.writer.add_scalar('Valid_avg_specificity', valid_specificity.mean(), self.epoch)
# self.writer.add_scalar('Valid_avg_precision', valid_precision.mean(), self.epoch)
# self.writer.add_scalar('Valid_avg_recall_sensitivity', valid_sensitivity.mean(), self.epoch)