-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_privdom.py
executable file
·876 lines (724 loc) · 43.6 KB
/
main_privdom.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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
"""
Created on Jan 20, 2023.
main_privdom.py
@author: Soroosh Tayebi Arasteh <[email protected]>
https://github.com/tayebiarasteh/
"""
import pdb
import torch
import os
from torch.utils.data import Dataset
from torch.nn import BCEWithLogitsLoss
from torchvision import models
from opacus.validators import ModuleValidator
from opacus import PrivacyEngine
import numpy as np
from config.serde import open_experiment, create_experiment, delete_experiment
from Train_Valid_privdom import Training
from Prediction_privdom import Prediction
from data.data_provider_privdom import UKA_data_loader, padchest_data_loader, mimic_data_loader, chexpert_data_loader, cxr14_data_loader, vindr_data_loader
import warnings
warnings.filterwarnings('ignore')
warnings.simplefilter("ignore")
def main_train_central(global_config_path="privacydomain/config/config.yaml", valid=False,
resume=False, augment=False, experiment_name='name', dataset_name='vindr', pretrained=False, resnet_num=50, mish=True):
"""Main function for training + validation centrally
Parameters
----------
global_config_path: str
always global_config_path="privacydomain/config/config.yaml"
valid: bool
if we want to do validation
resume: bool
if we are resuming training on a model
augment: bool
if we want to have data augmentation during training
experiment_name: str
name of the experiment, in case of resuming training.
name of new experiment, in case of new training.
"""
if resume == True:
params = open_experiment(experiment_name, global_config_path)
else:
params = create_experiment(experiment_name, global_config_path)
cfg_path = params["cfg_path"]
if dataset_name == 'vindr':
train_dataset = vindr_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = vindr_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'chexpert':
train_dataset = chexpert_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = chexpert_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'mimic':
train_dataset = mimic_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = mimic_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'UKA':
train_dataset = UKA_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = UKA_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'cxr14':
train_dataset = cxr14_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = cxr14_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'padchest':
train_dataset = padchest_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = padchest_data_loader(cfg_path=cfg_path, mode='test', augment=False)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=params['Network']['physical_batch_size'],
pin_memory=True, drop_last=True, shuffle=True, num_workers=10)
weight = train_dataset.pos_weight()
label_names = train_dataset.chosen_labels
if valid:
valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset, batch_size=params['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=5)
else:
valid_loader = None
# Changeable network parameters
model = load_pretrained_resnet(num_classes=len(weight), resnet_num=resnet_num, pretrained=pretrained, mish=mish)
# model = ModuleValidator.fix(model)
loss_function = BCEWithLogitsLoss
optimizer = torch.optim.NAdam(model.parameters(), lr=float(params['Network']['lr']),
weight_decay=float(params['Network']['weight_decay']))
trainer = Training(cfg_path, resume=resume, label_names=label_names)
if resume == True:
trainer.load_checkpoint(model=model, optimiser=optimizer, loss_function=loss_function, weight=weight, label_names=label_names)
else:
trainer.setup_model(model=model, optimiser=optimizer, loss_function=loss_function, weight=weight)
trainer.train_epoch(train_loader=train_loader, valid_loader=valid_loader)
def main_train_DP(global_config_path="privacydomain/config/config.yaml", valid=False,
resume=False, augment=False, experiment_name='name', dataset_name='vindr', pretrained=False, resnet_num=9, mish=True):
"""Main function for training + validation using DPSGD
Parameters
----------
global_config_path: str
always global_config_path="privacydomain/config/config.yaml"
valid: bool
if we want to do validation
resume: bool
if we are resuming training on a model
experiment_name: str
name of the experiment, in case of resuming training.
name of new experiment, in case of new training.
"""
if resume == True:
params = open_experiment(experiment_name, global_config_path)
else:
params = create_experiment(experiment_name, global_config_path)
cfg_path = params["cfg_path"]
if dataset_name == 'vindr':
train_dataset = vindr_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = vindr_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'chexpert':
train_dataset = chexpert_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = chexpert_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'mimic':
train_dataset = mimic_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = mimic_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'UKA':
train_dataset = UKA_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = UKA_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'cxr14':
train_dataset = cxr14_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = cxr14_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'padchest':
train_dataset = padchest_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset = padchest_data_loader(cfg_path=cfg_path, mode='test', augment=False)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=params['DP']['logical_batch_size'],
drop_last=True, shuffle=True, num_workers=10)
weight = train_dataset.pos_weight()
label_names = train_dataset.chosen_labels
if valid:
valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset, batch_size=params['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=5)
else:
valid_loader = None
# Changeable network parameters
model = load_pretrained_resnet(num_classes=len(weight), resnet_num=resnet_num, pretrained=pretrained, mish=mish)
# model = ModuleValidator.fix(model)
loss_function = BCEWithLogitsLoss
optimizer = torch.optim.NAdam(model.parameters(), lr=float(params['Network']['lr']),
weight_decay=float(params['Network']['weight_decay']))
errors = ModuleValidator.validate(model, strict=False)
assert len(errors) == 0
privacy_engine = PrivacyEngine()
model, optimizer, train_loader = privacy_engine.make_private_with_epsilon(
module=model,
optimizer=optimizer,
data_loader=train_loader,
epochs=params['Network']['num_epochs'],
target_epsilon=params['DP']['epsilon'],
target_delta=float(params['DP']['delta']),
max_grad_norm=params['DP']['max_grad_norm'])
trainer = Training(cfg_path, resume=resume, label_names=label_names)
if resume == True:
trainer.load_checkpoint_DP(model=model, optimiser=optimizer, loss_function=loss_function, weight=weight, label_names=label_names, privacy_engine=privacy_engine, train_loader=train_loader)
else:
trainer.setup_model(model=model, optimiser=optimizer, loss_function=loss_function, weight=weight, privacy_engine=privacy_engine)
trainer.train_epoch_DP(train_loader=train_loader, valid_loader=valid_loader)
def main_train_federated(global_config_path="privacydomain/config/config.yaml",
resume=False, augment=False, experiment_name='name', dataset_names_list=['vindr', 'vindr'], aggregationweight=[1, 1, 1], pretrained=False, resnet_num=9, mish=True):
"""Main function for training + validation centrally
Parameters
----------
global_config_path: str
always global_config_path="privacydomain/config/config.yaml"
resume: bool
if we are resuming training on a model
augment: bool
if we want to have data augmentation during training
experiment_name: str
name of the experiment, in case of resuming training.
name of new experiment, in case of new training.
"""
if resume == True:
params = open_experiment(experiment_name, global_config_path)
else:
params = create_experiment(experiment_name, global_config_path)
cfg_path = params["cfg_path"]
train_loader = []
valid_loader = []
weight_loader = []
for dataset_name in dataset_names_list:
if dataset_name == 'vindr':
train_dataset_model = vindr_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset_model = vindr_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'chexpert':
train_dataset_model = chexpert_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset_model = chexpert_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'mimic':
train_dataset_model = mimic_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset_model = mimic_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'UKA':
train_dataset_model = UKA_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset_model = UKA_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'cxr14':
train_dataset_model = cxr14_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset_model = cxr14_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'padchest':
train_dataset_model = padchest_data_loader(cfg_path=cfg_path, mode='train', augment=augment)
valid_dataset_model = padchest_data_loader(cfg_path=cfg_path, mode='test', augment=False)
weight_model = train_dataset_model.pos_weight()
label_names = train_dataset_model.chosen_labels
train_loader_model = torch.utils.data.DataLoader(dataset=train_dataset_model,
batch_size=params['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=True, num_workers=10)
train_loader.append(train_loader_model)
weight_loader.append(weight_model)
valid_loader_model = torch.utils.data.DataLoader(dataset=valid_dataset_model,
batch_size=params['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=4)
valid_loader.append(valid_loader_model)
model = load_pretrained_resnet(num_classes=len(weight_loader[0]), resnet_num=resnet_num, pretrained=pretrained, mish=mish)
trainer = Training(cfg_path, resume=resume, label_names=label_names)
loss_function = BCEWithLogitsLoss
optimizer = torch.optim.NAdam(model.parameters(), lr=float(params['Network']['lr']),
weight_decay=float(params['Network']['weight_decay']))
weight = None
if resume == True:
trainer.load_checkpoint(model=model, optimiser=optimizer, loss_function=loss_function, weight=weight, label_names=label_names)
else:
trainer.setup_model(model=model, optimiser=optimizer, loss_function=loss_function, weight=weight)
trainer.training_setup_federated(train_loader=train_loader, valid_loader=valid_loader, loss_weight_loader=weight_loader, aggregationweight=aggregationweight)
def main_test_normal(global_config_path="privacydomain/config/config.yaml", experiment_name='central_exp_for_test',
resnet_num=50, mish=True, experiment_epoch_num=10, dataset_name='vindr'):
"""Main function for multi label prediction with differential privacy
Parameters
----------
experiment_name: str
name of the experiment to be loaded.
"""
params = open_experiment(experiment_name, global_config_path)
cfg_path = params['cfg_path']
if dataset_name == 'vindr':
test_dataset = vindr_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'chexpert':
test_dataset = chexpert_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'mimic':
test_dataset = mimic_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'UKA':
test_dataset = UKA_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'cxr14':
test_dataset = cxr14_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'padchest':
test_dataset = padchest_data_loader(cfg_path=cfg_path, mode='test', augment=False)
weight = test_dataset.pos_weight()
label_names = test_dataset.chosen_labels
# Changeable network parameters
model = load_pretrained_resnet(num_classes=len(weight), resnet_num=resnet_num, mish=mish)
# model = ModuleValidator.fix(model)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=params['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=16)
errors = ModuleValidator.validate(model, strict=False)
assert len(errors) == 0
# Initialize prediction
predictor = Prediction(cfg_path, label_names)
predictor.setup_model(model=model, epoch_num=experiment_epoch_num)
average_f1_score, average_AUROC, average_accuracy, average_specificity, average_sensitivity, average_precision = predictor.evaluate_2D(test_loader)
print('------------------------------------------------------'
'----------------------------------')
print(f'\t experiment: {experiment_name}\n')
print(f'\t avg AUROC: {average_AUROC.mean() * 100:.2f}% | avg accuracy: {average_accuracy.mean() * 100:.2f}%'
f' | avg specificity: {average_specificity.mean() * 100:.2f}%'
f' | avg recall (sensitivity): {average_sensitivity.mean() * 100:.2f}% | avg precision: {average_precision.mean() * 100:.2f}% | avg F1: {average_f1_score.mean() * 100:.2f}%\n')
print('Individual AUROC:')
for idx, pathology in enumerate(predictor.label_names):
print(f'\t{pathology}: {average_AUROC[idx] * 100:.2f}%')
print('\nIndividual accuracy:')
for idx, pathology in enumerate(predictor.label_names):
print(f'\t{pathology}: {average_accuracy[idx] * 100:.2f}%')
print('\nIndividual specificity scores:')
for idx, pathology in enumerate(predictor.label_names):
print(f'\t{pathology}: {average_specificity[idx] * 100:.2f}%')
print('\nIndividual sensitivity scores:')
for idx, pathology in enumerate(predictor.label_names):
print(f'\t{pathology}: {average_sensitivity[idx] * 100:.2f}%')
print('------------------------------------------------------'
'----------------------------------')
# saving the stats
msg = f'----------------------------------------------------------------------------------------\n' \
f'\t experiment: {experiment_name}\n\n' \
f'avg AUROC: {average_AUROC.mean() * 100:.2f}% | avg accuracy: {average_accuracy.mean() * 100:.2f}% ' \
f' | avg specificity: {average_specificity.mean() * 100:.2f}%' \
f' | avg recall (sensitivity): {average_sensitivity.mean() * 100:.2f}% | avg precision: {average_precision.mean() * 100:.2f}% | avg F1: {average_f1_score.mean() * 100:.2f}%\n\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
msg = f'Individual AUROC:\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
msg = f'{pathology}: {average_AUROC[idx] * 100:.2f}% | '
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
msg = f'\n\nIndividual accuracy:\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
msg = f'{pathology}: {average_accuracy[idx] * 100:.2f}% | '
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
msg = f'\n\nIndividual specificity scores:\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
msg = f'{pathology}: {average_specificity[idx] * 100:.2f}% | '
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
msg = f'\n\nIndividual sensitivity scores:\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
msg = f'{pathology}: {average_sensitivity[idx] * 100:.2f}% | '
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
def main_test_DP_2D(global_config_path="privacydomain/config/config.yaml", experiment_name='central_exp_for_test',
resnet_num=50, mish=False, experiment_epoch_num=10, dataset_name='vindr'):
"""Main function for multi label prediction with differential privacy
Parameters
----------
experiment_name: str
name of the experiment to be loaded.
"""
params = open_experiment(experiment_name, global_config_path)
cfg_path = params['cfg_path']
if dataset_name == 'vindr':
test_dataset = vindr_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'chexpert':
test_dataset = chexpert_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'mimic':
test_dataset = mimic_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'UKA':
test_dataset = UKA_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'cxr14':
test_dataset = cxr14_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'padchest':
test_dataset = padchest_data_loader(cfg_path=cfg_path, mode='test', augment=False)
weight = test_dataset.pos_weight()
label_names = test_dataset.chosen_labels
# Changeable network parameters
model = load_pretrained_resnet(num_classes=len(weight), resnet_num=resnet_num, mish=mish)
# model = ModuleValidator.fix(model)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=params['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=16)
optimizer = torch.optim.NAdam(model.parameters(), lr=float(params['Network']['lr']),
weight_decay=float(params['Network']['weight_decay']))
errors = ModuleValidator.validate(model, strict=False)
assert len(errors) == 0
privacy_engine = PrivacyEngine()
model, _, _ = privacy_engine.make_private_with_epsilon(
module=model,
optimizer=optimizer, # not important during testing; you should only put a placeholder here
data_loader=test_loader, # not important during testing; you should only put a placeholder here
epochs=params['Network']['num_epochs'], # not important during testing; you should only put a placeholder here
target_epsilon=params['DP']['epsilon'], # not important during testing; you should only put a placeholder here
target_delta=float(params['DP']['delta']), # not important during testing; you should only put a placeholder here
max_grad_norm=params['DP']['max_grad_norm']) # not important during testing; you should only put a placeholder here
# Initialize prediction
predictor = Prediction(cfg_path, label_names)
predictor.setup_model_DP(model=model, privacy_engine=privacy_engine, epoch_num=experiment_epoch_num)
average_f1_score, average_AUROC, average_accuracy, average_specificity, average_sensitivity, average_precision = predictor.evaluate_2D(test_loader)
print('------------------------------------------------------'
'----------------------------------')
print(f'\t experiment: {experiment_name}\n')
print(f'\t avg AUROC: {average_AUROC.mean() * 100:.2f}% | avg accuracy: {average_accuracy.mean() * 100:.2f}%'
f' | avg specificity: {average_specificity.mean() * 100:.2f}%'
f' | avg recall (sensitivity): {average_sensitivity.mean() * 100:.2f}% | avg precision: {average_precision.mean() * 100:.2f}% | avg F1: {average_f1_score.mean() * 100:.2f}%\n')
print('Individual AUROC:')
for idx, pathology in enumerate(predictor.label_names):
print(f'\t{pathology}: {average_AUROC[idx] * 100:.2f}%')
print('\nIndividual accuracy:')
for idx, pathology in enumerate(predictor.label_names):
print(f'\t{pathology}: {average_accuracy[idx] * 100:.2f}%')
print('\nIndividual specificity scores:')
for idx, pathology in enumerate(predictor.label_names):
print(f'\t{pathology}: {average_specificity[idx] * 100:.2f}%')
print('\nIndividual sensitivity scores:')
for idx, pathology in enumerate(predictor.label_names):
print(f'\t{pathology}: {average_sensitivity[idx] * 100:.2f}%')
print('------------------------------------------------------'
'----------------------------------')
# saving the stats
msg = f'----------------------------------------------------------------------------------------\n' \
f'\t experiment: {experiment_name}\n\n' \
f'avg AUROC: {average_AUROC.mean() * 100:.2f}% | avg accuracy: {average_accuracy.mean() * 100:.2f}% ' \
f' | avg specificity: {average_specificity.mean() * 100:.2f}%' \
f' | avg recall (sensitivity): {average_sensitivity.mean() * 100:.2f}% | avg precision: {average_precision.mean() * 100:.2f}% | avg F1: {average_f1_score.mean() * 100:.2f}%\n\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
msg = f'Individual AUROC:\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
msg = f'{pathology}: {average_AUROC[idx] * 100:.2f}% | '
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
msg = f'\n\nIndividual accuracy:\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
msg = f'{pathology}: {average_accuracy[idx] * 100:.2f}% | '
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
msg = f'\n\nIndividual specificity scores:\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
msg = f'{pathology}: {average_specificity[idx] * 100:.2f}% | '
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
msg = f'\n\nIndividual sensitivity scores:\n'
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
msg = f'{pathology}: {average_sensitivity[idx] * 100:.2f}% | '
with open(os.path.join(params['target_dir'], params['stat_log_path']) + '/test_Stats', 'a') as f:
f.write(msg)
def main_test_normal_bootstrap(global_config_path="privacydomain/config/config.yaml", experiment_name='central_exp_for_test', experiment_epoch_num=100, dataset_name='vindr', resnet_num=9, mish=True):
"""Main function for multi label prediction
model1 must be DP model
Parameters
----------
experiment_name: str
name of the experiment to be loaded.
"""
params = open_experiment(experiment_name, global_config_path)
cfg_path = params['cfg_path']
if dataset_name == 'vindr':
test_dataset = vindr_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'chexpert':
test_dataset = chexpert_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'mimic':
test_dataset = mimic_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'UKA':
test_dataset = UKA_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'cxr14':
test_dataset = cxr14_data_loader(cfg_path=cfg_path, mode='test', augment=False)
elif dataset_name == 'padchest':
test_dataset = padchest_data_loader(cfg_path=cfg_path, mode='test', augment=False)
weight = test_dataset.pos_weight()
label_names = test_dataset.chosen_labels
index_list = []
for counter in range(1000):
index_list.append(np.random.choice(len(test_dataset), len(test_dataset)))
model = load_pretrained_resnet(num_classes=len(weight), resnet_num=resnet_num, mish=mish)
# model = ModuleValidator.fix(model)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=params['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=16)
errors = ModuleValidator.validate(model, strict=False)
assert len(errors) == 0
# Initialize prediction
predictor = Prediction(cfg_path, label_names)
predictor.setup_model(model=model, epoch_num=experiment_epoch_num)
pred_array1, target_array1 = predictor.predict_only(test_loader)
AUC_list1 = predictor.bootstrapper(pred_array1.cpu().numpy(), target_array1.int().cpu().numpy(), index_list, dataset_name)
def main_test_DP_bootstrap(global_config_path="privacydomain/config/config.yaml", experiment_name1='central_exp_for_test', experiment1_epoch_num=100, dataset_name='vindr', resnet_num=9, mish=True):
"""Main function for multi label prediction
model1 must be DP model
Parameters
----------
experiment_name: str
name of the experiment to be loaded.
"""
params1 = open_experiment(experiment_name1, global_config_path)
cfg_path1 = params1['cfg_path']
if dataset_name == 'vindr':
test_dataset = vindr_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'chexpert':
test_dataset = chexpert_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'mimic':
test_dataset = mimic_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'UKA':
test_dataset = UKA_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'cxr14':
test_dataset = cxr14_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'padchest':
test_dataset = padchest_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
weight = test_dataset.pos_weight()
label_names = test_dataset.chosen_labels
index_list = []
for counter in range(1000):
index_list.append(np.random.choice(len(test_dataset), len(test_dataset)))
model1 = load_pretrained_resnet(num_classes=len(weight), resnet_num=resnet_num, mish=mish)
# model1 = ModuleValidator.fix(model1)
optimizer1 = torch.optim.NAdam(model1.parameters(), lr=float(params1['Network']['lr']),
weight_decay=float(params1['Network']['weight_decay']))
errors = ModuleValidator.validate(model1, strict=False)
assert len(errors) == 0
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=params1['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=16)
privacy_engine1 = PrivacyEngine()
model1, _, _ = privacy_engine1.make_private_with_epsilon(
module=model1,
optimizer=optimizer1, # not important during testing; you should only put a placeholder here
data_loader=test_loader, # not important during testing; you should only put a placeholder here
epochs=params1['Network']['num_epochs'], # not important during testing; you should only put a placeholder here
target_epsilon=params1['DP']['epsilon'], # not important during testing; you should only put a placeholder here
target_delta=float(params1['DP']['delta']), # not important during testing; you should only put a placeholder here
max_grad_norm=params1['DP']['max_grad_norm']) # not important during testing; you should only put a placeholder here
# Initialize prediction 1
predictor1 = Prediction(cfg_path1, label_names)
predictor1.setup_model_DP(model=model1, privacy_engine=privacy_engine1, epoch_num=experiment1_epoch_num)
delta = float(params1['DP']['delta'])
epsilon = predictor1.privacy_engine.get_epsilon(delta)
print(f"\n(ε = {epsilon:.2f}, δ = {delta})\n")
msg = f"\n(ε = {epsilon:.2f}, δ = {delta})\n"
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
pred_array1, target_array1 = predictor1.predict_only(test_loader)
AUC_list1 = predictor1.bootstrapper(pred_array1.cpu().numpy(), target_array1.int().cpu().numpy(), index_list, dataset_name)
def main_test_2D_pvalue_out_of_bootstrap(global_config_path="privacydomain/config/config.yaml",
experiment_name1='central_exp_for_test', experiment_name2='central_exp_for_test', dataset_name='vindr',
experiment1_epoch_num=100, experiment2_epoch_num=100, resnet_num=9, mish=True):
"""Main function for multi label prediction
model1 must be DP model
model2 must be non DP model
Parameters
----------
experiment_name: str
name of the experiment to be loaded.
"""
params1 = open_experiment(experiment_name1, global_config_path)
cfg_path1 = params1['cfg_path']
if dataset_name == 'vindr':
test_dataset = vindr_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'chexpert':
test_dataset = chexpert_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'mimic':
test_dataset = mimic_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'UKA':
test_dataset = UKA_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'cxr14':
test_dataset = cxr14_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
elif dataset_name == 'padchest':
test_dataset = padchest_data_loader(cfg_path=cfg_path1, mode='test', augment=False)
weight = test_dataset.pos_weight()
label_names = test_dataset.chosen_labels
index_list = []
for counter in range(1000):
index_list.append(np.random.choice(len(test_dataset), len(test_dataset)))
model1 = load_pretrained_resnet(num_classes=len(weight), resnet_num=resnet_num, mish=mish)
# model1 = ModuleValidator.fix(model1)
optimizer1 = torch.optim.NAdam(model1.parameters(), lr=float(params1['Network']['lr']),
weight_decay=float(params1['Network']['weight_decay']))
errors = ModuleValidator.validate(model1, strict=False)
assert len(errors) == 0
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=params1['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=16)
privacy_engine1 = PrivacyEngine()
model1, _, _ = privacy_engine1.make_private_with_epsilon(
module=model1,
optimizer=optimizer1, # not important during testing; you should only put a placeholder here
data_loader=test_loader, # not important during testing; you should only put a placeholder here
epochs=params1['Network']['num_epochs'], # not important during testing; you should only put a placeholder here
target_epsilon=params1['DP']['epsilon'], # not important during testing; you should only put a placeholder here
target_delta=float(params1['DP']['delta']), # not important during testing; you should only put a placeholder here
max_grad_norm=params1['DP']['max_grad_norm']) # not important during testing; you should only put a placeholder here
# Initialize prediction 1
predictor1 = Prediction(cfg_path1, label_names)
predictor1.setup_model_DP(model=model1, privacy_engine=privacy_engine1, epoch_num=experiment1_epoch_num)
delta = float(6e-6)
epsilon = predictor1.privacy_engine.get_epsilon(delta)
print(f"\n(ε = {epsilon:.2f}, δ = {delta})\n")
msg = f"\n(ε = {epsilon:.2f}, δ = {delta})\n"
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
pred_array1, target_array1 = predictor1.predict_only(test_loader)
AUC_list1 = predictor1.bootstrapper(pred_array1.cpu().numpy(), target_array1.int().cpu().numpy(), index_list, dataset_name)
# Initialize prediction 2
params2 = open_experiment(experiment_name2, global_config_path)
cfg_path2 = params2['cfg_path']
model2 = load_pretrained_resnet(num_classes=len(weight), resnet_num=resnet_num, mish=False)
# model2 = ModuleValidator.fix(model2)
optimizer2 = torch.optim.NAdam(model2.parameters(), lr=float(params2['Network']['lr']),
weight_decay=float(params2['Network']['weight_decay']))
errors = ModuleValidator.validate(model2, strict=False)
assert len(errors) == 0
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=params2['Network']['physical_batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=16)
privacy_engine2 = PrivacyEngine()
model2, _, _ = privacy_engine2.make_private_with_epsilon(
module=model2,
optimizer=optimizer2, # not important during testing; you should only put a placeholder here
data_loader=test_loader, # not important during testing; you should only put a placeholder here
epochs=params2['Network']['num_epochs'], # not important during testing; you should only put a placeholder here
target_epsilon=params2['DP']['epsilon'], # not important during testing; you should only put a placeholder here
target_delta=float(params2['DP']['delta']), # not important during testing; you should only put a placeholder here
max_grad_norm=params2['DP']['max_grad_norm']) # not important during testing; you should only put a placeholder here
predictor2 = Prediction(cfg_path2, label_names)
predictor2.setup_model_DP(model=model2, privacy_engine=privacy_engine2, epoch_num=experiment2_epoch_num)
delta = float(6e-6)
epsilon = predictor1.privacy_engine.get_epsilon(delta)
print(f"\n(ε = {epsilon:.2f}, δ = {delta})\n")
msg = f"\n(ε = {epsilon:.2f}, δ = {delta})\n"
with open(os.path.join(params2['target_dir'], params2['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
pred_array2, target_array2 = predictor2.predict_only(test_loader)
AUC_list2 = predictor2.bootstrapper(pred_array2.cpu().numpy(), target_array2.int().cpu().numpy(), index_list, dataset_name)
print('individual labels p-values:\n')
for idx, pathology in enumerate(label_names):
counter = AUC_list1[:, idx] > AUC_list2[:, idx]
ratio1 = (len(counter) - counter.sum()) / len(counter)
if ratio1 <= 0.05:
print(f'\t{pathology} p-value: {ratio1}; model 1 significantly higher AUC than model 2')
else:
counter = AUC_list2[:, idx] > AUC_list1[:, idx]
ratio2 = (len(counter) - counter.sum()) / len(counter)
if ratio2 <= 0.05:
print(f'\t{pathology} p-value: {ratio2}; model 2 significantly higher AUC than model 1')
else:
print(f'\t{pathology} p-value: {ratio1}; models NOT significantly different for this label')
print('\nAvg AUC of labels p-values:\n')
avgAUC_list1 = AUC_list1.mean(1)
avgAUC_list2 = AUC_list2.mean(1)
counter = avgAUC_list1 > avgAUC_list2
ratio1 = (len(counter) - counter.sum()) / len(counter)
if ratio1 <= 0.05:
print(f'\tp-value: {ratio1}; model 1 significantly higher AUC than model 2 on average')
else:
counter = avgAUC_list2 > avgAUC_list1
ratio2 = (len(counter) - counter.sum()) / len(counter)
if ratio2 <= 0.05:
print(f'\tp-value: {ratio2}; model 2 significantly higher AUC than model 1 on average')
else:
print(f'\tp-value: {ratio1}; models NOT significantly different on average for all labels')
msg = f'\n\nindividual labels p-values:\n'
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
with open(os.path.join(params2['target_dir'], params2['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
counter = AUC_list1[:, idx] > AUC_list2[:, idx]
ratio1 = (len(counter) - counter.sum()) / len(counter)
if ratio1 <= 0.05:
msg = f'\t{pathology} p-value: {ratio1}; model 1 significantly higher AUC than model 2'
else:
counter = AUC_list2[:, idx] > AUC_list1[:, idx]
ratio2 = (len(counter) - counter.sum()) / len(counter)
if ratio2 <= 0.05:
msg = f'\t{pathology} p-value: {ratio2}; model 2 significantly higher AUC than model 1'
else:
msg = f'\t{pathology} p-value: {ratio1}; models NOT significantly different for this label'
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
with open(os.path.join(params2['target_dir'], params2['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
msg = f'\n\nAvg AUC of labels p-values:\n'
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
with open(os.path.join(params2['target_dir'], params2['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
avgAUC_list1 = AUC_list1.mean(1)
avgAUC_list2 = AUC_list2.mean(1)
counter = avgAUC_list1 > avgAUC_list2
ratio1 = (len(counter) - counter.sum()) / len(counter)
if ratio1 <= 0.05:
msg = f'\tp-value: {ratio1}; model 1 significantly higher AUC than model 2 on average'
else:
counter = avgAUC_list2 > avgAUC_list1
ratio2 = (len(counter) - counter.sum()) / len(counter)
if ratio2 <= 0.05:
msg = f'\tp-value: {ratio2}; model 2 significantly higher AUC than model 1 on average'
else:
msg = f'\tp-value: {ratio1}; models NOT significantly different on average for all labels'
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
with open(os.path.join(params2['target_dir'], params2['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
def load_pretrained_resnet(num_classes=2, resnet_num=34, pretrained=False, mish=True):
# Load a pre-trained model from config file
# Load a pre-trained model from Torchvision
if resnet_num == 9:
model = models.resnet.ResNet(models.resnet.BasicBlock, [1, 1, 1, 1])
in_features = model.fc.in_features
model.avgpool = torch.nn.AdaptiveAvgPool2d(1)
model.fc = torch.nn.Linear(in_features, num_classes)
model.bn1 = torch.nn.GroupNorm(32, 64)
model.layer1[0].bn1 = torch.nn.GroupNorm(32, 64)
model.layer1[0].bn2 = torch.nn.GroupNorm(32, 64)
model.layer2[0].bn1 = torch.nn.GroupNorm(32, 128)
model.layer2[0].bn2 = torch.nn.GroupNorm(32, 128)
model.layer2[0].downsample[1] = torch.nn.GroupNorm(32, 128)
model.layer3[0].bn1 = torch.nn.GroupNorm(32, 256)
model.layer3[0].bn2 = torch.nn.GroupNorm(32, 256)
model.layer3[0].downsample[1] = torch.nn.GroupNorm(32, 256)
model.layer4[0].bn1 = torch.nn.GroupNorm(32, 512)
model.layer4[0].bn2 = torch.nn.GroupNorm(32, 512)
model.layer4[0].downsample[1] = torch.nn.GroupNorm(32, 512)
if mish:
activation = torch.nn.Mish()
model.relu = activation
model.layer1[0].relu = activation
model.layer1[0].relu = activation
model.layer2[0].relu = activation
model.layer2[0].relu = activation
model.layer3[0].relu = activation
model.layer3[0].relu = activation
model.layer4[0].relu = activation
model.layer4[0].relu = activation
if pretrained:
model.load_state_dict(torch.load('/pretraining_resnet9_512.pth'))
for param in model.parameters():
param.requires_grad = True
elif resnet_num == 18:
if pretrained:
model = models.resnet18(weights='DEFAULT')
else:
model = models.resnet18()
for param in model.parameters():
param.requires_grad = True
model.fc = torch.nn.Sequential(
torch.nn.Linear(512, num_classes)) # for resnet 18
elif resnet_num == 34:
if pretrained:
model = models.resnet34(weights='DEFAULT')
else:
model = models.resnet34()
for param in model.parameters():
param.requires_grad = True
model.fc = torch.nn.Sequential(
torch.nn.Linear(512, num_classes)) # for resnet 34
elif resnet_num == 50:
if pretrained:
model = models.resnet50(weights='DEFAULT')
else:
model = models.resnet50()
for param in model.parameters():
param.requires_grad = True
model.fc = torch.nn.Sequential(
torch.nn.Linear(2048, num_classes)) # for resnet 50
return model