forked from ismorphism/DeepECG
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain_conv1D.py
More file actions
86 lines (63 loc) · 2.47 KB
/
train_conv1D.py
File metadata and controls
86 lines (63 loc) · 2.47 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
import os
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import gc
from keras.callbacks import ModelCheckpoint
from sklearn.metrics import confusion_matrix, accuracy_score
from models.Conv1d import *
from utils_data import *
from utils_ml import *
np.random.seed(7)
# project parameters
DATA_PATH = 'data/training2017/'
# lower bound of the length of the signal
LB_LEN_MAT = 100
# upper bound of the length of the signal
UB_LEN_MAT = 10100
LABELS = ["N", "A", "O"]
n_classes = len(LABELS) + 1
if __name__ == "__main__":
# this helps a lot when debugging
print(os.getcwd())
X, Y = load_cinc_data(DATA_PATH, LB_LEN_MAT, LABELS)
# data preprocessing
X = duplicate_padding(X, UB_LEN_MAT)
X = (X - X.mean()) / (X.std())
X = np.expand_dims(X, axis=2)
# shuffle the data
values = [i for i in range(len(X))]
permutations = np.random.permutation(values)
X = X[permutations, :]
Y = Y[permutations, :]
# train test split
train_test_ratio = 0.9
n_sample = X.shape[0]
X_train = X[:int(train_test_ratio * n_sample), :]
Y_train = Y[:int(train_test_ratio * n_sample), :]
X_test = X[int(train_test_ratio * n_sample):, :]
Y_test = Y[int(train_test_ratio * n_sample):, :]
# load the model and train it
model = conv1d(UB_LEN_MAT)
checkpointer = ModelCheckpoint(filepath='./trained_models/Best_model.h5',
monitor='val_acc',
verbose=1,
save_best_only=True)
print("x shape", X_train.shape)
print("y shape", Y_train.shape)
hist = model.fit(X_train, Y_train,
validation_data=(X_test, Y_test),
batch_size=275,
epochs=500,
verbose=1,
shuffle=True,
callbacks=[checkpointer])
# evaluation
predictions = model.predict(X_test)
score = accuracy_score(onehots2numbers(Y_test), predictions.argmax(axis=1))
print('Last epoch\'s validation score is ', score)
df = pd.DataFrame(predictions.argmax(axis=1))
df.to_csv('./trained_models/Preds_' + str(format(score, '.4f')) + '.csv', index=None, header=None)
confusion_matrix = confusion_matrix(onehots2numbers(Y_test), predictions.argmax(axis=1))
df = pd.DataFrame(confusion_matrix)
df.to_csv('./trained_models/Result_Conf' + str(format(score, '.4f')) + '.csv', index=None, header=None)
del model
gc.collect()