-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
436 lines (330 loc) · 15.7 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
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader
from models.fcn import VGGNet, FCN8sScaledBN, FCN8sScaled
from models.unet import UNet
from models.unet_resnet_encoder import UNetWithResnet50Encoder
from models.vgg_encoder import VGGEncoder
from models.resnet3D import resnet50_3D
from models.logistic import Logistic
from datasets.BRATS2018 import BRATS2018, NormalizeBRATS, ToTensor, ZeroPad
from datasets.BRATS2018_3D import BRATS2018_3D, NormalizeBRATS3D, CenterCropBRATS3D
from torchvision import transforms
import copy
from metrics.metrics import Evaluator
from metrics.torch_seg_metrics import *
from hyper_param_config import *
import numpy as np
import time
import datetime
import argparse
import os
import math
import signal
import logging
from logging.config import fileConfig
# global variables
if not os.path.exists('logs/'):
os.makedirs('logs/')
os.mknod('logs/basic_logs.log')
fileConfig('./logging_conf.ini')
logger = logging.getLogger('main')
logger.info('Configs: ')
logger.info(configs)
score_dir = os.path.join("scores", configs)
if not os.path.exists(score_dir):
os.makedirs(score_dir)
parser = argparse.ArgumentParser()
parser.add_argument("task", help='Train a segmentation model or a classification model.',\
default='seg', choices=['seg', 'cls'])
parser.add_argument('-i', '--input', help='input MRI scan modalities', default='all',\
choices=['t1ce', 'flair', 't2-flair', 'all'])
parser.add_argument('-g', '--grade', help='the grade of training data (HGG or LGG)',\
default='HGG', choices=['HGG', 'LGG'])
parser.add_argument('--seg_task', help='segmentaiton seg_task', default='seg',\
choices=['seg', 'wt', 'et', 'tc'])
parser.add_argument('--pre_trained', help='whether training from a pre-trained model',\
action='store_true')
args = parser.parse_args()
use_gpu = torch.cuda.is_available()
device = torch.device('cuda:0' if use_gpu else 'cpu')
# global variables
def get_dataset_dataloader(input_data_type, seg_type, batch_size, grade='HGG'):
data_transforms = transforms.Compose([
NormalizeBRATS(),
ToTensor()
])
if input_data_type == 't1ce':
data_set = {
phase: BRATS2018('./BRATS2018/',\
grade=grade,\
data_set=phase,\
seg_type=seg_type,\
transform=data_transforms)
for phase in ['train', 'val']
}
elif input_data_type == 'flair':
data_set = {
phase: BRATS2018('./BRATS2018/',\
grade=grade,\
data_set=phase,\
scan_type='flair',\
seg_type=seg_type,\
transform=data_transforms)
for phase in ['train', 'val']
}
elif input_data_type == 't2-flair':
data_set = {
phase: BRATS2018('./BRATS2018/',\
grade=grade,\
data_set=phase,\
scan_type='t2-flair',\
seg_type=seg_type,\
transform=data_transforms)
for phase in ['train', 'val']
}
elif input_data_type == 'all':
data_set = {
phase: BRATS2018('./BRATS2018/',\
grade=grade,\
data_set=phase,\
scan_type='all',\
seg_type=seg_type,\
transform=data_transforms)
for phase in ['train', 'val']
}
else:
raise ValueError('Scan type must be t1ce, flair, t2-flair, or all!')
data_loader = {
'train': DataLoader(data_set['train'], batch_size=batch_size, shuffle=True, num_workers=0),
'val': DataLoader(data_set['val'], batch_size=batch_size, shuffle=False, num_workers=0)
}
return data_set, data_loader
def time_stamp() -> str:
ts = time.time()
time_stamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
return time_stamp
class SoftDiceLoss(nn.Module):
def __init__(self, weight=None, size_average=True):
super(SoftDiceLoss, self).__init__()
def dice_coef(self, preds, targets):
smooth = 5e-3
num = preds.size(0) # batch size
preds_flat = preds.view(num, -1).float()
targets_flat = targets.view(num, -1).float()
intersection = (preds_flat * targets_flat).sum()
logger.debug('intersection: {:.4f}, sum_preds: {:.4f}, sum_targets: {:.4f}'.format(intersection,\
preds_flat.sum(),\
targets_flat.sum()))
return (2. * intersection + smooth) / (preds_flat.sum() + targets_flat.sum() + smooth)
def forward(self, logits, targets):
probs = torch.sigmoid(logits)
score = self.dice_coef(probs, targets)
score = 1 - score
return score
def train(input_data_type, grade, seg_type, num_classes, batch_size, epochs, use_gpu, learning_rate, w_decay, pre_trained=False):
logger.info('Start training using {} modal.'.format(input_data_type))
model = UNet(4, 4, residual=True, expansion=2)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(params=model.parameters(), lr=learning_rate, weight_decay=w_decay)
if pre_trained:
checkpoint = torch.load(pre_trained_path, map_location=device)
model.load_state_dict(checkpoint['model_state_dict'])
if use_gpu:
ts = time.time()
model.to(device)
print("Finish cuda loading, time elapsed {}".format(time.time() - ts))
scheduler = lr_scheduler.StepLR(optimizer, step_size=step_size, gamma=gamma) # decay LR by a factor of 0.5 every 5 epochs
data_set, data_loader = get_dataset_dataloader(input_data_type, seg_type, batch_size, grade=grade)
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_iou = 0.0
epoch_loss = np.zeros((2, epochs))
epoch_acc = np.zeros((2, epochs))
epoch_class_acc = np.zeros((2, epochs))
epoch_mean_iou = np.zeros((2, epochs))
evaluator = Evaluator(num_classes)
def term_int_handler(signal_num, frame):
np.save(os.path.join(score_dir, 'epoch_accuracy'), epoch_acc)
np.save(os.path.join(score_dir, 'epoch_mean_iou'), epoch_mean_iou)
np.save(os.path.join(score_dir, 'epoch_loss'), epoch_loss)
model.load_state_dict(best_model_wts)
logger.info('Got terminated and saved model.state_dict')
torch.save(model.state_dict(), os.path.join(score_dir, 'terminated_model.pt'))
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}, os.path.join(score_dir, 'terminated_model.tar'))
quit()
signal.signal(signal.SIGINT, term_int_handler)
signal.signal(signal.SIGTERM, term_int_handler)
for epoch in range(epochs):
logger.info('Epoch {}/{}'.format(epoch + 1, epochs))
logger.info('-' * 28)
for phase_ind, phase in enumerate(['train', 'val']):
if phase == 'train':
model.train()
logger.info(phase)
else:
model.eval()
logger.info(phase)
evaluator.reset()
running_loss = 0.0
running_dice = 0.0
for batch_ind, batch in enumerate(data_loader[phase]):
imgs, targets = batch
imgs = imgs.to(device)
targets = targets.to(device)
# zero the learnable parameters gradients
optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = model(imgs)
loss = criterion(outputs, targets)
if phase == 'train':
loss.backward()
optimizer.step()
preds = torch.argmax(F.softmax(outputs, dim=1), dim=1, keepdim=True)
running_loss += loss * imgs.size(0)
logger.debug('Batch {} running loss: {:.4f}'.format(batch_ind,\
running_loss))
# test the iou and pixelwise accuracy using evaluator
preds = torch.squeeze(preds, dim=1)
preds = preds.cpu().numpy()
targets = targets.cpu().numpy()
evaluator.add_batch(targets, preds)
epoch_loss[phase_ind, epoch] = running_loss / len(data_set[phase])
epoch_acc[phase_ind, epoch] = evaluator.Pixel_Accuracy()
epoch_class_acc[phase_ind, epoch] = evaluator.Pixel_Accuracy_Class()
epoch_mean_iou[phase_ind, epoch] = evaluator.Mean_Intersection_over_Union()
logger.info('{} loss: {:.4f}, acc: {:.4f}, class acc: {:.4f}, mean iou: {:.6f}'.format(phase,\
epoch_loss[phase_ind, epoch],\
epoch_acc[phase_ind, epoch],\
epoch_class_acc[phase_ind, epoch],\
epoch_mean_iou[phase_ind, epoch]))
if phase == 'val' and epoch_mean_iou[phase_ind, epoch] > best_iou:
best_iou = epoch_mean_iou[phase_ind, epoch]
best_model_wts = copy.deepcopy(model.state_dict())
if phase == 'val' and (epoch + 1) % 10 == 0:
logger.info('Saved model.state_dict in epoch {}'.format(epoch + 1))
torch.save(model.state_dict(), os.path.join(score_dir, 'epoch{}_model.pt'.format(epoch + 1)))
print()
time_elapsed = time.time() - since
logger.info('Training completed in {}m {}s'.format(int(time_elapsed / 60),\
int(time_elapsed) % 60))
# load best model weights
model.load_state_dict(best_model_wts)
# save numpy results
np.save(os.path.join(score_dir, 'epoch_accuracy'), epoch_acc)
np.save(os.path.join(score_dir, 'epoch_mean_iou'), epoch_mean_iou)
np.save(os.path.join(score_dir, 'epoch_loss'), epoch_loss)
return model, optimizer
def train_classification(num_classes, batch_size, epochs, use_gpu, learning_rate, w_decay, pre_trained=False):
logger.info('Starts training a classification model.')
model = Logistic(volume=(4, 160, 160, 144), num_classes=num_classes)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(params=model.parameters(), lr=learning_rate, weight_decay=w_decay)
if pre_trained:
checkpoint = torch.load(pre_trained_path, map_location=device)
model.load_state_dict(checkpoint['model_state_dict'])
if use_gpu:
ts = time.time()
model.to(device)
print("Finish cuda loading, time elapsed {}".format(time.time() - ts))
scheduler = lr_scheduler.StepLR(optimizer, step_size=step_size, gamma=gamma)
data_transforms = transforms.Compose([
CenterCropBRATS3D(),
NormalizeBRATS3D(),
ToTensor()
])
data_set = {
phase: BRATS2018_3D('BRATS2018/',\
data_set=phase,\
transform=data_transforms)
for phase in ['train', 'val']
}
data_loader = {
'train': DataLoader(data_set['train'], batch_size=batch_size, shuffle=True, num_workers=0),
'val': DataLoader(data_set['val'], batch_size=batch_size, shuffle=False, num_workers=0)
}
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
epoch_loss = np.zeros((2, epochs))
epoch_acc = np.zeros((2, epochs))
def term_int_handler(signal_num, frame):
np.save(os.path.join(score_dir, 'epoch_accuracy'), epoch_acc)
np.save(os.path.join(score_dir, 'epoch_loss'), epoch_loss)
model.load_state_dict(best_model_wts)
logger.info('Got terminated and saved model.state_dict')
torch.save(model.state_dict(), os.path.join(score_dir, 'terminated_model.pt'))
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}, os.path.join(score_dir, 'terminated_model.tar'))
quit()
signal.signal(signal.SIGINT, term_int_handler)
signal.signal(signal.SIGTERM, term_int_handler)
for epoch in range(epochs):
logger.info('Epoch {}/{}'.format(epoch + 1, epochs))
logger.info('-' * 28)
for phase_ind, phase in enumerate(['train', 'val']):
if phase == 'train':
model.train()
logger.info(phase)
else:
model.eval()
logger.info(phase)
running_loss = torch.tensor(0, dtype=torch.float32)
running_acc = torch.tensor(0, dtype=torch.float32)
for batch_ind, batch in enumerate(data_loader[phase]):
imgs, labels = batch
imgs = imgs.view(imgs.size(0), -1).to(device)
labels = labels.view(-1,).to(device)
# zero the learnable parameters gradients
optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = model(imgs)
loss = criterion(outputs, labels)
if phase == 'train':
loss.backward()
optimizer.step()
preds = torch.argmax(F.softmax(outputs, dim=1), dim=1).view(-1,)
running_loss += loss * imgs.size(0)
running_acc += torch.sum(preds == labels)
logger.debug('Batch {} running loss: {:.4f}'.format(batch_ind,\
running_loss))
epoch_loss[phase_ind, epoch] = running_loss.cpu().detach().numpy() / len(data_set[phase])
epoch_acc[phase_ind, epoch] = running_acc.cpu().numpy() / len(data_set[phase])
logger.info('{} loss: {:.4f}, acc: {:.4f}'.format(phase,\
epoch_loss[phase_ind, epoch],\
epoch_acc[phase_ind, epoch]))
if phase == 'val' and epoch_acc[phase_ind, epoch] > best_acc:
best_acc = epoch_acc[phase_ind, epoch]
best_model_wts = copy.deepcopy(model.state_dict())
if phase == 'val' and (epoch + 1) % 10 == 0:
logger.info('Saved model.state_dict in epoch {}'.format(epoch + 1))
torch.save(model.state_dict(), os.path.join(score_dir, 'epoch{}_model.pt'.format(epoch + 1)))
time_elapsed = time.time() - since
logger.info('Training completed in {}m {}s'.format(int(time_elapsed / 60),\
int(time_elapsed) % 60))
# load best model weights
model.load_state_dict(best_model_wts)
# save numpy results
np.save(os.path.join(score_dir, 'epoch_accuracy'), epoch_acc)
np.save(os.path.join(score_dir, 'epoch_loss'), epoch_loss)
return model, optimizer
if __name__ == "__main__":
if args.task == 'seg':
model, optimizer = train(args.input, args.grade, args.seg_task, n_classes, batch_size, epochs, use_gpu, lr, w_decay, args.pre_trained)
else:
model, optimizer = train_classification(n_classes, batch_size, epochs, use_gpu, lr, w_decay, args.pre_trained)
logger.info('Saved model.state_dict')
torch.save(model.state_dict(), os.path.join(score_dir, 'trained_model.pt'))
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}, os.path.join(score_dir, 'trained_model_checkpoint.tar'))