-
Notifications
You must be signed in to change notification settings - Fork 36
/
CNN.py
314 lines (269 loc) · 12.7 KB
/
CNN.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
#https://medium.com/@naomi.fridman/install-conda-tensorflow-gpu-and-keras-on-ubuntu-18-04-1b403e740e25 install gpu
#INIZIO codice per allenare la rete sulla cpu
import os
#os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
#os.environ["CUDA_VISIBLE_DEVICES"] = ""
#FINE codice per allenare la rete sulla cpu
import keras
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Conv3D, Dropout, Flatten, BatchNormalization
from keras.callbacks import EarlyStopping
from random import shuffle
import math
#to plot the model
#from keras.utils.vis_utils import plot_model
#from keras.models import load_model
# Returns a compiled model identical to the saved one
#model = load_model('my_model.h5')
PathSpectogramFolder=''
OutputPath=''
OutputPathModels=''
interictalSpectograms=[]
preictalSpectograms=[] #This array contains syntetic data, it's created to have a balance dataset and it's used for training
preictalRealSpectograms=[] #This array containt the real preictal data, it's used for testing
patients = ["01", "02", "05", "19", "21", "23"]
nSeizure=0
def loadParametersFromFile(filePath):
global PathSpectogramFolder
global OutputPath
global OutputPathModels
if(os.path.isfile(filePath)):
with open(filePath, "r") as f:
line=f.readline()
if(line.split(":")[0]=="PathSpectogramFolder"):
PathSpectogramFolder=line.split(":")[1].strip()
line=f.readline()
if(line.split(":")[0]=="OutputPath"):
OutputPath=line.split(":")[1].strip()
line=f.readline()
if(line.split(":")[0]=="OutputPathModels"):
OutputPathModels=line.split(":")[1].strip()
def loadSpectogramData(indexPat):
global interictalSpectograms
global preictalSpectograms
global preictalRealSpectograms
global nSeizure
nFileForSeizure=0
interictalSpectograms=[]
preictalSpectograms=[]
preictalRealSpectograms=[]
f = open(PathSpectogramFolder+'/paz'+patients[indexPat]+'/legendAllData.txt', 'r')
line=f.readline()
while(not "SEIZURE" in line):
line=f.readline()
nSeizure=int(line.split(":")[1].strip())
line=f.readline()
line=f.readline()#legge il numero di spectogrammi. non lo salvo dato che non mi serve
nSpectograms=int(line.strip())
nFileForSeizure=math.ceil(math.ceil(nSpectograms/50)/nSeizure)
line=f.readline()#leggo il percorso del primo file
#Lettura path files Interictal
cont=-1
indFilePathRead=0
while("npy" in line and indFilePathRead<nSeizure*nFileForSeizure):
if(indFilePathRead%nFileForSeizure==0):
interictalSpectograms.append([])
cont=cont+1
interictalSpectograms[cont].append(line.split(' ')[2].rstrip())#.rstrip() remove \n
indFilePathRead=indFilePathRead+1
else:
if(len(line.split(' '))>=3):
interictalSpectograms[cont].append(line.split(' ')[2].rstrip())
indFilePathRead=indFilePathRead+1
line=f.readline()
line=f.readline()#leggo PREICTAL
line=f.readline()#leggo n° spectogram
line=f.readline()#leggo n°seizure(SEIZURE X)
#Lettura path files Preictal
cont=-1
indFilePathRead=0
#while(line and indFilePathRead<nSeizure*nFileForSeizure):
while(line.strip()!=""):
if("SEIZURE" in line):
line=f.readline()#ho letto n°seizure(SEIZURE X) perciò scorro in avanti
if(len(line.split(' '))>=3):
preictalSpectograms.append([])
cont=cont+1
preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
indFilePathRead=indFilePathRead+1
else:
if(len(line.split(' '))>=3):
preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
indFilePathRead=indFilePathRead+1
line=f.readline()
line=f.readline()#leggo REAL_PREICTAL
line=f.readline()#leggo n° spectogram
line=f.readline()#leggo n°seizure(SEIZURE X)
#Lettura path files Real Preictal
cont=-1
while(line):
if("SEIZURE" in line):
line=f.readline()#ho letto n°seizure(SEIZURE X) perciò scorro in avanti
preictalRealSpectograms.append([])
cont=cont+1
preictalRealSpectograms[cont].append(line.split(' ')[2].rstrip())
else:
preictalRealSpectograms[cont].append(line.split(' ')[2].rstrip())
line=f.readline()
f.close()
def createModel():
input_shape=(1, 22, 59, 114)
model = Sequential()
#C1
model.add(Conv3D(16, (22, 5, 5), strides=(1, 2, 2), padding='valid',activation='relu',data_format= "channels_first", input_shape=input_shape))
model.add(keras.layers.MaxPooling3D(pool_size=(1, 2, 2),data_format= "channels_first", padding='same'))
model.add(BatchNormalization())
#C2
model.add(Conv3D(32, (1, 3, 3), strides=(1, 1,1), padding='valid',data_format= "channels_first", activation='relu'))#incertezza se togliere padding
model.add(keras.layers.MaxPooling3D(pool_size=(1,2, 2),data_format= "channels_first", ))
model.add(BatchNormalization())
#C3
model.add(Conv3D(64, (1,3, 3), strides=(1, 1,1), padding='valid',data_format= "channels_first", activation='relu'))#incertezza se togliere padding
model.add(keras.layers.MaxPooling3D(pool_size=(1,2, 2),data_format= "channels_first", ))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(256, activation='sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
opt_adam = keras.optimizers.Adam(lr=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='categorical_crossentropy', optimizer=opt_adam, metrics=['accuracy'])
return model
def getFilesPathWithoutSeizure(indexSeizure, indexPat):
filesPath=[]
for i in range(0, nSeizure):
if(i!=indexSeizure):
filesPath.extend(interictalSpectograms[i])
filesPath.extend(preictalSpectograms[i])
shuffle(filesPath)
return filesPath
def generate_arrays_for_training(indexPat, paths, start=0, end=100):
while True:
from_=int(len(paths)/100*start)
to_=int(len(paths)/100*end)
for i in range(from_, int(to_)):
f=paths[i]
x = np.load(PathSpectogramFolder+f)
x=np.array([x])
x=x.swapaxes(0,1)
if('P' in f):
y = np.repeat([[0,1]],x.shape[0], axis=0)
else:
y =np.repeat([[1,0]],x.shape[0], axis=0)
yield(x,y)
def generate_arrays_for_predict(indexPat, paths, start=0, end=100):
while True:
from_=int(len(paths)/100*start)
to_=int(len(paths)/100*end)
for i in range(from_, int(to_)):
f=paths[i]
x = np.load(PathSpectogramFolder+f)
x=np.array([x])
x=x.swapaxes(0,1)
yield(x)
class EarlyStoppingByLossVal(keras.callbacks.Callback):
def __init__(self, monitor='val_loss', value=0.00001, verbose=0, lower=True):
super(keras.callbacks.Callback, self).__init__()
self.monitor = monitor
self.value = value
self.verbose = verbose
self.lower=lower
def on_epoch_end(self, epoch, logs={}):
current = logs.get(self.monitor)
if self.lower:
if current < self.value:
if self.verbose > 0:
print("Epoch %05d: early stopping THR" % epoch)
self.model.stop_training = True
else:
if current > self.value:
if self.verbose > 0:
print("Epoch %05d: early stopping THR" % epoch)
self.model.stop_training = True
def main():
print("START")
loadParametersFromFile("PARAMETERS_CNN.txt")
if not os.path.exists(OutputPathModels):
os.makedirs(OutputPathModels)
#callback=EarlyStopping(monitor='val_acc', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None)
callback=EarlyStoppingByLossVal(monitor='val_acc', value=0.975, verbose=1, lower=False)
print("Parameters loaded")
for indexPat in range(0, len(patients)):
print('Patient '+patients[indexPat])
if not os.path.exists(OutputPathModels+"ModelPat"+patients[indexPat]+"/"):
os.makedirs(OutputPathModels+"ModelPat"+patients[indexPat]+"/")
loadSpectogramData(indexPat)
print('Spectograms data loaded')
result='Patient '+patients[indexPat]+'\n'
result='Out Seizure, True Positive, False Positive, False negative, Second of Inter in Test, Sensitivity, FPR \n'
for i in range(0, nSeizure):
print('SEIZURE OUT: '+str(i+1))
print('Training start')
model = createModel()
filesPath=getFilesPathWithoutSeizure(i, indexPat)
model.fit_generator(generate_arrays_for_training(indexPat, filesPath, end=75), #end=75),#It take the first 75%
validation_data=generate_arrays_for_training(indexPat, filesPath, start=75),#start=75), #It take the last 25%
#steps_per_epoch=10000, epochs=10)
steps_per_epoch=int((len(filesPath)-int(len(filesPath)/100*25))),#*25),
validation_steps=int((len(filesPath)-int(len(filesPath)/100*75))),#*75),
verbose=2,
epochs=300, max_queue_size=2, shuffle=True, callbacks=[callback])# 100 epochs è meglio #aggiungere criterio di stop in base accuratezza
print('Training end')
print('Testing start')
filesPath=interictalSpectograms[i]
interPrediction=model.predict_generator(generate_arrays_for_predict(indexPat, filesPath), max_queue_size=4, steps=len(filesPath))
filesPath=preictalRealSpectograms[i]
preictPrediction=model.predict_generator(generate_arrays_for_predict(indexPat, filesPath), max_queue_size=4, steps=len(filesPath))
print('Testing end')
# Creates a HDF5 file
model.save(OutputPathModels+"ModelPat"+patients[indexPat]+"/"+'ModelOutSeizure'+str(i+1)+'.h5')
print("Model saved")
#to plot the model
#plot_model(model, to_file="CNNModel", show_shapes=True, show_layer_names=True)
if not os.path.exists(OutputPathModels+"OutputTest"+"/"):
os.makedirs(OutputPathModels+"OutputTest"+"/")
np.savetxt(OutputPathModels+"OutputTest"+"/"+"Int_"+patients[indexPat]+"_"+str(i+1)+".csv", interPrediction, delimiter=",")
np.savetxt(OutputPathModels+"OutputTest"+"/"+"Pre_"+patients[indexPat]+"_"+str(i+1)+".csv", preictPrediction, delimiter=",")
secondsInterictalInTest=len(interictalSpectograms[i])*50*30#50 spectograms for file, 30 seconds for each spectogram
acc=0#accumulator
fp=0
tp=0
fn=0
lastTenResult=list()
for el in interPrediction:
if(el[1]>0.5):
acc=acc+1
lastTenResult.append(1)
else:
lastTenResult.append(0)
if(len(lastTenResult)>10):
acc=acc-lastTenResult.pop(0)
if(acc>=8):
fp=fp+1
lastTenResult=list()
acc=0
lastTenResult=list()
for el in preictPrediction:
if(el[1]>0.5):
acc=acc+1
lastTenResult.append(1)
else:
lastTenResult.append(0)
if(len(lastTenResult)>10):
acc=acc-lastTenResult.pop(0)
if(acc>=8):
tp=tp+1
else:
if(len(lastTenResult)==10):
fn=fn+1
sensitivity=tp/(tp+fn)
FPR=fp/(secondsInterictalInTest/(60*60))
result=result+str(i+1)+','+str(tp)+','+str(fp)+','+str(fn)+','+str(secondsInterictalInTest)+','
result=result+str(sensitivity)+','+str(FPR)+'\n'
print('True Positive, False Positive, False negative, Second of Inter in Test, Sensitivity, FPR')
print(str(tp)+','+str(fp)+','+str(fn)+','+str(secondsInterictalInTest)+','+str(sensitivity)+','+str(FPR))
with open(OutputPath, "a+") as myfile:
myfile.write(result)
if __name__ == '__main__':
main()