-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
367 lines (333 loc) · 17.1 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
import argparse
import logging
import os
import sys
import timeit
import numpy as np
import torch
import torch.nn as nn
from torch import optim
from tqdm import tqdm
from unet import *
from dataset import WMHChallengeDataset
from tversky_loss import *
from torch.utils.data import (DataLoader, RandomSampler, SequentialSampler,
TensorDataset)
from tqdm import tqdm, trange
from data_util import Utrecht_postprocessing, GE3T_postprocessing
from evaluation import getDSC, getHausdorff, getLesionDetection, getAVD, getImages, getAUC
import SimpleITK as sitk
try:
from torch.utils.tensorboard import SummaryWriter
except:
from tensorboardX import SummaryWriter
import matplotlib.pyplot as plt
#set up logger for outputing logs and writer for tensorboardX
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
tb_writer = SummaryWriter("experiments")
def get_args():
parser = argparse.ArgumentParser(description='Train the UNet on images and target masks',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-e', '--epochs', metavar='E', type=int, default=5,
help='Number of epochs', dest='epochs')
parser.add_argument('-b', '--batch_size', metavar='B', type=int, nargs='?', default=1,
help='Batch size', dest='batchsize')
parser.add_argument('-l', '--learning-rate', metavar='LR', type=float, nargs='?', default=0.1,
help='Learning rate', dest='lr')
parser.add_argument('-g', '--number_of_gpu', dest='n_gpu', type=int, default=1,
help='Number of GPU')
parser.add_argument("--local_rank", type=int, default=-1,
help="local_rank for distributed training on gpus")
parser.add_argument("--save_dir", type=str, default="",
help="directory for saving the model")
parser.add_argument("--data_dir", type=str, default="",
help="directory for raw data")
parser.add_argument("--output_dir", type=str, default="",
help="directory for output results")
parser.add_argument("--do_train", action='store_true',
help="Whether to run training.")
parser.add_argument("--do_eval", action='store_true',
help="Whether to run eval on the dev set.")
parser.add_argument("--model_dir", type=str, default="",
help="Model directory for loading")
parser.add_argument("--test_subject", type=int, default=59, nargs='+',
help="subject id for leave-one-out testing")
parser.add_argument('--seed', type=int, default=25,
help='random seed')
parser.add_argument('--model_type', type=int, default=0,
help='model type for training, 0 for regular unet, 1 for attention unet, 2 for recurrent unet, 3 for domainnet, 4 for domainnetX, 5 for domainXR, 6 for unet++')
parser.add_argument('--eval_epochs', type=int, default=10,
help='interval in epochs for doing evaluation during training')
parser.add_argument('--gradient_accumulation_steps', type=int, default=1,
help="Number of updates steps to accumulate before performing a backward/update pass.")
parser.add_argument('--silent', action='store_true',
help="whether to show progress bar for tqdm")
parser.add_argument('--domain_knowledge', action='store_true',
help="whether to use altas(domain knowledge) as additional input")
parser.add_argument('--aging', action='store_true',
help="whether to use aging atlas as additional input")
parser.add_argument('--large', action='store_true',
help="whether to use more attention channels for domain net")
parser.add_argument('--thresh_hold', type=float, default=0.5,
help="threshhold value for segmentation")
parser.add_argument('--T1', action='store_true',
help="whether to use T1 as input")
parser.add_argument('--alpha', type=float, default=0.7,
help="alpha value for various loss functions")
parser.add_argument('--abvib', action='store_true',
help="whether to use the ABVIB dataset for training")
parser.add_argument('--abvib_eval', action='store_true',
help="whether to evaluate on ABVIB")
parser.add_argument('--weight_decay', type=float, default=0,
help="regularization term")
parser.add_argument('--kernel', type=int, default=5,
help='kernel size for BAGAU-Net')
return parser.parse_args()
def tversky_loss(y_pred, y_true, alpha):
return 1 - tversky_coeff(y_pred, y_true, alpha=alpha)
def train(model,
device,
dataset,
args):
try:
os.mkdir(args.save_dir)
logging.info('Created model directory')
except OSError:
pass
train_sampler = RandomSampler(dataset) if args.local_rank == -1 else DistributedSampler(dataset)
train_dataloader = DataLoader(dataset, sampler=train_sampler, batch_size=args.batchsize)
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
if args.n_gpu > 1:
model = torch.nn.DataParallel(model)
#preparing eval and test dataset
if len(args.test_subject) == 1:
args.test_subject = int(args.test_subject[0])
if isinstance(args.test_subject, int):
if args.abvib:
eval_dataset = ABVIBDataset(directory=args.data_dir, train=False, test_subject=args.test_subject, domain_knowledge=args.domain_knowledge, aging=args.aging)
else:
eval_dataset = WMHChallengeDataset(directory=args.data_dir, train=False, test_subject=args.test_subject, aug=True, domain_knowledge=args.domain_knowledge, aging=args.aging, T1=args.T1)
else:
args.test_subject = [int(x) for x in args.test_subject]
eval_dataset1, eval_dataset2 = [], []
for idx, item in enumerate(args.test_subject):
if args.abvib:
item_dataset = ABVIBDataset(directory=args.data_dir, train=False, test_subject=item, domain_knowledge=args.domain_knowledge, aging=args.aging)
else:
item_dataset = WMHChallengeDataset(directory=args.data_dir, train=False, test_subject=item, aug=True, domain_knowledge=args.domain_knowledge, aging=args.aging, T1=args.T1)
if idx % 2 == 0:
eval_dataset1.append(item_dataset)
else:
eval_dataset2.append(item_dataset)
test_subject = args.test_subject
#loss function
criterion = tversky_loss
if args.abvib:
standard = evaluateABVIB
dataset_name = 'ABVIB'
else:
standard = evaluate
dataset_name = 'WMH'
logger.info("***** Running training *****")
logger.info(" Num examples = %d", len(dataset))
logger.info(" Num Epochs = %d", args.epochs)
logger.info(" Total train batch size (w. parallel, distributed & accumulation) = %d",
args.batchsize * args.gradient_accumulation_steps)
logger.info(" Test Subject = %s", str(args.test_subject))
logger.info(" Loss function : %s", str(criterion).split()[1])
logger.info(" Dataset : %s", dataset_name)
model.zero_grad()
train_iterator = trange(int(args.epochs), desc="Epoch", disable=args.silent)
epoch, global_step = 0, 0
best_auc, curr_auc = 0, 0
best_dsc, curr_dsc = 0, 0
for _ in train_iterator:
epoch_iterator = tqdm(train_dataloader, desc="Iteration", disable=args.silent)
epoch += 1
epoch_loss = 0
for step, batch in enumerate(epoch_iterator):
model.train()
imgs, masks = batch['image'], batch['mask']
imgs = imgs.to(device=device)
masks = masks.to(device=device)
masks_pred = model(imgs)
loss = criterion(masks_pred, masks, alpha=args.alpha)
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
loss.backward()
epoch_loss += loss.item()
if (step + 1) % args.gradient_accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
global_step += 1
#add loss to tensorboardX
tb_writer.add_scalar('loss', loss*args.gradient_accumulation_steps, global_step)
#evalution during training
if epoch % args.eval_epochs == 0:
if isinstance(test_subject, int):
try:
dsc, avd, recall, f1, auc = standard(model, device, eval_dataset, args, training=True)
except:
#model has yet ready for prediction
logger.info('Evaluation skipped')
continue
tb_writer.add_scalar('dsc', dsc, epoch)
tb_writer.add_scalar('avd', avd, epoch)
tb_writer.add_scalar('recall', recall, epoch)
tb_writer.add_scalar('f1', f1, epoch)
tb_writer.add_scalar('auc', auc, epoch)
logger.info('-' * 100)
log_str = '| Eval {:3d} at epoch {:>8d} ' \
'| dsc {:5.2f} | avd {:5.2f} | recall {:5.2f} ' \
'| f1 {:5.2f} | auc {:5.2f} '.format(
epoch // args.eval_epochs, epoch, dsc, avd, recall, f1, auc)
logger.info(log_str)
logger.info('-' * 100)
curr_auc = auc
curr_dsc = dsc
else:
results = np.zeros(5)
for i in range(len(eval_dataset1)):
try:
args.test_subject = eval_dataset1[i].test_subject
metrics = standard(model, device, eval_dataset1[i], args, training=True)
results += np.asarray(metrics)
except:
logger.info('Evaluation skipped')
continue
results /= len(eval_dataset1)
logger.info('-' * 110)
log_str = '| Eval1 {:3d} at epoch {:>8d} ' \
'| dsc {:7.4f} | avd {:7.4f} | recall {:7.4f} ' \
'| f1 {:7.4f} | auc {:7.4f} '.format(
epoch // args.eval_epochs, epoch, results[0], results[1], results[2], results[3], results[4])
logger.info(log_str)
logger.info('-' * 110)
curr_auc = results[4]
curr_dsc = results[0]
results = np.zeros(5)
for i in range(len(eval_dataset2)):
try:
args.test_subject = eval_dataset2[i].test_subject
metrics = standard(model, device, eval_dataset2[i], args, training=True)
results += np.asarray(metrics)
except:
logger.info('Evaluation skipped')
continue
results /= len(eval_dataset2)
logger.info('-' * 110)
log_str = '| Eval2 {:3d} at epoch {:>8d} ' \
'| dsc {:7.4f} | avd {:7.4f} | recall {:7.4f} ' \
'| f1 {:7.4f} | auc {:7.4f} '.format(
epoch // args.eval_epochs, epoch, results[0], results[1], results[2], results[3], results[4])
logger.info(log_str)
logger.info('-' * 110)
curr_auc = results[4]
curr_dsc = results[0]
#save the model when it achieves highest auc or highest dsc
if curr_auc > best_auc:
torch.save(model.state_dict(),
args.save_dir + f'/fold_{args.fold}.pth')
logger.info(f'model saved !')
best_auc = curr_auc
if curr_dsc > best_dsc:
torch.save(model.state_dict(),
args.save_dir + f'/dsc_fold_{args.fold}.pth')
logger.info(f'model saved !')
best_dsc = curr_dsc
logger.info(" Epoch = {}, loss = {}".format(epoch, epoch_loss/(step+1)))
if args.do_train:
torch.save(model.state_dict(),
args.save_dir + f'/model.pth')
tb_writer.close()
def evaluate(model,
device,
dataset,
args,
training=False):
if not os.path.exists(args.output_dir) and args.local_rank in [-1, 0]:
os.makedirs(args.output_dir)
eval_sampler = SequentialSampler(dataset) if args.local_rank == -1 else DistributedSampler(dataset)
eval_dataloader = DataLoader(dataset, sampler=eval_sampler, batch_size=args.batchsize)
if not training and not args.thresh_hold_finding:
logger.info("***** Running evaluation *****")
logger.info(" Num examples = %d", len(dataset))
logger.info(" Batch size = %d", args.batchsize)
start_time = timeit.default_timer()
pred = torch.tensor([], dtype=torch.float32, device=device)
for batch in tqdm(eval_dataloader, desc="Evaluating", disable=args.silent):
model.eval()
with torch.no_grad():
imgs, masks = batch['image'], batch['mask']
imgs = imgs.to(device=device)
masks = masks.to(device=device)
masks_pred = model(imgs).squeeze(dim=1)
masks_pred = (masks_pred > args.thresh_hold).float()
pred = torch.cat((pred, masks_pred), dim=0)
if torch.cuda.is_available():
pred = pred.cpu()
pred = torch.unsqueeze(pred, -1).numpy()
if args.test_subject < 20:
original = GE3T_postprocessing(dataset.eval_mask, pred)
else:
original = Utrecht_postprocessing(dataset.eval_mask, pred)
filename_resultImage = args.output_dir + f'/{args.test_subject}.nii.gz'
sitk.WriteImage(sitk.GetImageFromArray(original), filename_resultImage)
testImage, resultImage = getImages(dataset.eval_dir, filename_resultImage)
dsc = getDSC(testImage, resultImage)
avd = getAVD(testImage, resultImage)
#h95 distance is comment out due to numeric issues with python3 with evalution script
#h95 = getHausdorff(testImage, resultImage)
recall, f1 = getLesionDetection(testImage, resultImage)
auc = getAUC(dataset.eval_dir, filename_resultImage)
if not training and not args.thresh_hold_finding:
evalTime = timeit.default_timer() - start_time
logger.info(" Evaluation done in total %f secs (%f sec per example)", evalTime, evalTime / len(dataset))
logger.info("***** Evaluation Results *****")
logger.info(f'Result of patient {args.test_subject}')
logger.info(f'Dice {dsc} (higher is better, max=1)')
#logger.info(f'HD {h95}mm (lower is better, min=0)')
logger.info(f'AVD {avd}% (lower is better, min=0)')
logger.info(f'Lesion detection {recall} (higher is better, max=1)')
logger.info(f'Lesion F1 {f1} (higher is better, max=1)')
logger.info(f'AUC {auc} (higher is better, max=1)')
return dsc, avd, recall, f1, auc
if __name__ == '__main__':
args = get_args()
# Set the random seed manually for reproducibility.
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(args.seed)
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logger.info(f'Using device {device}')
channel = 1 + args.domain_knowledge + args.aging + args.T1
if args.model_type == 0:
net = UNet(n_channels=channel, n_classes=1)
model_type = "U-net"
else:
net = Dk_UNet_XR(n_channels=channel, n_classes=1, aging=args.aging, size=args.kernel)
model_type = "BAGAU-Net"
logger.info(f'Network: {model_type}\n'
f'\t{net.n_channels} input channels\n'
f'\t{net.n_classes} output channels (classes)\n'
f'\t{"Bilinear" if net.bilinear else "Dilated conv"} upscaling')
if args.model_dir:
net.load_state_dict(
torch.load(args.model_dir, map_location=device)
)
logger.info(f'Model loaded from {args.model_dir}')
net.to(device=device)
logger.info("***** Preparing dataset *****")
if args.do_train:
if args.abvib:
train_dataset = ABVIBDataset(directory=args.data_dir, train=True, test_subject=args.test_subject, domain_knowledge=args.domain_knowledge, aging=args.aging)
else:
train_dataset = WMHChallengeDataset(directory=args.data_dir, train=True, test_subject=args.test_subject, aug=True, domain_knowledge=args.domain_knowledge, aging=args.aging, T1=args.T1)
train(net, device, train_dataset, args)
if args.do_eval:
args.test_subject = args.test_subject[0]
eval_dataset = WMHChallengeDataset(directory=args.data_dir, train=False, test_subject=args.test_subject, aug=True, domain_knowledge=args.domain_knowledge, aging=args.aging, T1=args.T1)
evaluate(net, device, eval_dataset, args)