-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructureTrain.py
More file actions
306 lines (254 loc) · 11.6 KB
/
structureTrain.py
File metadata and controls
306 lines (254 loc) · 11.6 KB
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
# Team 1
# Training loop to segment GM & WM structures from MRI images
import numpy as np
import torch as th
import torch.optim as optim
import nibabel as nib
# import matplotlib.pyplot as plt
import json
import random
import torchvision
import time
import copy
from Unet import ResNetUNet
from collections import defaultdict
import datetime as dt
import sys
import os
# import visdom as vis
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader
import datasetFull as ds
from losses import *
np.set_printoptions(precision=2, suppress=True)
# Saving the training starting date in case it runs over night
dateToday = dt.date.today().strftime("%Y%m%d")
def print_metrics(metrics, epoch_samples, phase):
outputs = []
for k in metrics.keys():
outputs.append("{}: {:4f}".format(k, metrics[k] / epoch_samples))
print("{}: {}".format(phase, ", ".join(outputs)))
##################################################################
# Training loop
def train_model(dataloaders,
device,
model,
criterion,
optimizer,
scheduler,
softmax,
path,
modelname,
num_epochs=25):
filetmp = path + '%s%s%s.png'
best_model_wts = copy.deepcopy(model.state_dict())
# best_loss = 1e10 # to save best loss-based performance
best_score = 1e-10 # to save best score-based performance
for epoch in range(num_epochs):
print('-' * 20)
print('Epoch {}/{}'.format(epoch+1, num_epochs))
print('-' * 10)
since = time.time()
# Each epoch has a training and validation phase
for phase in ['train', 'val']:
if phase == 'train':
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluation mode
metrics = defaultdict(float)
epoch_samples = 0
sumLoss = 0
sumCe = 0
dicesGM = []
dicesWM = []
HGMs = []
HWMs = []
for item in dataloaders[phase]:
inputs = item['inputs'].to(device)
labels = item['labels'].to(device)
name = item['name'][0]
# print('Label shape:',labels.shape) # [B, 1, H, W]
# zero the parameter gradients
optimizer.zero_grad()
# forward
# track history if only in train
with th.set_grad_enabled(phase == 'train'):
outputs = model(inputs) # [B, C, H, W]
# outputs = outputs.squeeze(0)
# print('Label shape:', labels.shape)
# print('Output shape:', outputs.shape)
# labels = labels.squeeze(0)
loss, ce, dice_gm, dice_wm = calc_loss(outputs, labels, criterion)
sumLoss += loss.item()
sumCe += ce.item()
dicesGM.append(dice_gm.item())
dicesWM.append(dice_wm.item())
print('Loss: %.5f ' % loss, end='')
print('CE: %.5f ' % ce)
print('DiceGM: %.5f ' % dice_gm, end='')
print('DiceWM: %.5f ' % dice_wm)
# To save the output as .png
outputs = softmax(outputs)
outputs = outputs.squeeze(0) # [C, H, W]
# To save the output as in label shape [1, H, W]
correct = th.argmax(outputs, dim=0)
# print('corrected shape:', correct.shape)
########################
# Calculate Hausdorff Distance
labels = labels.squeeze(0) # [1, H, W]
hausdorffGM = symHausdorff(correct == 2, labels == 2)
hausdorffWM = symHausdorff(correct == 1, labels == 1)
HGMs.append(hausdorffGM)
HWMs.append(hausdorffWM)
print('Hausdorff GM: %.5f, Hausdorff WM: %.5f'
% (hausdorffGM, hausdorffWM))
print('-----------------------')
# backward + optimize only if in training phase
if phase == 'train':
# outputs.backward()
loss.backward()
optimizer.step()
epoch_samples += inputs.size(0) # number of samples trained
print_metrics(metrics, epoch_samples, phase)
# epoch_loss = sumLoss / epoch_samples # to save best loss-based performance
diceMeanGM, meanHGM, scoreGM = calc_score(dicesGM, HGMs)
diceMeanWM, meanHWM, scoreWM = calc_score(dicesWM, HWMs)
epoch_score = .5 * (scoreGM + scoreWM) # to save best score-based performance
print('Score: ', epoch_score)
print('Average loss: %.5f, Average Ce: %.5f, Average Dice: %.5f'
% (sumLoss/epoch_samples, sumCe/epoch_samples, (diceMeanGM+diceMeanWM)/2))
print('Average HGM: %.5f, Average HWM: %.5f'
% (meanHGM, meanHWM))
if phase == 'train':
scheduler.step()
for param_group in optimizer.param_groups:
print("LR", param_group['lr'])
# deep copy the model
# if phase == 'val' and epoch_loss < best_loss: # to save best loss-based performance
if phase == 'val' and epoch_score > best_score: # to save best score-based performance
print("saving best model") # save a hard copy of the model!
# best_loss = epoch_loss # to save best loss-based performance
best_score = epoch_score # to save best score-based performance
best_model_wts = copy.deepcopy(model.state_dict())
th.save(model.state_dict(), path + modelname
+ str(epoch+1) + 'best' + '.pt')
# Save the last image as .png (optional)
torchvision.utils.save_image(
outputs,
filetmp % (modelname,
str(epoch+1),
name),
normalize=True)
time_elapsed = time.time() - since
print('{:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
# To save each epoch model independently from best model (optinal)
th.save(model.state_dict(), path + modelname
+ str(epoch+1) + '.pt')
print('Best val loss: {:4f}'.format(best_score)) # or best_loss
##################################################################
# load best model weights and save them
model.load_state_dict(best_model_wts)
return model
def main():
# Give model a name
modelname = 'Training'
##################################################################
# Step 1. Load Dataset (saved as a list in a text file)
train_data_full = json.load(open('data_mialab/data_subjects.txt'))
random.shuffle(train_data_full) # shuffle subject-wise
print('Num. of subjects ...', len(train_data_full))
# save 90% for training and 10% for testing
train_data, test_data = ds.splitData(train_data_full, proportion=0.9)
# print('Num. of subjects in train data ...', len(train_data))
# separating patients and healthy subjects
patients = [sub for sub in train_data if sub['is_patient']]
healthy = [sub for sub in train_data if not sub['is_patient']]
# shuffle the lists
random.shuffle(patients)
random.shuffle(healthy)
# extract 80% for training and 20% for validation from each group
training_patients, validation_patients = ds.splitData(patients)
training_healthy, validation_healthy = ds.splitData(healthy)
# putting together shuffles patients and HC
training_data = training_patients + training_healthy
validation_data = validation_patients + validation_healthy
# flattened training data into slice level
training_slices = ds.flattenSlices(training_data)
validation_slices = ds.flattenSlices(validation_data)
test_slices = ds.flattenSlices(test_data)
# create training dataset
augrate = 2 # augrate - 1 = nr. of augmentations of the original image
training_dataset = ds.Dataset(training_slices, augrate=augrate)
validation_dataset = ds.Dataset(validation_slices, train=False)
# Print the length of data
print('Num. of subjects in training data ...', len(training_data))
print('Num. of patients slices in training data...',
len([s for s in training_slices if s['is_patient']]))
print('Num. of healthy slices in training data...',
len([s for s in training_slices if not s['is_patient']]))
print('Num. of slices in training dataset (x%.0f with augmentation) ...'
% augrate, len(training_dataset))
print('Num. of subjects in validation data ...', len(validation_data))
print('Num. of slices validation dataset ...', len(validation_dataset))
print('Num. of subjects in test data...', len(test_data))
print('Num. of slices in test data...', len(test_slices))
##################################################################
# Step 2. Instantiate Model Class
device = th.device('cuda: 0' if th.cuda.is_available() else 'cpu')
print('Device:', device)
batch_size = 1 # currently, loss function only works with size=1
print('Batch size:', batch_size)
num_class = 3 # background, WM, GM
epochs = 50
lr_rate = 1e-4
model = ResNetUNet(num_class).to(device)
modelname = dateToday + modelname + str(epochs) + 'e'
print('Model name:', modelname)
path = 'MIAL/Train_outputs/'+modelname+'/'
# Create a path to save outputs
if not os.path.exists(path):
os.makedirs(path)
# saving the test data
json.dump(test_slices, open(path + modelname
+ 'test_data.txt', "w"))
# json.dump(test_slices, open('madina/mial_code/'
# + dateToday + modelname
# + 'test_data.txt', "w"))
##################################################################
# Step 3. Make Dataset Iterable
dataloaders = {
'train': DataLoader(dataset=training_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=0),
'val': DataLoader(dataset=validation_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=0)
}
##################################################################
# 4. Choose optimizer & loss function
optimizer_ft = optim.Adam(filter(lambda p: p.requires_grad,
model.parameters()),
lr=lr_rate)
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft,
step_size=30,
gamma=0.1)
# weight = th.Tensor([0.05, 0.35, 0.60]).to(device) # to assign weight to class losses
weight = None
criterion = th.nn.CrossEntropyLoss(weight=weight)
softmax = th.nn.Softmax(dim=1)
##################################################################
# 5. Use the model to train
model = train_model(dataloaders,
device,
model,
criterion,
optimizer_ft,
exp_lr_scheduler,
softmax,
path,
modelname,
num_epochs=epochs)
if __name__ == '__main__':
main()