-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
166 lines (116 loc) · 4.72 KB
/
models.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
# utf-8
# Python 3.6
import numpy as np
import pandas as pd
import joblib
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Dropout
import config
class BinaryClassifier():
def __init__(self):
"""
Initialization.
"""
self.preproc_path = config.PREPROC_BINARY_PATH
self.model_path = config.MODEL_BINARY_PATH
self.create_preprocess()
self.create_model()
def create_preprocess(self):
"""
Load pipeline to preprocess input data.
"""
self.pipeline = joblib.load(self.preproc_path+"pipeline.pkl")
self.pca_2 = joblib.load(self.preproc_path+"pca_2.pkl")
self.kmeans_2 = joblib.load(self.preproc_path+"kmeans_2.pkl")
self.pca_3 = joblib.load(self.preproc_path+"pca_3.pkl")
self.kmeans_3 = joblib.load(self.preproc_path+"kmeans_3.pkl")
self.pca_4 = joblib.load(self.preproc_path+"pca_4.pkl")
self.kmeans_4 = joblib.load(self.preproc_path+"kmeans_4.pkl")
def preprocess(self, X):
"""
Preprocess data.
"""
X = pd.DataFrame(X, columns=config.columns)
X["protocol_type"] = X["protocol_type"].replace(config.protocol_type_dct)
X["service"] = X["service"].replace(config.service_dct)
X["flag"] = X["flag"].replace(config.flag_dct)
return self.pipeline.transform(X)
def create_model(self):
"""
Create model and load weights.
"""
self.detector = joblib.load(self.model_path+"model.pkl")
def predict(self, X):
"""
Predict attack.
Parameters:
X (np.array) - feature vector (n, )
Returns:
y (1/0) - attack
"""
return self.detector.predict(self.preprocess([X]))[0]
class MultyClassifier():
"""
Attack types classifier.
"""
def __init__(self):
"""
Initialization.
"""
self.preproc_path = config.PREPROC_MULTY_PATH
self.model_path = config.MODEL_MULTY_PATH
self.create_preprocess()
self.create_model()
def create_preprocess(self):
"""
Load pipeline to preprocess input data.
"""
self.pipeline = joblib.load(self.preproc_path+"pipeline.pkl")
self.pca_2 = joblib.load(self.preproc_path+"pca_2.pkl")
self.kmeans_2 = joblib.load(self.preproc_path+"kmeans_2.pkl")
self.pca_3 = joblib.load(self.preproc_path+"pca_3.pkl")
self.kmeans_3 = joblib.load(self.preproc_path+"kmeans_3.pkl")
self.pca_4 = joblib.load(self.preproc_path+"pca_4.pkl")
self.kmeans_4 = joblib.load(self.preproc_path+"kmeans_4.pkl")
def preprocess(self, X):
"""
Preprocess data.
"""
X = pd.DataFrame(X, columns=config.columns)
X["protocol_type"] = X["protocol_type"].replace(config.protocol_type_dct)
X["service"] = X["service"].replace(config.service_dct)
X["flag"] = X["flag"].replace(config.flag_dct)
X = self.pipeline.transform(X)
X_cl = self.kmeans_2.transform(self.pca_2.transform(X))
X = np.hstack([X, X_cl])
X_cl = self.kmeans_3.transform(self.pca_3.transform(X))
X = np.hstack([X, X_cl])
X_cl = self.kmeans_4.transform(self.pca_4.transform(X))
X = np.hstack([X, X_cl])
return X
def create_model(self):
"""
Create model and load weights.
"""
inp = Input(shape=(105, ), name="inp")
dens_1 = Dense(256, activation='relu', name="dens_1")(inp)
drop_1 = Dropout(0.7, name="drop_1")(dens_1)
dens_2 = Dense(128, activation='sigmoid', name="dens_2")(drop_1)
drop_2 = Dropout(0.5, name="drop_2")(dens_2)
dens_3 = Dense(64, activation='sigmoid', name="dens_3")(drop_2)
drop_3 = Dropout(0.3, name="drop_3")(dens_3)
out = Dense(39, activation="softmax", name="out")(drop_3)
self.detector = Model(inputs=inp, outputs=out)
self.detector.compile(loss="categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"])
self.detector.load_weights(self.model_path + "weights/model")
def predict(self, X):
"""
Predict type of attack.
Parameters:
X (np.array) - feature matrix (m, n)
Returns:
y (np.array) - attack types vector (m, )
"""
return self.detector.predict(self.preprocess(X)).argmax(1)