-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
1306 lines (1091 loc) · 48.7 KB
/
train.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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Run this file, giving a configuration file as input, to train models, e.g.:
python train.py --config configfile.yaml
"""
import argparse
import os
import random
import sys
from collections import Counter
from os.path import join
from pathlib import Path
import numpy as np
import torch
import yaml
from datasets.app_dataset import AppendicitisDataset
from datasets.mawa_dataset import MultiViewAnimalDataset, get_MAwA_datasets
from datasets.synthetic_dataset import get_synthetic_datasets
from sklearn.model_selection import StratifiedKFold
from sklearn.utils.class_weight import compute_class_weight
from torch import nn
from torch.utils.data import DataLoader, SubsetRandomSampler
from tqdm import tqdm
from utils.logging import save_data_split
from utils.printing import print_epoch_val_scores, print_epoch_val_scores_
from utils.metrics import PRC, ROC
from loss import create_loss, calc_concept_weights, calc_concept_sample_weights
from networks import create_model
from validate import validate_epoch_mvcbm, validate_epoch_ssmvcbm
import progressbar
def create_optimizer(config, model, mode):
"""
Parse the configuration file and return a relevant optimizer object
"""
assert config["optimizer"] in ["sgd", "adam"], "Only SGD and Adam optimizers are available!"
optim_params = [
{'params': filter(lambda p: p.requires_grad, model.parameters()), 'lr': config[mode + "_learning_rate"],
'weight_decay': config['weight_decay']}
]
if config["optimizer"] == "sgd":
return torch.optim.SGD(optim_params)
elif config["optimizer"] == "adam":
return torch.optim.Adam(optim_params)
def _create_data_loaders(config, gen, trainset, train_ids, validset=None, val_ids=None):
"""
Construct dataloaders based on the given datasets and config arguments
"""
train_subsampler = SubsetRandomSampler(train_ids, gen)
if val_ids is not None:
val_subsampler = SubsetRandomSampler(val_ids, gen)
pm = config["device"] == "cuda"
train_loader = DataLoader(trainset, batch_size=config["train_batch_size"], sampler=train_subsampler,
num_workers=config["workers"], pin_memory=pm, generator=gen, drop_last=True)
if validset is not None and val_ids is not None:
val_loader = DataLoader(validset, batch_size=config["val_batch_size"], sampler=val_subsampler,
num_workers=config["workers"], pin_memory=pm, generator=gen)
else:
val_loader = None
return train_loader, val_loader
def set_bn_to_eval(m):
if isinstance(m, nn.BatchNorm2d):
m.eval()
def freeze_module(m):
m.eval()
for param in m.parameters():
param.requires_grad = False
def unfreeze_module(m):
m.train()
for param in m.parameters():
param.requires_grad = True
def _get_data(config):
"""
Parse the configuration file and return a relevant dataset
"""
if config['dataset'] == 'mawa':
if config['model'] == 'MVCBM' or config['model'] == 'CBM':
num_concepts = config['num_concepts']
elif config['model'] == 'SSMVCBM':
num_concepts = config['num_s_concepts']
trainset, validset, testset = get_MAwA_datasets(
n_views=config['num_views'], width=60, height=60, classes_file='all_classes.txt', train_ratio=0.6,
val_ratio=0.2, seed=config['seed'], partial_predicates=config['partial_concepts'],
num_predicates=num_concepts, preload=config['preload'])
elif config['dataset'] == 'app':
trainset = AppendicitisDataset(config, augmentation=config["augmentation"], visualize=False, train_data=True)
testset = AppendicitisDataset(config, augmentation=False, visualize=False, train_data=False)
validset = AppendicitisDataset(config, augmentation=False, visualize=False, train_data=True)
elif config['dataset'] == 'synthetic':
if config['model'] == 'MVCBM' or config['model'] == 'CBM':
num_concepts = config['num_synthetic_concepts']
num_partial_concepts = config['num_concepts']
elif config['model'] == 'SSMVCBM':
num_concepts = config['num_synthetic_concepts']
num_partial_concepts = config['num_s_concepts']
trainset, validset, testset = get_synthetic_datasets(
num_vars=config['num_vars'], num_views=config['num_views'], num_points=config['num_points'],
num_predicates=num_concepts, partial_predicates=config['partial_concepts'],
num_partial_predicates=num_partial_concepts, train_ratio=0.6, val_ratio=0.2, seed=config['seed'])
else:
NotImplementedError('ERROR: Dataset not supported!')
return trainset, validset, testset
def _train_one_epoch_mvcbm(mode, epoch, config, model, optimizer, loss_fn, train_loader, target_class_weights,
concepts_class_weights):
"""
Train an MVCBM for one epoch
"""
running_len = 0
running_target_loss = 0
running_concepts_loss = [0] * config['num_concepts']
running_summed_concepts_loss = 0
running_total_loss = 0
# Training mode and the number of epochs
if mode == "j":
num_epochs = config["j_epochs"]
elif mode == "c":
num_epochs = config["c_epochs"]
elif mode == "t":
num_epochs = config["t_epochs"]
else:
raise ValueError("Training mode unknown!")
# Decrease the learning rate, if applicable
if epoch >= 0 and config["decrease_every"] > 0 and (epoch + 1) % config["decrease_every"] == 0:
for g in optimizer.param_groups:
g["lr"] = g["lr"] / config["lr_divisor"]
with tqdm(total=len(train_loader) * config["train_batch_size"], desc=f"Epoch {epoch + 1}/{num_epochs}",
unit="img", position=0, leave=True) as pbar:
model.train()
if config["model"] == "MVCBM" and config["training_mode"] == "sequential" and mode == "t":
model.apply(set_bn_to_eval)
for k, batch in enumerate(train_loader):
# Address the class imbalance for target variable and categorical concepts
labels_temp = batch["label"].numpy()
concepts_temp = batch["concepts"].numpy()
target_sample_weights = [target_class_weights[int(labels_temp[i])] for i in range(len(labels_temp))]
target_sample_weights = torch.FloatTensor(target_sample_weights).to(config["device"])
concepts_sample_weights = calc_concept_sample_weights(config, concepts_class_weights, concepts_temp)
# NOTE: class weights have to be specified per class and per sample
loss_fn.target_sample_weight = target_sample_weights
loss_fn.target_class_weight = torch.FloatTensor(target_class_weights).to(config["device"])
loss_fn.c_weights = concepts_sample_weights
batch_images, target_true, batch_names = batch["images"].to(config["device"]), batch["label"].float().to(
config["device"]), batch["file_names"] # put the data on the device
concepts_true = batch["concepts"].to(config["device"])
additional_features = batch["features"].to(config["device"])
if config['dataset'] == 'app':
batch_names = np.array(list(map(list, zip(*batch_names))), dtype=object) # transpose list
# Mask tensor indicating which views were added for padding
if config['dataset'] == 'app':
mask = torch.tensor(batch_names != "padding.bmp").to(config["device"])
elif config['dataset'] == 'mawa':
mask = torch.ones((batch_images.shape[0], config['num_views'])).to(config['device'])
elif config['dataset'] == 'synthetic':
mask = torch.ones((batch_images.shape[0], config['num_views'])).to(config['device'])
# Forward pass
concepts_pred, target_pred_probs, target_pred_logits, attn_weights = model(
batch_images, mask, additional_features)
target_pred_probs = target_pred_probs.squeeze(1)
target_pred_logits = target_pred_logits.squeeze(1)
# Backward pass depends on the training mode of the model
optimizer.zero_grad()
# Compute the loss
target_loss, concepts_loss, summed_concepts_loss, total_loss = loss_fn(
concepts_pred, concepts_true, target_pred_probs, target_pred_logits, target_true)
running_target_loss += target_loss.item() * batch_images.size(0)
for concept_idx in range(len(concepts_loss)):
running_concepts_loss[concept_idx] += concepts_loss[concept_idx].item() * batch_images.size(0)
running_summed_concepts_loss += summed_concepts_loss.item() * batch_images.size(0)
running_total_loss += total_loss.item() * batch_images.size(0)
running_len += batch_images.size(0)
if mode == "j":
total_loss.backward()
elif mode == "c":
summed_concepts_loss.backward()
else:
target_loss.backward()
optimizer.step() # perform an update
# Update the progress bar
pbar.set_postfix(**{"Target loss": running_target_loss / running_len,
"Concepts loss": running_summed_concepts_loss / running_len,
"Total loss": running_total_loss / running_len, "lr": optimizer.param_groups[0]["lr"]})
pbar.update(config["train_batch_size"])
return None
def _train_one_epoch_ssmvcbm(mode, epoch, config, model, optimizer, loss_fn, train_loader, target_class_weights,
concepts_class_weights, beta, gamma, adv_it=None):
"""
Train an SSMVCBM for on epoch
"""
running_len = 0
running_loss = 0
running_s_concepts_loss = [0] * config["num_s_concepts"] if mode == "sc" else None
try: # if sample size for computing correlation matrix calculation isn't specified, batch size will be used
corr_sample_size = config["corr_sample_size"]
except KeyError:
corr_sample_size = config["train_batch_size"]
if mode == "sc":
num_epochs = config["sc_epochs"]
elif mode == "usc":
num_epochs = config["usc_epochs"]
elif mode == "d":
num_epochs = config["d_epochs"]
elif mode == "t":
num_epochs = config["t_epochs"]
else:
raise ValueError("Training mode unknown!")
# Decrease the learning rate, if applicable
if epoch >= 0 and config["decrease_every"] > 0 and (epoch + 1) % config["decrease_every"] == 0:
for g in optimizer.param_groups:
g["lr"] = g["lgr"] / config["lr_divisor"]
# Keep track of the past concept values
past_us_concepts = torch.zeros(corr_sample_size - config["train_batch_size"], config["num_us_concepts"]).to(
config["device"]) if corr_sample_size > config["train_batch_size"] else None
with tqdm(total=len(train_loader) * config["train_batch_size"], desc=f"Epoch {epoch + 1}/{num_epochs}",
unit="img", position=0, leave=True) as pbar:
model.train()
for name, child in model.named_children():
if mode != "sc" and name == "sc_model":
child.apply(set_bn_to_eval)
if mode != "usc" and name == "usc_model":
child.apply(set_bn_to_eval)
for k, batch in enumerate(train_loader):
# Address the class imbalance for target variable and categorical concepts
labels_temp = batch["label"].numpy()
concepts_temp = batch["concepts"].numpy()
target_sample_weights = [target_class_weights[int(labels_temp[i])] for i in range(len(labels_temp))]
target_sample_weights = torch.FloatTensor(target_sample_weights).to(config["device"])
concepts_sample_weights = calc_concept_sample_weights(config, concepts_class_weights, concepts_temp)
# NOTE: class weights have to be specified per class and per sample
loss_fn.target_sample_weight = target_sample_weights
loss_fn.target_class_weight = torch.FloatTensor(target_class_weights).to(config["device"])
loss_fn.c_weights = concepts_sample_weights
batch_images, target_true, batch_names = batch["images"].to(config["device"]), batch["label"].float().to(
config["device"]), batch["file_names"] # put the data on the specified device
concepts_true = batch["concepts"].to(config["device"])
additional_features = batch["features"].to(config["device"])
if config['dataset'] == 'app':
batch_names = np.array(list(map(list, zip(*batch_names))), dtype=object) # transpose list
# Mask tensor indicating which views were added for padding
if config['dataset'] == 'app':
mask = torch.tensor(batch_names != "padding.bmp").to(config["device"])
elif config['dataset'] == 'mawa':
mask = torch.ones((batch_images.shape[0], config['num_views'])).to(config['device'])
elif config['dataset'] == 'synthetic':
mask = torch.ones((batch_images.shape[0], config['num_views'])).to(config['device'])
# Forward pass
s_concepts_pred, us_concepts_pred, s_attn_weights, us_attn_weights, discr_concepts_pred, \
target_pred_probs, target_pred_logits = model(batch_images, mask, additional_features)
target_pred_probs = target_pred_probs.squeeze(1)
target_pred_logits = target_pred_logits.squeeze(1)
if (k + 1) * config["train_batch_size"] < corr_sample_size:
if k == 0:
us_concepts_sample = us_concepts_pred
else:
us_concepts_sample = torch.cat(
(past_us_concepts[-k * config["train_batch_size"]:, :].detach(), us_concepts_pred), dim=0)
else:
us_concepts_sample = torch.cat((past_us_concepts.detach(), us_concepts_pred),
dim=0) if past_us_concepts is not None else us_concepts_pred
if past_us_concepts is not None:
past_us_concepts = past_us_concepts.roll(shifts=-config["train_batch_size"], dims=0)
past_us_concepts[-config["train_batch_size"]:, :] = us_concepts_pred
# Backward pass depends on the training mode of the model
optimizer.zero_grad()
# Compute the loss
target_loss, s_concepts_loss, summed_s_concepts_loss, summed_discr_concepts_loss, summed_gen_concepts_loss, us_corr_loss = \
loss_fn(s_concepts_pred, discr_concepts_pred, concepts_true, target_pred_probs, target_pred_logits,
target_true, us_concepts_sample)
running_len += batch_images.size(0)
if mode == "t":
running_loss += target_loss.item() * batch_images.size(0)
target_loss.backward()
elif mode == "sc":
for concept_idx in range(len(s_concepts_loss)):
running_s_concepts_loss[concept_idx] += s_concepts_loss[concept_idx].item() * batch_images.size(0)
running_loss += summed_s_concepts_loss.item() * batch_images.size(0)
summed_s_concepts_loss.backward()
elif mode == "usc":
running_loss += target_loss.item() * batch_images.size(
0) + beta * summed_gen_concepts_loss.item() * batch_images.size(0)
total_loss = target_loss + beta * summed_gen_concepts_loss + gamma * us_corr_loss
total_loss.backward()
else:
running_loss += summed_discr_concepts_loss.item() * batch_images.size(0)
if config['adversary']:
summed_discr_concepts_loss.backward()
optimizer.step() # perform an update
# Update the progress bar
pbar.set_postfix(**{f"{mode} loss": running_loss / running_len, "lr": optimizer.param_groups[0]["lr"]})
pbar.update(config["train_batch_size"])
return None
def train_mvcbm_kfold(config, gen):
"""
Run the k-fold cross-validation for the MVCBM
"""
# Log the print-outs
old_stdout = sys.stdout
log_file = open(os.path.join(
config["log_directory"], config['run_name'] + '_' + config['experiment_name'] + '_' +
str(config['seed']) + '.log'), 'w')
sys.stdout = log_file
# ---------------------------------
# Prepare data
# ---------------------------------
kfold = StratifiedKFold(n_splits=config["k_folds"], shuffle=True, random_state=config["seed"])
trainset, validset, testset = _get_data(config=config)
labels = []
for x in iter(DataLoader(trainset)):
labels.append(x["label"].item())
labels = np.array(labels)
# ---------------------------------
# Create temporary directories for models and data splits
# ---------------------------------
checkpoint_dir = os.path.join(config['log_directory'], 'checkpoints')
splits_dir = os.path.join(config['log_directory'], 'splits')
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
if not os.path.exists(splits_dir):
os.makedirs(splits_dir)
# Numbers of training epochs for different modules
if config["model"] in ["MVCBM", "CBM", "Dummy"] and \
config["training_mode"] == "joint":
c_epochs = config["j_epochs"]
t_epochs = config["j_epochs"]
elif (config["model"] == "MVCBM" or config["model"] == "CBM") and \
config["training_mode"] == "sequential":
c_epochs = config["c_epochs"]
t_epochs = config["t_epochs"]
else:
c_epochs = None
t_epochs = config["t_epochs"]
c_results = np.empty((config["k_folds"], c_epochs, config["num_concepts"], (4 + 1))) if c_epochs is not None else None
t_results = np.empty((config["k_folds"], t_epochs, (11 + 3))) # 11 metrics + 3 loss functions
roc = ROC(config["k_folds"], range(1, t_epochs + 1))
pr = PRC(config["k_folds"], range(1, t_epochs + 1))
# Iterate over all folds
for fold, (train_ids, val_ids) in enumerate(kfold.split(trainset, labels)):
print()
print("------------------------")
print(f'FOLD {fold + 1}/{config["k_folds"]}')
print("------------------------")
# Instantiate dataloaders
train_loader, val_loader = _create_data_loaders(config, gen, trainset, train_ids=train_ids,
validset=validset, val_ids=val_ids)
test_loader = DataLoader(
testset, batch_size=config["val_batch_size"], num_workers=config["workers"], generator=gen)
save_data_split(splits_dir, trainset, train_ids, validset, val_ids)
# Retrieve labels
train_labels = []
val_labels = []
all_c = []
for concept_idx in range(config["num_concepts"]):
c = []
for x in iter(train_loader):
if concept_idx == 0:
train_labels.extend(x["label"].tolist())
concepts = x["concepts"].numpy()
c.extend(concepts[:, concept_idx])
all_c.append(c)
print("Length of training array", len(train_labels))
print("Train target class distribution: ", Counter(train_labels))
print("Train concepts class distribution: ")
for concept_idx in range(len(all_c)):
print("...", Counter(all_c[concept_idx]))
val_all_c = []
for concept_idx in range(config["num_concepts"]):
c = []
for x in iter(val_loader):
if concept_idx == 0:
val_labels.extend(x["label"].tolist())
concepts = x["concepts"].numpy()
c.extend(concepts[:, concept_idx])
val_all_c.append(c)
print("\nLength of validation array", len(val_labels))
print("Validation target class distribution: ", Counter(val_labels))
print("Validation concepts class distribution: ")
for concept_idx in range(len(val_all_c)):
print("...", Counter(val_all_c[concept_idx]))
target_class_weights = compute_class_weight(
class_weight="balanced", classes=np.unique(train_labels), y=train_labels)
concepts_class_weights = calc_concept_weights(all_c)
# Initialize the model and training objects
model = create_model(config)
model.to(config["device"])
loss_fn = create_loss(config)
# Training the concept prediction model
if (config["model"] == "MVCBM" or config["model"] == "CBM") and \
config["training_mode"] == "sequential":
print("\nStarting concepts training!\n")
mode = "c"
for name, child in model.named_children():
if name.split("_")[0] == "t":
child.apply(freeze_module)
c_optimizer = create_optimizer(config, model, mode)
for epoch in range(0, c_epochs):
_train_one_epoch_mvcbm(mode, epoch, config, model, c_optimizer, loss_fn, train_loader,
target_class_weights, concepts_class_weights)
target_loss, concepts_loss, summed_concepts_loss, total_loss, tMetrics, all_cMetrics, _, _, _ = \
validate_epoch_mvcbm(epoch, config, model, val_loader, loss_fn)
for concept_idx in range(len(concepts_loss)):
c_results[fold, epoch, concept_idx, 0] = concepts_loss[concept_idx]
c_results[fold, epoch, concept_idx, 1:5] = all_cMetrics[concept_idx].get_cMetrics()
print_epoch_val_scores(
config, mode, target_loss, concepts_loss, summed_concepts_loss, total_loss, tMetrics,
all_cMetrics, print_all_c=(config['dataset'] == 'app'))
# Prepare parameters for the target model training
for name, child in model.named_children():
if name.split("_")[0] == "t":
child.apply(unfreeze_module)
else:
child.apply(freeze_module)
# Sequential vs. joint optimisation
if (config["model"] == "MVCBM" or config["model"] == "CBM") and \
config["training_mode"] == "sequential":
print("\nStarting target training!\n")
mode = "t"
optimizer = create_optimizer(config, model, mode)
else:
print("\nStarting joint training!\n")
mode = "j"
optimizer = create_optimizer(config, model, mode)
# Training the target prediction model / performing joint training, depending on the given configuration
for epoch in range(0, t_epochs):
if config["model"] != "Dummy":
_train_one_epoch_mvcbm(mode, epoch, config, model, optimizer, loss_fn,
train_loader, target_class_weights, concepts_class_weights)
target_loss, concepts_loss, summed_concepts_loss, total_loss, tMetrics, all_cMetrics, conf_matrix, \
FP_names, FN_names = validate_epoch_mvcbm(epoch, config, model, val_loader, loss_fn, fold, roc, pr)
t_results[fold, epoch, 0] = target_loss
t_results[fold, epoch, 1] = summed_concepts_loss
t_results[fold, epoch, 2] = total_loss
t_results[fold, epoch, 3:] = tMetrics.get_tMetrics()
if config["model"] in ["MVCBM", "CBM", "Dummy"] and \
config["training_mode"] == "joint":
for concept_idx in range(len(concepts_loss)):
c_results[fold, epoch, concept_idx, 0] = concepts_loss[concept_idx]
c_results[fold, epoch, concept_idx, 1:5] = all_cMetrics[concept_idx].get_cMetrics()
print_epoch_val_scores(
config, mode, target_loss, concepts_loss, summed_concepts_loss, total_loss, tMetrics,
all_cMetrics, print_all_c=(config['dataset'] == 'app'))
torch.save(model.state_dict(), join(checkpoint_dir, f"fold{fold}_model.pth"))
print("\nTraining finished, model saved!", flush=True)
print("Confusion matrix:")
print(conf_matrix)
print()
print()
print("------------------------")
print(f'{config["k_folds"]}-FOLD CROSS VALIDATION COMPLETED')
print("------------------------")
print()
roc.save(Path("ROC_curves"))
pr.save(Path("PR_curves"))
# Stop logging print-outs
sys.stdout = old_stdout
log_file.close()
return None
def train_ssmvcbm_kfold(config, gen):
"""
Run the k-fold cross-validation for the SSMVCBM
"""
# Log the print-outs
old_stdout = sys.stdout
log_file = open(
os.path.join(config["log_directory"], config['run_name'] + '_' + config['experiment_name'] + '_' +
str(config['seed']) + '.log'), 'w')
sys.stdout = log_file
# ---------------------------------
# Prepare data
# ---------------------------------
kfold = StratifiedKFold(n_splits=config["k_folds"], shuffle=True, random_state=config["seed"])
trainset, validset, testset = _get_data(config=config)
labels = []
for x in iter(DataLoader(trainset)):
labels.append(x["label"].item())
labels = np.array(labels)
# ---------------------------------
# Create temporary directories for models and data splits
# ---------------------------------
checkpoint_dir = os.path.join(config['log_directory'], 'checkpoints')
splits_dir = os.path.join(config['log_directory'], 'splits')
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
if not os.path.exists(splits_dir):
os.makedirs(splits_dir)
# Important configuration parameters
sc_epochs = config["sc_epochs"]
adv_it = config["adversarial_it"]
usc_epochs = config["usc_epochs"]
d_epochs = config["d_epochs"]
t_epochs = config["t_epochs"]
try:
beta = config["beta"]
except KeyError:
beta = 1
try:
gamma = config["usc_gamma"]
except KeyError:
gamma = 0
# 4 metrics + loss for every concept, and summed concept loss
sc_results = np.empty((config["k_folds"], sc_epochs, config["num_s_concepts"] + 1, (4 + 1)))
t_results = np.empty((config["k_folds"], t_epochs, (11 + 1))) # 11 metrics + target loss
roc = ROC(config["k_folds"], range(1, t_epochs + 1))
pr = PRC(config["k_folds"], range(1, t_epochs + 1))
# Iterate over all folds
for fold, (train_ids, val_ids) in enumerate(kfold.split(trainset, labels)):
print()
print("------------------------")
print(f'FOLD {fold + 1}/{config["k_folds"]}')
print("------------------------")
# Instantiate dataloaders
train_loader, val_loader = _create_data_loaders(config, gen, trainset, train_ids=train_ids,
validset=validset, val_ids=val_ids)
test_loader = DataLoader(
testset, batch_size=config["val_batch_size"], num_workers=config["workers"], generator=gen)
save_data_split(splits_dir, trainset, train_ids, validset, val_ids)
# Retrieve labels
train_labels = []
val_labels = []
all_c = []
for concept_idx in range(config["num_s_concepts"]):
c = []
for x in iter(train_loader):
if concept_idx == 0:
train_labels.extend(x["label"].tolist())
concepts = x["concepts"].numpy()
c.extend(concepts[:, concept_idx])
all_c.append(c)
print("Length of training array", len(train_labels))
print("Train target class distribution: ", Counter(train_labels))
print("Train concepts class distribution: ")
for concept_idx in range(len(all_c)):
print("...", Counter(all_c[concept_idx]))
val_all_c = []
for concept_idx in range(config["num_s_concepts"]):
c = []
for x in iter(val_loader):
if concept_idx == 0:
val_labels.extend(x["label"].tolist())
concepts = x["concepts"].numpy()
c.extend(concepts[:, concept_idx])
val_all_c.append(c)
print("\nLength of validation array", len(val_labels))
print("Validation target class distribution: ", Counter(val_labels))
print("Validation concepts class distribution: ")
for concept_idx in range(len(val_all_c)):
print("...", Counter(val_all_c[concept_idx]))
target_class_weights = compute_class_weight(
class_weight="balanced", classes=np.unique(train_labels), y=train_labels)
concepts_class_weights = calc_concept_weights(all_c)
# Initialize model and training objects
model = create_model(config)
model.to(config["device"])
loss_fn = create_loss(config)
###########################################################################################################
# Supervised concept learning
###########################################################################################################
mode = "sc"
print("\nStarting supervised concepts training!\n")
# Prepare parameters
for name, child in model.named_children():
if name == "sc_model":
child.apply(unfreeze_module)
else:
print(f"Freezing {name}...")
child.apply(freeze_module)
sc_optimizer = create_optimizer(config, model, mode)
for epoch in range(0, sc_epochs):
_train_one_epoch_ssmvcbm(
mode, epoch, config, model, sc_optimizer, loss_fn, train_loader, target_class_weights,
concepts_class_weights, beta, gamma)
val_loss, val_s_concepts_loss, tMetrics, all_cMetrics, _, _, _, _ = validate_epoch_ssmvcbm(
epoch, mode, config, model, val_loader, loss_fn, beta, gamma)
for concept_idx in range(len(val_s_concepts_loss)):
sc_results[fold, epoch, concept_idx, 0] = val_s_concepts_loss[concept_idx]
sc_results[fold, epoch, concept_idx, 1:5] = all_cMetrics[concept_idx].get_cMetrics()
sc_results[fold, epoch, -1, 0] = val_loss
print_all_c = (config['dataset'] != 'mawa')
print_epoch_val_scores_(config, mode, val_loss, val_s_concepts_loss, tMetrics, all_cMetrics,
print_all_c)
###########################################################################################################
# Representation learning
###########################################################################################################
print("\nStarting representation learning!\n")
for k in range(adv_it):
# Train the representation encoder module
print(f"\nAdversarial training iteration: {k + 1}/{adv_it}\n")
mode = "usc"
print("\nFitting representation encoder...\n")
# prepare parameters
for name, child in model.named_children():
if name in ["usc_model", "t_model"]:
child.apply(unfreeze_module)
else:
print(f"Freezing {name}...")
child.apply(freeze_module)
usc_optimizer = create_optimizer(config, model, mode)
for epoch in range(0, usc_epochs):
_train_one_epoch_ssmvcbm(
mode, epoch, config, model, usc_optimizer, loss_fn, train_loader, target_class_weights,
concepts_class_weights, beta, gamma)
val_loss, val_s_concepts_loss, tMetrics, all_cMetrics, _, _, _, us_cov = validate_epoch_ssmvcbm(
epoch, mode, config, model, val_loader, loss_fn, beta, gamma)
print(f" -- Epoch {epoch} val loss: ", val_loss)
us_cov = us_cov.cpu().detach().numpy()
# Train the adversary
mode = "d"
print("\nFitting the adversary...\n")
# prepare parameters
for name, child in model.named_children():
if name == "discriminator":
child.apply(unfreeze_module)
else:
print(f"Freezing {name}...")
child.apply(freeze_module)
d_optimizer = create_optimizer(config, model, mode)
for epoch in range(0, d_epochs):
_train_one_epoch_ssmvcbm(
mode, epoch, config, model, d_optimizer, loss_fn, train_loader, target_class_weights,
concepts_class_weights, beta, gamma)
val_loss, val_s_concepts_loss, tMetrics, all_cMetrics, _, _, _, _ = validate_epoch_ssmvcbm(
epoch, mode, config, model, val_loader, loss_fn, beta, gamma)
print(f" -- Epoch {epoch} val loss: ", val_loss)
###########################################################################################################
# Target learning
###########################################################################################################
mode = "t"
print("\nStarting target training!\n")
# Prepare parameters
for name, child in model.named_children():
if name == "t_model":
child.apply(unfreeze_module)
else:
print(f"Freezing {name}...")
child.apply(freeze_module)
# Re-initialise target model weights
for name, child in model.named_children():
if name == "t_model":
for t_name, t_child in child.named_children():
t_child.reset_parameters()
t_optimizer = create_optimizer(config, model, mode)
for epoch in range(0, t_epochs):
_train_one_epoch_ssmvcbm(
mode, epoch, config, model, t_optimizer, loss_fn, train_loader, target_class_weights,
concepts_class_weights, beta, gamma)
val_loss, val_s_concepts_loss, tMetrics, all_cMetrics, conf_matrix, FP_names, FN_names, _ = \
validate_epoch_ssmvcbm(epoch, mode, config, model, val_loader, loss_fn, beta, gamma, fold, roc, pr)
t_results[fold, epoch, 0] = val_loss
t_results[fold, epoch, 1:] = tMetrics.get_tMetrics()
print_all_c = (config['dataset'] != 'mawa')
print_epoch_val_scores_(config, mode, val_loss, val_s_concepts_loss, tMetrics, all_cMetrics,
print_all_c)
torch.save(model.state_dict(), join(checkpoint_dir, f"fold{fold}_model.pth"))
print("\nTraining finished, model saved!", flush=True)
print("Confusion matrix:")
print(conf_matrix)
print("List of false positive images: ", FP_names)
print("List of false negative images: ", FN_names)
print()
print()
print("------------------------")
print(f'{config["k_folds"]}-FOLD CROSS VALIDATION COMPLETED')
print("------------------------")
print()
roc.save(Path("ROC_curves"))
pr.save(Path("PR_curves"))
# Stop logging print-outs
sys.stdout = old_stdout
log_file.close()
return None
def train_mvcbm(config, gen):
"""
Train and test an MVCBM model on a single train-test split
"""
# Log the print-outs
old_stdout = sys.stdout
log_file = open(
os.path.join(config["log_directory"], config['run_name'] + '_' + config['experiment_name'] + '_' +
str(config['seed']) + '.log'), 'w')
sys.stdout = log_file
# ---------------------------------
# Prepare data
# ---------------------------------
trainset, validset, testset = _get_data(config=config)
# Retrieve labels
train_labels = []
test_labels = []
all_c = [[] for _ in range(config['num_concepts'])]
tmp_loader = DataLoader(trainset, batch_size=config['train_batch_size'])
bar = progressbar.ProgressBar(maxval=len(tmp_loader))
bar.start()
cnt = 0
for x in iter(tmp_loader):
train_labels.extend(x["label"].cpu().numpy().tolist())
concepts = x["concepts"].cpu().numpy()
for concept_idx in range(len(all_c)):
all_c[concept_idx].extend(concepts[:, concept_idx].tolist())
bar.update(cnt)
cnt += 1
bar.finish()
tmp_loader = DataLoader(testset, batch_size=config['train_batch_size'])
bar = progressbar.ProgressBar(maxval=len(tmp_loader))
bar.start()
cnt = 0
for x in iter(tmp_loader):
test_labels.extend(x["label"].cpu().numpy().tolist())
bar.update(cnt)
cnt += 1
bar.finish()
train_labels = np.array(train_labels)
test_labels = np.array(test_labels)
print("Length of training array", len(train_labels))
print("Length of test array", len(test_labels))
print("Train target class distribution: ", Counter(train_labels))
print("Train concepts class distribution: ")
for concept_idx in range(len(all_c)):
print("...", Counter(all_c[concept_idx]))
print("Test target class distribution: ", Counter(test_labels))
print()
# ---------------------------------
# Create temporary directory for models
# ---------------------------------
checkpoint_dir = os.path.join(config['log_directory'], 'checkpoints')
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
# Numbers of training epochs
if config["model"] in ["MVCBM", "CBM", "Dummy"] and \
config["training_mode"] == "joint":
c_epochs = config["j_epochs"]
t_epochs = config["j_epochs"]
elif (config["model"] == "MVCBM" or config["model"] == "CBM") and \
config["training_mode"] == "sequential":
c_epochs = config["c_epochs"]
t_epochs = config["t_epochs"]
else:
c_epochs = None
t_epochs = config["t_epochs"]
c_results = np.empty((c_epochs, config["num_concepts"], (4 + 1))) if c_epochs is not None else None
t_results = np.empty((t_epochs, (17 + 3))) # 17 metrics + 3 losses
# Instantiate dataloaders
train_loader, _ = _create_data_loaders(config, gen, trainset, train_ids=np.arange(len(train_labels)))
test_loader = DataLoader(testset, batch_size=config["val_batch_size"], num_workers=config["workers"],
generator=gen)
# Concept and class weights
target_class_weights = compute_class_weight(
class_weight="balanced", classes=np.unique(train_labels), y=train_labels)
concepts_class_weights = calc_concept_weights(all_c)
# Initialize model and training objects
model = create_model(config)
model.to(config["device"])
loss_fn = create_loss(config)
print("STARTING FINAL MODEL TRAINING!")
print()
# Concept learning
if (config["model"] == "MVCBM" or config["model"] == "CBM") and \
config["training_mode"] == "sequential":
print("\nStarting concepts training!\n")
mode = "c"
for name, child in model.named_children():
if name.split("_")[0] == "t":
child.apply(freeze_module)
c_optimizer = create_optimizer(config, model, mode)
for epoch in range(0, c_epochs):
if config["model"] != "Dummy":
_train_one_epoch_mvcbm(mode, epoch, config, model, c_optimizer, loss_fn,
train_loader, target_class_weights, concepts_class_weights)
# Validate the model (either every epoc or at the very end)
if config['validate_every_epoch'] or epoch == c_epochs - 1:
target_loss, concepts_loss, summed_concepts_loss, total_loss, tMetrics, all_cMetrics, _, _, _ = \
validate_epoch_mvcbm(epoch, config, model, test_loader, loss_fn)
for concept_idx in range(len(concepts_loss)):
c_results[epoch, concept_idx, 0] = concepts_loss[concept_idx]
c_results[epoch, concept_idx, 1:6] = all_cMetrics[concept_idx].get_cMetrics()
print_epoch_val_scores(config, mode, target_loss, concepts_loss,
summed_concepts_loss, total_loss, tMetrics, all_cMetrics,
print_all_c=(config['dataset'] == 'app'))
# Prepare parameters for target training
for name, child in model.named_children():
if name.split("_")[0] == "t":
child.apply(unfreeze_module)
else:
child.apply(freeze_module)
# Sequential vs. joint optimisation
if (config["model"] == "MVCBM" or config["model"] == "CBM") and \
config["training_mode"] == "sequential":
print("\nStarting target training!\n")
mode = "t"
optimizer = create_optimizer(config, model, mode)
else:
print("\nStarting joint training!\n")
mode = "j"
optimizer = create_optimizer(config, model, mode)
for epoch in range(0, t_epochs):
_train_one_epoch_mvcbm(
mode, epoch, config, model, optimizer, loss_fn, train_loader, target_class_weights, concepts_class_weights)
# Validate the model (either every epoch or at the very end)
if config['validate_every_epoch'] or epoch == t_epochs - 1:
target_loss, concepts_loss, summed_concepts_loss, total_loss, tMetrics, all_cMetrics, conf_matrix, \
FP_names, FN_names = validate_epoch_mvcbm(epoch, config, model, test_loader, loss_fn)
t_results[epoch, 0] = target_loss
t_results[epoch, 1] = summed_concepts_loss
t_results[epoch, 2] = total_loss
tmp_metrics = tMetrics.get_tMetrics()
t_results[epoch, 3:14] = tmp_metrics[:11]
t_results[epoch, 14:19] = np.array([tmp_metrics[11]['FPR at 0.75'], tmp_metrics[11]['FPR at 0.8'],
tmp_metrics[11]['FPR at 0.9'], tmp_metrics[11]['FPR at 0.95'],
tmp_metrics[11]['FPR at 0.99']])
t_results[epoch, 19:] = tmp_metrics[12:]
if config["model"] in ["MVCBM", "CBM", "Dummy"] and \
config["training_mode"] == "joint":
for concept_idx in range(len(concepts_loss)):
c_results[epoch, concept_idx, 0] = concepts_loss[concept_idx]
c_results[epoch, concept_idx, 1:6] = all_cMetrics[concept_idx].get_cMetrics()
print_epoch_val_scores(
config, mode, target_loss, concepts_loss, summed_concepts_loss, total_loss, tMetrics, all_cMetrics,
print_all_c=(config['dataset'] == 'app'))
torch.save(model.state_dict(), join(checkpoint_dir, "final_model_" + config['run_name'] + '_' +
config['experiment_name'] + ".pth"))
print("\nTraining finished, model saved!", flush=True)
print("\nEVALUATION ON THE TEST SET:\n")
t_metric_names = ["t_loss", "c_loss", "total_loss", "t_ppv", "t_npv", "t_sensitivity",
"t_specificity", "t_accuracy", "t_balanced_accuracy", "t_f1_1", "t_f1_0", "t_f1_macro",
"t_auroc", "t_aupr"]
if c_results is not None:
print(f"Summed concepts loss on last epoch: {c_results[-1, -1, 0]}")
if config['dataset'] != 'mawa':
for concept_idx in range(config["num_concepts"]):
c_metric_names = [f"sc{concept_idx}_loss", f"sc{concept_idx}_accuracy",
f"sc{concept_idx}_f1_macro", f"sc{concept_idx}_auroc", f"sc{concept_idx}_aupr"]
print(f"(Concept {concept_idx}) Test results on last epoch: ")
for metric_idx, metric_name in enumerate(c_metric_names):
print(f" {metric_name}: {c_results[-1, concept_idx, metric_idx]}")
print(f"Averaged concepts test metrics on last epoch:")
c_metric_names = [f"loss", f"accuracy", f"f1_macro", f"auroc", f"aupr"]
for metric_idx, metric_name in enumerate(c_metric_names):
print(f" {metric_name}: {sum([c_results[-1, i, metric_idx] for i in range(config['num_concepts'])]) / config['num_concepts']}")
print(f"(Target) Test results on last epoch: ")
for metric_idx, metric_name in enumerate(t_metric_names):