|
a |
|
b/CNN_ECG.py |
|
|
1 |
import numpy as np |
|
|
2 |
import pandas as pd |
|
|
3 |
import math |
|
|
4 |
from keras.models import Sequential |
|
|
5 |
from keras.layers import Dense, LSTM, Dropout, Conv1D, Conv2D, MaxPooling2D, Flatten |
|
|
6 |
from keras.callbacks import ModelCheckpoint |
|
|
7 |
from sklearn.preprocessing import MinMaxScaler |
|
|
8 |
from sklearn.metrics import mean_squared_error |
|
|
9 |
import scipy.io as sio |
|
|
10 |
from os import listdir |
|
|
11 |
from os.path import isfile, join |
|
|
12 |
import keras |
|
|
13 |
from sklearn.metrics import accuracy_score |
|
|
14 |
from keras import backend as K |
|
|
15 |
import sys |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
K.set_image_data_format('channels_last') #For problems with ordering |
|
|
19 |
|
|
|
20 |
number_of_classes = 4 |
|
|
21 |
|
|
|
22 |
def change(x): |
|
|
23 |
return np.argmax(x, axis=1) |
|
|
24 |
|
|
|
25 |
if sys.argv[1] == 'cinc': |
|
|
26 |
#Loading of .mat files from training directory. Only 9000 time steps from every ECG file is loaded |
|
|
27 |
mypath = 'training2017/' |
|
|
28 |
onlyfiles = [f for f in listdir(mypath) if (isfile(join(mypath, f)) and f[0] == 'A')] |
|
|
29 |
bats = [f for f in onlyfiles if f[7] == 'm'] |
|
|
30 |
mats = [f for f in bats if (np.shape(sio.loadmat(mypath + f)['val'])[1] >= 9000)] #Choic of only 9k time steps |
|
|
31 |
if not mats: |
|
|
32 |
raise ValueError("No valid .mat files found with at least 9000 time steps.") |
|
|
33 |
check = np.shape(sio.loadmat(mypath + mats[0])['val'])[1] |
|
|
34 |
X = np.zeros((len(mats), check)) |
|
|
35 |
for i, mat in enumerate(mats): |
|
|
36 |
X[i, :] = sio.loadmat(join(mypath, mat))['val'][0, :9000] |
|
|
37 |
|
|
|
38 |
#Transformation from literals (Noisy, Arithm, Other, Normal) |
|
|
39 |
target_train = np.zeros((len(mats), 1)) |
|
|
40 |
Train_data = pd.read_csv(mypath + 'REFERENCE.csv', sep=',', header=None, names=None) |
|
|
41 |
for i in range(len(mats)): |
|
|
42 |
if Train_data.loc[Train_data[0] == mats[i][:6], 1].values == 'N': |
|
|
43 |
target_train[i] = 0 |
|
|
44 |
elif Train_data.loc[Train_data[0] == mats[i][:6], 1].values == 'A': |
|
|
45 |
target_train[i] = 1 |
|
|
46 |
elif Train_data.loc[Train_data[0] == mats[i][:6], 1].values == 'O': |
|
|
47 |
target_train[i] = 2 |
|
|
48 |
else: |
|
|
49 |
target_train[i] = 3 |
|
|
50 |
|
|
|
51 |
'''Label_set = np.zeros((len(mats), number_of_classes)) |
|
|
52 |
for i in range(np.shape(target_train)[0]): |
|
|
53 |
dummy = np.zeros((number_of_classes)) |
|
|
54 |
dummy[int(target_train[i])] = 1 |
|
|
55 |
Label_set[i, :] = dummy''' |
|
|
56 |
Label_set = np.eye(number_of_classes)[target_train.astype(int)] |
|
|
57 |
|
|
|
58 |
elif sys.argv[1] == 'mit': |
|
|
59 |
print('In proces...') |
|
|
60 |
sys.exit() |
|
|
61 |
|
|
|
62 |
#X = np.abs(numpy.fft.fft(X)) #some stuff |
|
|
63 |
|
|
|
64 |
# Normalization part |
|
|
65 |
#scaler = MinMaxScaler(feature_range=(0, 1)) |
|
|
66 |
#X = scaler.fit_transform(X) |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
train_len = 0.8 #Choice of training size |
|
|
70 |
X_train = X[:int(train_len*len(mats)), :] |
|
|
71 |
Y_train = Label_set[:int(train_len*len(mats)), :] |
|
|
72 |
X_val = X[int(train_len*len(mats)):, :] |
|
|
73 |
Y_val = Label_set[int(train_len*len(mats)):, :] |
|
|
74 |
|
|
|
75 |
# reshape input to be [samples, tensor shape (30 x 300)] |
|
|
76 |
n = 20 |
|
|
77 |
m = 450 |
|
|
78 |
c = 1 #number of channels |
|
|
79 |
|
|
|
80 |
X_train = numpy.reshape(X_train, (X_train.shape[0], n, m, c)) |
|
|
81 |
X_val = numpy.reshape(X_val, (X_val.shape[0], n, m, c)) |
|
|
82 |
image_size = (n, m, c) |
|
|
83 |
|
|
|
84 |
# create and fit the CNN network |
|
|
85 |
|
|
|
86 |
batch_size = 32 |
|
|
87 |
model = Sequential() |
|
|
88 |
#model.load_weights('my_model_weights.h5') |
|
|
89 |
#64 conv |
|
|
90 |
model.add(Conv2D(64, (3, 3), activation='relu', input_shape=image_size, padding='same')) |
|
|
91 |
model.add(Conv2D(64, (3, 3), activation='relu', padding='same')) |
|
|
92 |
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) |
|
|
93 |
|
|
|
94 |
#128 conv |
|
|
95 |
model.add(Conv2D(128, (3, 3), activation='relu', padding='same' )) |
|
|
96 |
model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) |
|
|
97 |
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) |
|
|
98 |
|
|
|
99 |
# #256 conv |
|
|
100 |
model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) |
|
|
101 |
model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) |
|
|
102 |
model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) |
|
|
103 |
model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) |
|
|
104 |
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) |
|
|
105 |
|
|
|
106 |
# #512 conv |
|
|
107 |
# model.add(Conv2D(512, (3, 3), activation='relu')) |
|
|
108 |
# model.add(Conv2D(512, (3, 3), activation='relu')) |
|
|
109 |
# model.add(Conv2D(512, (3, 3), activation='relu')) |
|
|
110 |
# model.add(Conv2D(512, (3, 3), activation='relu')) |
|
|
111 |
# model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) |
|
|
112 |
# model.add(Conv2D(512, (3, 3), activation='relu')) |
|
|
113 |
# model.add(Conv2D(512, (3, 3), activation='relu')) |
|
|
114 |
# model.add(Conv2D(512, (3, 3), activation='relu')) |
|
|
115 |
# model.add(Conv2D(512, (3, 3), activation='relu')) |
|
|
116 |
# model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) |
|
|
117 |
|
|
|
118 |
#Dense part |
|
|
119 |
model.add(Flatten()) |
|
|
120 |
model.add(Dense(4096, activation='relu')) |
|
|
121 |
model.add(Dropout(0.5)) |
|
|
122 |
model.add(Dense(4096, activation='relu')) |
|
|
123 |
model.add(Dropout(0.5)) |
|
|
124 |
model.add(Dense(1000, activation='relu')) |
|
|
125 |
model.add(Dense(number_of_classes, activation='softmax')) |
|
|
126 |
|
|
|
127 |
#Callbacks and accuracy calculation |
|
|
128 |
#early_stopping = keras.callbacks.EarlyStopping(monitor='val_acc', min_delta=0, patience=50, verbose=1, mode='auto') |
|
|
129 |
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) |
|
|
130 |
checkpointer = ModelCheckpoint(filepath="Keras_models/weights.{epoch:02d}-{val_acc:.2f}.hdf5", monitor='val_loss', save_weights_only=True, period=1, verbose=1, save_best_only=False) |
|
|
131 |
model.fit(X_train, Y_train, epochs=250, batch_size=batch_size, validation_data=(X_val, Y_val), verbose=2, shuffle=False, callbacks=[checkpointer]) |
|
|
132 |
model.save('Keras_models/my_model_' + str(i) + '_' + str(j) + '_' + str() + '.h5') |
|
|
133 |
predictions = model.predict(X_val) |
|
|
134 |
score = accuracy_score(change(Y_val), change(predictions)) |
|
|
135 |
print(score) |
|
|
136 |
# Data[i - starti, j - starti] = str(format(score, '.5f')) |
|
|
137 |
# Output = pd.DataFrame(Data) |
|
|
138 |
# name = str(batch_size) + '.csv' |
|
|
139 |
# Output.to_csv(path_or_buf='Keras_models/' + name, index=None, header=None) |