forked from PkuClosed/MEDN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PMEDN.py
275 lines (235 loc) · 9.38 KB
/
PMEDN.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
# -*- coding: utf-8 -*-
"""
Created on Tue May 9 10:41:04 2017
@author: cyye
"""
import sys
import os
import nibabel as nib
import numpy as np
from keras.models import Sequential, Model
from keras.layers.core import Lambda
from keras.optimizers import Adam
from keras.layers.advanced_activations import ThresholdedReLU
from keras.layers import merge, Dense, Input
from keras.constraints import nonneg
import time
#%%
def split_last(x):
Viso = x[:,-1]
return Viso.reshape((x.shape[0],1))
def split_last_output_shape(input_shape):
shape = list(input_shape)
assert len(shape) == 2 # only valid for 2D tensors
shape[-1] = 1
return tuple(shape)
def split_others(x):
Vother = x[:,:-1]
return Vother
def split_others_output_shape(input_shape):
shape = list(input_shape)
assert len(shape) == 2 # only valid for 2D tensors
shape[-1] = shape[-1] - 1
return tuple(shape)
#%%
dwinames = None
masknames = None
icvfnames = None
isonames = None
odnames = None
directory = None
trained = None
dwiname = None
maskname = None
modelname = None
nLabels = 3 # ICVF, ISO, and OD
if len(sys.argv) == 9:
dwinames = sys.argv[1]
masknames = sys.argv[2]
icvfnames = sys.argv[3]
isonames = sys.argv[4]
odnames = sys.argv[5]
testdwinames = sys.argv[6]
testmasknames = sys.argv[7]
directory = sys.argv[8]
else:
print("Wrong Input")
if os.path.exists(directory) == False:
os.mkdir(directory)
start = time.time()
###### Training #######
print("Training Phase")
#### load images
print("Loading")
with open(dwinames) as f:
allDwiNames = f.readlines()
with open(masknames) as f:
allMaskNames = f.readlines()
with open(icvfnames) as f:
allICVFNames = f.readlines()
with open(isonames) as f:
alISONames = f.readlines()
with open(odnames) as f:
allODNames = f.readlines()
allDwiNames = [x.strip('\n') for x in allDwiNames]
allMaskNames = [x.strip('\n') for x in allMaskNames]
allICVFNames = [x.strip('\n') for x in allICVFNames]
alISONames = [x.strip('\n') for x in alISONames]
allODNames = [x.strip('\n') for x in allODNames]
### setting voxels ###
Np = 10
nVox = 0
for iMask in range(len(allMaskNames)):
print("Counting Voxels for Subject %d" % iMask)
mask = nib.load(allMaskNames[iMask]).get_data()
# number of voxels
for i in range(mask.shape[0]):
for j in range(mask.shape[1]):
for k in range(mask.shape[2]):
if mask[i,j,k] > 0:
nVox = nVox + 1
neighbor_size = 27
dwi = nib.load(allDwiNames[0]).get_data()
comps = dwi.shape[3]
dwiTraining = np.zeros([nVox, dwi.shape[3]*neighbor_size])
icvfTraining = np.zeros([nVox, 1])
isoTraining = np.zeros([nVox, 1])
kappaTraining = np.zeros([nVox, 1])
odTraining = np.zeros([nVox, 1])
print("Initializing Voxel List")
nVox = 0
for iMask in range(len(allDwiNames)):
print("Setting Voxel List for Subject: %d" % iMask)
dwi_nii = nib.load(allDwiNames[iMask])
dwi = dwi_nii.get_data()
mask = nib.load(allMaskNames[iMask]).get_data()
icvf = nib.load(allICVFNames[iMask]).get_data()
iso = nib.load(alISONames[iMask]).get_data()
od = nib.load(allODNames[iMask]).get_data()
# number of voxels
for i in range(dwi.shape[0]):
for j in range(dwi.shape[1]):
for k in range(dwi.shape[2]):
if mask[i,j,k] > 0:
icvfTraining[nVox,0] = icvf[i,j,k]
isoTraining[nVox,0] = iso[i,j,k]
odTraining[nVox,0] = od[i,j,k]
for ii in [-1,0,1]:
for jj in [-1,0,1]:
for kk in [-1,0,1]:
if i + ii >= 0 and i + ii < dwi.shape[0] and j + jj >= 0 and j + jj < dwi.shape[1] \
and k + kk >= 0 and k + kk < dwi.shape[2] and mask[i + ii, j + jj, k + kk] > 0:
dwiTraining[nVox, ((ii+1)*9 + (jj+1)*3 + (kk+1))*dwi.shape[3]:((ii+1)*9 + (jj+1)*3 + (kk+1) + 1)*dwi.shape[3]] = dwi[i+ii, j+jj, k+kk, :]
else:
dwiTraining[nVox, ((ii+1)*9 + (jj+1)*3 + (kk+1))*dwi.shape[3]:((ii+1)*9 + (jj+1)*3 + (kk+1) + 1)*dwi.shape[3]] = dwi[i, j, k, :]
nVox = nVox + 1
#%%
### setting architechture ###
print("Setting Architechture")
nDict = 301
tau = 1e-10
inputs = Input(shape=(dwiTraining.shape[1],))
Q = Sequential()
Q.add(Dense(comps, activation='relu', bias = True, input_shape=(comps*neighbor_size,)))
smooth_inputs = Q(inputs)
W = Dense(nDict, activation='linear', bias = True)(smooth_inputs)
TNS = Sequential()
ReLUThres = 0.01
TNS.add(ThresholdedReLU(theta = ReLUThres, input_shape=(nDict,)))
TNS.add(Dense(nDict, activation='linear', bias = True))
Z = TNS(W)
nLayers = 8
for l in range(nLayers-1):
Y = merge([Z,W],"sum")
Z = TNS(Y)
Y = merge([Z,W],"sum")
T = ThresholdedReLU(theta = ReLUThres)(Y)
Viso = Lambda(split_last, output_shape=split_last_output_shape)(T)
Vother = Lambda(split_others, output_shape=split_others_output_shape)(T)
normVother = Lambda(lambda x: (x+tau)/(x+tau).norm(1, axis = 1).reshape((x.shape[0],1)))(Vother)
VicK = Dense(2, W_constraint = nonneg(), activation='linear', bias = True)(normVother)
Kappa = Lambda(split_last, output_shape=split_last_output_shape)(VicK)
Vic = Lambda(split_others, output_shape=split_others_output_shape)(VicK)
OD = Lambda(lambda x: 2.0/np.pi*np.arctan(1.0/(x+tau)))(Kappa)
weight = [1.0,1.0,1.0]
epoch = 10
#epoch = 15
print("nLayers, ReLUThres, epoch, weight, nDict: %d %r %d %r %d" % (nLayers, ReLUThres, epoch, weight, nDict))
### fitting the model ###
print("Fitting")
clf = Model(input=inputs,output=[Vic,OD,Viso])
clf.compile(optimizer=Adam(lr=0.0001), loss='mse', loss_weights = weight)
hist = clf.fit(dwiTraining, [icvfTraining, odTraining, isoTraining], batch_size=128, nb_epoch=epoch, verbose=1, validation_split=0.1)
print(hist.history)
end = time.time()
print("Training took %f" % (end-start))
#%%###### Test #######
print("Test Phase")
start = time.time()
with open(testdwinames) as f:
allTestDwiNames = f.readlines()
with open(testmasknames) as f:
allTestMaskNames = f.readlines()
allTestDwiNames = [x.strip('\n') for x in allTestDwiNames]
allTestMaskNames = [x.strip('\n') for x in allTestMaskNames]
for iMask in range(len(allTestDwiNames)):
print("Processing Subject: %d" % iMask)
#### load images
print("Loading")
dwi_nii = nib.load(allTestDwiNames[iMask])
dwi = dwi_nii.get_data()
mask = nib.load(allTestMaskNames[iMask]).get_data()
print("Counting Voxels")
nVox = 0
for i in range(dwi.shape[0]):
for j in range(dwi.shape[1]):
for k in range(dwi.shape[2]):
if mask[i,j,k] > 0:
nVox = nVox + 1
voxelList = np.zeros([nVox, 3], int)
dwiTest = np.zeros([nVox, dwi.shape[3]*neighbor_size])
print("Setting Voxels")
nVox = 0
for i in range(dwi.shape[0]):
for j in range(dwi.shape[1]):
for k in range(dwi.shape[2]):
if mask[i,j,k] > 0:
for ii in [-1,0,1]:
for jj in [-1,0,1]:
for kk in [-1,0,1]:
if i + ii >= 0 and i + ii < dwi.shape[0] and j + jj >= 0 and j + jj < dwi.shape[1] \
and k + kk >= 0 and k + kk < dwi.shape[2] and mask[i + ii, j + jj, k + kk] > 0:
dwiTest[nVox, ((ii+1)*9 + (jj+1)*3 + (kk+1))*dwi.shape[3]:((ii+1)*9 + (jj+1)*3 + (kk+1) + 1)*dwi.shape[3]] = dwi[i+ii,j+jj,k+kk,:]
else:
dwiTest[nVox, ((ii+1)*9 + (jj+1)*3 + (kk+1))*dwi.shape[3]:((ii+1)*9 + (jj+1)*3 + (kk+1) + 1)*dwi.shape[3]] = dwi[i,j,k,:]
voxelList[nVox,0] = i
voxelList[nVox,1] = j
voxelList[nVox,2] = k
nVox = nVox + 1
rows = mask.shape[0]
cols = mask.shape[1]
slices = mask.shape[2]
icvf = np.zeros([rows,cols,slices])
od = np.zeros([rows,cols,slices])
iso = np.zeros([rows,cols,slices])
print("Computing")
icvfList, odList, isoList = clf.predict(dwiTest)
for nVox in range(voxelList.shape[0]):
x = voxelList[nVox,0]
y = voxelList[nVox,1]
z = voxelList[nVox,2]
icvf[x,y,z] = icvfList[nVox,0]
od[x,y,z] = odList[nVox,0]
iso[x,y,z] = isoList[nVox,0]
hdr = dwi_nii.header
icvf_nii = nib.Nifti1Image(icvf, dwi_nii.get_affine(), hdr)
icvf_name = os.path.join(directory,"DN_AMICO_ICVF_sub_" + str(iMask) + ".nii.gz")
icvf_nii.to_filename(icvf_name)
od_nii = nib.Nifti1Image(od, dwi_nii.get_affine(), hdr)
od_name = os.path.join(directory,"DN_AMICO_OD_sub_" + str(iMask) + ".nii.gz")
od_nii.to_filename(od_name)
iso_nii = nib.Nifti1Image(iso, dwi_nii.get_affine(), hdr)
iso_name = os.path.join(directory,"DN_AMICO_ISO_sub_" + str(iMask) + ".nii.gz")
iso_nii.to_filename(iso_name)
end = time.time()
print("Test took %d" % (end-start))