|
a |
|
b/Dense_ECG.py |
|
|
1 |
from sklearn.metrics import confusion_matrix |
|
|
2 |
from keras.callbacks import ModelCheckpoint |
|
|
3 |
from biosppy.signals import ecg |
|
|
4 |
from sklearn.model_selection import StratifiedKFold |
|
|
5 |
from sklearn.metrics import accuracy_score |
|
|
6 |
from sklearn.preprocessing import MinMaxScaler, RobustScaler |
|
|
7 |
import pandas as pd |
|
|
8 |
import scipy.io as sio |
|
|
9 |
from os import listdir |
|
|
10 |
from os.path import isfile, join |
|
|
11 |
import numpy as np |
|
|
12 |
from keras.models import Sequential |
|
|
13 |
from keras.layers import Dense, Activation, Dropout, Conv2D, MaxPooling2D, Flatten, LSTM |
|
|
14 |
import keras |
|
|
15 |
from keras import regularizers |
|
|
16 |
from matplotlib import pyplot as plt |
|
|
17 |
|
|
|
18 |
np.random.seed(7) |
|
|
19 |
|
|
|
20 |
number_of_classes = 4 |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
def change(x): # Для получения чисел от 0 до 3 |
|
|
24 |
answer = np.zeros((np.shape(x)[0])) |
|
|
25 |
for i in range(np.shape(x)[0]): |
|
|
26 |
max_value = max(x[i, :]) |
|
|
27 |
max_index = list(x[i, :]).index(max_value) |
|
|
28 |
answer[i] = max_index |
|
|
29 |
return answer.astype(np.int) |
|
|
30 |
|
|
|
31 |
|
|
|
32 |
mypath = 'training2017/' |
|
|
33 |
onlyfiles = [f for f in listdir(mypath) if (isfile(join(mypath, f)) and f[0] == 'A')] |
|
|
34 |
bats = [f for f in onlyfiles if f[7] == 'm'] |
|
|
35 |
check = 3000 |
|
|
36 |
mats = [f for f in bats if (np.shape(sio.loadmat(mypath + f)['val'])[1] >= check)] |
|
|
37 |
size = len(mats) |
|
|
38 |
print('Training size is ', len(mats)) |
|
|
39 |
X = np.zeros((len(mats), check)) |
|
|
40 |
for i in range(len(mats)): |
|
|
41 |
X[i, :] = sio.loadmat(mypath + mats[i])['val'][0, :check] |
|
|
42 |
|
|
|
43 |
target_train = np.zeros((len(mats), 1)) |
|
|
44 |
Train_data = pd.read_csv(mypath + 'REFERENCE.csv', sep=',', header=None, names=None) |
|
|
45 |
for i in range(len(mats)): |
|
|
46 |
if Train_data.loc[Train_data[0] == mats[i][:6], 1].values == 'N': |
|
|
47 |
target_train[i] = 0 |
|
|
48 |
elif Train_data.loc[Train_data[0] == mats[i][:6], 1].values == 'A': |
|
|
49 |
target_train[i] = 1 |
|
|
50 |
elif Train_data.loc[Train_data[0] == mats[i][:6], 1].values == 'O': |
|
|
51 |
target_train[i] = 2 |
|
|
52 |
else: |
|
|
53 |
target_train[i] = 3 |
|
|
54 |
|
|
|
55 |
Label_set = np.zeros((len(mats), number_of_classes)) |
|
|
56 |
for i in range(np.shape(target_train)[0]): |
|
|
57 |
dummy = np.zeros((number_of_classes)) |
|
|
58 |
dummy[int(target_train[i])] = 1 |
|
|
59 |
Label_set[i, :] = dummy |
|
|
60 |
|
|
|
61 |
inputs = 60 # Previus value for 9k check is 95 |
|
|
62 |
X_new = np.zeros((size, inputs)) |
|
|
63 |
for i in range(size): |
|
|
64 |
out = ecg.christov_segmenter(signal=X[i, :], sampling_rate=300.) |
|
|
65 |
A = np.hstack((0, out[0][:len(out[0]) - 1])) |
|
|
66 |
B = out[0] |
|
|
67 |
dummy = np.lib.pad(B - A, (0, inputs - len(B)), 'constant', constant_values=(0)) |
|
|
68 |
X_new[i, :] = dummy |
|
|
69 |
|
|
|
70 |
print('All is OK') |
|
|
71 |
X = X_new |
|
|
72 |
X = (X - X.mean()) / (X.std()) |
|
|
73 |
Label_set = Label_set[:size, :] |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
# X_new = np.zeros((size, check)) |
|
|
77 |
# Label_new = np.zeros((size, 4)) |
|
|
78 |
# stop = 0 |
|
|
79 |
# j = -1 |
|
|
80 |
# for i in range(np.shape(X)[0]): |
|
|
81 |
# if (stop == 1000) and (np.array_equal(Label_set[i, :], [1, 0, 0, 0])): |
|
|
82 |
# continue |
|
|
83 |
# else: |
|
|
84 |
# j += 1 |
|
|
85 |
# if j != size: |
|
|
86 |
# if np.array_equal(Label_set[i, :], [1, 0, 0, 0]): |
|
|
87 |
# stop += 1 |
|
|
88 |
# X_new[j, :] = X[i, :] |
|
|
89 |
# Label_new[j, :] = Label_set[i, :] |
|
|
90 |
# else: |
|
|
91 |
# break |
|
|
92 |
# |
|
|
93 |
# X = X_new |
|
|
94 |
# Label_set = Label_new[:, :] |
|
|
95 |
|
|
|
96 |
# scaler = MinMaxScaler(feature_range=(0, 1)) |
|
|
97 |
# X = scaler.fit_transform(X) |
|
|
98 |
|
|
|
99 |
|
|
|
100 |
def train_and_evaluate__model(model, X_train, Y_train, X_val, Y_val, i): |
|
|
101 |
checkpointer = ModelCheckpoint(filepath='Dense_models/Best_model of ' + str(i) + '.h5', monitor='val_acc', |
|
|
102 |
verbose=1, save_best_only=True) |
|
|
103 |
# early_stopping = keras.callbacks.EarlyStopping(monitor='val_acc', min_delta=0, patience=50, verbose=1, mode='auto') |
|
|
104 |
hist = model.fit(X_train, Y_train, epochs=500, batch_size=256, validation_data=(X_val, Y_val), verbose=2, |
|
|
105 |
shuffle=True, callbacks=[checkpointer]) |
|
|
106 |
pd.DataFrame(hist.history).to_csv(path_or_buf='Dense_models/History ' + str(i) + '.csv') |
|
|
107 |
model.save('my_model ' + str(i) + '.h5') |
|
|
108 |
predictions = model.predict(X_val) |
|
|
109 |
score = accuracy_score(change(Y_val), change(predictions)) |
|
|
110 |
print(score) |
|
|
111 |
df = pd.DataFrame(change(predictions)) |
|
|
112 |
df.to_csv(path_or_buf='Dense_models/Preds_' + str(format(score, '.4f')) + '__' + str(i) + '.csv', index=None, |
|
|
113 |
header=None) |
|
|
114 |
model.save('Dense_models/' + str(format(score, '.4f')) + '__' + str(i) + '_my_model.h5') |
|
|
115 |
pd.DataFrame(confusion_matrix(change(Y_val), change(predictions))).to_csv( |
|
|
116 |
path_or_buf='Dense_models/Result_Conf' + str(format(score, '.4f')) + '__' + str(i) + '.csv', index=None, |
|
|
117 |
header=None) |
|
|
118 |
|
|
|
119 |
|
|
|
120 |
def create_model(): |
|
|
121 |
model = Sequential() |
|
|
122 |
model.add(Dense(1024, input_shape=(inputs,), kernel_initializer='normal', activation='relu')) |
|
|
123 |
model.add(Dense(1024, kernel_initializer='normal', activation='relu')) |
|
|
124 |
model.add(Dropout(0.5)) |
|
|
125 |
model.add(Dense(1024, kernel_initializer='normal', activation='relu')) |
|
|
126 |
model.add(Dense(1024, kernel_initializer='normal', activation='relu')) |
|
|
127 |
model.add(Dropout(0.5)) |
|
|
128 |
model.add(Dense(512, kernel_initializer='normal', activation='relu')) |
|
|
129 |
model.add(Dense(512, kernel_initializer='normal', activation='relu')) |
|
|
130 |
model.add(Dense(512, kernel_initializer='normal', activation='relu')) |
|
|
131 |
model.add(Dropout(0.5)) |
|
|
132 |
model.add(Dense(256, kernel_initializer='normal', activation='relu')) |
|
|
133 |
model.add(Dense(256, kernel_initializer='normal', activation='relu')) |
|
|
134 |
model.add(Dropout(0.5)) |
|
|
135 |
model.add(Dense(128, kernel_initializer='normal', activation='relu')) |
|
|
136 |
model.add(Dense(128, kernel_initializer='normal', activation='relu')) |
|
|
137 |
model.add(Dropout(0.5)) |
|
|
138 |
model.add(Dense(64, kernel_initializer='normal', activation='relu')) |
|
|
139 |
model.add(Dense(64, kernel_initializer='normal', activation='relu')) |
|
|
140 |
model.add(Dropout(0.5)) |
|
|
141 |
model.add(Dense(number_of_classes, kernel_initializer='normal', activation='softmax')) |
|
|
142 |
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) |
|
|
143 |
return model |
|
|
144 |
|
|
|
145 |
|
|
|
146 |
skf = StratifiedKFold(n_splits=10, shuffle=True) |
|
|
147 |
target_train = target_train.reshape(size, ) |
|
|
148 |
# print(skf.get_n_splits(X, target_train)) |
|
|
149 |
# print(skf.split(X, target_train)) |
|
|
150 |
for i, (train_index, test_index) in enumerate(skf.split(X, target_train)): |
|
|
151 |
print("TRAIN:", train_index, "TEST:", test_index) |
|
|
152 |
# train = 0.9 |
|
|
153 |
# print('Training_size is ', int(train*size)) |
|
|
154 |
# print('Validation_size is ', size - int(train*size)) |
|
|
155 |
X_train = X[train_index, :] |
|
|
156 |
Y_train = Label_set[train_index, :] |
|
|
157 |
X_val = X[test_index, :] |
|
|
158 |
Y_val = Label_set[test_index, :] |
|
|
159 |
# X_train = X[:int(train*size), :] |
|
|
160 |
# Y_train = Label_set[:int(train*size), :] |
|
|
161 |
# X_val = X[int(train*size):, :] |
|
|
162 |
# Y_val = Label_set[int(train*size):, :] |
|
|
163 |
# model = None |
|
|
164 |
model = create_model() |
|
|
165 |
train_and_evaluate__model(model, X_train, Y_train, X_val, Y_val, i) |