-
Notifications
You must be signed in to change notification settings - Fork 45
/
runCTRecon3d.py
482 lines (410 loc) · 22.3 KB
/
runCTRecon3d.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# from __future__ import print_function
import argparse, os
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import torch.optim as optim
import torch
import torch.utils.data as data_utils
from utils import *
from ResUnet3d_pytorch import UNet, ResUNet, UNet_LRes, ResUNet_LRes, Discriminator
# from Unet3d_pytorch import UNet3D
from nnBuildUnits import CrossEntropy3d, topK_RegLoss, RelativeThreshold_RegLoss, adjust_learning_rate
import time
import SimpleITK as sitk
# Training settings
parser = argparse.ArgumentParser(description="PyTorch InfantSeg")
parser.add_argument("--gpuID", type=int, default=3, help="how to normalize the data")
parser.add_argument("--isAdLoss", action="store_true", help="is adversarial loss used?", default=False)
parser.add_argument("--lambda_AD", default=0.05, type=float, help="Momentum, Default: 0.05")
parser.add_argument("--how2normalize", type=int, default=5, help="how to normalize the data")
parser.add_argument("--whichLoss", type=int, default=1, help="which loss to use: 1. LossL1, 2. lossRTL1, 3. MSE (default)")
parser.add_argument("--whichNet", type=int, default=4, help="which loss to use: 1. UNet, 2. ResUNet, 3. UNet_LRes and 4. ResUNet_LRes (default, 3)")
parser.add_argument("--lossBase", type=int, default=1, help="The base to multiply the lossG_G, Default (1)")
parser.add_argument("--batchSize", type=int, default=10, help="training batch size")
parser.add_argument("--isMultiSource", action="store_true", help="is multiple modality used?", default=False)
parser.add_argument("--numOfChannel_singleSource", type=int, default=5, help="# of channels for a 2D patch for the main modality (Default, 5)")
parser.add_argument("--numOfChannel_allSource", type=int, default=1, help="# of channels for a 2D patch for all the concatenated modalities (Default, 5)")
parser.add_argument("--numofIters", type=int, default=200000, help="number of iterations to train for")
parser.add_argument("--showTrainLossEvery", type=int, default=100, help="number of iterations to show train loss")
parser.add_argument("--saveModelEvery", type=int, default=5000, help="number of iterations to save the model")
parser.add_argument("--showValPerformanceEvery", type=int, default=1000, help="number of iterations to show validation performance")
parser.add_argument("--showTestPerformanceEvery", type=int, default=5000, help="number of iterations to show test performance")
parser.add_argument("--lr", type=float, default=5e-3, help="Learning Rate. Default=1e-4")
parser.add_argument("--dropout_rate", default=0.2, type=float, help="prob to drop neurons to zero: 0.2")
parser.add_argument("--decLREvery", type=int, default=10000, help="Sets the learning rate to the initial LR decayed by momentum every n iterations, Default: n=40000")
parser.add_argument("--cuda", action="store_true", help="Use cuda?", default=True)
parser.add_argument("--resume", default="/home/niedong/Data4LowDosePET/pytorch_UNet/resunet3d_dp_pet_BatchAug_noNorm_lres_bn_lr5e3_lrdec_base1_lossL1_0p005_0627_5000.pt", type=str, help="Path to checkpoint (default: none)")
parser.add_argument("--start_epoch", default=1, type=int, help="Manual epoch number (useful on restarts)")
parser.add_argument("--threads", type=int, default=1, help="Number of threads for data loader to use, Default: 1")
parser.add_argument("--momentum", default=0.9, type=float, help="Momentum, Default: 0.9")
parser.add_argument("--weight-decay", "--wd", default=1e-4, type=float, help="weight decay, Default: 1e-4")
parser.add_argument("--RT_th", default=0.005, type=float, help="Relative thresholding: 0.005")
parser.add_argument("--pretrained", default="", type=str, help="path to pretrained model (default: none)")
parser.add_argument("--prefixModelName", default="/home/niedong/Data4LowDosePET/pytorch_UNet/resunet3d_dp_pet_BatchAug_noNorm_lres_bn_lr5e3_lrdec_base1_lossL1_0p005_0627_", type=str, help="prefix of the to-be-saved model name")
parser.add_argument("--prefixPredictedFN", default="preSub1_pet_BatchAug_noNorm_resunet3d_dp_lres_bn_lr5e3_lrdec_base1_lossL1_0p005_0627_", type=str, help="prefix of the to-be-saved predicted filename")
global opt, model
opt = parser.parse_args()
def main():
print opt
# prefixModelName = 'Regressor_1112_'
# prefixPredictedFN = 'preSub1_1112_'
# showTrainLossEvery = 100
# lr = 1e-4
# showTestPerformanceEvery = 2000
# saveModelEvery = 2000
# decLREvery = 40000
# numofIters = 200000
# how2normalize = 0
netD = Discriminator()
netD.apply(weights_init)
netD.cuda()
optimizerD = optim.Adam(netD.parameters(),lr=1e-3)
criterion_bce=nn.BCELoss()
criterion_bce.cuda()
#net=UNet()
if opt.whichNet==1:
net = UNet(in_channel=opt.numOfChannel_allSource, n_classes=1)
elif opt.whichNet==2:
net = ResUNet(in_channel=opt.numOfChannel_allSource, n_classes=1)
elif opt.whichNet==3:
net = UNet_LRes(in_channel=opt.numOfChannel_allSource, n_classes=1)
elif opt.whichNet==4:
net = ResUNet_LRes(in_channel=opt.numOfChannel_allSource, n_classes=1, dp_prob = opt.dropout_rate)
#net.apply(weights_init)
net.cuda()
params = list(net.parameters())
print('len of params is ')
print(len(params))
print('size of params is ')
print(params[0].size())
optimizer = optim.Adam(net.parameters(),lr=opt.lr)
criterion_L2 = nn.MSELoss()
criterion_L1 = nn.L1Loss()
criterion_RTL1 = RelativeThreshold_RegLoss(opt.RT_th)
#criterion = nn.CrossEntropyLoss()
# criterion = nn.NLLLoss2d()
given_weight = torch.cuda.FloatTensor([1,4,4,2])
criterion_3d = CrossEntropy3d(weight=given_weight)
criterion_3d = criterion_3d.cuda()
criterion_L2 = criterion_L2.cuda()
criterion_L1 = criterion_L1.cuda()
criterion_RTL1 = criterion_RTL1.cuda()
#inputs=Variable(torch.randn(1000,1,32,32)) #here should be tensor instead of variable
#targets=Variable(torch.randn(1000,10,1,1)) #here should be tensor instead of variable
# trainset=data_utils.TensorDataset(inputs, targets)
# trainloader = data_utils.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)
# inputs=torch.randn(1000,1,32,32)
# targets=torch.LongTensor(1000)
path_test ='/home/niedong/DataCT/data_niigz/'
path_patients_h5 = '/home/niedong/DataCT/h5Data3D_noNorm/trainBatch3D_H5'
path_patients_h5_test ='/home/niedong/DataCT/h5Data3D_noNorm/val3D_H5'
# path_patients_h5_test ='/home/niedong/Data4LowDosePET/test2D_H5'
# batch_size=10
#data_generator = Generator_2D_slices(path_patients_h5,opt.batchSize,inputKey='data3T',outputKey='data7T')
#data_generator_test = Generator_2D_slices(path_patients_h5_test,opt.batchSize,inputKey='data3T',outputKey='data7T')
data_generator = Generator_3D_patches(path_patients_h5,opt.batchSize, inputKey='dataLPET', outputKey='dataHPET')
data_generator_test = Generator_3D_patches(path_patients_h5_test,opt.batchSize, inputKey='dataLPET', outputKey='dataHPET')
if opt.resume:
if os.path.isfile(opt.resume):
print("=> loading checkpoint '{}'".format(opt.resume))
checkpoint = torch.load(opt.resume)
net.load_state_dict(checkpoint['model'])
opt.start_epoch = 100000
opt.start_epoch = checkpoint["epoch"] - 1
# net.load_state_dict(checkpoint["model"].state_dict())
else:
print("=> no checkpoint found at '{}'".format(opt.resume))
########### We'd better use dataloader to load a lot of data,and we also should train several epoches###############
########### We'd better use dataloader to load a lot of data,and we also should train several epoches###############
running_loss = 0.0
start = time.time()
for iter in range(opt.start_epoch, opt.numofIters+1):
#print('iter %d'%iter)
# inputs, exinputs, labels = data_generator.next()
inputs, labels = data_generator.next()
# xx = np.transpose(inputs,(5,64,64))
# print 'size of inputs: ', inputs.shape
inputs = np.transpose(inputs,(0,4,1,2,3))
# inputs = np.squeeze(inputs) #16x64x64
# exinputs = np.squeeze(exinputs) #5x64x64
# print 'shape is ....',inputs.shape
labels = np.squeeze(labels) #64x64
# labels = labels.astype(int)
inputs = inputs.astype(float)
inputs = torch.from_numpy(inputs)
inputs = inputs.float()
# exinputs = exinputs.astype(float)
# exinputs = torch.from_numpy(exinputs)
# exinputs = exinputs.float()
labels = labels.astype(float)
labels = torch.from_numpy(labels)
labels = labels.float()
#print type(inputs), type(exinputs)
if opt.isMultiSource:
# source = torch.cat((inputs, exinputs),dim=1)
print 'you have to tune the multi source part'
else:
source = inputs
#source = inputs
# mid_slice = opt.numOfChannel_singleSource//2
residual_source = inputs
#inputs = inputs.cuda()
#exinputs = exinputs.cuda()
source = source.cuda()
residual_source = residual_source.cuda()
labels = labels.cuda()
#we should consider different data to train
#wrap them into Variable
source, residual_source, labels = Variable(source),Variable(residual_source), Variable(labels)
#inputs, exinputs, labels = Variable(inputs),Variable(exinputs), Variable(labels)
## (1) update D network: maximize log(D(x)) + log(1 - D(G(z)))
if opt.isAdLoss:
if opt.whichNet == 3 or opt.whichNet == 4:
outputG = net(source, residual_source) # 5x64x64->1*64x64
else:
outputG = net(source) # 5x64x64->1*64x64
#outputG = net(source,residual_source) #5x64x64->1*64x64
if len(labels.size())==3:
labels = labels.unsqueeze(1)
outputD_real = netD(labels)
outputD_real = F.sigmoid(outputD_real)
if len(outputG.size())==3:
outputG = outputG.unsqueeze(1)
outputD_fake = netD(outputG)
outputD_fake = F.sigmoid(outputD_fake)
netD.zero_grad()
batch_size = inputs.size(0)
real_label = torch.ones(batch_size,1)
real_label = real_label.cuda()
#print(real_label.size())
real_label = Variable(real_label)
#print(outputD_real.size())
loss_real = criterion_bce(outputD_real,real_label)
loss_real.backward()
#train with fake data
fake_label = torch.zeros(batch_size,1)
# fake_label = torch.FloatTensor(batch_size)
# fake_label.data.resize_(batch_size).fill_(0)
fake_label = fake_label.cuda()
fake_label = Variable(fake_label)
loss_fake = criterion_bce(outputD_fake,fake_label)
loss_fake.backward()
lossD = loss_real + loss_fake
# print 'loss_real is ',loss_real.data[0],'loss_fake is ',loss_fake.data[0],'outputD_real is',outputD_real.data[0]
# print('loss for discriminator is %f'%lossD.data[0])
#update network parameters
optimizerD.step()
## (2) update G network: minimize the L1/L2 loss, maximize the D(G(x))
# print inputs.data.shape
#outputG = net(source) #here I am not sure whether we should use twice or not
if opt.whichNet == 3 or opt.whichNet == 4:
outputG = net(source, residual_source) # 5x64x64->1*64x64
else:
outputG = net(source) # 5x64x64->1*64x64
#outputG = net(source,residual_source) #5x64x64->1*64x64
net.zero_grad()
if opt.whichLoss==1:
lossG_G = criterion_L1(torch.squeeze(outputG), torch.squeeze(labels))
elif opt.whichLoss==2:
lossG_G = criterion_RTL1(torch.squeeze(outputG), torch.squeeze(labels))
else:
lossG_G = criterion_L2(torch.squeeze(outputG), torch.squeeze(labels))
lossG_G = opt.lossBase * lossG_G
lossG_G.backward() #compute gradients
if opt.isAdLoss:
#we want to fool the discriminator, thus we pretend the label here to be real. Actually, we can explain from the
#angel of equation (note the max and min difference for generator and discriminator)
#outputG = net(inputs)
#outputG = net(source,residual_source) #5x64x64->1*64x64
if opt.whichNet == 3 or opt.whichNet == 4:
outputG = net(source, residual_source) # 5x64x64->1*64x64
else:
outputG = net(source) # 5x64x64->1*64x64
if len(outputG.size())==3:
outputG = outputG.unsqueeze(1)
outputD = netD(outputG)
outputD = F.sigmoid(outputD)
lossG_D = opt.lambda_AD*criterion_bce(outputD,real_label) #note, for generator, the label for outputG is real, because the G wants to confuse D
lossG_D.backward()
#for other losses, we can define the loss function following the pytorch tutorial
optimizer.step() #update network parameters
#print('loss for generator is %f'%lossG.data[0])
#print statistics
running_loss = running_loss + lossG_G.data[0]
if iter%opt.showTrainLossEvery==0: #print every 2000 mini-batches
print '************************************************'
print 'time now is: ' + time.asctime(time.localtime(time.time()))
# print 'running loss is ',running_loss
print 'average running loss for generator between iter [%d, %d] is: %.5f'%(iter - 100 + 1,iter,running_loss/100)
print 'lossG_G is %.5f respectively.'%(lossG_G.data[0])
if opt.isAdLoss:
print 'loss_real is ',loss_real.data[0],'loss_fake is ',loss_fake.data[0],'outputD_real is',outputD_real.data[0]
print('loss for discriminator is %f'%lossD.data[0])
print 'cost time for iter [%d, %d] is %.2f'%(iter - 100 + 1,iter, time.time()-start)
print '************************************************'
running_loss = 0.0
start = time.time()
if iter%opt.saveModelEvery==0: #save the model
state = {
'epoch': iter+1,
'model': net.state_dict()
}
torch.save(state, opt.prefixModelName+'%d.pt'%iter)
print 'save model: '+opt.prefixModelName+'%d.pt'%iter
if opt.isAdLoss:
torch.save(netD.state_dict(), opt.prefixModelName+'_net_D%d.pt'%iter)
if iter%opt.decLREvery==0:
opt.lr = opt.lr*0.5
adjust_learning_rate(optimizer, opt.lr)
if iter%opt.showValPerformanceEvery==0: #test one subject
# to test on the validation dataset in the format of h5
# inputs,exinputs,labels = data_generator_test.next()
inputs, labels = data_generator_test.next()
inputs = np.transpose(inputs,(0,4,1,2,3))
# inputs = np.squeeze(inputs)
# exinputs = np.transpose(exinputs, (0, 3, 1, 2))
# exinputs = np.squeeze(exinputs) # 5x64x64
labels = np.squeeze(labels)
inputs = torch.from_numpy(inputs)
inputs = inputs.float()
# exinputs = torch.from_numpy(exinputs)
# exinputs = exinputs.float()
labels = torch.from_numpy(labels)
labels = labels.float()
# mid_slice = opt.numOfChannel_singleSource // 2
residual_source = inputs
if opt.isMultiSource:
# source = torch.cat((inputs, exinputs), dim=1)
print 'you have to tune the multi source part'
else:
source = inputs
source = source.cuda()
residual_source = residual_source.cuda()
labels = labels.cuda()
source,residual_source,labels = Variable(source),Variable(residual_source), Variable(labels)
# source = inputs
#outputG = net(inputs)
if opt.whichNet == 3 or opt.whichNet == 4:
outputG = net(source, residual_source) # 5x64x64->1*64x64
else:
outputG = net(source) # 5x64x64->1*64x64
#outputG = net(source,residual_source) #5x64x64->1*64x64
if opt.whichLoss == 1:
lossG_G = criterion_L1(torch.squeeze(outputG), torch.squeeze(labels))
elif opt.whichLoss == 2:
lossG_G = criterion_RTL1(torch.squeeze(outputG), torch.squeeze(labels))
else:
lossG_G = criterion_L2(torch.squeeze(outputG), torch.squeeze(labels))
lossG_G = opt.lossBase * lossG_G
print '.......come to validation stage: iter {}'.format(iter),'........'
print 'lossG_G is %.5f.'%(lossG_G.data[0])
if iter % opt.showTestPerformanceEvery == 0: # test one subject
mr_test_itk=sitk.ReadImage(os.path.join(path_test,'sub1_sourceCT.nii.gz'))
ct_test_itk=sitk.ReadImage(os.path.join(path_test,'sub1_extraCT.nii.gz'))
hpet_test_itk = sitk.ReadImage(os.path.join(path_test, 'sub1_targetCT.nii.gz'))
spacing = hpet_test_itk.GetSpacing()
origin = hpet_test_itk.GetOrigin()
direction = hpet_test_itk.GetDirection()
mrnp=sitk.GetArrayFromImage(mr_test_itk)
ctnp=sitk.GetArrayFromImage(ct_test_itk)
hpetnp=sitk.GetArrayFromImage(hpet_test_itk)
##### specific normalization #####
# mu = np.mean(mrnp)
# maxV, minV = np.percentile(mrnp, [99 ,25])
# #mrimg=mrimg
# mrnp = (mrnp-minV)/(maxV-minV)
#for training data in pelvicSeg
if opt.how2normalize == 1:
maxV, minV = np.percentile(mrnp, [99 ,1])
print 'maxV,',maxV,' minV, ',minV
mrnp = (mrnp-mu)/(maxV-minV)
print 'unique value: ',np.unique(ctnp)
#for training data in pelvicSeg
if opt.how2normalize == 2:
maxV, minV = np.percentile(mrnp, [99 ,1])
print 'maxV,',maxV,' minV, ',minV
mrnp = (mrnp-mu)/(maxV-minV)
print 'unique value: ',np.unique(ctnp)
#for training data in pelvicSegRegH5
if opt.how2normalize== 3:
std = np.std(mrnp)
mrnp = (mrnp - mu)/std
print 'maxV,',np.ndarray.max(mrnp),' minV, ',np.ndarray.min(mrnp)
if opt.how2normalize == 4:
maxLPET = 149.366742
maxPercentLPET = 7.76
minLPET = 0.00055037
meanLPET = 0.27593288
stdLPET = 0.75747500
# for rsCT
maxCT = 27279
maxPercentCT = 1320
minCT = -1023
meanCT = -601.1929
stdCT = 475.034
# for s-pet
maxSPET = 156.675962
maxPercentSPET = 7.79
minSPET = 0.00055037
meanSPET = 0.284224789
stdSPET = 0.7642257
#matLPET = (mrnp - meanLPET) / (stdLPET)
matLPET = (mrnp - minLPET) / (maxPercentLPET - minLPET)
matCT = (ctnp - meanCT) / stdCT
matSPET = (hpetnp - minSPET) / (maxPercentSPET - minSPET)
if opt.how2normalize == 5:
# for rsCT
maxCT = 27279
maxPercentCT = 1320
minCT = -1023
meanCT = -601.1929
stdCT = 475.034
print
'ct, max: ', np.amax(ctnp), ' ct, min: ', np.amin(ctnp)
# matLPET = (mrnp - meanLPET) / (stdLPET)
matLPET = mrnp
matCT = (ctnp - meanCT) / stdCT
matSPET = hpetnp
if not opt.isMultiSource:
# matFA = matLPET
# matGT = matSPET
matFA = mrnp
matGT = hpetnp
print 'matFA shape: ',matFA.shape, ' matGT shape: ', matGT.shape
matOut = testOneSubject_aver_res(matFA,matGT,[16,64,64],[16,64,64],[8,32,32],net,opt.prefixModelName+'%d.pt'%iter, nd=3)
print 'matOut shape: ',matOut.shape
ct_estimated = matOut
itspsnr = psnr(ct_estimated, matGT)
print 'pred: ',ct_estimated.dtype, ' shape: ',ct_estimated.shape
print 'gt: ',ctnp.dtype,' shape: ',ct_estimated.shape
print 'psnr = ',itspsnr
volout = sitk.GetImageFromArray(ct_estimated)
volout.SetSpacing(spacing)
volout.SetOrigin(origin)
volout.SetDirection(direction)
sitk.WriteImage(volout,opt.prefixPredictedFN+'{}'.format(iter)+'.nii.gz')
else:
# matFA = matLPET
# matGT = matSPET
matFA = mrnp
matGT = hpetnp
print 'matFA shape: ', matFA.shape, ' matGT shape: ', matGT.shape
matOut = testOneSubject_aver_res_multiModal(matFA, matCT, matGT, [16, 64, 64], [16, 64, 64], [8, 32, 32], net,
opt.prefixModelName + '%d.pt' % iter)
print 'matOut shape: ', matOut.shape
ct_estimated = matOut
itspsnr = psnr(ct_estimated, matGT)
print 'pred: ', ct_estimated.dtype, ' shape: ', ct_estimated.shape
print 'gt: ', ctnp.dtype, ' shape: ', ct_estimated.shape
print 'psnr = ', itspsnr
volout = sitk.GetImageFromArray(ct_estimated)
volout.SetSpacing(spacing)
volout.SetOrigin(origin)
volout.SetDirection(direction)
sitk.WriteImage(volout, opt.prefixPredictedFN + '{}'.format(iter) + '.nii.gz')
print('Finished Training')
if __name__ == '__main__':
os.environ['CUDA_VISIBLE_DEVICES'] = str(opt.gpuID)
main()