forked from qrfaction/segment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.py
407 lines (354 loc) · 12.4 KB
/
tool.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
from nipy.io.api import save_image,load_image
from nipy.core.api import Image, AffineTransform
from setting import LABEL_PATH,IMAGE_PATH,WINDOW,OUTPUT,PRE_LABEL_PATH,INFO,crop_setting
import numpy as np
import json
from random import randint,choice
def get_files(path,prefix = True):
import os
files = os.listdir(path) #一对label和image文件同名
if prefix:
files = [path + f for f in files]
else:
files = [f for f in files]
return np.array(files)
def get_image(f):
return np.load(f)
def get_batch_images(files):
return [get_image(f) for f in files]
def swap_axis(image,task,axis):
if task == 'slice':
if axis == 'x':
return image.transpose((2,1,0))
elif axis == 'y':
return image.transpose((0,2,1))
elif axis == 'z':
return image
else:
raise ValueError("swapaxis error task slice")
elif task == 'convlstm':
if axis == 'x':
return image
elif axis == 'y':
return image.transpose((1, 0, 2, 3))
elif axis == 'z':
return image.transpose((2, 1, 0, 3))
else:
raise ValueError("swapaxis error task convlstm")
else:
raise ValueError("don't have this task")
def crop_3d(image,id_h,area=None):
"""
x1 y1 z1 x2 y2 z2 x y z
180 115:161 118:172 55:81 90 115:162 112:174 99:121 37:214 36:239 28:149
192 75:120 81:135 46:70 78 74:122 80:134 86:114 15:160 20:182 16:143
256_166 100:163 105:173 48:75 82 98:162 107:173 90:117 25:215 28:237 19:144
80 * 80 * 40
:param image: 图像
:param id_h: 海马体ID
:param area: crop区域
:return:
"""
if area is None:
area = crop_setting['3d'+str(image.shape[2])]
z = 'z'+str(id_h)
return image[
area['x'][0]:area['x'][1],
area['y'][0]:area['y'][1],
area[z][0]:area[z][1]
]
def crop_2d_slice(image,id_h,axis,shift,window=WINDOW,area=None):
window = window//2
if area is None:
area = crop_setting['2d'+str(image.shape[2])]
z = 'z'+str(id_h)
if axis == 'x':
return image[
area['x'][0]+shift-window : area['x'][0]+shift+window+1,
area['y'][0]:area['y'][1],
area[z][0]:area[z][1]
]
elif axis == 'y':
return image[
area['x'][0]:area['x'][1],
area['y'][0] + shift - window : area['y'][0] + shift + window + 1,
area[z][0]:area[z][1]
]
elif axis == 'z':
return image[
area['x'][0]:area['x'][1],
area['y'][0]:area['y'][1],
area[z][0] + shift - window : area[z][0] + shift + window + 1,
]
else:
raise ValueError('crop_2d axis error')
class Generator_3d:
def __init__(self,files,batchsize):
self.images = []
self.labels = []
for i in [1,2]:
for f in files:
self.images.append((IMAGE_PATH+f,i))
self.labels.append((LABEL_PATH+f,i))
self.batchsize = batchsize
self.begin = 0
self.end = self.batchsize
self.index = list(range(0, len(self.images)))
np.random.shuffle(self.index)
def getitem(self, index): # 返回的是tensor
image_file,h_id = self.images[index]
label_file,h_id = self.labels[index]
image = get_image(image_file)
label = get_image(label_file)
data = np.concatenate([image,label],axis=-1)
data = self.process(data, h_id)
assert data.shape == (80,80,40,2)
image = data[:, :, :, :1]
label = data[:, :, :, 1:]
return image,label
def process(self,image,h_id):
image = crop_3d(image,h_id)
image = self.flip(image)
return image
def get_batch_data(self):
batch_image = []
batch_label = []
for i in self.index[self.begin:self.end]:
image,label = self.getitem(i)
batch_image.append(image)
batch_label.append(label)
self.begin = self.end
self.end += self.batchsize
if self.end > len(self.labels):
np.random.shuffle(self.index)
self.begin = 0
self.end = self.batchsize
return np.array(batch_image),np.array(batch_label)
def flip(self,image):
axis = randint(0, 2)
if axis == 0:
image = image[::-1, :, :, :]
elif axis == 1:
image = image[:, ::-1, :, :]
else:
image = image[:, :, ::-1, :]
return image
class Generator_convlstm:
def __init__(self,files,axis,batchsize):
self.images = []
self.labels = []
for i in [1,2]:
for f in files:
self.images.append((IMAGE_PATH+f,i))
self.labels.append((LABEL_PATH+f,i))
self.axis = axis
self.batchsize = batchsize
self.begin = 0
self.end = self.batchsize
self.index = list(range(0, len(self.images)))
np.random.shuffle(self.index)
def getitem(self, index): # 返回的是tensor
image_file,h_id = self.images[index]
label_file,h_id = self.labels[index]
image = get_image(image_file)
label = get_image(label_file)
data = np.concatenate([image,label],axis=-1)
assert data.shape == (80,80,40,2)
data = self.process(data,h_id)
image = data[:,:,:,:1]
label = data[:,:,:,1:]
return image,label
def process(self,image,h_id):
image = crop_3d(image,h_id)
image = self.flip(image)
image = swap_axis(image,'convlstm',self.axis)
return image
def get_batch_data(self):
batch_image = []
batch_label = []
for i in self.index[self.begin:self.end]:
image,label = self.getitem(i)
batch_image.append(image)
batch_label.append(label)
self.begin = self.end
self.end += self.batchsize
if self.end > len(self.labels):
np.random.shuffle(self.index)
self.begin = 0
self.end = self.batchsize
return batch_image,batch_label
def flip(self,image):
axis = randint(0, 2)
if axis == 0:
image = image[::-1, :, :, :]
elif axis == 1:
image = image[:, ::-1, :, :]
elif axis == 2:
image = image[:, :, ::-1, :]
else:
raise ValueError('3d generator flip error')
return image
class Generator_2d_slice:
def __init__(self, files,axis,batchsize):
self.axis = axis
if axis == 'x':
size = crop_setting['2d166']['x'][1] - crop_setting['2d166']['x'][0]
elif axis == 'y':
size = crop_setting['2d166']['y'][1] - crop_setting['2d166']['y'][0]
elif axis == 'z':
size = crop_setting['2d166']['z1'][1] - crop_setting['2d166']['z1'][0]
else:
raise ValueError('generator 2d slice axis error')
self.flip_axis = ['x','y','z']
self.flip_axis.remove(axis)
self.images = []
self.labels = []
for i in [1, 2]:
for shift in range(size):
for f in files:
self.images.append((IMAGE_PATH+f,i,shift))
self.labels.append((LABEL_PATH+f,i,shift))
self.batchsize = batchsize
self.begin = 0
self.end = self.batchsize
self.index = list(range(0, len(self.images)))
np.random.shuffle(self.index)
def getitem(self, index): # 返回的是tensor
image_file,h_id,shift = self.images[index]
label_file,h_id,shift = self.labels[index]
image = get_image(image_file)
label = get_image(label_file)
data = np.concatenate([image, label], axis=-1)
data = self.process(data, h_id,shift)
image = data[:,:,:,0]
label = data[:,:,:,1]
postion = 1 + WINDOW // 2
label = label[:, :, postion:postion + 1]
return image,label
def process(self, image, h_id,shift):
image = crop_2d_slice(image,h_id,self.axis,shift)
image = self.flip(image)
image = swap_axis(image,'slice',self.axis)
return image
def get_batch_data(self):
batch_image = []
batch_label = []
for i in self.index[self.begin:self.end]:
image,label = self.getitem(i)
batch_image.append(image)
batch_label.append(label)
self.begin = self.end
self.end += self.batchsize
if self.end > len(self.labels):
np.random.shuffle(self.index)
self.begin = 0
self.end = self.batchsize
return batch_image,batch_label
def flip(self,image):
axis = choice(self.flip_axis)
if axis == 'x':
image = image[::-1, :, :, :]
elif axis == 'y':
image = image[:, ::-1, :, :]
elif axis == 'z':
image = image[:, :, ::-1, :]
else:
raise ValueError('3d generator flip error')
return image
def seg_recovery(y_pred,filename):
label = load_image(PRE_LABEL_PATH + filename)
shape = label.get_data().shape
segment = np.zeros(shape)
h1 = y_pred[0].around()
h2 = y_pred[1].around()
area = crop_setting['3d' + str(shape[2])]
segment[
area['x'][0]:area['x'][1],
area['y'][0]:area['y'][1],
area['z1'][0]:area['z1'][1],
0
] = h1[0]
segment[
area['x'][0]:area['x'][1],
area['y'][0]:area['y'][1],
area['z2'][0]:area['z2'][1],
1
] = h2[0]
img = Image(segment,label.coordmap)
save_image(img,OUTPUT+filename)
def deal_label(modelname,label,axis=None):
if modelname == 'slice':
assert axis is not None
if axis == 'x':
size = label.shape[0]
elif axis == 'y':
size = label.shape[1]
elif axis == 'z':
size = label.shape[2]
else:
raise ValueError('3d generator flip error')
h1 = []
h2 = []
for i in range(size):
slice_h1 = crop_2d_slice(label,1,axis,i)
slice_h2 = crop_2d_slice(label,2,axis,i)
slice_h1 = swap_axis(slice_h1,'slice',axis)
slice_h2 = swap_axis(slice_h2,'slice',axis)
h1.append(slice_h1)
h2.append(slice_h2)
elif modelname == 'convlstm':
assert axis is not None
h1 = crop_3d(label,1)
h2 = crop_3d(label,2)
h1 = swap_axis(h1,'convlstm',axis)
h2 = swap_axis(h2,'convlstm', axis)
elif modelname == 'Unet':
h1 = crop_3d(label, 1)
h2 = crop_3d(label, 2)
else:
raise ValueError("don't have this model")
return h1,h2
def inference(model,modelname,image,axis=None):
if modelname == 'slice':
assert axis is not None
if axis == 'x':
size = image.shape[0]
elif axis == 'y':
size = image.shape[1]
elif axis == 'z':
size = image.shape[2]
else:
raise ValueError('3d generator flip error')
seq_slice_h1 = []
seq_slice_h2 = []
for i in range(size):
slice_h1 = crop_2d_slice(image,1,axis,i)
slice_h2 = crop_2d_slice(image,2,axis,i)
slice_h1 = swap_axis(slice_h1,'slice',axis)
slice_h2 = swap_axis(slice_h2,'slice',axis)
seq_slice_h1.append(slice_h1)
seq_slice_h2.append(slice_h2)
y_pred_h1 = model.predict(seq_slice_h1)
y_pred_h2 = model.predict(seq_slice_h2)
h1 = []
h2 = []
for i in range(size):
h1.append(swap_axis(y_pred_h1[i],'slice',axis))
h2.append(swap_axis(y_pred_h2[i],'slice',axis))
elif modelname == 'convlstm':
assert axis is not None
h1 = crop_3d(image,1)
h2 = crop_3d(image,2)
h1 = swap_axis(h1,'convlstm',axis)
h2 = swap_axis(h2,'convlstm', axis)
h1 = model.predict([h1])[0]
h2 = model.predict([h2])[0]
print(h1.shape)
elif modelname == 'Unet':
h1 = crop_3d(image, 1)
h2 = crop_3d(image, 2)
h1 = model.predict_on_batch(np.array([h1]))[0]
h2 = model.predict_on_batch(np.array([h2]))[0]
else:
raise ValueError("don't have this model")
return np.array(h1),np.array(h2)