-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdl_retrain.py
465 lines (386 loc) · 18.9 KB
/
dl_retrain.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import os
import sys
import codecs
import pickle
import gensim
import urllib
import logging
import pandas as pd
from pathlib import Path
from bs4 import BeautifulSoup
from connection import DBConnection
import numpy as np
from numpy import load
from numpy import save
from numpy import asarray
from sklearn import metrics
from sklearn.metrics import confusion_matrix
import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras import backend as K
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.preprocessing.text import Tokenizer
from spektral.layers import GraphConvSkip, GlobalAvgPool
from spektral.layers.ops import sp_matrix_to_sp_tensor
from spektral.layers.pooling import MinCutPool
from spektral.utils.convolution import normalized_adjacency
from spektral.utils import batch_iterator
from spektral.utils.data import Batch
import networkx as nx
from scipy.sparse import csr_matrix
import warnings
warnings.filterwarnings("ignore")
class RetrainModel:
def __init__(self):
# Using sys.setrecursionlimit() method
sys.setrecursionlimit(3000)
self.data_dir = "data"
self.tmp_dir = "tmp"
self.doc2vec = gensim.models.doc2vec.Doc2Vec.load("doc2vec.model")
self.df_tr, self.df_val, self.df_te = self.getData()
with open('tokenizer.pkl', 'rb') as handle:
tokenizer = pickle.load(handle)
self.tokenizer = tokenizer
self.reset()
def reset(self):
self.node = 0
self.nodes_list = []
self.attr_list = []
def getData(self):
try:
connection = DBConnection("moraphishdet").get_connection()
cursor = connection.cursor()
cursor.callproc('sp_set_dl_training_data')
connection.commit()
cursor.close()
except mysql.connector.Error as error:
print("Failed to execute stored procedure: {}".format(error))
df_tr = pd.read_sql('SELECT * FROM tmp_tr_data', con= DBConnection("moraphishdet").get_connection())
df_val = pd.read_sql('SELECT * FROM tmp_val_data', con= DBConnection("moraphishdet").get_connection())
df_te = pd.read_sql('SELECT * FROM tmp_te_data', con= DBConnection("moraphishdet").get_connection())
return df_tr, df_val, df_te
def convert(self, list):
return tuple(list)
def callTag(self, tag):
node_from = self.node
self.node = self.node + 1
for tag in tag.children:
attr_node = 1
if (tag.name is not None):
self.nodes_list.append(self.convert([str(node_from), str(self.node)]))
self.attr_list.append(str(self.node) + ",nname," + str(tag.name))
self.attr_list.append(str(self.node) + ",value," + "")
for attr in tag.attrs:
self.nodes_list.append(self.convert([str(self.node), str(self.node) + "_" + str(attr_node)]))
self.attr_list.append(str(self.node) + "_" + str(attr_node) + ",nname," + str(attr))
self.attr_list.append(str(self.node) + "_" + str(attr_node) + ",value," + str(tag.get(attr)))
attr_node = attr_node + 1
self.callTag(tag)
def read_corpus(self, fname, tokens_only=False):
for i, line in enumerate(fname):
tokens = gensim.utils.simple_preprocess(line)
if tokens_only:
yield tokens
else:
# For training data, add tags
yield gensim.models.doc2vec.TaggedDocument(tokens, [i])
def get_udst_inputs(self, url, html_file, phishing_flag):
f = codecs.open(Path(self.data_dir + '/' + html_file), 'r', encoding='utf-8', errors='ignore')
soup = BeautifulSoup(f)
self.callTag(soup)
self.nodes_list.pop(0)
self.attr_list.pop(1)
self.attr_list.insert(1, '1, value, ' + url)
G = nx.Graph()
G.add_edges_from(self.nodes_list)
A = nx.adjacency_matrix(G) # N*N Adjacency matrix
df_features = pd.DataFrame(0.0, index=G.nodes(), columns=['nname', 'value'])
attr_val_list = []
for x in self.attr_list:
y = x.split(',')
if y[1] == "value":
attr_val_list.append(y[2])
for x in self.attr_list:
y = x.split(',')
test_corpus = list(self.read_corpus([y[2]], tokens_only=True))
vector = self.doc2vec.infer_vector(test_corpus[0])
if y[1] == "nname":
df_features.loc[y[0]]["nname"] = vector
else:
df_features.loc[y[0]]["value"] = vector
X = df_features.values # N*d Feature Matrix
y = to_categorical(phishing_flag, num_classes=2).tolist() # Label
X_ = np.array([np.array(ai, dtype=np.float32) for ai in X.tolist()])
url_token = self.url_tokenizer([url])[0]
return X_, A, y, url_token, url
def url_tokenizer(self, url):
url_int_tokens = self.tokenizer.texts_to_sequences(url)
return sequence.pad_sequences(url_int_tokens, maxlen=150, padding='post')
def fetch_tr_data(self):
for _, data in self.df_tr.iterrows():
try:
X, A, y, X_A, _ = self.get_udst_inputs(data['url'], data['website'], data['result'])
except:
logging.warning(data['url'] + " | " + data['website'] + " is having an error.")
self.reset()
continue
with open(Path(self.tmp_dir + '/datasets/tr_feat.pkl'), 'ab') as f1:
pickle.dump(X, f1)
with open(Path(self.tmp_dir + '/datasets/tr_adj.pkl'), 'ab') as f2:
pickle.dump(A, f2)
with open(Path(self.tmp_dir + '/datasets/tr_class.pkl'), 'ab') as f3:
pickle.dump(y, f3)
with open(Path(self.tmp_dir + '/datasets/tr_ma.pkl'), 'ab') as f4:
pickle.dump(X_A, f4)
self.reset()
for _type_ in ['adj','class','feat','ma']:
file = Path(self.tmp_dir + '/datasets/tr_' + _type_ + '.pkl')
data = []
with open(file, 'rb') as f:
try:
while True:
data.append(pickle.load(f))
except EOFError:
pass
# save numpy array as npy file
if _type_ == 'feat':
data_feat = np.array([np.array(ai, dtype=np.float32) for ai in data])
save(self.tmp_dir + '/datasets/tr_feat.npy', data_feat)
elif _type_ == 'adj':
data_adj = asarray(data)
save(self.tmp_dir + '/datasets/tr_adj.npy', data_adj)
elif _type_ == 'class':
data_class = asarray(data, dtype=np.float32)
save(self.tmp_dir + '/datasets/tr_class.npy', data_class)
elif _type_ == 'ma':
data_ma = asarray(data)
save(self.tmp_dir + '/datasets/tr_ma.npy', data_ma)
data = None
os.remove(file)
def fetch_val_data(self):
for _, data in self.df_val.iterrows():
try:
X, A, y, X_A, _ = self.get_udst_inputs(data['url'], data['website'], data['result'])
except:
logging.warning(data['url'] + " | " + data['website'] + " is having an error.")
self.reset()
continue
with open(Path(self.tmp_dir + '/datasets/val_feat.pkl'), 'ab') as f1:
pickle.dump(X, f1)
with open(Path(self.tmp_dir + '/datasets/val_adj.pkl'), 'ab') as f2:
pickle.dump(A, f2)
with open(Path(self.tmp_dir + '/datasets/val_class.pkl'), 'ab') as f3:
pickle.dump(y, f3)
with open(Path(self.tmp_dir + '/datasets/val_ma.pkl'), 'ab') as f4:
pickle.dump(X_A, f4)
self.reset()
for _type_ in ['adj','class','feat','ma']:
file = Path(self.tmp_dir + '/datasets/val_' + _type_ + '.pkl')
data = []
with open(file, 'rb') as f:
try:
while True:
data.append(pickle.load(f))
except EOFError:
pass
# save numpy array as npy file
if _type_ == 'feat':
data_feat = np.array([np.array(ai, dtype=np.float32) for ai in data])
save(self.tmp_dir + '/datasets/val_feat.npy', data_feat)
elif _type_ == 'adj':
data_adj = asarray(data)
save(self.tmp_dir + '/datasets/val_adj.npy', data_adj)
elif _type_ == 'class':
data_class = asarray(data, dtype=np.float32)
save(self.tmp_dir + '/datasets/val_class.npy', data_class)
elif _type_ == 'ma':
data_ma = asarray(data)
save(self.tmp_dir + '/datasets/val_ma.npy', data_ma)
data = None
os.remove(file)
def fetch_te_data(self):
for _, data in self.df_te.iterrows():
try:
X, A, y, X_A, _ = self.get_udst_inputs(data['url'], data['website'], data['result'])
except:
logging.warning(data['url'] + " | " + data['website'] + " is having an error.")
self.reset()
continue
with open(Path(self.tmp_dir + '/datasets/te_feat.pkl'), 'ab') as f1:
pickle.dump(X, f1)
with open(Path(self.tmp_dir + '/datasets/te_adj.pkl'), 'ab') as f2:
pickle.dump(A, f2)
with open(Path(self.tmp_dir + '/datasets/te_class.pkl'), 'ab') as f3:
pickle.dump(y, f3)
with open(Path(self.tmp_dir + '/datasets/te_ma.pkl'), 'ab') as f4:
pickle.dump(X_A, f4)
self.reset()
for _type_ in ['adj','class','feat','ma']:
file = Path(self.tmp_dir + '/datasets/te_' + _type_ + '.pkl')
data = []
with open(file, 'rb') as f:
try:
while True:
data.append(pickle.load(f))
except EOFError:
pass
# save numpy array as npy file
if _type_ == 'feat':
data_feat = np.array([np.array(ai, dtype=np.float32) for ai in data])
save(self.tmp_dir + '/datasets/te_feat.npy', data_feat)
elif _type_ == 'adj':
data_adj = asarray(data)
save(self.tmp_dir + '/datasets/te_adj.npy', data_adj)
elif _type_ == 'class':
data_class = asarray(data, dtype=np.float32)
save(self.tmp_dir + '/datasets/te_class.npy', data_class)
elif _type_ == 'ma':
data_ma = asarray(data)
save(self.tmp_dir + '/datasets/te_ma.npy', data_ma)
data = None
os.remove(file)
def train(self):
# Load data
X_train_A = load(self.tmp_dir + '/datasets/tr_ma.npy', allow_pickle=True)
X_test_A = load(self.tmp_dir + '/datasets/te_ma.npy', allow_pickle=True)
X_val_A = load(self.tmp_dir + '/datasets/val_ma.npy', allow_pickle=True)
X_train_B, A_train_B, y_train_B = load(self.tmp_dir + '/datasets/tr_feat.npy', allow_pickle=True), list(load(self.tmp_dir + '/datasets/tr_adj.npy', allow_pickle=True)), load(self.tmp_dir + '/datasets/tr_class.npy', allow_pickle=True)
X_test_B, A_test_B, y_test_B = load(self.tmp_dir + '/datasets/te_feat.npy', allow_pickle=True), list(load(self.tmp_dir + '/datasets/te_adj.npy', allow_pickle=True)), load(self.tmp_dir + '/datasets/te_class.npy', allow_pickle=True)
X_val_B, A_val_B, y_val_B = load(self.tmp_dir + '/datasets/val_feat.npy', allow_pickle=True), list(load(self.tmp_dir + '/datasets/val_adj.npy', allow_pickle=True)), load(self.tmp_dir + '/datasets/val_class.npy', allow_pickle=True)
# Preprocessing adjacency matrices for convolution
A_train_B = [normalized_adjacency(a) for a in A_train_B]
A_val_B = [normalized_adjacency(a) for a in A_val_B]
A_test_B = [normalized_adjacency(a) for a in A_test_B]
# Load pre-trained model
model = load_model('moraphishdet.h5', custom_objects={'GraphConvSkip': GraphConvSkip, 'MinCutPool': MinCutPool,
'GlobalAvgPool': GlobalAvgPool})
# Model evaluation function
def evaluate(A_list, X_list, y_list, X_A_list, ops, batch_size):
batches = batch_iterator([A_list, X_list, y_list, X_A_list], batch_size=batch_size)
output = []
for b in batches:
X, A, I = Batch(b[0], b[1]).get('XAI')
A = sp_matrix_to_sp_tensor(A)
y = b[2]
X_A = b[3]
pred = model([X_A, X, A, I], training=False)
outs = [o(y, pred) for o in ops]
output.append(outs)
return np.mean(output, 0)
################################################################################
# PARAMETERS
################################################################################
epochs = 500 # Number of training epochs
es_patience = 20 # Patience for early stopping
learning_rate = 1e-5 # Learning rate
batch_size = 1 # Batch size. NOTE: it MUST be 1 when using MinCutPool and DiffPool
#model.summary()
# Training setup
opt = tf.keras.optimizers.Adam(learning_rate=learning_rate)
loss_fn = model.loss_functions[0]
acc_fn = lambda x, y: K.mean(tf.keras.metrics.categorical_accuracy(x, y))
# Training function
@tf.function(experimental_relax_shapes=True)
def train_step(inputs, targets):
with tf.GradientTape() as tape:
predictions = model(inputs, training=True)
loss = loss_fn(targets, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
opt.apply_gradients(zip(gradients, model.trainable_variables))
return loss, acc_fn(targets, predictions)
################################################################################
current_batch = 0
model_loss = 0
model_loss_values = []
model_val_loss_values = []
model_acc = 0
model_acc_values = []
model_val_acc_values = []
best_val_loss = np.inf
best_weights = None
patience = es_patience
batches_in_epoch = np.ceil(y_train_B.shape[0] / batch_size)
################################################################################
# FITTING MODEL
################################################################################
logging.warning('Fitting model')
batches = batch_iterator([A_train_B, X_train_B, y_train_B, X_train_A], batch_size=batch_size, epochs=epochs)
for b in batches:
X_, A_, I_ = Batch(b[0], b[1]).get('XAI')
A_ = sp_matrix_to_sp_tensor(A_)
y_ = b[2]
X_A_ = b[3]
outs = train_step([X_A_, X_, A_, I_], y_)
model_loss += outs[0]
model_acc += outs[1]
current_batch += 1
if current_batch % batches_in_epoch == 0:
model_loss /= batches_in_epoch
model_acc /= batches_in_epoch
# Compute validation loss and accuracy
val_loss, val_acc = evaluate(A_val_B, X_val_B, y_val_B, X_val_A, [loss_fn, acc_fn], batch_size=batch_size)
logging.warning('Ep. {} - Loss: {:.2f} - Acc: {:.2f} - Val loss: {:.2f} - Val acc: {:.2f}'
.format(current_batch // batches_in_epoch, model_loss, model_acc, val_loss, val_acc))
# Check if loss improved for early stopping
if val_loss < best_val_loss:
best_val_loss = val_loss
patience = es_patience
logging.warning('New best val_loss {:.3f}'.format(val_loss))
best_weights = model.get_weights()
else:
patience -= 1
if patience == 0:
logging.warning('Early stopping (best val_loss: {})'.format(best_val_loss))
break
model_loss_values.append(model_loss)
model_acc_values.append(model_acc)
model_val_loss_values.append(val_loss)
model_val_acc_values.append(val_acc)
model_loss = 0
model_acc = 0
################################################################################
# EVALUATE MODEL
################################################################################
# Load best model
model.set_weights(best_weights)
# Save model
model.save(self.tmp_dir + '/model/moraphishdet.h5')
# Test model
logging.warning('Testing model')
def prf1(A_list, X_list, y_list, X_A_list, batch_size):
batches = batch_iterator([A_list, X_list, y_list, X_A_list], batch_size=batch_size)
y_pred = []
y_true = []
for b in batches:
X, A, I = Batch(b[0], b[1]).get('XAI')
A = sp_matrix_to_sp_tensor(A)
X_A = b[3]
y_pred.append(np.argmax(model([X_A, X, A, I], training=False)))
y_true.append(np.argmax(b[2]))
return y_pred,y_true
test_loss, test_acc = evaluate(A_test_B, X_test_B, y_test_B, X_test_A, [loss_fn, acc_fn], batch_size=batch_size)
y_pred, y_true = prf1(A_test_B, X_test_B, y_test_B, X_test_A, batch_size=batch_size)
precision = metrics.precision_score(y_true, y_pred) * 100
recall = metrics.recall_score(y_true, y_pred) * 100
f1 = metrics.f1_score(y_true, y_pred) * 100
test_acc = test_acc * 100
logging.warning("+++++++++=RESULTS=+++++++++")
logging.warning("Accuracy: %.2f%%" % test_acc)
logging.warning("Precision: %.2f%%" % precision)
logging.warning("Recall: %.2f%%" % recall)
logging.warning("F1-Score: %.2f%%" % f1)
logging.warning("Loss: %.4f" % test_loss)
logging.warning("+++++++++++++==++++++++++++")
with open(Path(self.tmp_dir + '/model/model_acc.pkl'), 'wb') as f1:
pickle.dump(model_acc_values,f1)
with open(Path(self.tmp_dir + '/model/model_loss.pkl'), 'wb') as f2:
pickle.dump(model_loss_values,f2)
with open(Path(self.tmp_dir + '/model/model_val_acc.pkl'), 'wb') as f3:
pickle.dump(model_val_acc_values,f3)
with open(Path(self.tmp_dir + '/model/model_val_loss.pkl'), 'wb') as f4:
pickle.dump(model_val_loss_values,f4)
confusion_m = confusion_matrix(y_true, y_pred)
logging.warning(confusion_m)
logging.warning("DL retrain process completed")