-
Notifications
You must be signed in to change notification settings - Fork 0
/
annMLPMultiClass.py
308 lines (263 loc) · 13 KB
/
annMLPMultiClass.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
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
===========================================================================
Multi Layer Perceptron - Multi-class
===========================================================================
Multi Layer Perceptron - Multi-class
"""
import os
import sys
from contextlib import contextmanager
import time
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, StratifiedKFold
import tensorflow as tf
from tensorflow.python.keras.callbacks import TensorBoard
from keras import models, layers, optimizers
from keras.utils import plot_model
import keras.backend as K
from filehandler import Filehandler
from dataset import KDDCup1999
from visualize import Visualize
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
@contextmanager
def timer(title):
t0 = time.time()
yield
print('{} - done in {:.0f}s'.format(title, time.time() - t0))
class AnnMLPMulti:
def __init__(self):
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Ignore low level instruction warnings
tf.logging.set_verbosity(tf.logging.ERROR) # Set tensorflow verbosity
self.g = tf.Graph()
self.tf_sess = tf.Session(config=tf.ConfigProto(log_device_placement=True), graph=self.g)
self.logfile = None
self.gettrace = getattr(sys, 'gettrace', None)
self.original_stdout = sys.stdout
self.timestr = time.strftime("%Y%m%d-%H%M%S")
self.log_file()
print(__doc__)
self.random_state = 20
self.filehandler = Filehandler()
self.ds = KDDCup1999()
self.visualize = Visualize()
self.folder = 'viz'
# Datasets
self.X = None
self.y = None
self.X_train = None
self.X_test = None
self.y_train = None
self.y_test = None
self.n_features = None
self.label_map_int_2_string = {0: 'normal', 1: 'dos', 2: 'u2r', 3: 'r2l', 4: 'probe'}
self.label_map_string_2_int = {'normal': 0, 'dos': 1, 'u2r': 2, 'r2l': 3, 'probe': 4}
# K-fold validation
self.splits = 5
self.kfold = StratifiedKFold(n_splits=self.splits, shuffle=True, random_state=self.random_state)
# Network parameters
self.epochs = 10
self.batch_size = 100
self.verbose = 0
# Scores
self.metric_loss = []
self.metric_acc = []
self.metric_dr = []
self.metric_far = []
self.metric_val_loss = []
self.metric_val_acc = []
self.metric_val_dr = []
self.metric_val_far = []
with timer('\nPreparing dataset'):
self.load_data()
self.set_y()
self.remove_target_from_X()
self.n_features = self.X.shape[1]
self.train_test_split()
with timer('\nTraining & validating model with kfold'):
self.g.as_default() # Reset graph for tensorboard display
K.clear_session()
self.index = 0
# Train model on K-1 and validate using remaining fold
for train, val in self.kfold.split(self.X_train, self.y_train):
self.index += 1
#self.tensorboard = TensorBoard(log_dir='logs/tb/annmlpmulticlass_cv')
self.model = self.get_model()
self.y_train_onehotencoded = pd.get_dummies(self.y_train.iloc[train])
self.y_val_onehotencoded = pd.get_dummies(self.y_train.iloc[val])
self.history = self.model.fit(self.X_train.iloc[train], self.y_train_onehotencoded,
validation_data=(self.X_train.iloc[val], self.y_val_onehotencoded),
epochs=self.epochs, batch_size=self.batch_size, verbose=self.verbose)
#callbacks=[self.tensorboard])
self.metric_loss.append(self.history.history['loss'])
self.metric_acc.append(self.history.history['acc'])
self.metric_dr.append(self.history.history['dr'])
self.metric_far.append(self.history.history['far'])
self.metric_val_loss.append(self.history.history['val_loss'])
self.metric_val_acc.append(self.history.history['val_acc'])
self.metric_val_dr.append(self.history.history['val_dr'])
self.metric_val_far.append(self.history.history['val_far'])
print('\nTraining mean loss', np.mean(self.metric_loss))
print('Training mean acc', np.mean(self.metric_acc))
print('Training mean dr', np.mean(self.metric_dr))
print('Training mean far', np.mean(self.metric_far))
print('\nValidation mean loss', np.mean(self.metric_val_loss))
print('Validation mean acc', np.mean(self.metric_val_acc))
print('Validation mean dr', np.mean(self.metric_val_dr))
print('Validation mean far', np.mean(self.metric_val_far))
with timer('\nTesting model on unseen test set'):
self.g.as_default() # Reset graph for tensorboard display
K.clear_session()
self.tensorboard = TensorBoard(log_dir='logs/tb/annmlpmulticlass_test')
self.model = self.get_model()
self.y_test_onehotencoded = pd.get_dummies(self.y_test)
self.y_train_onehotencoded = pd.get_dummies(self.y_train)
# Train model on complete train set and validate with unseen test set
self.history = self.model.fit(self.X_train, self.y_train_onehotencoded,
validation_data=(self.X_test, self.y_test_onehotencoded),
epochs=self.epochs, batch_size=self.batch_size, verbose=self.verbose,
callbacks=[self.tensorboard])
with timer('\nVisualising results'):
# Plot model
plot_model(self.model, to_file='viz/annMLPMultiClass - model plot.png')
# Get single class prediction (rather than multi class probability summing to 1)
y_pred = self.model.predict_classes(self.X_test)
print('Test loss', np.mean(self.history.history['loss']))
print('Test acc', np.mean(self.history.history['acc']))
print('Test dr', np.mean(self.history.history['dr']))
print('Test far', np.mean(self.history.history['far']))
# Remap to string class targets
self.y_pred = pd.Series(y_pred)
self.y_pred = self.map_target_to_label(self.y_pred)
self.y_test = self.map_target_to_label(self.y_test)
# To numpy arrays for cm
self.y_pred = self.y_pred.values
self.y_test = self.y_test.values
self.visualize.confusion_matrix(self.y_test, self.y_pred, self.__class__.__name__)
epochs = range(1, len(self.history.history['loss']) + 1)
# Plot loss
fig, ax = plt.subplots(figsize=(15, 8))
plt.style.use('ggplot')
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.tick_params(axis='both', which='major', labelsize=12)
ax.plot(epochs, np.mean(self.metric_loss, axis=0), 'g', label='Training')
ax.plot(epochs, np.mean(self.metric_val_loss, axis=0), 'b', label='Validation')
ax.plot(epochs, self.history.history['loss'], 'r', label='Test')
self.title = '{} - {}'.format(self.__class__.__name__, 'Loss')
plt.title(self.title, fontsize=18)
plt.xlabel('Epochs', fontsize=14)
plt.ylabel('Loss', fontsize=14)
plt.legend(loc=1, prop={'size': 14})
plt.savefig(fname=self.fname(self.title), dpi=300, format='png')
plt.show()
# Plot accuracy
plt.clf()
fig, ax = plt.subplots(figsize=(15, 8))
plt.style.use('ggplot')
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.tick_params(axis='both', which='major', labelsize=12)
ax.plot(epochs, np.mean(self.metric_acc, axis=0), 'g', label='Training')
ax.plot(epochs, np.mean(self.metric_val_acc, axis=0), 'b', label='Validation')
ax.plot(epochs, self.history.history['acc'], 'r', label='Test')
self.title = '{} - {}'.format(self.__class__.__name__, 'Accuracy')
plt.title(self.title, fontsize=18)
plt.xlabel('Epochs', fontsize=14)
plt.ylabel('Accuracy', fontsize=14)
plt.legend(loc=4, prop={'size': 14})
plt.savefig(fname=self.fname(self.title), dpi=300, format='png')
plt.show()
# Plot detection rate
plt.clf()
fig, ax = plt.subplots(figsize=(15, 8))
plt.style.use('ggplot')
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.tick_params(axis='both', which='major', labelsize=12)
ax.plot(epochs, np.mean(self.metric_dr, axis=0), 'g', label='Training')
ax.plot(epochs, np.mean(self.metric_val_dr, axis=0), 'b', label='Validation')
ax.plot(epochs, self.history.history['dr'], 'r', label='Test')
self.title = '{} - {}'.format(self.__class__.__name__, 'Detection Rate')
plt.title(self.title, fontsize=18)
plt.xlabel('Epochs', fontsize=14)
plt.ylabel('Detection Rate', fontsize=14)
plt.legend(loc=4, prop={'size': 14})
plt.savefig(fname=self.fname(self.title), dpi=300, format='png')
plt.show()
# Plot false alarm rate
plt.clf()
fig, ax = plt.subplots(figsize=(15, 8))
plt.style.use('ggplot')
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.tick_params(axis='both', which='major', labelsize=12)
ax.plot(epochs, np.mean(self.metric_far, axis=0), 'g', label='Training')
ax.plot(epochs, np.mean(self.metric_val_far, axis=0), 'b', label='Validation')
ax.plot(epochs, self.history.history['far'], 'r', label='Test')
self.title = '{} - {}'.format(self.__class__.__name__, 'False Alarm Rate')
plt.title(self.title, fontsize=18)
plt.xlabel('Epochs', fontsize=14)
plt.ylabel('False Alarm Rate', fontsize=14)
plt.legend(loc=1, prop={'size': 14})
plt.savefig(fname=self.fname(self.title), dpi=300, format='png')
plt.show()
self.log_file()
print('Finished')
@staticmethod
def dr(y_true, y_pred):
y_pred_pos = K.round(K.clip(y_pred, 0, 1))
y_pred_neg = 1 - y_pred_pos
y_pos = K.round(K.clip(y_true, 0, 1))
tp = K.sum(y_pos * y_pred_pos)
fn = K.sum(y_pos * y_pred_neg)
return tp / (tp + fn + K.epsilon())
@staticmethod
def far(y_true, y_pred):
y_pred_pos = K.round(K.clip(y_pred, 0, 1))
y_pred_neg = 1 - y_pred_pos
y_pos = K.round(K.clip(y_true, 0, 1))
y_neg = 1 - y_pos
tn = K.sum(y_neg * y_pred_neg)
fp = K.sum(y_neg * y_pred_pos)
return fp / (tn + fp + K.epsilon())
def get_model(self):
model = models.Sequential()
model.add(layers.Dense(84, activation='relu', input_shape=(self.n_features,)))
model.add(layers.Dropout(0.12))
model.add(layers.Dense(53, activation='relu'))
model.add(layers.Dropout(0.12))
model.add(layers.Dense(53, activation='relu'))
model.add(layers.Dropout(0.12))
model.add(layers.Dense(5, activation='softmax'))
model.compile(optimizer=optimizers.RMSprop(lr=0.0032), loss='categorical_crossentropy',
metrics=['accuracy', self.dr, self.far])
return model
def log_file(self):
if self.gettrace is None:
pass
elif self.gettrace():
pass
else:
if self.logfile:
sys.stdout = self.original_stdout
self.logfile.close()
self.logfile = False
else:
# Redirect stdout to file for logging if not in debug mode
self.logfile = open('logs/{}_{}_stdout.txt'.format(self.__class__.__name__, self.timestr), 'w')
sys.stdout = self.logfile
def load_data(self):
self.X = self.filehandler.read_csv(self.ds.config['path'], self.ds.config['file'] + '_Tensor2d_type_1')
print('\tRow count:\t', '{}'.format(self.X.shape[0]))
print('\tColumn count:\t', '{}'.format(self.X.shape[1]))
def set_y(self):
self.y = self.X['attack_category']
self.y = self.y.map(self.label_map_string_2_int)
def remove_target_from_X(self):
self.X.drop('attack_category', axis=1, inplace=True)
def train_test_split(self):
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(self.X, self.y, test_size=0.30,
random_state=self.random_state)
def map_target_to_label(self, t):
return t.map(self.label_map_int_2_string)
def fname(self, title):
return '{}/{}.png'.format(self.folder, title)
annmlpmulti = AnnMLPMulti()