-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline_experiments.py
156 lines (128 loc) · 6.05 KB
/
baseline_experiments.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
import numpy as np
import pickle
import matplotlib.pyplot as plt
from sklearn.preprocessing import normalize
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.gaussian_process.kernels import DotProduct
from sklearn.naive_bayes import GaussianNB
from keras.optimizers import Adam
from utils.evaluation import categorical_evaluation
from model.baseline_models import deepLSTM, deepBiLSTM, ResNet_model, VGG_model, Inception_model
rseed = 13
np.random.seed(rseed)
n_features = 8
timesteps = 24
epochs = 50
params_rf = {'n_estimators': 166,
'max_features': 'sqrt',
'max_depth': 110,
'min_samples_split': 2,
'min_samples_leaf': 1,
'bootstrap': False}
params_svm = {'C': 100000.0,
'gamma': 1e-07,
'kernel': 'rbf',
'degree': 1,
'class_weight': None}
params_gp = {'kernel': 1**2 * DotProduct(sigma_0=1)}
lstm_params = {
'n_class': 2,
'n_layer': 2,
'n_units': 200,
'drop_out': 0.2,
'lr': 0.0005623413251903491,
'loss': 'binary_crossentropy',
'active': 'relu'
}
bilstm_params = {
'n_class': 2,
'n_layer': 1,
'n_units': 1000,
'drop_out': 0.1,
'lr': 0.0031622776601683794,
'loss': 'binary_crossentropy'
}
res_params = {
'n_channel': 3,
'n_class': 2,
'lr': 0.0031622776601683794,
'loss': 'binary_crossentropy',
'n_layers': 3,
'n_units': 200
}
vgg_params = {
'n_channel': 3,
'n_class': 2,
'lr': 0.0001,
'loss': 'binary_crossentropy',
'n_layers': 2,
'n_units': 1000
}
inc_params = {
'n_channel': 3,
'n_class': 2,
'lr': 0.0001,
'loss': 'binary_crossentropy',
'n_layers': 1,
'n_units': 200
}
data_label = np.load('data/data_label_train.pkl', allow_pickle=True)
label = np.load('data/label_train.pkl', allow_pickle=True)
data_label = np.reshape(data_label, (-1, timesteps*n_features))
data_label = normalize(data_label, norm='max', axis=0)
label = np.reshape(label, (-1,))
x_test = np.load('data/data_label_test.pkl', allow_pickle=True)
x_test = np.reshape(x_test, (-1, timesteps*n_features))
x_test = normalize(x_test, norm='max', axis=0)
y_test = np.load('data/label_test.pkl', allow_pickle=True)
y_test = np.reshape(y_test, (-1,))
rf_model = RandomForestClassifier().set_params(**params_rf)
rf_model.fit(data_label, label)
print("RF model evaluation on train data: ", categorical_evaluation(rf_model, data_label, label))
print("RF model evaluation on test data: ", categorical_evaluation(rf_model, x_test, y_test))
pickle.dump(rf_model, open("saved_models/model_RF.sav", 'wb'))
svm_model = SVC(probability=True, random_state=37).set_params(**params_svm)
svm_model.fit(data_label, label)
print("SVM model evaluation on train data: ", categorical_evaluation(svm_model, data_label, label))
print("SVM model evaluation on test data: ", categorical_evaluation(svm_model, x_test, y_test))
pickle.dump(svm_model, open("saved_models/model_SVM.sav", 'wb'))
gp_model = GaussianProcessClassifier(random_state=37).set_params(**params_gp)
gp_model.fit(data_label, label)
print("GP model evaluation on train data: ", categorical_evaluation(gp_model, data_label, label))
print("GP model evaluation on test data: ", categorical_evaluation(gp_model, x_test, y_test))
pickle.dump(gp_model, open("saved_models/model_GP.sav", 'wb'))
lstm_model = deepLSTM(timesteps=timesteps, n_features=n_features, **lstm_params)
lstm_model.compile(optimizer=Adam(lr=lstm_params['lr']), loss=lstm_params['loss'], metrics=['acc'])
history = lstm_model.fit(data_label, label_enc, batch_size=32, epochs=epochs, verbose=0)
print("LSTM model evaluation on train data: ", categorical_evaluation(lstm_model, data_label, label_enc))
print("LSTM model evaluation on test data: ", categorical_evaluation(lstm_model, x_test, y_test))
lstm_model.save("saved_models/model_LSTM.h5")
bilstm_model = deepBiLSTM(timesteps=timesteps, n_features=n_features, **bilstm_params)
bilstm_model.compile(optimizer=Adam(lr=bilstm_params['lr']), loss=bilstm_params['loss'],
metrics=['acc'])
history = bilstm_model.fit(data_label, label_enc, batch_size=32, epochs=epochs, verbose=0)
print("BiLSTM model evaluation on train data: ", categorical_evaluation(bilstm_model, data_label, label_enc))
print("BiLSTM model evaluation on test data: ", categorical_evaluation(bilstm_model, x_test, y_test))
lstm_model.save("saved_models/model_BiLSTM.h5")
res_model = ResNet_model(n_feat=n_features, **res_params)
res_model.compile(optimizer=Adam(lr=res_params['lr']), loss=res_params['loss'], metrics=['acc'])
history = res_model.fit(data_label, label_enc, batch_size=32, epochs=epochs, verbose=0)
print("ResNet model evaluation on train data: ", categorical_evaluation(res_model, data_label, label_enc))
print("ResNet model evaluation on test data: ", categorical_evaluation(res_model, x_test, y_test))
res_model.save("saved_models/model_ResNet.h5")
vgg_model = VGG_model(n_feat=n_features, **vgg_params)
vgg_model.compile(optimizer=Adam(lr=vgg_params['lr']), loss=vgg_params['loss'], metrics=['acc'])
history = vgg_model.fit(data_label, label_enc, batch_size=32, epochs=epochs, verbose=0)
print("VGG model evaluation on train data: ", categorical_evaluation(vgg_model, data_label, label_enc))
print("VGG model evaluation on test data: ", categorical_evaluation(vgg_model, x_test, y_test))
vgg_model.save("saved_models/model_VGG.h5")
inc_model = Inception_model(n_feat=n_features, **inc_params)
inc_model.compile(optimizer=Adam(lr=inc_params['lr']), loss=inc_params['loss'], metrics=['acc'])
history = inc_model.fit(data_label, label_enc, batch_size=32, epochs=epochs, verbose=0)
print("Inception model evaluation on train data: ", categorical_evaluation(inc_model, data_label, label_enc))
print("Inception model evaluation on test data: ", categorical_evaluation(inc_model, x_test, y_test))
inc_model.save("saved_models/model_Inception.h5")