-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsleep_nn.py
253 lines (199 loc) · 8.87 KB
/
sleep_nn.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
import numpy as np
import pandas as pd
import sys
import time
import os
import random
#
from sleep_misc import load_dataset
from sleep_eval import eval_f1, eval_acc
from sklearn.preprocessing import StandardScaler
#
from keras.layers import Flatten, Conv1D, concatenate, Input
from keras.layers.core import Activation
from keras.models import Model
from keras.models import Sequential
from keras.models import load_model
from keras import layers
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib
import tensorflow as tf
TASK = int(sys.argv[1])
SEQ_LEN = int(sys.argv[2]) # used in the experiments 20, 50 and 100
NN_TYPE = sys.argv[3] # LSTM or CNN
TRAINING = True
USING_MESA_VARIABLES = False
np.random.seed(42)
#tf.set_random_seed(42)
os.environ['PYTHONHASHSEED']=str(42)
random.seed(42)
# Parameters used in the experiments
input_type = "raw"
epochs = 30
batch_size = 32
#from keras import backend as K
#session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
#sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
#K.set_session(sess)
MODEL_OUTFILE = "model_%s_task%d_%s_seq%d.pkl" % (NN_TYPE, TASK, input_type, SEQ_LEN)
RESULTS_OUTFILE = "task%d_%s_%s_%d.csv" % (TASK, NN_TYPE, input_type, SEQ_LEN)
DATASET_PATH = "hdf_task%d" % (TASK)
print("...Loading dataset into memory...")
dftrain, dftest, featnames = load_dataset(DATASET_PATH, useCache=True)
print("...Done...")
if USING_MESA_VARIABLES:
mesa_cols = ["gender1", "sleepage5c", "insmnia5", "rstlesslgs5", "slpapnea5"]
variables = pd.read_csv("./data/mesa-sleep-dataset-0.3.0.csv.gz")[["mesaid"] + mesa_cols].fillna(0.0)
dftrain = pd.merge(dftrain, variables)
dftest = pd.merge(dftest, variables)
scaler = StandardScaler()
scaler.fit(dftrain[["activity"]].fillna(0.0))
dftrain["activity"] = scaler.transform(dftrain[["activity"]].fillna(0.0))
dftest["activity"] = scaler.transform(dftest[["activity"]].fillna(0.0))
scaler_filename = "scaler_activity_%s_task%d_seq%d.save" % (NN_TYPE, TASK, SEQ_LEN)
joblib.dump(scaler, scaler_filename)
def extract_x_y(df, seq_len, mesaid):
if USING_MESA_VARIABLES:
variables = df[df["mesaid"] == mesaid][mesa_cols].copy()
variables = variables.fillna(0.0).values
else:
variables = None
df = df[df["mesaid"] == mesaid][["activity","gt"]].copy()
for s in range(1, seq_len//2 + 1):
df["shift_%d" % (s)] = df["activity"].shift(s)
for s in range(1, seq_len//2 + 1):
df["shift_-%d" % (s)] = df["activity"].shift(-s)
y = df["gt"]
y = np.array([[1] if v else [0] for v in y])
del df["gt"]
x = df.fillna(-1).values
return x, y, variables
def get_data(df, seq_len):
mesaids = df.mesaid.unique()
x_, y_, variables_ = extract_x_y(df, seq_len, mesaids[0])
for mid in mesaids[1:]:
x_tmp, y_tmp, variables_tmp = extract_x_y(df, seq_len, mid)
x_ = np.concatenate((x_, x_tmp))
y_ = np.concatenate((y_, y_tmp))
if variables_ is not None:
variables_ = np.concatenate((variables_, variables_tmp))
return x_, y_, variables_
def build_model(input_dim, input2_dim=0, nn_type="CNN"):
if nn_type == "CNN":
return build_model_CNN(input_dim, input2_dim)
elif nn_type == "LSTM":
return build_model_LSTM(input_dim, input2_dim)
else:
print("Invalid nn_type '%s'. Options are 'CNN' or 'LSTM'" % (nn_type))
def build_model_LSTM(input1, input2 = 0):
start = time.time()
if input2 == 0:
model = Sequential()
model.add(layers.LSTM(32, input_shape=input1))
model.add(Dense(1, activation='sigmoid'))
else:
in1 = Input(shape=input1)
x1 = layers.LSTM(32)(in1)
in2 = Input(shape=input2, name="mesa_variables")
x2 = Flatten()(in2)
x = concatenate([x1, x2])
predictions = Dense(1, activation="sigmoid")(x)
model = Model(inputs=[in1, in2], outputs=predictions)
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
print("> Compilation Time : ", time.time() - start)
model.summary()
return model
def build_model_CNN(input1, input2 = 0):
start = time.time()
if input2 == 0:
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=2, input_shape=input1))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
#Accuracy:
#F1:
else:
inputs = Input(shape=input1)
x = Conv1D(64, 2, activation='relu')(inputs)
x = Flatten()(x)
variables = Input(shape=input2, name="mesa_variables")
y = Flatten()(variables)
x = concatenate([x, y])
x = Dense(64, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(inputs=[inputs,variables], outputs=predictions)
#Accuracy: 0.811463
#F1: 0.840017
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
print("> Compilation Time : ", time.time() - start)
model.summary()
return model
# Process input:
if input_type == "raw":
print("Generating dataset from Raw signal")
x_train, y_train, variables_train = get_data(dftrain, SEQ_LEN)
x_test, y_test, variables_test = get_data(dftest, SEQ_LEN)
x_train = np.reshape(x_train, x_train.shape + (1,))
x_test = np.reshape(x_test, x_test.shape + (1,))
if USING_MESA_VARIABLES:
variables_train = np.reshape(variables_train, variables_train.shape + (1,))
variables_test = np.reshape(variables_test, variables_test.shape + (1,))
x_train, x_val, y_train, y_val, variables_train, variables_val = train_test_split(x_train, y_train, variables_train, test_size=0.20, random_state=43, shuffle=False)
else:
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.20, random_state=43, shuffle=False)
elif input_type == "feat":
print("Generating dataset from feature set")
x_train_feat = dftrain[featnames].values
x_test = dftest[featnames].values
y_test = dftest["gt"].values
scaler_feat = StandardScaler()
scaler_feat.fit(x_train_feat)
x_train_feat = scaler.transform(x_train_feat)
x_test = scaler.transform(x_test)
x_train_feat = np.reshape(x_train_feat, x_train_feat.shape + (1,))
x_test = np.reshape(x_test, x_test.shape + (1,))
x_train, x_val, y_train, y_val = train_test_split(x_train_feat, y_train, test_size=0.20, random_state=43, shuffle=False)
print("Done")
def run_experiment(input_type, epochs, batch_size, x_train, x_val, x_test, y_train, y_val, y_test):
print("Training Shape: " ,x_train.shape[1:])
if input_type == "raw":
if USING_MESA_VARIABLES:
model = build_model(x_train.shape[1:], variables_train.shape[1:], NN_TYPE)
model.fit([x_train, variables_train], y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_data=([x_val, variables_val], y_val))
print("Accuracy:", eval_acc(np.round(model.predict([x_test, variables_test])), y_test))
print("F1:", eval_f1(np.round(model.predict([x_test, variables_test])), y_test))
else:
model = build_model(x_train.shape[1:], 0, NN_TYPE)
model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_data=(x_val, y_val))
print("Accuracy:", eval_acc(np.round(model.predict(x_test)), y_test))
print("F1:", eval_f1(np.round(model.predict(x_test)), y_test))
elif input_type == "feat":
model = build_model(x_train.shape[1:], NN_TYPE)
model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_val, y_val))
print("Accuracy:", eval_acc(np.round(model.predict(x_test)), y_test))
print("F1:", eval_f1(np.round(model.predict(x_test)), y_test))
return model
def load_trained_model(input_type):
return
if TRAINING:
print("Start training model...")
model = run_experiment(input_type, epochs, batch_size, x_train, x_val, x_test, y_train, y_val, y_test)
model.save(MODEL_OUTFILE)
print("Model trained!")
else:
model = load_model("models/" + MODEL_OUTFILE)
print("Model loaded from disk!")
if USING_MESA_VARIABLES:
predictions = model.predict([x_test, variables_test])
else:
predictions = model.predict(x_test)
dftest["p_%s_%d" % (NN_TYPE, SEQ_LEN)] = predictions
dftest["%s_%d" % (NN_TYPE, SEQ_LEN)] = np.round(predictions)
dftest["gt_sleep_block"] = dftest["gt_sleep_block"].astype(int)
dftest["gt"] = dftest["gt"].astype(int)
dftest["actValue"] = dftest["actValue"].fillna(0.0).astype(int)
dftest[["mesaid","linetime","actValue","gt","gt_sleep_block","%s_%d" % (NN_TYPE, SEQ_LEN),"p_%s_%d" % (NN_TYPE, SEQ_LEN) ]].to_csv(RESULTS_OUTFILE, index=False)
print("Predictions made. Result save to %s." % (RESULTS_OUTFILE))