|
a |
|
b/code/baseline_ptbdb.py |
|
|
1 |
import pandas as pd |
|
|
2 |
import numpy as np |
|
|
3 |
|
|
|
4 |
from keras import optimizers, losses, activations, models |
|
|
5 |
from keras.callbacks import ModelCheckpoint, EarlyStopping, LearningRateScheduler, ReduceLROnPlateau |
|
|
6 |
from keras.layers import Dense, Input, Dropout, Convolution1D, MaxPool1D, GlobalMaxPool1D, GlobalAveragePooling1D, \ |
|
|
7 |
concatenate |
|
|
8 |
from sklearn.metrics import accuracy_score, f1_score |
|
|
9 |
from sklearn.model_selection import train_test_split |
|
|
10 |
|
|
|
11 |
df_1 = pd.read_csv("../input/ptbdb_normal.csv", header=None) |
|
|
12 |
df_2 = pd.read_csv("../input/ptbdb_abnormal.csv", header=None) |
|
|
13 |
df = pd.concat([df_1, df_2]) |
|
|
14 |
|
|
|
15 |
df_train, df_test = train_test_split(df, test_size=0.2, random_state=1337, stratify=df[187]) |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
Y = np.array(df_train[187].values).astype(np.int8) |
|
|
19 |
X = np.array(df_train[list(range(187))].values)[..., np.newaxis] |
|
|
20 |
|
|
|
21 |
Y_test = np.array(df_test[187].values).astype(np.int8) |
|
|
22 |
X_test = np.array(df_test[list(range(187))].values)[..., np.newaxis] |
|
|
23 |
|
|
|
24 |
|
|
|
25 |
def get_model(): |
|
|
26 |
nclass = 1 |
|
|
27 |
inp = Input(shape=(187, 1)) |
|
|
28 |
img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(inp) |
|
|
29 |
img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(img_1) |
|
|
30 |
img_1 = MaxPool1D(pool_size=2)(img_1) |
|
|
31 |
img_1 = Dropout(rate=0.1)(img_1) |
|
|
32 |
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) |
|
|
33 |
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) |
|
|
34 |
img_1 = MaxPool1D(pool_size=2)(img_1) |
|
|
35 |
img_1 = Dropout(rate=0.1)(img_1) |
|
|
36 |
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) |
|
|
37 |
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) |
|
|
38 |
img_1 = MaxPool1D(pool_size=2)(img_1) |
|
|
39 |
img_1 = Dropout(rate=0.1)(img_1) |
|
|
40 |
img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1) |
|
|
41 |
img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1) |
|
|
42 |
img_1 = GlobalMaxPool1D()(img_1) |
|
|
43 |
img_1 = Dropout(rate=0.2)(img_1) |
|
|
44 |
|
|
|
45 |
dense_1 = Dense(64, activation=activations.relu, name="dense_1")(img_1) |
|
|
46 |
dense_1 = Dense(64, activation=activations.relu, name="dense_2")(dense_1) |
|
|
47 |
dense_1 = Dense(nclass, activation=activations.sigmoid, name="dense_3_ptbdb")(dense_1) |
|
|
48 |
|
|
|
49 |
model = models.Model(inputs=inp, outputs=dense_1) |
|
|
50 |
opt = optimizers.Adam(0.001) |
|
|
51 |
|
|
|
52 |
model.compile(optimizer=opt, loss=losses.binary_crossentropy, metrics=['acc']) |
|
|
53 |
model.summary() |
|
|
54 |
return model |
|
|
55 |
|
|
|
56 |
model = get_model() |
|
|
57 |
file_path = "baseline_cnn_ptbdb.h5" |
|
|
58 |
checkpoint = ModelCheckpoint(file_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max') |
|
|
59 |
early = EarlyStopping(monitor="val_acc", mode="max", patience=5, verbose=1) |
|
|
60 |
redonplat = ReduceLROnPlateau(monitor="val_acc", mode="max", patience=3, verbose=2) |
|
|
61 |
callbacks_list = [checkpoint, early, redonplat] # early |
|
|
62 |
|
|
|
63 |
model.fit(X, Y, epochs=1000, verbose=2, callbacks=callbacks_list, validation_split=0.1) |
|
|
64 |
model.load_weights(file_path) |
|
|
65 |
|
|
|
66 |
pred_test = model.predict(X_test) |
|
|
67 |
pred_test = (pred_test>0.5).astype(np.int8) |
|
|
68 |
|
|
|
69 |
f1 = f1_score(Y_test, pred_test) |
|
|
70 |
|
|
|
71 |
print("Test f1 score : %s "% f1) |
|
|
72 |
|
|
|
73 |
acc = accuracy_score(Y_test, pred_test) |
|
|
74 |
|
|
|
75 |
print("Test accuracy score : %s "% acc) |