-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmainLiviaNet.py
More file actions
executable file
·298 lines (228 loc) · 11.4 KB
/
mainLiviaNet.py
File metadata and controls
executable file
·298 lines (228 loc) · 11.4 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
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_train
from sampling import load_data_test
import torch
import torch.nn as nn
from LiviaNET 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_1, moda_g, imageNames, epoch, folder_save):
'''root_dir = './Data/MRBrainS/DataNii/'
model_dir = 'model'
moda_1 = root_dir + 'Training/T1'
moda_g = root_dir + 'Training/GT'''
network.eval()
softMax = nn.Softmax()
numClasses = 4 # Move this out
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)):
patch_1, patch_g, img_shape = load_data_test(moda_1, moda_g, imageNames[i_s]) # hardcoded to read the first file. Loop this to get all files
patchSize = 27
patchSize_gt = 9
x = np.zeros((0, 3, patchSize, patchSize, patchSize))
x = np.vstack((x, np.zeros((patch_1.shape[0], 3, patchSize, patchSize, patchSize))))
x[:, 0, :, :, :] = patch_1
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,3,patchSize,patchSize,patchSize))
for i_p in range(patch_1.shape[0]):
pred = network(numpy_to_var(x[i_p,:,:,:,:].reshape(1,3,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 = 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 classes: {}'.format(opts.numClasses))
print(' - Directory to load images: {}'.format(opts.root_dir))
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(' - Perform validation each {} epochs'.format(opts.freq_inference))
print('' * 41)
print('-' * 41)
print('~~~~~~~~ Starting the training... ~~~~~~')
print('-' * 41)
print('' * 40)
samplesPerEpoch = opts.numSamplesEpoch
batch_size = opts.batchSize
lr = 0.0002
epoch = opts.numEpochs
root_dir = opts.root_dir
model_name = opts.modelName
moda_1 = root_dir + 'Training/T1'
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/T1'
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.network == 'liviaNet'):
print('.... Building LiviaNET architecture....')
liviaNet = LiviaNet(num_classes)
else:
print('.... Building SemiDenseNet architecture....')
liviaNet = LiviaSemiDenseNet(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():
liviaNet.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(liviaNet.parameters(), lr=lr, betas=(0.9, 0.999))
print(" ~~~~~~~~~~~ Starting the training ~~~~~~~~~~")
print(' --------- Params: ---------')
numBatches = int(samplesPerEpoch/batch_size)
print(' - Number of batches: {} ----'.format(numBatches) )
dscAll = []
for e_i in range(epoch):
liviaNet.train()
lossEpoch = []
x_train, y_train, img_shape = load_data_train(moda_1, moda_g, imageNames_train, samplesPerEpoch) # hardcoded to read the first file. Loop this to get all files. Karthik
for b_i in range(numBatches):
optimizer.zero_grad()
liviaNet.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 = liviaNet(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%opts.freq_inference)==0:
dsc = inference(liviaNet,moda_1_val, moda_g_val, imageNames_val,e_i, opts.save_dir)
dscAll.append(dsc)
print(' Metrics: DSC(mean): {} per class: 1({}) 2({}) 3({})'.format(np.mean(dsc),np.mean(dsc[:,0]),np.mean(dsc[:,1]),np.mean(dsc[:,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(liviaNet, os.path.join(model_name, "Best_" + 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('--save_dir', type=str, default='./Results/', help='directory ot save results')
parser.add_argument('--modelName', type=str, default='liviaNet', help='name of the model')
parser.add_argument('--network', type=str, default='liviaNet', choices=['liviaNet','SemiDenseNet'],help='network to employ')
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')
parser.add_argument('--freq_inference', type=int, default=10, help='Frequency to do the inference on the validation set (i.e., number of epochs between validations)')
opts = parser.parse_args()
print(opts)
runTraining(opts)