-
Notifications
You must be signed in to change notification settings - Fork 12
/
mainHyperDenseNet.py
executable file
·332 lines (251 loc) · 12.6 KB
/
mainHyperDenseNet.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
from os.path import isfile, join
import os
import numpy as np
from sampling import reconstruct_volume
from sampling import my_reconstruct_volume
from sampling import load_data_trainG
from sampling import load_data_test
import torch
import torch.nn as nn
from HyperDenseNet import *
from medpy.metric.binary import dc,hd
import argparse
import pdb
from torch.autograd import Variable
from progressBar import printProgressBar
import nibabel as nib
def evaluateSegmentation(gt,pred):
pred = pred.astype(dtype='int')
numClasses = np.unique(gt)
dsc = np.zeros((1, len(numClasses) - 1))
for i_n in range(1,len(numClasses)):
gt_c = np.zeros(gt.shape)
y_c = np.zeros(gt.shape)
gt_c[np.where(gt==i_n)]=1
y_c[np.where(pred==i_n)]=1
dsc[0, i_n - 1] = dc(gt_c, y_c)
return dsc
def numpy_to_var(x):
torch_tensor = torch.from_numpy(x).type(torch.FloatTensor)
if torch.cuda.is_available():
torch_tensor = torch_tensor.cuda()
return Variable(torch_tensor)
def inference(network, moda_n, moda_g, imageNames, epoch, folder_save, number_modalities):
'''root_dir = './Data/MRBrainS/DataNii/'
model_dir = 'model'
moda_1 = root_dir + 'Training/T1'
moda_2 = root_dir + 'Training/T1_IR'
moda_3 = root_dir + 'Training/T2_FLAIR'
moda_g = root_dir + 'Training/GT'''
network.eval()
softMax = nn.Softmax()
numClasses = 4
if torch.cuda.is_available():
softMax.cuda()
network.cuda()
dscAll = np.zeros((len(imageNames), numClasses - 1)) # 1 class is the background!!
for i_s in range(len(imageNames)):
if number_modalities == 2:
patch_1, patch_2, patch_g, img_shape = load_data_test(moda_n, moda_g, imageNames[i_s], number_modalities) # hardcoded to read the first file. Loop this to get all files
if number_modalities == 3:
patch_1, patch_2, patch_3, patch_g, img_shape = load_data_test([moda_n], moda_g, imageNames[i_s], number_modalities) # hardcoded to read the first file. Loop this to get all files
patchSize = 27
patchSize_gt = 9
x = np.zeros((0, number_modalities, patchSize, patchSize, patchSize))
x = np.vstack((x, np.zeros((patch_1.shape[0], number_modalities, patchSize, patchSize, patchSize))))
x[:, 0, :, :, :] = patch_1
x[:, 1, :, :, :] = patch_2
if (number_modalities==3):
x[:, 2, :, :, :] = patch_3
pred_numpy = np.zeros((0,numClasses,patchSize_gt,patchSize_gt,patchSize_gt))
pred_numpy = np.vstack((pred_numpy, np.zeros((patch_1.shape[0], numClasses, patchSize_gt, patchSize_gt, patchSize_gt))))
totalOp = len(imageNames)*patch_1.shape[0]
pred = network(numpy_to_var(x[0,:,:,:,:]).view(1,number_modalities,patchSize,patchSize,patchSize))
for i_p in range(patch_1.shape[0]):
pred = network(numpy_to_var(x[i_p,:,:,:,:].reshape(1,number_modalities,patchSize,patchSize,patchSize)))
pred_y = softMax(pred)
pred_numpy[i_p,:,:,:,:] = pred_y.cpu().data.numpy()
printProgressBar(i_s * ((totalOp + 0.0) / len(imageNames)) + i_p + 1, totalOp,
prefix="[Validation] ",
length=15)
# To reconstruct the predicted volume
extraction_step_value = 9
pred_classes = np.argmax(pred_numpy, axis=1)
pred_classes = pred_classes.reshape((len(pred_classes), patchSize_gt, patchSize_gt, patchSize_gt))
#bin_seg = reconstruct_volume(pred_classes, (img_shape[1], img_shape[2], img_shape[3]))
bin_seg = my_reconstruct_volume(pred_classes,
(img_shape[1], img_shape[2], img_shape[3]),
patch_shape=(27, 27, 27),
extraction_step=(extraction_step_value, extraction_step_value, extraction_step_value))
bin_seg = bin_seg[:,:,extraction_step_value:img_shape[3]-extraction_step_value]
gt = nib.load(moda_g + '/' + imageNames[i_s]).get_data()
img_pred = nib.Nifti1Image(bin_seg, np.eye(4))
img_gt = nib.Nifti1Image(gt, np.eye(4))
img_name = imageNames[i_s].split('.nii')
name = 'Pred_' + img_name[0] + '_Epoch_' + str(epoch) + '.nii.gz'
namegt = 'GT_' + img_name[0] + '_Epoch_' + str(epoch) + '.nii.gz'
if not os.path.exists(folder_save + 'Segmentations/'):
os.makedirs(folder_save + 'Segmentations/')
if not os.path.exists(folder_save + 'GT/'):
os.makedirs(folder_save + 'GT/')
nib.save(img_pred, folder_save + 'Segmentations/' + name)
nib.save(img_gt, folder_save + 'GT/' + namegt)
dsc = evaluateSegmentation(gt,bin_seg)
dscAll[i_s, :] = dsc
return dscAll
def runTraining(opts):
print('' * 41)
print('~' * 50)
print('~~~~~~~~~~~~~~~~~ PARAMETERS ~~~~~~~~~~~~~~~~')
print('~' * 50)
print(' - Number of image modalities: {}'.format(opts.numModal))
print(' - Number of classes: {}'.format(opts.numClasses))
print(' - Directory to load images: {}'.format(opts.root_dir))
for i in range(len(opts.modality_dirs)):
print(' - Modality {}: {}'.format(i+1,opts.modality_dirs[i]))
print(' - Directory to save results: {}'.format(opts.save_dir))
print(' - To model will be saved as : {}'.format(opts.modelName))
print('-' * 41)
print(' - Number of epochs: {}'.format(opts.numClasses))
print(' - Batch size: {}'.format(opts.batchSize))
print(' - Number of samples per epoch: {}'.format(opts.numSamplesEpoch))
print(' - Learning rate: {}'.format(opts.l_rate))
print('' * 41)
print('-' * 41)
print('~~~~~~~~ Starting the training... ~~~~~~')
print('-' * 41)
print('' * 40)
samplesPerEpoch = opts.numSamplesEpoch
batch_size = opts.batchSize
lr = opts.l_rate
epoch = opts.numEpochs
root_dir = opts.root_dir
model_name = opts.modelName
if not (len(opts.modality_dirs)== opts.numModal): raise AssertionError
moda_1 = root_dir + 'Training/' + opts.modality_dirs[0]
moda_2 = root_dir + 'Training/' + opts.modality_dirs[1]
if (opts.numModal == 3):
moda_3 = root_dir + 'Training/' + opts.modality_dirs[2]
moda_g = root_dir + 'Training/GT'
print(' --- Getting image names.....')
print(' - Training Set: -')
if os.path.exists(moda_1):
imageNames_train = [f for f in os.listdir(moda_1) if isfile(join(moda_1, f))]
imageNames_train.sort()
print(' ------- Images found ------')
for i in range(len(imageNames_train)):
print(' - {}'.format(imageNames_train[i]))
else:
raise Exception(' - {} does not exist'.format(moda_1))
moda_1_val = root_dir + 'Validation/' + opts.modality_dirs[0]
moda_2_val = root_dir + 'Validation/' + opts.modality_dirs[1]
if (opts.numModal == 3):
moda_3_val = root_dir + 'Validation/' + opts.modality_dirs[2]
moda_g_val = root_dir + 'Validation/GT'
print(' --------------------')
print(' - Validation Set: -')
if os.path.exists(moda_1):
imageNames_val = [f for f in os.listdir(moda_1_val) if isfile(join(moda_1_val, f))]
imageNames_val.sort()
print(' ------- Images found ------')
for i in range(len(imageNames_val)):
print(' - {}'.format(imageNames_val[i]))
else:
raise Exception(' - {} does not exist'.format(moda_1_val))
print("~~~~~~~~~~~ Creating the model ~~~~~~~~~~")
num_classes = opts.numClasses
# Define HyperDenseNet
# To-Do. Get as input the config settings to create different networks
if (opts.numModal == 2):
hdNet = HyperDenseNet_2Mod(num_classes)
if (opts.numModal == 3):
hdNet = HyperDenseNet(num_classes)
#
'''try:
hdNet = torch.load(os.path.join(model_name, "Best_" + model_name + ".pkl"))
print("--------model restored--------")
except:
print("--------model not restored--------")
pass'''
softMax = nn.Softmax()
CE_loss = nn.CrossEntropyLoss()
if torch.cuda.is_available():
hdNet.cuda()
softMax.cuda()
CE_loss.cuda()
# To-DO: Check that optimizer is the same (and same values) as the Theano implementation
optimizer = torch.optim.Adam(hdNet.parameters(), lr=lr, betas=(0.9, 0.999))
print(" ~~~~~~~~~~~ Starting the training ~~~~~~~~~~")
numBatches = int(samplesPerEpoch/batch_size)
dscAll = []
for e_i in range(epoch):
hdNet.train()
lossEpoch = []
if (opts.numModal == 2):
imgPaths = [moda_1, moda_2]
if (opts.numModal == 3):
imgPaths = [moda_1, moda_2, moda_3]
x_train, y_train, img_shape = load_data_trainG(imgPaths, moda_g, imageNames_train, samplesPerEpoch, opts.numModal) # hardcoded to read the first file. Loop this to get all files. Karthik
for b_i in range(numBatches):
optimizer.zero_grad()
hdNet.zero_grad()
MRIs = numpy_to_var(x_train[b_i*batch_size:b_i*batch_size+batch_size,:,:,:,:])
Segmentation = numpy_to_var(y_train[b_i*batch_size:b_i*batch_size+batch_size,:,:,:])
segmentation_prediction = hdNet(MRIs)
predClass_y = softMax(segmentation_prediction)
# To adapt CE to 3D
# LOGITS:
segmentation_prediction = segmentation_prediction.permute(0,2,3,4,1).contiguous()
segmentation_prediction = segmentation_prediction.view(segmentation_prediction.numel() // num_classes, num_classes)
CE_loss_batch = CE_loss(segmentation_prediction, Segmentation.view(-1).type(torch.cuda.LongTensor))
loss = CE_loss_batch
loss.backward()
optimizer.step()
lossEpoch.append(CE_loss_batch.cpu().data.numpy())
printProgressBar(b_i + 1, numBatches,
prefix="[Training] Epoch: {} ".format(e_i),
length=15)
del MRIs
del Segmentation
del segmentation_prediction
del predClass_y
if not os.path.exists(model_name):
os.makedirs(model_name)
np.save(os.path.join(model_name, model_name + '_loss.npy'), dscAll)
print(' Epoch: {}, loss: {}'.format(e_i,np.mean(lossEpoch)))
if (e_i%10)==0:
if (opts.numModal == 2):
moda_n = [moda_1_val, moda_2_val]
if (opts.numModal == 3):
moda_n = [moda_1_val, moda_2_val, moda_3_val]
dsc = inference(hdNet,moda_n, moda_g_val, imageNames_val,e_i, opts.save_dir,opts.numModal)
dscAll.append(dsc)
print(' Metrics: DSC(mean): {} per class: 1({}) 2({}) 3({})'.format(np.mean(dsc),dsc[0][0],dsc[0][1],dsc[0][2]))
if not os.path.exists(model_name):
os.makedirs(model_name)
np.save(os.path.join(model_name, model_name + '_DSCs.npy'), dscAll)
d1 = np.mean(dsc)
if (d1>0.60):
if not os.path.exists(model_name):
os.makedirs(model_name)
torch.save(hdNet, os.path.join(model_name, "Best2_" + model_name + ".pkl"))
if (100+e_i%20)==0:
lr = lr/2
print(' Learning rate decreased to : {}'.format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--root_dir', type=str, default='./Data/MRBrainS/DataNii/', help='directory containing the train and val folders')
parser.add_argument('--modality_dirs', nargs='+', default=['T1','T2_FLAIR'], help='subdirectories containing the multiple modalities')
parser.add_argument('--save_dir', type=str, default='./Results/', help='directory ot save results')
parser.add_argument('--modelName', type=str, default='HyperDenseNet_2Mod', help='name of the model')
parser.add_argument('--numModal', type=int, default=2, help='Number of image modalities')
parser.add_argument('--numClasses', type=int, default=4, help='Number of classes (Including background)')
parser.add_argument('--numSamplesEpoch', type=int, default=1000, help='Number of samples per epoch')
parser.add_argument('--numEpochs', type=int, default=500, help='Number of epochs')
parser.add_argument('--batchSize', type=int, default=10, help='Batch size')
parser.add_argument('--l_rate', type=float, default=0.0002, help='Learning rate')
opts = parser.parse_args()
print(opts)
runTraining(opts)