-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataLoad.py
More file actions
200 lines (129 loc) · 6.16 KB
/
DataLoad.py
File metadata and controls
200 lines (129 loc) · 6.16 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
import SimpleITK as sitk
import numpy as np
import Show
def load3D(image, mask, numberOfImages=1000, epochs=10, batch_size=1):
img1 = sitk.ReadImage(image)
img2 = sitk.ReadImage(mask)
normalizeFilter = sitk.NormalizeImageFilter()
size_x = img1.GetWidth()
size_y = img1.GetHeight()
size_z = img1.GetDepth()
MirrorPaddingFilter = sitk.MirrorPadImageFilter()
MirrorPaddingFilter.SetPadLowerBound([5,5,5])
MirrorPaddingFilter.SetPadUpperBound([5,5,5])
PaddedImage = MirrorPaddingFilter.Execute(normalizeFilter.Execute(img1))
for i in range(0,epochs,1):
counter = 0
ImageTrain = None
MaskTrain = None
for x in range(0, size_x-121, 122):
for y in range(0, size_y-121, 122):
for z in range(0, size_z-121, 122):
image = sitk.RegionOfInterest(PaddedImage,(132,132,132),(x,y,z))
mask = sitk.RegionOfInterest(img2,(122,122,122),(x,y,z))
#sitk.WriteImage(image,'Train/Image'+str(counter)+".mhd")
#sitk.WriteImage(mask,'Train/Mask'+str(counter)+".mhd")
image = sitk.GetArrayFromImage(image)
MaskSeg = sitk.GetArrayFromImage(mask)
voxelNotzero = np.count_nonzero(MaskSeg)
imageVoxelNotzero = np.count_nonzero(image)
if ((voxelNotzero>50 or counter%9==0) and imageVoxelNotzero > 500):
ImageTrain = np.reshape(image,(1,132,132,132,1))
MaskTrain = np.reshape(MaskSeg,(1,122,122,122,1))
counter = counter +1
if ((counter<=numberOfImages)) :
yield (ImageTrain,MaskTrain)
def load3D_XY(image, mask, filterData=False, border=0):
img1 = sitk.ReadImage(image)
img2 = sitk.ReadImage(mask)
normalizeFilter = sitk.NormalizeImageFilter()
size_x = img1.GetWidth()
size_y = img1.GetHeight()
size_z = img1.GetDepth()
#size_x = size_x if size_x <= 400 else 400
#size_y = size_y if size_y <= 400 else 400
#size_z = size_z if size_z <= 600 else 600
Data_X = []
Data_Y = []
MirrorPaddingFilter = sitk.MirrorPadImageFilter()
MirrorPaddingFilter.SetPadLowerBound([5,5,5])
MirrorPaddingFilter.SetPadUpperBound([5,5,5])
PaddedImage = MirrorPaddingFilter.Execute(normalizeFilter.Execute(img1))
img1=0
counter = 0
for x in range(border, size_x-121-border, 122):
for y in range(border, size_y-121-border, 122):
for z in range(border, size_z-121-border, 122):
image = sitk.RegionOfInterest(PaddedImage,(132,132,132),(x,y,z))
mask = sitk.RegionOfInterest(img2,(122,122,122),(x,y,z))
image = sitk.GetArrayFromImage(image)
MaskSeg = sitk.GetArrayFromImage(mask)
ImageTrain = np.reshape(image,(1,132,132,132,1))
MaskTrain = np.reshape(MaskSeg,(1,122,122,122,1))
voxelNotzero = np.count_nonzero(MaskSeg)
imageVoxelNotzero = np.count_nonzero(image)
if filterData:
if ((voxelNotzero>50 or counter%9==0) and imageVoxelNotzero > 100000):
ImageTrain = np.reshape(image,(1,132,132,132,1))
MaskTrain = np.reshape(MaskSeg,(1,122,122,122,1))
Data_X.append(ImageTrain)
Data_Y.append(MaskTrain)
counter = counter +1
else:
ImageTrain = np.reshape(image,(1,132,132,132,1))
MaskTrain = np.reshape(MaskSeg,(1,122,122,122,1))
Data_X.append(ImageTrain)
Data_Y.append(MaskTrain)
counter = counter +1
Data_X = np.reshape(Data_X,(counter,132,132,132,1))
Data_Y = np.reshape(Data_Y,(counter,122,122,122,1))
return Data_X, Data_Y
def load3D_file_generator(PathsTrain, PathsLabel, epochs=10, batch_size=1):
for i in range(0,epochs,1):
counter = 0
ImageTrain = []
MaskTrain = []
for x in range(0,len(PathsTrain),1):
image = sitk.ReadImage(PathsTrain[x])
mask = sitk.ReadImage(PathsLabel[x])
image = sitk.GetArrayFromImage(image)
MaskSeg = sitk.GetArrayFromImage(mask)
ImageTrain.append(np.reshape(image,(1,132,132,132,1)))
MaskTrain.append(np.reshape(MaskSeg,(1,122,122,122,1)))
if x%batch_size == 0:
Data_X = np.reshape(ImageTrain,(len(ImageTrain),132,132,132,1))
Data_Y = np.reshape(MaskTrain,(len(MaskTrain),122,122,122,1))
yield (Data_X,Data_Y.astype(np.float32))
ImageTrain = []
MaskTrain = []
if len(ImageTrain) > 0:
Data_X = np.reshape(ImageTrain,(len(ImageTrain),132,132,132,1))
Data_Y = np.reshape(MaskTrain,(len(MaskTrain),122,122,122,1))
yield (Data_X,Data_Y.astype(np.float32))
def getPaths(dirTrain, dirLabel, TrainDatasize):
import os
chunksTrain = [x[0] for x in os.walk(dirTrain)]
chunksTrain.remove(dirTrain)
chunksLabel = [x[0] for x in os.walk(dirLabel)]
chunksLabel.remove(dirLabel)
TrainX =[]
TrainY =[]
ValX = []
ValY = []
for i in range(len(chunksTrain)):
import os
for file in os.listdir(chunksTrain[i]):
if file.endswith(".mhd"):
if i<TrainDatasize:
TrainX.append(chunksTrain[i]+"\\"+file)
else:
ValX.append(chunksTrain[i]+"\\"+file)
for i in range(len(chunksLabel)):
import os
for file in os.listdir(chunksLabel[i]):
if file.endswith(".mhd"):
if i<TrainDatasize:
TrainY.append(chunksLabel[i]+"\\"+file)
else:
ValY.append(chunksLabel[i]+"\\"+file)
return TrainX, TrainY, ValX, ValY