-
Notifications
You must be signed in to change notification settings - Fork 53
/
model_wrapper.py
670 lines (610 loc) · 37.3 KB
/
model_wrapper.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
import os
import numpy as np
from model import _CNN, _FCN, _MLP_A, _MLP_B, _MLP_C, _MLP_D
from utils import matrix_sum, get_accu, get_MCC, get_confusion_matrix, write_raw_score, DPM_statistics, timeit, read_csv
from dataloader import CNN_Data, FCN_Data, MLP_Data, MLP_Data_apoe, CNN_MLP_Data
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torch.optim as optim
from tqdm import tqdm
import numpy as np
"""
model wrapper class are defined in this scripts which includes the following methods:
1. init: initialize dataloader, model
2. train:
3. valid:
4. test:
5. ...
1. FCN wrapper
2. MLP wrapper
3. CNN wrapper
"""
class CNN_Wrapper:
def __init__(self,
fil_num,
drop_rate,
seed,
batch_size,
balanced,
Data_dir,
exp_idx,
model_name,
metric):
"""
:param fil_num: output channel number of the first convolution layer
:param drop_rate: dropout rate of the last 2 layers, see model.py for details
:param seed: random seed
:param batch_size: batch size for training CNN
:param balanced: balanced could take value 0 or 1, corresponding to different approaches to handle data imbalance,
see self.prepare_dataloader for more details
:param Data_dir: data path for training data
:param exp_idx: experiment index maps to different data splits
:param model_name: give a name to the model
:param metric: metric used for saving model during training, can be either 'accuracy' or 'MCC'
for example, if metric == 'accuracy', then the time point where validation set has best accuracy will be saved
"""
self.seed = seed
self.exp_idx = exp_idx
self.Data_dir = Data_dir
self.model_name = model_name
self.eval_metric = get_accu if metric == 'accuracy' else get_MCC
self.model = _CNN(fil_num=fil_num, drop_rate=drop_rate).cuda()
self.prepare_dataloader(batch_size, balanced, Data_dir)
self.checkpoint_dir = './checkpoint_dir/{}_exp{}/'.format(self.model_name, exp_idx)
if not os.path.exists(self.checkpoint_dir):
os.mkdir(self.checkpoint_dir)
def train(self, lr, epochs):
self.optimizer = optim.Adam(self.model.parameters(), lr=lr, betas=(0.5, 0.999))
self.criterion = nn.CrossEntropyLoss(weight=torch.Tensor([1, self.imbalanced_ratio])).cuda()
self.optimal_valid_matrix = [[0, 0], [0, 0]]
self.optimal_valid_metric = 0
self.optimal_epoch = -1
for self.epoch in range(epochs):
self.train_model_epoch()
valid_matrix = self.valid_model_epoch()
print('{}th epoch validation confusion matrix:'.format(self.epoch), valid_matrix, 'eval_metric:', "%.4f" % self.eval_metric(valid_matrix))
self.save_checkpoint(valid_matrix)
print('Best model saved at the {}th epoch:'.format(self.optimal_epoch), self.optimal_valid_metric, self.optimal_valid_matrix)
return self.optimal_valid_metric
def test(self):
print('testing ... ')
self.model.load_state_dict(torch.load('{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch)))
self.model.train(False)
with torch.no_grad():
for stage in ['train', 'valid', 'test', 'AIBL', 'NACC', 'FHS']:
Data_dir = self.Data_dir
if stage in ['AIBL', 'NACC', 'FHS']:
Data_dir = Data_dir.replace('ADNI', stage)
data = CNN_Data(Data_dir, self.exp_idx, stage=stage, seed=self.seed)
dataloader = DataLoader(data, batch_size=10, shuffle=False)
f = open(self.checkpoint_dir + 'raw_score_{}.txt'.format(stage), 'w')
matrix = [[0, 0], [0, 0]]
for idx, (inputs, labels) in enumerate(dataloader):
inputs, labels = inputs.cuda(), labels.cuda()
preds = self.model(inputs)
write_raw_score(f, preds, labels)
matrix = matrix_sum(matrix, get_confusion_matrix(preds, labels))
print(stage + ' confusion matrix ', matrix, ' accuracy ', self.eval_metric(matrix))
f.close()
def gen_features(self):
self.model.load_state_dict(
torch.load('{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch)))
self.model.train(False)
self.feature_dir = './DPMs/{}_exp{}/'.format(self.model_name, self.exp_idx)
if not os.path.exists(self.feature_dir):
os.mkdir(self.feature_dir)
with torch.no_grad():
for stage in ['train', 'valid', 'test', 'AIBL', 'NACC', 'FHS']:
Data_dir = self.Data_dir
if stage in ['AIBL', 'NACC', 'FHS']:
Data_dir = Data_dir.replace('ADNI', stage)
data = CNN_Data(Data_dir, self.exp_idx, stage=stage, seed=self.seed)
filenames = data.Data_list
dataloader = DataLoader(data, batch_size=1, shuffle=False)
for idx, (inputs, labels) in enumerate(dataloader):
inputs, labels = inputs.cuda(), labels.cuda()
preds = self.model(inputs, stage="get_features").cpu().numpy().squeeze()
np.save(self.feature_dir + filenames[idx] + '.npy', preds)
def save_checkpoint(self, valid_matrix):
if self.eval_metric(valid_matrix) >= self.optimal_valid_metric:
self.optimal_epoch = self.epoch
self.optimal_valid_matrix = valid_matrix
self.optimal_valid_metric = self.eval_metric(valid_matrix)
for root, Dir, Files in os.walk(self.checkpoint_dir):
for File in Files:
if File.endswith('.pth'):
try:
os.remove(self.checkpoint_dir + File)
except:
pass
torch.save(self.model.state_dict(), '{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch))
def train_model_epoch(self):
self.model.train(True)
for inputs, labels in self.train_dataloader:
inputs, labels = inputs.cuda(), labels.cuda()
self.model.zero_grad()
preds = self.model(inputs)
loss = self.criterion(preds, labels)
loss.backward()
self.optimizer.step()
def valid_model_epoch(self):
with torch.no_grad():
self.model.train(False)
valid_matrix = [[0, 0], [0, 0]]
for inputs, labels in self.valid_dataloader:
inputs, labels = inputs.cuda(), labels.cuda()
preds = self.model(inputs)
valid_matrix = matrix_sum(valid_matrix, get_confusion_matrix(preds, labels))
return valid_matrix
def prepare_dataloader(self, batch_size, balanced, Data_dir):
train_data = CNN_Data(Data_dir, self.exp_idx, stage='train', seed=self.seed)
valid_data = CNN_Data(Data_dir, self.exp_idx, stage='valid', seed=self.seed)
test_data = CNN_Data(Data_dir, self.exp_idx, stage='test', seed=self.seed)
sample_weight, self.imbalanced_ratio = train_data.get_sample_weights()
# the following if else blocks represent two ways of handling class imbalance issue
if balanced == 1:
# use pytorch sampler to sample data with probability according to the count of each class
# so that each mini-batch has the same expectation counts of samples from each class
sampler = torch.utils.data.sampler.WeightedRandomSampler(sample_weight, len(sample_weight))
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, sampler=sampler)
self.imbalanced_ratio = 1
elif balanced == 0:
# sample data from the same probability, but
# self.imbalanced_ratio will be used in the weighted cross entropy loss to handle imbalanced issue
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)
self.valid_dataloader = DataLoader(valid_data, batch_size=batch_size, shuffle=False)
self.test_dataloader = DataLoader(test_data, batch_size=batch_size, shuffle=False)
class FCN_Wrapper(CNN_Wrapper):
def __init__(self, fil_num,
drop_rate,
seed,
batch_size,
balanced,
Data_dir,
exp_idx,
model_name,
metric,
patch_size):
"""
:param fil_num: output channel number of the first convolution layer
:param drop_rate: dropout rate of the last 2 layers, see model.py for details
:param seed: random seed
:param batch_size: batch size for training FCN
:param balanced: balanced could take value 0 or 1, corresponding to different approaches to handle data imbalance,
see self.prepare_dataloader for more details
:param Data_dir: data path for training data
:param exp_idx: experiment index maps to different data splits
:param model_name: give a name to the model
:param metric: metric used for saving model during training, can take 'accuracy' or 'MCC'
for example, if metric == 'accuracy', then the time point where validation set has best accuracy will be saved
:param patch_size: size of patches for FCN training, must be 47. otherwise model has to be changed accordingly
"""
self.seed = seed
self.exp_idx = exp_idx
self.Data_dir = Data_dir
self.patch_size = patch_size
self.model_name = model_name
self.eval_metric = get_accu if metric == 'accuracy' else get_MCC
self.model = _FCN(num=fil_num, p=drop_rate).cuda()
self.prepare_dataloader(batch_size, balanced, Data_dir)
if not os.path.exists('./checkpoint_dir/'): os.mkdir('./checkpoint_dir/')
self.checkpoint_dir = './checkpoint_dir/{}_exp{}/'.format(self.model_name, exp_idx)
if not os.path.exists(self.checkpoint_dir):
os.mkdir(self.checkpoint_dir)
if not os.path.exists('./DPMs/'): os.mkdir('./DPMs/')
self.DPMs_dir = './DPMs/{}_exp{}/'.format(self.model_name, exp_idx)
if not os.path.exists(self.DPMs_dir):
os.mkdir(self.DPMs_dir)
def train(self, lr, epochs):
self.optimizer = optim.Adam(self.model.parameters(), lr=lr, betas=(0.5, 0.999))
self.criterion = nn.CrossEntropyLoss(weight=torch.Tensor([1, self.imbalanced_ratio])).cuda()
self.optimal_valid_matrix = [[0, 0], [0, 0]]
self.optimal_valid_metric = 0
self.optimal_epoch = -1
for self.epoch in range(epochs):
self.train_model_epoch()
if self.epoch % 20 == 0:
valid_matrix = self.valid_model_epoch()
print('{}th epoch validation confusion matrix:'.format(self.epoch), valid_matrix, 'eval_metric:', "%.4f" % self.eval_metric(valid_matrix))
self.save_checkpoint(valid_matrix)
print('Best model saved at the {}th epoch:'.format(self.optimal_epoch), self.optimal_valid_metric, self.optimal_valid_matrix)
return self.optimal_valid_metric
def valid_model_epoch(self):
self.fcn = self.model.dense_to_conv()
DPMs, Labels = [], []
with torch.no_grad():
self.fcn.train(False)
for idx, (inputs, labels) in enumerate(self.valid_dataloader):
inputs, labels = inputs.cuda(), labels.cuda()
DPM = self.fcn(inputs, stage='inference')
DPMs.append(DPM.cpu().numpy().squeeze())
Labels.append(labels)
valid_matrix, ACCU, F1, MCC = DPM_statistics(DPMs, Labels)
return valid_matrix
def prepare_dataloader(self, batch_size, balanced, Data_dir):
train_data = FCN_Data(Data_dir, self.exp_idx, stage='train', seed=self.seed, patch_size=self.patch_size)
valid_data = FCN_Data(Data_dir, self.exp_idx, stage='valid', seed=self.seed, patch_size=self.patch_size)
test_data = FCN_Data(Data_dir, self.exp_idx, stage='test', seed=self.seed, patch_size=self.patch_size)
sample_weight, self.imbalanced_ratio = train_data.get_sample_weights()
# the following if else blocks represent two ways of handling class imbalance issue
if balanced == 1:
# use pytorch sampler to sample data with probability according to the count of each class
# so that each mini-batch has the same expectation counts of samples from each class
sampler = torch.utils.data.sampler.WeightedRandomSampler(sample_weight, len(sample_weight))
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, sampler=sampler)
self.imbalanced_ratio = 1
elif balanced == 0:
# sample data from the same probability, but
# self.imbalanced_ratio will be used in the weighted cross entropy loss to handle imbalanced issue
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)
self.valid_dataloader = DataLoader(valid_data, batch_size=1, shuffle=False)
self.test_dataloader = DataLoader(test_data, batch_size=1, shuffle=False)
def test_and_generate_DPMs(self):
print('testing and generating DPMs ... ')
self.model.load_state_dict(torch.load('{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch)))
self.fcn = self.model.dense_to_conv()
self.fcn.train(False)
with torch.no_grad():
for stage in ['train', 'valid', 'test', 'AIBL', 'NACC', 'FHS']:
Data_dir = self.Data_dir
if stage in ['AIBL', 'NACC', 'FHS']:
Data_dir = Data_dir.replace('ADNI', stage)
data = FCN_Data(Data_dir, self.exp_idx, stage=stage, whole_volume=True, seed=self.seed, patch_size=self.patch_size)
filenames = data.Data_list
dataloader = DataLoader(data, batch_size=1, shuffle=False)
DPMs, Labels = [], []
for idx, (inputs, labels) in enumerate(dataloader):
inputs, labels = inputs.cuda(), labels.cuda()
DPM = self.fcn(inputs, stage='inference').cpu().numpy().squeeze()
np.save(self.DPMs_dir + filenames[idx] + '.npy', DPM)
DPMs.append(DPM)
Labels.append(labels)
matrix, ACCU, F1, MCC = DPM_statistics(DPMs, Labels)
np.save(self.DPMs_dir + '{}_MCC.npy'.format(stage), MCC)
np.save(self.DPMs_dir + '{}_F1.npy'.format(stage), F1)
np.save(self.DPMs_dir + '{}_ACCU.npy'.format(stage), ACCU)
print(stage + ' confusion matrix ', matrix, ' accuracy ', self.eval_metric(matrix))
print('DPM generation is done')
class MLP_Wrapper_A(CNN_Wrapper):
def __init__(self, imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric, roi_threshold, roi_count=200, choice='count'):
self.seed = seed
self.imbalan_ratio = imbalan_ratio
self.choice = choice
self.exp_idx = exp_idx
self.model_name = model_name
self.roi_count = roi_count
self.roi_threshold = roi_threshold
self.eval_metric = get_accu if metric == 'accuracy' else get_MCC
self.checkpoint_dir = './checkpoint_dir/{}_exp{}/'.format(self.model_name, exp_idx)
if not os.path.exists(self.checkpoint_dir):
os.mkdir(self.checkpoint_dir)
self.Data_dir = './DPMs/fcn_exp{}/'.format(exp_idx)
self.prepare_dataloader(batch_size, balanced, self.Data_dir)
self.model = _MLP_A(in_size=self.in_size, fil_num=fil_num, drop_rate=drop_rate)
def prepare_dataloader(self, batch_size, balanced, Data_dir):
train_data = MLP_Data(Data_dir, self.exp_idx, stage='train', roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
valid_data = MLP_Data(Data_dir, self.exp_idx, stage='valid', roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
test_data = MLP_Data(Data_dir, self.exp_idx, stage='test', roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
sample_weight, self.imbalanced_ratio = train_data.get_sample_weights()
# the following if else blocks represent two ways of handling class imbalance issue
if balanced == 1:
# use pytorch sampler to sample data with probability according to the count of each class
# so that each mini-batch has the same expectation counts of samples from each class
sampler = torch.utils.data.sampler.WeightedRandomSampler(sample_weight, len(sample_weight))
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, sampler=sampler)
self.imbalanced_ratio = 1
elif balanced == 0:
# sample data from the same probability, but
# self.imbalanced_ratio will be used in the weighted cross entropy loss to handle imbalanced issue
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)
self.imbalanced_ratio *= self.imbalan_ratio
self.valid_dataloader = DataLoader(valid_data, batch_size=1, shuffle=False)
self.test_dataloader = DataLoader(test_data, batch_size=1, shuffle=False)
self.in_size = train_data.in_size
def train(self, lr, epochs):
self.optimizer = optim.Adam(self.model.parameters(), lr=lr, betas=(0.5, 0.999))
self.criterion = nn.CrossEntropyLoss(weight=torch.Tensor([1, self.imbalanced_ratio]))
self.optimal_valid_matrix = [[0, 0], [0, 0]]
self.optimal_valid_metric = 0
self.optimal_epoch = -1
for self.epoch in range(epochs):
self.train_model_epoch()
valid_matrix = self.valid_model_epoch()
#print('{}th epoch validation confusion matrix:'.format(self.epoch), valid_matrix, 'eval_metric:', "%.4f" % self.eval_metric(valid_matrix))
self.save_checkpoint(valid_matrix)
#print('Best model saved at the {}th epoch:'.format(self.optimal_epoch), self.optimal_valid_metric, self.optimal_valid_matrix)
return self.optimal_valid_metric
def train_model_epoch(self):
self.model.train(True)
for inputs, labels, _ in self.train_dataloader:
inputs, labels = inputs, labels
self.model.zero_grad()
preds = self.model(inputs)
loss = self.criterion(preds, labels)
loss.backward()
self.optimizer.step()
def valid_model_epoch(self):
with torch.no_grad():
self.model.train(False)
valid_matrix = [[0, 0], [0, 0]]
for inputs, labels, _ in self.valid_dataloader:
inputs, labels = inputs, labels
preds = self.model(inputs)
valid_matrix = matrix_sum(valid_matrix, get_confusion_matrix(preds, labels))
return valid_matrix
def test(self, repe_idx):
self.model.load_state_dict(torch.load('{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch)))
self.model.train(False)
accu_list = []
with torch.no_grad():
for stage in ['train', 'valid', 'test', 'AIBL', 'NACC', 'FHS_Full']:
data = MLP_Data(self.Data_dir, self.exp_idx, stage=stage, roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
dataloader = DataLoader(data, batch_size=10, shuffle=False)
f = open(self.checkpoint_dir + 'raw_score_{}_{}.txt'.format(stage, repe_idx), 'w')
matrix = [[0, 0], [0, 0]]
for idx, (inputs, labels, _) in enumerate(dataloader):
inputs, labels = inputs, labels
preds = self.model(inputs)
write_raw_score(f, preds, labels)
matrix = matrix_sum(matrix, get_confusion_matrix(preds, labels))
# print(stage + ' confusion matrix ', matrix, ' accuracy ', self.eval_metric(matrix))
f.close()
accu_list.append(self.eval_metric(matrix))
return accu_list
class MLP_Wrapper_B(MLP_Wrapper_A):
def __init__(self, imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric, roi_threshold, roi_count, choice):
super().__init__(imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric, roi_threshold, roi_count, choice)
self.model = _MLP_B(in_size=4, fil_num=fil_num, drop_rate=drop_rate)
def train_model_epoch(self):
self.model.train(True)
for _, labels, inputs in self.train_dataloader:
inputs, labels = inputs, labels
self.model.zero_grad()
preds = self.model(inputs)
loss = self.criterion(preds, labels)
loss.backward()
self.optimizer.step()
def valid_model_epoch(self):
with torch.no_grad():
self.model.train(False)
valid_matrix = [[0, 0], [0, 0]]
for _, labels, inputs in self.valid_dataloader:
inputs, labels = inputs, labels
preds = self.model(inputs)
valid_matrix = matrix_sum(valid_matrix, get_confusion_matrix(preds, labels))
return valid_matrix
def test(self, repe_idx):
accu_list = []
self.model.load_state_dict(torch.load('{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch)))
self.model.train(False)
with torch.no_grad():
for stage in ['train', 'valid', 'test', 'AIBL', 'NACC', 'FHS']:
data = MLP_Data(self.Data_dir, self.exp_idx, stage=stage, roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
dataloader = DataLoader(data, batch_size=10, shuffle=False)
f = open(self.checkpoint_dir + 'raw_score_{}_{}.txt'.format(stage, repe_idx), 'w')
matrix = [[0, 0], [0, 0]]
for idx, (_, labels, inputs) in enumerate(dataloader):
inputs, labels = inputs, labels
preds = self.model(inputs)
write_raw_score(f, preds, labels)
matrix = matrix_sum(matrix, get_confusion_matrix(preds, labels))
# print(stage + ' confusion matrix ', matrix, ' accuracy ', self.eval_metric(matrix))
f.close()
accu_list.append(self.eval_metric(matrix))
return accu_list
class MLP_Wrapper_C(MLP_Wrapper_A):
def __init__(self, imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric, roi_threshold, roi_count, choice):
super().__init__(imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric, roi_threshold, roi_count, choice)
self.model = _MLP_C(in_size=self.in_size+4, fil_num=fil_num, drop_rate=drop_rate)
def train_model_epoch(self):
self.model.train(True)
for inputs, labels, demors in self.train_dataloader:
inputs, labels, demors = inputs, labels, demors
self.model.zero_grad()
preds = self.model(inputs, demors)
loss = self.criterion(preds, labels)
loss.backward()
self.optimizer.step()
def valid_model_epoch(self):
with torch.no_grad():
self.model.train(False)
valid_matrix = [[0, 0], [0, 0]]
for inputs, labels, demors in self.valid_dataloader:
inputs, labels, demors = inputs, labels, demors
preds = self.model(inputs, demors)
valid_matrix = matrix_sum(valid_matrix, get_confusion_matrix(preds, labels))
return valid_matrix
def test(self, repe_idx):
accu_list = []
self.model.load_state_dict(torch.load('{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch)))
self.model.train(False)
with torch.no_grad():
for stage in ['train', 'valid', 'test', 'AIBL', 'NACC', 'FHS']:
data = MLP_Data(self.Data_dir, self.exp_idx, stage=stage, roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
dataloader = DataLoader(data, batch_size=10, shuffle=False)
f = open(self.checkpoint_dir + 'raw_score_{}_{}.txt'.format(stage, repe_idx), 'w')
matrix = [[0, 0], [0, 0]]
for idx, (inputs, labels, demors) in enumerate(dataloader):
inputs, labels, demors = inputs, labels, demors
preds = self.model(inputs, demors)
write_raw_score(f, preds, labels)
matrix = matrix_sum(matrix, get_confusion_matrix(preds, labels))
# print(stage + ' confusion matrix ', matrix, ' accuracy ', self.eval_metric(matrix))
f.close()
accu_list.append(self.eval_metric(matrix))
return accu_list
class MLP_Wrapper_D(CNN_Wrapper):
def __init__(self, imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric):
self.seed = seed
self.imbalan_ratio = imbalan_ratio
self.exp_idx = exp_idx
self.model_name = model_name
self.eval_metric = get_accu if metric == 'accuracy' else get_MCC
self.checkpoint_dir = './checkpoint_dir/{}_exp{}/'.format(self.model_name, exp_idx)
if not os.path.exists(self.checkpoint_dir):
os.mkdir(self.checkpoint_dir)
self.Data_dir = './DPMs/cnn_exp{}/'.format(exp_idx)
self.prepare_dataloader(batch_size, balanced, self.Data_dir)
self.model = _MLP_D(in_size=self.in_size+4, fil_num=fil_num, drop_rate=drop_rate)
def prepare_dataloader(self, batch_size, balanced, Data_dir):
train_data = CNN_MLP_Data(Data_dir, self.exp_idx, stage='train', seed=self.seed)
valid_data = CNN_MLP_Data(Data_dir, self.exp_idx, stage='valid', seed=self.seed)
test_data = CNN_MLP_Data(Data_dir, self.exp_idx, stage='test', seed=self.seed)
sample_weight, self.imbalanced_ratio = train_data.get_sample_weights()
# the following if else blocks represent two ways of handling class imbalance issue
if balanced == 1:
# use pytorch sampler to sample data with probability according to the count of each class
# so that each mini-batch has the same expectation counts of samples from each class
sampler = torch.utils.data.sampler.WeightedRandomSampler(sample_weight, len(sample_weight))
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, sampler=sampler)
self.imbalanced_ratio = 1
elif balanced == 0:
# sample data from the same probability, but
# self.imbalanced_ratio will be used in the weighted cross entropy loss to handle imbalanced issue
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)
self.imbalanced_ratio *= self.imbalan_ratio
self.valid_dataloader = DataLoader(valid_data, batch_size=1, shuffle=False)
self.test_dataloader = DataLoader(test_data, batch_size=1, shuffle=False)
self.in_size = train_data.in_size
def train(self, lr, epochs):
self.optimizer = optim.Adam(self.model.parameters(), lr=lr, betas=(0.5, 0.999))
self.criterion = nn.CrossEntropyLoss(weight=torch.Tensor([1, self.imbalanced_ratio]))
self.optimal_valid_matrix = [[0, 0], [0, 0]]
self.optimal_valid_metric = 0
self.optimal_epoch = -1
for self.epoch in range(epochs):
self.train_model_epoch()
valid_matrix = self.valid_model_epoch()
# print('{}th epoch validation confusion matrix:'.format(self.epoch), valid_matrix, 'eval_metric:', "%.4f" % self.eval_metric(valid_matrix))
self.save_checkpoint(valid_matrix)
# print('Best model saved at the {}th epoch:'.format(self.optimal_epoch), self.optimal_valid_metric, self.optimal_valid_matrix)
return self.optimal_valid_metric
def train_model_epoch(self):
self.model.train(True)
for inputs, labels, demors in self.train_dataloader:
inputs, labels = inputs, labels
self.model.zero_grad()
preds = self.model(inputs, demors)
loss = self.criterion(preds, labels)
loss.backward()
self.optimizer.step()
def valid_model_epoch(self):
with torch.no_grad():
self.model.train(False)
valid_matrix = [[0, 0], [0, 0]]
for inputs, labels, demors in self.valid_dataloader:
inputs, labels = inputs, labels
preds = self.model(inputs, demors)
valid_matrix = matrix_sum(valid_matrix, get_confusion_matrix(preds, labels))
return valid_matrix
def test(self, repe_idx):
self.model.load_state_dict(
torch.load('{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch)))
self.model.train(False)
accu_list = []
with torch.no_grad():
for stage in ['train', 'valid', 'test', 'AIBL', 'NACC', 'FHS']:
data = CNN_MLP_Data(self.Data_dir, self.exp_idx, stage=stage, seed=self.seed)
dataloader = DataLoader(data, batch_size=10, shuffle=False)
f = open(self.checkpoint_dir + 'raw_score_{}_{}.txt'.format(stage, repe_idx), 'w')
matrix = [[0, 0], [0, 0]]
for idx, (inputs, labels, demors) in enumerate(dataloader):
inputs, labels = inputs, labels
preds = self.model(inputs, demors)
write_raw_score(f, preds, labels)
matrix = matrix_sum(matrix, get_confusion_matrix(preds, labels))
print(stage + ' confusion matrix ', matrix, ' accuracy ', self.eval_metric(matrix))
f.close()
accu_list.append(self.eval_metric(matrix))
return accu_list
class MLP_Wrapper_E(MLP_Wrapper_B):
def __init__(self, imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric, roi_threshold, roi_count, choice):
super().__init__(imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric, roi_threshold, roi_count, choice)
self.prepare_dataloader(batch_size, balanced, self.Data_dir)
self.model = _MLP_B(in_size=5, fil_num=fil_num, drop_rate=drop_rate)
def prepare_dataloader(self, batch_size, balanced, Data_dir):
train_data = MLP_Data_apoe(Data_dir, self.exp_idx, stage='train', roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
valid_data = MLP_Data_apoe(Data_dir, self.exp_idx, stage='valid', roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
test_data = MLP_Data_apoe(Data_dir, self.exp_idx, stage='test', roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
sample_weight, self.imbalanced_ratio = train_data.get_sample_weights()
# the following if else blocks represent two ways of handling class imbalance issue
if balanced == 1:
# use pytorch sampler to sample data with probability according to the count of each class
# so that each mini-batch has the same expectation counts of samples from each class
sampler = torch.utils.data.sampler.WeightedRandomSampler(sample_weight, len(sample_weight))
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, sampler=sampler)
self.imbalanced_ratio = 1
elif balanced == 0:
# sample data from the same probability, but
# self.imbalanced_ratio will be used in the weighted cross entropy loss to handle imbalanced issue
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)
self.imbalanced_ratio *= self.imbalan_ratio
self.valid_dataloader = DataLoader(valid_data, batch_size=1, shuffle=False)
self.test_dataloader = DataLoader(test_data, batch_size=1, shuffle=False)
self.in_size = train_data.in_size
def test(self, repe_idx):
accu_list = []
self.model.load_state_dict(torch.load('{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch)))
self.model.train(False)
with torch.no_grad():
for stage in ['train', 'valid', 'test', 'AIBL', 'NACC', 'FHS']:
data = MLP_Data_apoe(self.Data_dir, self.exp_idx, stage=stage, roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
dataloader = DataLoader(data, batch_size=10, shuffle=False)
f = open(self.checkpoint_dir + 'raw_score_{}_{}.txt'.format(stage, repe_idx), 'w')
matrix = [[0, 0], [0, 0]]
for idx, (_, labels, inputs) in enumerate(dataloader):
inputs, labels = inputs, labels
preds = self.model(inputs)
write_raw_score(f, preds, labels)
matrix = matrix_sum(matrix, get_confusion_matrix(preds, labels))
# print(stage + ' confusion matrix ', matrix, ' accuracy ', self.eval_metric(matrix))
f.close()
accu_list.append(self.eval_metric(matrix))
return accu_list
class MLP_Wrapper_F(MLP_Wrapper_C):
def __init__(self, imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric, roi_threshold, roi_count, choice):
super().__init__(imbalan_ratio, fil_num, drop_rate, seed, batch_size, balanced, exp_idx, model_name, metric, roi_threshold, roi_count, choice)
self.prepare_dataloader(batch_size, balanced, self.Data_dir)
self.model = _MLP_C(in_size=self.in_size + 5, fil_num=fil_num, drop_rate=drop_rate)
def prepare_dataloader(self, batch_size, balanced, Data_dir):
train_data = MLP_Data_apoe(Data_dir, self.exp_idx, stage='train', roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
valid_data = MLP_Data_apoe(Data_dir, self.exp_idx, stage='valid', roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
test_data = MLP_Data_apoe(Data_dir, self.exp_idx, stage='test', roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
sample_weight, self.imbalanced_ratio = train_data.get_sample_weights()
# the following if else blocks represent two ways of handling class imbalance issue
if balanced == 1:
# use pytorch sampler to sample data with probability according to the count of each class
# so that each mini-batch has the same expectation counts of samples from each class
sampler = torch.utils.data.sampler.WeightedRandomSampler(sample_weight, len(sample_weight))
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, sampler=sampler)
self.imbalanced_ratio = 1
elif balanced == 0:
# sample data from the same probability, but
# self.imbalanced_ratio will be used in the weighted cross entropy loss to handle imbalanced issue
self.train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)
self.imbalanced_ratio *= self.imbalan_ratio
self.valid_dataloader = DataLoader(valid_data, batch_size=1, shuffle=False)
self.test_dataloader = DataLoader(test_data, batch_size=1, shuffle=False)
self.in_size = train_data.in_size
def test(self, repe_idx):
accu_list = []
self.model.load_state_dict(torch.load('{}{}_{}.pth'.format(self.checkpoint_dir, self.model_name, self.optimal_epoch)))
self.model.train(False)
with torch.no_grad():
for stage in ['train', 'valid', 'test', 'AIBL', 'NACC', 'FHS']:
data = MLP_Data_apoe(self.Data_dir, self.exp_idx, stage=stage, roi_threshold=self.roi_threshold, roi_count=self.roi_count, choice=self.choice, seed=self.seed)
dataloader = DataLoader(data, batch_size=10, shuffle=False)
f = open(self.checkpoint_dir + 'raw_score_{}_{}.txt'.format(stage, repe_idx), 'w')
matrix = [[0, 0], [0, 0]]
for idx, (inputs, labels, demors) in enumerate(dataloader):
inputs, labels, demors = inputs, labels, demors
preds = self.model(inputs, demors)
write_raw_score(f, preds, labels)
matrix = matrix_sum(matrix, get_confusion_matrix(preds, labels))
# print(stage + ' confusion matrix ', matrix, ' accuracy ', self.eval_metric(matrix))
f.close()
accu_list.append(self.eval_metric(matrix))
return accu_list
if __name__ == "__main__":
pass