-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
750 lines (652 loc) · 35 KB
/
main.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
import time
import torch
import wandb
import numpy as np
import torch.nn as nn
from dataloader import *
from torch.utils.data import DataLoader, RandomSampler
import argparse, os
from modules import attmil,clam,dsmil,transmil,mean_max,rrt,attmil_ibmil
from torch.nn.functional import one_hot
from torch.cuda.amp import GradScaler
from contextlib import suppress
import time
from timm.utils import AverageMeter,dispatch_clip_grad
from timm.models import model_parameters
from collections import OrderedDict
from utils import *
def main(args):
# set seed
seed_torch(args.seed)
# --->generate dataset
if args.datasets.lower() == 'camelyon16':
label_path=os.path.join(args.dataset_root,'label.csv')
p, l = get_patient_label(label_path)
index = [i for i in range(len(p))]
random.shuffle(index)
p = p[index]
l = l[index]
elif args.datasets.lower() == 'tcga':
label_path=os.path.join(args.dataset_root,'label.csv')
p, l = get_patient_label(label_path)
index = [i for i in range(len(p))]
random.shuffle(index)
p = p[index]
l = l[index]
if args.cv_fold > 1:
train_p, train_l, test_p, test_l,val_p,val_l = get_kflod(args.cv_fold, p, l,args.val_ratio)
acs, pre, rec,fs,auc,te_auc,te_fs=[],[],[],[],[],[],[]
ckc_metric = [acs, pre, rec,fs,auc,te_auc,te_fs]
if not args.no_log:
print('Dataset: ' + args.datasets)
# resume
if args.auto_resume and not args.no_log:
ckp = torch.load(os.path.join(args.model_path,'ckp.pt'))
args.fold_start = ckp['k']
if len(ckp['ckc_metric']) == 6:
acs, pre, rec,fs,auc,te_auc = ckp['ckc_metric']
elif len(ckp['ckc_metric']) == 7:
acs, pre, rec,fs,auc,te_auc,te_fs = ckp['ckc_metric']
else:
acs, pre, rec,fs,auc = ckp['ckc_metric']
for k in range(args.fold_start, args.cv_fold):
if not args.no_log:
print('Start %d-fold cross validation: fold %d ' % (args.cv_fold, k))
ckc_metric = one_fold(args,k,ckc_metric,train_p, train_l, test_p, test_l,val_p,val_l)
if args.always_test:
if args.wandb:
wandb.log({
"cross_val/te_auc_mean":np.mean(np.array(te_auc)),
"cross_val/te_auc_std":np.std(np.array(te_auc)),
"cross_val/te_f1_mean":np.mean(np.array(te_fs)),
"cross_val/te_f1_std":np.std(np.array(te_fs)),
})
if args.wandb:
wandb.log({
"cross_val/acc_mean":np.mean(np.array(acs)),
"cross_val/auc_mean":np.mean(np.array(auc)),
"cross_val/f1_mean":np.mean(np.array(fs)),
"cross_val/pre_mean":np.mean(np.array(pre)),
"cross_val/recall_mean":np.mean(np.array(rec)),
"cross_val/acc_std":np.std(np.array(acs)),
"cross_val/auc_std":np.std(np.array(auc)),
"cross_val/f1_std":np.std(np.array(fs)),
"cross_val/pre_std":np.std(np.array(pre)),
"cross_val/recall_std":np.std(np.array(rec)),
})
if not args.no_log:
print('Cross validation accuracy mean: %.3f, std %.3f ' % (np.mean(np.array(acs)), np.std(np.array(acs))))
print('Cross validation auc mean: %.3f, std %.3f ' % (np.mean(np.array(auc)), np.std(np.array(auc))))
print('Cross validation precision mean: %.3f, std %.3f ' % (np.mean(np.array(pre)), np.std(np.array(pre))))
print('Cross validation recall mean: %.3f, std %.3f ' % (np.mean(np.array(rec)), np.std(np.array(rec))))
print('Cross validation fscore mean: %.3f, std %.3f ' % (np.mean(np.array(fs)), np.std(np.array(fs))))
def one_fold(args,k,ckc_metric,train_p, train_l, test_p, test_l,val_p,val_l):
# ---> Initialization
seed_torch(args.seed)
loss_scaler = GradScaler() if args.amp else None
amp_autocast = torch.cuda.amp.autocast if args.amp else suppress
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
acs,pre,rec,fs,auc,te_auc,te_fs = ckc_metric
# ---> Loading data
if args.datasets.lower() == 'camelyon16':
train_set = C16Dataset(train_p[k],train_l[k],root=args.dataset_root,persistence=args.persistence,keep_same_psize=args.same_psize,is_train=True)
test_set = C16Dataset(test_p[k],test_l[k],root=args.dataset_root,persistence=args.persistence,keep_same_psize=args.same_psize)
if args.val_ratio != 0.:
val_set = C16Dataset(val_p[k],val_l[k],root=args.dataset_root,persistence=args.persistence,keep_same_psize=args.same_psize)
else:
val_set = test_set
elif args.datasets.lower() == 'tcga':
train_set = TCGADataset(train_p[k],train_l[k],args.tcga_max_patch,args.dataset_root,persistence=args.persistence,keep_same_psize=args.same_psize,is_train=True,_type=args.tcga_sub)
test_set = TCGADataset(test_p[k],test_l[k],args.tcga_max_patch,args.dataset_root,persistence=args.persistence,keep_same_psize=args.same_psize,_type=args.tcga_sub)
if args.val_ratio != 0.:
val_set = TCGADataset(val_p[k],val_l[k],args.tcga_max_patch,args.dataset_root,persistence=args.persistence,keep_same_psize=args.same_psize,_type=args.tcga_sub)
else:
val_set = test_set
if args.fix_loader_random:
# generated by int(torch.empty((), dtype=torch.int64).random_().item())
big_seed_list = 7784414403328510413
generator = torch.Generator()
generator.manual_seed(big_seed_list)
train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers,generator=generator)
else:
train_loader = DataLoader(train_set, batch_size=args.batch_size, sampler=RandomSampler(train_set), num_workers=args.num_workers)
val_loader = DataLoader(val_set, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers)
test_loader = DataLoader(test_set, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers)
# bulid networks
if args.only_rrt_enc and args.model != 'rrtmil':
model_params = {
'region_num': args.region_num, # fixed better
'drop_path': args.drop_path, # fixed better
'n_layers': args.n_trans_layers, # fixed better
'attn': args.attn, # fixed better
'epeg': args.epeg, # fixed better
'cr_msa': args.cr_msa, # fixed better
'all_shortcut': args.all_shortcut, # fixed better
'crmsa_mlp':args.crmsa_mlp, # fixed better
'crmsa_heads':args.crmsa_heads,# fixed better
'crmsa_k': args.crmsa_k, # hyper-para [1,3,5]
'epeg_k': args.epeg_k, # hyper-para [9,15,21]
'need_init': True
}
rrt_enc = rrt.RRTEncoder(**model_params).to(device)
else:
rrt_enc = None
if args.model == 'rrtmil':
model_params = {
'input_dim': args.input_dim,
'n_classes': args.n_classes,
'dropout': args.dropout,
'act': args.act,
'region_num': args.region_num,
'pos': args.pos,
'pos_pos': args.pos_pos,
'pool': args.pool,
'peg_k': args.peg_k,
'drop_path': args.drop_path,
'n_layers': args.n_trans_layers,
'n_heads': args.n_heads,
'attn': args.attn,
'da_act': args.da_act,
'trans_dropout': args.trans_drop_out,
'ffn': args.ffn,
'mlp_ratio': args.mlp_ratio,
'trans_dim': args.trans_dim,
'epeg': args.epeg,
'min_region_num': args.min_region_num,
'qkv_bias': args.qkv_bias,
'epeg_k': args.epeg_k,
'epeg_2d': args.epeg_2d,
'epeg_bias': args.epeg_bias,
'epeg_type': args.epeg_type,
'region_attn': args.region_attn,
'peg_1d': args.peg_1d,
'cr_msa': args.cr_msa,
'crmsa_k': args.crmsa_k,
'all_shortcut': args.all_shortcut,
'crmsa_mlp':args.crmsa_mlp,
'crmsa_heads':args.crmsa_heads,
}
model = rrt.RRTMIL(**model_params).to(device)
elif args.model == 'attmil':
model = attmil.DAttention(input_dim=args.input_dim,n_classes=args.n_classes,dropout=args.dropout,act=args.act,rrt=rrt_enc).to(device)
elif args.model == 'gattmil':
model = attmil.AttentionGated(input_dim=args.input_dim,dropout=args.dropout,rrt=rrt_enc).to(device)
elif args.model == 'ibmil':
if not args.confounder_path.endswith('.npy'):
_confounder_path = os.path.join(args.confounder_path,str(k),'train_bag_cls_agnostic_feats_proto_'+str(args.confounder_k)+'.npy')
else:
_confounder_path =args.confounder_path
model = attmil_ibmil.Dattention_ori(out_dim=args.n_classes,dropout=args.dropout,in_size=args.input_dim,confounder_path=_confounder_path,rrt=rrt_enc).to(device)
# follow the official code
# ref: https://github.com/mahmoodlab/CLAM
elif args.model == 'clam_sb':
model = clam.CLAM_SB(input_dim=args.input_dim,n_classes=args.n_classes,dropout=args.dropout,act=args.act,rrt=rrt_enc).to(device)
elif args.model == 'clam_mb':
model = clam.CLAM_MB(input_dim=args.input_dim,n_classes=args.n_classes,dropout=args.dropout,act=args.act,rrt=rrt_enc).to(device)
elif args.model == 'transmil':
model = transmil.TransMIL(input_dim=args.input_dim,n_classes=args.n_classes,dropout=args.dropout,act=args.act).to(device)
elif args.model == 'dsmil':
model = dsmil.MILNet(input_dim=args.input_dim,n_classes=args.n_classes,dropout=args.dropout,act=args.act,rrt=rrt_enc).to(device)
args.cls_alpha = 0.5
args.aux_alpha = 0.5
state_dict_weights = torch.load('./modules/init_cpk/dsmil_init.pth')
info = model.load_state_dict(state_dict_weights, strict=False)
if not args.no_log:
print(info)
elif args.model == 'meanmil':
model = mean_max.MeanMIL(input_dim=args.input_dim,n_classes=args.n_classes,dropout=args.dropout,act=args.act,rrt=rrt_enc).to(device)
elif args.model == 'maxmil':
model = mean_max.MaxMIL(input_dim=args.input_dim,n_classes=args.n_classes,dropout=args.dropout,act=args.act,rrt=rrt_enc).to(device)
if args.loss == 'bce':
criterion = nn.BCEWithLogitsLoss()
elif args.loss == 'ce':
criterion = nn.CrossEntropyLoss()
# optimizer
if args.opt == 'adamw':
optimizer = torch.optim.AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, weight_decay=args.weight_decay)
elif args.opt == 'adam':
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, weight_decay=args.weight_decay)
if args.lr_sche == 'cosine':
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.num_epoch, 0) if not args.lr_supi else torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.num_epoch*len(train_loader), 0)
elif args.lr_sche == 'step':
assert not args.lr_supi
# follow the DTFD-MIL
# ref:https://github.com/hrzhang1123/DTFD-MIL
scheduler = torch.optim.lr_scheduler.StepLR(optimizer,args.num_epoch / 2, 0.2)
elif args.lr_sche == 'const':
scheduler = None
if args.early_stopping:
early_stopping = EarlyStopping(patience=30 if args.datasets=='camelyon16' else 20, stop_epoch=args.max_epoch if args.datasets=='camelyon16' else 70,save_best_model_stage=np.ceil(args.save_best_model_stage * args.num_epoch))
else:
early_stopping = None
optimal_ac, opt_pre, opt_re, opt_fs, opt_auc,opt_epoch = 0, 0, 0, 0,0,0
opt_te_auc,opt_tea_auc,opt_te_fs,opt_te_tea_auc,opt_te_tea_fs = 0., 0., 0., 0., 0.
epoch_start = 0
if args.fix_train_random:
seed_torch(args.seed)
# resume
if args.auto_resume and not args.no_log:
ckp = torch.load(os.path.join(args.model_path,'ckp.pt'))
epoch_start = ckp['epoch']
model.load_state_dict(ckp['model'])
optimizer.load_state_dict(ckp['optimizer'])
scheduler.load_state_dict(ckp['lr_sche'])
early_stopping.load_state_dict(ckp['early_stop'])
optimal_ac, opt_pre, opt_re, opt_fs, opt_auc,opt_epoch = ckp['val_best_metric']
opt_te_auc = ckp['te_best_metric'][0]
if len(ckp['te_best_metric']) > 1:
opt_te_fs = ckp['te_best_metric'][1]
opt_te_tea_auc,opt_te_tea_fs = ckp['te_best_metric'][2:4]
np.random.set_state(ckp['random']['np'])
torch.random.set_rng_state(ckp['random']['torch'])
random.setstate(ckp['random']['py'])
if args.fix_loader_random:
train_loader.sampler.generator.set_state(ckp['random']['loader'])
args.auto_resume = False
train_time_meter = AverageMeter()
# wandb.watch(model, log_freq=100)
for epoch in range(epoch_start, args.num_epoch):
train_loss,start,end = train_loop(args,model,train_loader,optimizer,device,amp_autocast,criterion,loss_scaler,scheduler,k,epoch)
train_time_meter.update(end-start)
stop,accuracy, auc_value, precision, recall, fscore, test_loss = val_loop(args,model,val_loader,device,criterion,early_stopping,epoch)
if args.always_test:
_te_accuracy, _te_auc_value, _te_precision, _te_recall, _te_fscore,_te_test_loss_log = test(args,model,test_loader,device,criterion)
if args.wandb:
rowd = OrderedDict([
("te_acc",_te_accuracy),
("te_precision",_te_precision),
("te_recall",_te_recall),
("te_fscore",_te_fscore),
("te_auc",_te_auc_value),
("te_loss",_te_test_loss_log),
])
rowd = OrderedDict([ (str(k)+'-fold/'+_k,_v) for _k, _v in rowd.items()])
wandb.log(rowd,commit=False)
if _te_auc_value > opt_te_auc:
opt_te_auc = _te_auc_value
opt_te_fs = _te_fscore
if args.wandb:
rowd = OrderedDict([
("best_te_auc",opt_te_auc),
("best_te_f1",_te_fscore)
])
rowd = OrderedDict([ (str(k)+'-fold/'+_k,_v) for _k, _v in rowd.items()])
wandb.log(rowd,commit=False)
if not args.no_log:
print('\r Epoch [%d/%d] train loss: %.1E, test loss: %.1E, accuracy: %.3f, auc_value:%.3f, precision: %.3f, recall: %.3f, fscore: %.3f , time: %.3f(%.3f)' %
(epoch+1, args.num_epoch, train_loss, test_loss, accuracy, auc_value, precision, recall, fscore, train_time_meter.val,train_time_meter.avg))
if args.wandb:
rowd = OrderedDict([
("val_acc",accuracy),
("val_precision",precision),
("val_recall",recall),
("val_fscore",fscore),
("val_auc",auc_value),
("val_loss",test_loss),
("epoch",epoch),
])
rowd = OrderedDict([ (str(k)+'-fold/'+_k,_v) for _k, _v in rowd.items()])
wandb.log(rowd,commit=False)
if auc_value > opt_auc and epoch >= args.save_best_model_stage*args.num_epoch:
optimal_ac = accuracy
opt_pre = precision
opt_re = recall
opt_fs = fscore
opt_auc = auc_value
opt_epoch = epoch
if not os.path.exists(args.model_path):
os.mkdir(args.model_path)
if not args.no_log:
best_pt = {
'model': model.state_dict(),
}
torch.save(best_pt, os.path.join(args.model_path, 'fold_{fold}_model_best_auc.pt'.format(fold=k)))
if args.wandb:
rowd = OrderedDict([
("val_best_acc",optimal_ac),
("val_best_precesion",opt_pre),
("val_best_recall",opt_re),
("val_best_fscore",opt_fs),
("val_best_auc",opt_auc),
("val_best_epoch",opt_epoch),
])
rowd = OrderedDict([ (str(k)+'-fold/'+_k,_v) for _k, _v in rowd.items()])
wandb.log(rowd)
# save checkpoint
random_state = {
'np': np.random.get_state(),
'torch': torch.random.get_rng_state(),
'py': random.getstate(),
'loader': train_loader.sampler.generator.get_state() if args.fix_loader_random else '',
}
ckp = {
'model': model.state_dict(),
'lr_sche': scheduler.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': epoch+1,
'k': k,
'early_stop': early_stopping.state_dict(),
'random': random_state,
'ckc_metric': [acs,pre,rec,fs,auc,te_auc,te_fs],
'val_best_metric': [optimal_ac, opt_pre, opt_re, opt_fs, opt_auc,opt_epoch],
'te_best_metric': [opt_te_auc,opt_te_fs,opt_te_tea_auc,opt_te_tea_fs],
'wandb_id': wandb.run.id if args.wandb else '',
}
if not args.no_log:
torch.save(ckp, os.path.join(args.model_path, 'ckp.pt'))
if stop:
break
# test
if not args.no_log:
best_std = torch.load(os.path.join(args.model_path, 'fold_{fold}_model_best_auc.pt'.format(fold=k)))
info = model.load_state_dict(best_std['model'])
print(info)
accuracy, auc_value, precision, recall, fscore,test_loss_log = test(args,model,test_loader,device,criterion)
if args.wandb:
wandb.log({
"test_acc":accuracy,
"test_precesion":precision,
"test_recall":recall,
"test_fscore":fscore,
"test_auc":auc_value,
"test_loss":test_loss_log,
})
if not args.no_log:
print('\n Optimal accuracy: %.3f ,Optimal auc: %.3f,Optimal precision: %.3f,Optimal recall: %.3f,Optimal fscore: %.3f' % (optimal_ac,opt_auc,opt_pre,opt_re,opt_fs))
acs.append(accuracy)
pre.append(precision)
rec.append(recall)
fs.append(fscore)
auc.append(auc_value)
if args.always_test:
te_auc.append(opt_te_auc)
te_fs.append(opt_te_fs)
return [acs,pre,rec,fs,auc,te_auc,te_fs]
def train_loop(args,model,loader,optimizer,device,amp_autocast,criterion,loss_scaler,scheduler,k,epoch):
start = time.time()
loss_cls_meter = AverageMeter()
loss_cl_meter = AverageMeter()
patch_num_meter = AverageMeter()
keep_num_meter = AverageMeter()
train_loss_log = 0.
model.train()
for i, data in enumerate(loader):
optimizer.zero_grad()
if isinstance(data[0],(list,tuple)):
for i in range(len(data[0])):
data[0][i] = data[0][i].to(device)
bag=data[0]
batch_size=data[0][0].size(0)
else:
bag=data[0].to(device) # b*n*1024
batch_size=bag.size(0)
label=data[1].to(device)
with amp_autocast():
if args.patch_shuffle:
bag = patch_shuffle(bag,args.shuffle_group)
elif args.group_shuffle:
bag = group_shuffle(bag,args.shuffle_group)
if args.model in ('clam_sb','clam_mb','dsmil'):
train_logits,cls_loss,patch_num = model(bag,label,criterion)
keep_num = patch_num
else:
train_logits = model(bag)
cls_loss,patch_num,keep_num = 0.,0.,0.
if args.loss == 'ce':
logit_loss = criterion(train_logits.view(batch_size,-1),label)
elif args.loss == 'bce':
logit_loss = criterion(train_logits.view(batch_size,-1),one_hot(label.view(batch_size,-1).float(),num_classes=2))
train_loss = args.cls_alpha * logit_loss + cls_loss*args.aux_alpha
train_loss = train_loss / args.accumulation_steps
if args.clip_grad > 0.:
dispatch_clip_grad(
model_parameters(model),
value=args.clip_grad, mode='norm')
if (i+1) % args.accumulation_steps == 0:
train_loss.backward()
optimizer.step()
if args.lr_supi and scheduler is not None:
scheduler.step()
loss_cls_meter.update(logit_loss,1)
loss_cl_meter.update(cls_loss,1)
patch_num_meter.update(patch_num,1)
keep_num_meter.update(keep_num,1)
if i % args.log_iter == 0 or i == len(loader)-1:
lrl = [param_group['lr'] for param_group in optimizer.param_groups]
lr = sum(lrl) / len(lrl)
rowd = OrderedDict([
('cls_loss',loss_cls_meter.avg),
('lr',lr),
('cl_loss',loss_cl_meter.avg),
('patch_num',patch_num_meter.avg),
('keep_num',keep_num_meter.avg),
])
if not args.no_log:
print('[{}/{}] logit_loss:{}, cls_loss:{}, patch_num:{}, keep_num:{} '.format(i,len(loader)-1,loss_cls_meter.avg,loss_cl_meter.avg,patch_num_meter.avg, keep_num_meter.avg))
rowd = OrderedDict([ (str(k)+'-fold/'+_k,_v) for _k, _v in rowd.items()])
if args.wandb:
wandb.log(rowd)
train_loss_log = train_loss_log + train_loss.item()
end = time.time()
train_loss_log = train_loss_log/len(loader)
if not args.lr_supi and scheduler is not None:
scheduler.step()
return train_loss_log,start,end
def val_loop(args,model,loader,device,criterion,early_stopping,epoch):
model.eval()
loss_cls_meter = AverageMeter()
test_loss_log = 0.
bag_logit, bag_labels=[], []
# pred= []
with torch.no_grad():
for i, data in enumerate(loader):
if len(data[1]) > 1:
bag_labels.extend(data[1].tolist())
else:
bag_labels.append(data[1].item())
if isinstance(data[0],(list,tuple)):
for i in range(len(data[0])):
data[0][i] = data[0][i].to(device)
bag=data[0]
batch_size=data[0][0].size(0)
else:
bag=data[0].to(device) # b*n*1024
batch_size=bag.size(0)
label=data[1].to(device)
if args.model == 'dsmil':
test_logits,_ = model(bag)
else:
test_logits = model(bag)
if args.loss == 'ce':
if (args.model == 'dsmil' and args.ds_average) or (args.model == 'mhim' and isinstance(test_logits,(list,tuple))):
test_loss = criterion(test_logits[0].view(batch_size,-1),label)
bag_logit.append((0.5*torch.softmax(test_logits[1],dim=-1)+0.5*torch.softmax(test_logits[0],dim=-1))[:,1].cpu().squeeze().numpy())
else:
test_loss = criterion(test_logits.view(batch_size,-1),label)
if batch_size > 1:
bag_logit.extend(torch.softmax(test_logits,dim=-1)[:,1].cpu().squeeze().numpy())
else:
bag_logit.append(torch.softmax(test_logits,dim=-1)[:,1].cpu().squeeze().numpy())
elif args.loss == 'bce':
if args.model == 'dsmil' and args.ds_average:
test_loss = criterion(test_logits.view(batch_size,-1),label)
bag_logit.append((0.5*torch.sigmoid(test_logits[1])+0.5*torch.sigmoid(test_logits[0]).cpu().squeeze().numpy()))
else:
test_loss = criterion(test_logits[0].view(batch_size,-1),label.view(batch_size,-1).float())
bag_logit.append(torch.sigmoid(test_logits).cpu().squeeze().numpy())
loss_cls_meter.update(test_loss,1)
accuracy, auc_value, precision, recall, fscore = five_scores(bag_labels, bag_logit, not args.datasets.lower() == 'camelyon16')
# early stop
if early_stopping is not None:
early_stopping(epoch,-auc_value,model)
stop = early_stopping.early_stop
else:
stop = False
return stop,accuracy, auc_value, precision, recall, fscore,loss_cls_meter.avg
def test(args,model,loader,device,criterion):
model.eval()
test_loss_log = 0.
bag_logit, bag_labels=[], []
with torch.no_grad():
for i, data in enumerate(loader):
if len(data[1]) > 1:
bag_labels.extend(data[1].tolist())
else:
bag_labels.append(data[1].item())
if isinstance(data[0],(list,tuple)):
for i in range(len(data[0])):
data[0][i] = data[0][i].to(device)
bag=data[0]
batch_size=data[0][0].size(0)
else:
bag=data[0].to(device) # b*n*1024
batch_size=bag.size(0)
label=data[1].to(device)
if args.model == 'dsmil':
test_logits,_ = model(bag)
else:
test_logits = model(bag)
if args.loss == 'ce':
if (args.model == 'dsmil' and args.ds_average) or (args.model == 'mhim' and isinstance(test_logits,(list,tuple))):
test_loss = criterion(test_logits[0].view(batch_size,-1),label)
bag_logit.append((0.5*torch.softmax(test_logits[1],dim=-1)+0.5*torch.softmax(test_logits[0],dim=-1))[:,1].cpu().squeeze().numpy())
else:
test_loss = criterion(test_logits.view(batch_size,-1),label)
if batch_size > 1:
bag_logit.extend(torch.softmax(test_logits,dim=-1)[:,1].cpu().squeeze().numpy())
else:
bag_logit.append(torch.softmax(test_logits,dim=-1)[:,1].cpu().squeeze().numpy())
elif args.loss == 'bce':
if args.model == 'dsmil' and args.ds_average:
test_loss = criterion(test_logits[0].view(batch_size,-1),label)
bag_logit.append((0.5*torch.sigmoid(test_logits[1])+0.5*torch.sigmoid(test_logits[0]).cpu().squeeze().numpy()))
else:
test_loss = criterion(test_logits.view(batch_size,-1),label.view(1,-1).float())
bag_logit.append(torch.sigmoid(test_logits).cpu().squeeze().numpy())
test_loss_log = test_loss_log + test_loss.item()
accuracy, auc_value, precision, recall, fscore = five_scores(bag_labels, bag_logit, not args.datasets.lower() == 'camelyon16')
test_loss_log = test_loss_log/len(loader)
return accuracy, auc_value, precision, recall, fscore,test_loss_log
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='MIL Training Script')
# Dataset
parser.add_argument('--datasets', default='camelyon16', type=str, help='[camelyon16, tcga]')
parser.add_argument('--dataset_root', default='/data/xxx/TransMIL', type=str, help='Dataset root path')
parser.add_argument('--tcga_max_patch', default=-1, type=int, help='Max Number of patch in TCGA [-1]')
parser.add_argument('--fix_loader_random', action='store_true', help='Fix random seed of dataloader')
parser.add_argument('--fix_train_random', action='store_true', help='Fix random seed of Training')
parser.add_argument('--val_ratio', default=0., type=float, help='Val-set ratio')
parser.add_argument('--fold_start', default=0, type=int, help='Start validation fold [0]')
parser.add_argument('--cv_fold', default=3, type=int, help='Number of cross validation fold [3]')
parser.add_argument('--persistence', action='store_true', help='Load data into memory')
parser.add_argument('--same_psize', default=0, type=int, help='Keep the same size of all patches [0]')
parser.add_argument('--tcga_sub', default='nsclc', type=str, help='[nsclc,brca]')
# Train
parser.add_argument('--cls_alpha', default=1.0, type=float, help='Main loss alpha')
parser.add_argument('--aux_alpha', default=1.0, type=float, help='Auxiliary loss alpha')
parser.add_argument('--auto_resume', action='store_true', help='Resume from the auto-saved checkpoint')
parser.add_argument('--num_epoch', default=200, type=int, help='Number of total training epochs [200]')
parser.add_argument('--early_stopping', action='store_false', help='Early stopping')
parser.add_argument('--max_epoch', default=130, type=int, help='Number of max training epochs in the earlystopping [130]')
parser.add_argument('--input_dim', default=1024, type=int, help='dim of input features. PLIP features should be [512]')
parser.add_argument('--n_classes', default=2, type=int, help='Number of classes')
parser.add_argument('--batch_size', default=1, type=int, help='Number of batch size')
parser.add_argument('--num_workers', default=2, type=int, help='Number of workers in the dataloader')
parser.add_argument('--loss', default='ce', type=str, help='Classification Loss [ce, bce]')
parser.add_argument('--opt', default='adam', type=str, help='Optimizer [adam, adamw]')
parser.add_argument('--save_best_model_stage', default=0., type=float, help='See DTFD')
parser.add_argument('--model', default='rrtmil', type=str, help='Model name')
parser.add_argument('--seed', default=2021, type=int, help='random number [2021]' )
parser.add_argument('--lr', default=2e-4, type=float, help='Initial learning rate [0.0002]')
parser.add_argument('--lr_sche', default='cosine', type=str, help='Deacy of learning rate [cosine, step, const]')
parser.add_argument('--lr_supi', action='store_true', help='LR scheduler update per iter')
parser.add_argument('--weight_decay', default=1e-5, type=float, help='Weight decay [5e-3]')
parser.add_argument('--accumulation_steps', default=1, type=int, help='Gradient accumulate')
parser.add_argument('--clip_grad', default=.0, type=float, help='Gradient clip')
parser.add_argument('--always_test', action='store_true', help='Test model in the training phase')
# Model
# Other models
parser.add_argument('--ds_average', action='store_true', help='DSMIL hyperparameter')
# Our
parser.add_argument('--only_rrt_enc',action='store_true', help='RRT+other MIL models [dsmil,clam,]')
parser.add_argument('--act', default='relu', type=str, help='Activation func in the projection head [gelu,relu]')
parser.add_argument('--dropout', default=0.25, type=float, help='Dropout in the projection head')
# Transformer
parser.add_argument('--attn', default='rmsa', type=str, help='Inner attention')
parser.add_argument('--pool', default='attn', type=str, help='Classification poolinp. use abmil.')
parser.add_argument('--ffn', action='store_true', help='Feed-forward network. only for ablation')
parser.add_argument('--n_trans_layers', default=2, type=int, help='Number of layer in the transformer')
parser.add_argument('--mlp_ratio', default=4., type=int, help='Ratio of MLP in the FFN')
parser.add_argument('--qkv_bias', action='store_false')
parser.add_argument('--all_shortcut', action='store_true', help='x = x + rrt(x)')
# R-MSA
parser.add_argument('--region_attn', default='native', type=str, help='only for ablation')
parser.add_argument('--min_region_num', default=0, type=int, help='only for ablation')
parser.add_argument('--region_num', default=8, type=int, help='Number of the region. [8,12,16,...]')
parser.add_argument('--trans_dim', default=64, type=int, help='only for ablation')
parser.add_argument('--n_heads', default=8, type=int, help='Number of head in the R-MSA')
parser.add_argument('--trans_drop_out', default=0.1, type=float, help='Dropout in the R-MSA')
parser.add_argument('--drop_path', default=0., type=float, help='Droppath in the R-MSA')
# PEG or PPEG. only for alation
parser.add_argument('--pos', default='none', type=str, help='Position embedding, enable PEG or PPEG')
parser.add_argument('--pos_pos', default=0, type=int, help='Position of pos embed [-1,0]')
parser.add_argument('--peg_k', default=7, type=int, help='K of the PEG and PPEG')
parser.add_argument('--peg_1d', action='store_true', help='1-D PEG and PPEG')
# EPEG
parser.add_argument('--epeg', action='store_false', help='enable epeg')
parser.add_argument('--epeg_bias', action='store_false', help='enable conv bias')
parser.add_argument('--epeg_2d', action='store_true', help='enable 2d conv. only for ablation')
parser.add_argument('--epeg_k', default=15, type=int, help='K of the EPEG. [9,15,21,...]')
parser.add_argument('--epeg_type', default='attn', type=str, help='only for ablation')
# CR-MSA
parser.add_argument('--cr_msa', action='store_false', help='enable CR-MSA')
parser.add_argument('--crmsa_k', default=3, type=int, help='K of the CR-MSA. [1,3,5]')
parser.add_argument('--crmsa_heads', default=8, type=int, help='head of CR-MSA. [1,8,...]')
parser.add_argument('--crmsa_mlp', action='store_true', help='mlp phi of CR-MSA?')
# DAttention
parser.add_argument('--da_act', default='relu', type=str, help='Activation func in the DAttention [gelu,relu]')
# Shuffle
parser.add_argument('--patch_shuffle', action='store_true', help='2-D group shuffle')
parser.add_argument('--group_shuffle', action='store_true', help='Group shuffle')
parser.add_argument('--shuffle_group', default=0, type=int, help='Number of the shuffle group')
# Misc
parser.add_argument('--title', default='default', type=str, help='Title of exp')
parser.add_argument('--project', default='mil_new_c16', type=str, help='Project name of exp')
parser.add_argument('--log_iter', default=100, type=int, help='Log Frequency')
parser.add_argument('--amp', action='store_true', help='Automatic Mixed Precision Training')
parser.add_argument('--wandb', action='store_true', help='Weight&Bias')
parser.add_argument('--no_log', action='store_true', help='Without log')
parser.add_argument('--model_path', type=str, help='Output path')
args = parser.parse_args()
if not os.path.exists(os.path.join(args.model_path,args.project)):
os.mkdir(os.path.join(args.model_path,args.project))
args.model_path = os.path.join(args.model_path,args.project,args.title)
if not os.path.exists(args.model_path):
os.mkdir(args.model_path)
# follow the official code
# ref: https://github.com/mahmoodlab/CLAM
if args.model == 'clam_sb':
args.cls_alpha= .7
args.aux_alpha = .3
elif args.model == 'clam_mb':
args.cls_alpha= .7
args.aux_alpha = .3
elif args.model == 'dsmil':
args.cls_alpha = 0.5
args.aux_alpha = 0.5
if args.datasets == 'camelyon16':
args.fix_loader_random = True
args.fix_train_random = True
if args.datasets == 'tcga':
args.num_workers = 0
args.always_test = True
if args.wandb:
if args.auto_resume:
ckp = torch.load(os.path.join(args.model_path,'ckp.pt'))
wandb.init(project=args.project, entity='dearcat',name=args.title,config=args,dir=os.path.join(args.model_path),id=ckp['wandb_id'],resume='must')
else:
wandb.init(project=args.project, entity='dearcat',name=args.title,config=args,dir=os.path.join(args.model_path))
print(args)
localtime = time.asctime( time.localtime(time.time()) )
print(localtime)
main(args=args)