-
Notifications
You must be signed in to change notification settings - Fork 180
/
occupancy_detect_model.py
294 lines (217 loc) · 9.34 KB
/
occupancy_detect_model.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from keras.models import Model
from keras.layers import Input, Dense, LSTM, multiply, concatenate, Activation, Masking, Reshape
from keras.layers import Conv1D, BatchNormalization, GlobalAveragePooling1D, Permute, Dropout
from utils.constants import MAX_NB_VARIABLES, NB_CLASSES_LIST, MAX_TIMESTEPS_LIST
from utils.keras_utils import train_model, evaluate_model, set_trainable, f1_score
from utils.layer_utils import AttentionLSTM
import os
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.style.use('seaborn-paper')
from sklearn.preprocessing import LabelEncoder
import warnings
warnings.simplefilter('ignore', category=DeprecationWarning)
from keras.models import Model
from keras.layers import Permute
from keras.optimizers import Adam
from keras.utils import to_categorical
from keras.preprocessing.sequence import pad_sequences
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras import backend as K
from utils.generic_utils import load_dataset_at, calculate_dataset_metrics, cutoff_choice, \
cutoff_sequence
from utils.constants import MAX_NB_VARIABLES, MAX_TIMESTEPS_LIST
import numpy as np
from sklearn import metrics
DATASET_INDEX = 25
MAX_TIMESTEPS = MAX_TIMESTEPS_LIST[DATASET_INDEX]
MAX_NB_VARIABLES = MAX_NB_VARIABLES[DATASET_INDEX]
NB_CLASS = NB_CLASSES_LIST[DATASET_INDEX]
TRAINABLE = True
def generate_model():
ip = Input(shape=(MAX_NB_VARIABLES, MAX_TIMESTEPS))
x = Masking()(ip)
x = LSTM(8)(x)
x = Dropout(0.8)(x)
y = Permute((2, 1))(ip)
y = Conv1D(128, 8, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = squeeze_excite_block(y)
y = Conv1D(256, 5, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = squeeze_excite_block(y)
y = Conv1D(128, 3, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = GlobalAveragePooling1D()(y)
x = concatenate([x, y])
out = Dense(NB_CLASS, activation='softmax')(x)
model = Model(ip, out)
model.summary()
# add load model code here to fine-tune
return model
def generate_model_2():
ip = Input(shape=(MAX_NB_VARIABLES, MAX_TIMESTEPS))
# stride = 10
# x = Permute((2, 1))(ip)
# x = Conv1D(MAX_NB_VARIABLES // stride, 8, strides=stride, padding='same', activation='relu', use_bias=False,
# kernel_initializer='he_uniform')(x) # (None, variables / stride, timesteps)
# x = Permute((2, 1))(x)
#ip1 = K.reshape(ip,shape=(MAX_TIMESTEPS,MAX_NB_VARIABLES))
#x = Permute((2, 1))(ip)
x = Masking()(ip)
x = AttentionLSTM(8)(x)
x = Dropout(0.8)(x)
y = Permute((2, 1))(ip)
y = Conv1D(128, 8, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = squeeze_excite_block(y)
y = Conv1D(256, 5, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = squeeze_excite_block(y)
y = Conv1D(128, 3, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = GlobalAveragePooling1D()(y)
x = concatenate([x, y])
out = Dense(NB_CLASS, activation='softmax')(x)
model = Model(ip, out)
model.summary()
# add load model code here to fine-tune
return model
def generate_model_3():
ip = Input(shape=(MAX_NB_VARIABLES, MAX_TIMESTEPS))
x = Masking()(ip)
x = LSTM(8)(x)
x = Dropout(0.8)(x)
y = Permute((2, 1))(ip)
y = Conv1D(128, 8, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
#y = squeeze_excite_block(y)
y = Conv1D(256, 5, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
#y = squeeze_excite_block(y)
y = Conv1D(128, 3, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = GlobalAveragePooling1D()(y)
x = concatenate([x, y])
out = Dense(NB_CLASS, activation='softmax')(x)
model = Model(ip, out)
model.summary()
# add load model code here to fine-tune
return model
def generate_model_4():
ip = Input(shape=(MAX_NB_VARIABLES, MAX_TIMESTEPS))
# stride = 3
#
# x = Permute((2, 1))(ip)
# x = Conv1D(MAX_NB_VARIABLES // stride, 8, strides=stride, padding='same', activation='relu', use_bias=False,
# kernel_initializer='he_uniform')(x) # (None, variables / stride, timesteps)
# x = Permute((2, 1))(x)
x = Masking()(ip)
x = AttentionLSTM(8)(x)
x = Dropout(0.8)(x)
y = Permute((2, 1))(ip)
y = Conv1D(128, 8, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
#y = squeeze_excite_block(y)
y = Conv1D(256, 5, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
#y = squeeze_excite_block(y)
y = Conv1D(128, 3, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = GlobalAveragePooling1D()(y)
x = concatenate([x, y])
out = Dense(NB_CLASS, activation='softmax')(x)
model = Model(ip, out)
model.summary()
# add load model code here to fine-tune
return model
def squeeze_excite_block(input):
''' Create a squeeze-excite block
Args:
input: input tensor
filters: number of output filters
k: width factor
Returns: a keras tensor
'''
filters = input._keras_shape[-1] # channel_axis = -1 for TF
se = GlobalAveragePooling1D()(input)
se = Reshape((1, filters))(se)
se = Dense(filters // 16, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)
se = multiply([input, se])
return se
def classifaction_report_csv(report):
report_data = []
lines = report.split('\n')
for line in lines[2:-3]:
row = {}
row_data = line.split(' ')
row['class'] = row_data[0]
row['precision'] = float(row_data[1])
row['recall'] = float(row_data[2])
row['f1_score'] = float(row_data[3])
row['support'] = float(row_data[4])
report_data.append(row)
dataframe = pd.DataFrame.from_dict(report_data)
return(dataframe)
def predict_model(model:Model, dataset_id, dataset_prefix, dataset_fold_id=None, batch_size=128, test_data_subset=None,
cutoff=None, normalize_timeseries=False):
_, _, X_test, y_test, is_timeseries = load_dataset_at(dataset_id,
fold_index=dataset_fold_id,
normalize_timeseries=normalize_timeseries)
max_timesteps, max_nb_variables = calculate_dataset_metrics(X_test)
if not is_timeseries:
X_test = pad_sequences(X_test, maxlen=MAX_NB_VARIABLES[dataset_id], padding='post', truncating='post')
y_test = to_categorical(y_test, len(np.unique(y_test)))
optm = Adam(lr=1e-3)
model.compile(optimizer=optm, loss='categorical_crossentropy', metrics=['accuracy', f1_score])
if dataset_fold_id is None:
weight_fn = "./weights/%s_weights.h5" % dataset_prefix
else:
weight_fn = "./weights/%s_fold_%d_weights.h5" % (dataset_prefix, dataset_fold_id)
model.load_weights(weight_fn)
if test_data_subset is not None:
X_test = X_test[:test_data_subset]
y_test = y_test[:test_data_subset]
print("\nPredicting : ")
# metrics = model.evaluate(X_test, y_test, batch_size=batch_size)
# print()
# print("Final metrics %s: " % model.metrics_names, metrics)
y_pred = model.predict(X_test, batch_size=batch_size)
y_test = (np.argmax(y_test, axis=1)).tolist()
y_pred = (np.argmax(y_pred, axis=1)).tolist()
cfmatrix = metrics.confusion_matrix(y_test, y_pred)
report = metrics.classification_report(y_test, y_pred)
averagef_mahi = np.mean(classifaction_report_csv(report).iloc[:, 1])
microf = metrics.f1_score(y_test, y_pred, average='micro')
averagef = metrics.f1_score(y_test, y_pred, average='macro')
weightedf_mahi = sum((classifaction_report_csv(report).iloc[:,1])*(cfmatrix.sum(axis=1)/sum(cfmatrix.sum(axis=1))))
weightedf = metrics.f1_score(y_test, y_pred, average='weighted')
print()
print("Binary F-Score : ", microf)
print("Final F-Score : ", averagef)
print("Weighted F-Score : ", weightedf)
print("Mahis F-Score : ", averagef_mahi)
print("Mahis W F-Score : ", weightedf_mahi)
return averagef
if __name__ == "__main__":
model = generate_model_2()
model.compile('adam', 'categorical_crossentropy', metrics=['accuracy', f1_score])
train_model(model, DATASET_INDEX, dataset_prefix='opportunity_new', epochs=1000, batch_size=128,
monitor='val_f1_score', optimization_mode='max', compile_model=False)
#evaluate_model(model, DATASET_INDEX, dataset_prefix='opportunity', batch_size=128)
#predict_model(model, DATASET_INDEX, dataset_prefix='opportunity_weights_attention_9208_512_lstm_128', batch_size=512)