-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsupervised_CNN_training.py
181 lines (123 loc) · 3.99 KB
/
supervised_CNN_training.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
"""
Extra data of between the attack triggering time and the attack success time (from container 1).
"""
import joblib
import pathlib
import random
import numpy as np
from baseline import prepare
from count_time import count_time
from supervised_training import get_data
# In[2]:
from keras.models import Sequential
from keras.layers import Dense, Conv1D, Flatten, MaxPooling1D, Dropout
from keras.regularizers import l2
from baseline import prepare
from keras.utils import to_categorical
import numpy as np
from sklearn.metrics import classification_report, roc_curve, auc
# In[3]:
# read from apps-all.txt
application_list = []
with open("data/apps-all.txt") as fin_apps:
for line in fin_apps:
application_list.append(line.strip())
print(f"There are {len(application_list)} applications. \n{application_list}")
# In[4]:
def convert(data):
for app in data:
item = np.array(data[app])
item = item.reshape(item.shape[0], item.shape[1], 1)
data[app] = item
return data
# In[6]:
# X_train, y_train, normal_data = get_data([1])
# print(len(X_train), len(y_train), len(normal_data))
# y_train = [1 for _ in range(len(X_train))] # label all as abnormal
# total = len(X_train)
# X_train.extend(random.sample(normal_data, total))
# y_train.extend([0 for i in range(total)])
# # 0 for normal, 1 for abnormal
# X_train = convert(X_train)
# In[7]:
def get_X_y(container):
X, y = get_data(application_list, container)
X = convert(X)
return X, y
# In[8]:
X_train, y_train = get_X_y([1, 2])
# print(len(X_train), len(y_train))
# In[9]:
# X_valid, y_valid = get_X_y([2])
# # In[10]:
# X_test, y_test = get_X_y([3])
# In[11]:
classifier_type = 'CNN'
folder = f'./data/supervised_model/{classifier_type}'
pathlib.Path(folder).mkdir(parents=True, exist_ok=True)
last_time = 0
last_time = count_time(last_time, "before training")
for app in application_list:
# create model
model = Sequential()
step = 10
# add model layers
model.add(Conv1D(8, kernel_size=step,
activation='relu', input_shape=(555, 1)))
model.add(Conv1D(4, kernel_size=step, activation='relu'))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
# model.add(Dense(1, activation='sigmoid', kernel_regularizer=l2(l=0.01)))
model.summary()
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
# In[12]:
model.fit(X_train[app], y_train[app], epochs=35, shuffle=True)
# print(f"Training dataset size: {len(X_train)}")
model.save(f'{folder}/{app}.h5')
last_time = count_time(last_time, "after training")
# In[13]:
# # testing
# y_pred = model.predict(X_test).ravel()
# y_pred_num = [0 if i < 0.5 else 1 for i in y_pred]
# print(classification_report(y_test, y_pred_num))
# cnf_matrix = confusion_matrix(y_test, y_pred_num)
# print(cnf_matrix)
# tn, fp, fn, tp = cnf_matrix.ravel()
# fpr = fp / (fp + tn) * 100
# tpr = tp / (tp + fn) * 100
# print(fpr, tpr)
# In[14]:
# fpr, tpr, _ = roc_curve(y_test, y_pred)
# roc_auc = auc(fpr, tpr)
# plt.figure()
# lw = 2
# plt.plot(fpr, tpr, color='darkorange',
# lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
# plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
# plt.xlim([0.0, 1.0])
# plt.ylim([0.0, 1.05])
# plt.xlabel('False Positive Rate')
# plt.ylabel('True Positive Rate')
# plt.title('Receiver operating characteristic of CNN')
# plt.legend(loc="lower right")
# plt.show()
# In[15]:
# In[21]:
# fpr = [0, 0.091, 0.1706, 0.2432, 0.3332, 0.6992,1]
# tpr = [0, 0.1067, 0.1916, 0.2718, 0.3723, 0.7494, 1]
# roc_auc = auc(fpr, tpr)
# plt.figure()
# lw = 2
# plt.plot(fpr, tpr, color='darkorange', lw=lw, marker='D', label='ROC curve (area = %0.2f)' % roc_auc)
# plt.xlim([0.0, 1.0])
# plt.ylim([0.0, 1.05])
# plt.xlabel('False Positive Rate')
# plt.ylabel('True Positive Rate')
# plt.title('Receiver operating characteristic of CNN')
# plt.legend(loc="lower right")
# plt.show()
# In[ ]: