-
Notifications
You must be signed in to change notification settings - Fork 6
/
GANv2.py
495 lines (407 loc) · 17.5 KB
/
GANv2.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
"""
File used to modelize GAN for phishing detection
-----------
Generative Adversarial Networks (GAN) research applied to the phishing detection.
University of Gloucestershire
Author : Pierrick ROBIC--BUTEZ
2019
Copyright (c) 2019 Khuzd
"""
# ---------------------
# Define different seeds to permit repeatability
# ---------------------
seed_value = 42
# 1. Set the `PYTHONHASHSEED` environment variable at a fixed value
import os
os.environ['PYTHONHASHSEED'] = '0'
os.environ['CUDA_VISIBLE_DEVICES'] = ''
# 2. Set the `python` built-in pseudo-random generator at a fixed value
import random
random.seed(seed_value)
# 3. Set the `numpy` pseudo-random generator at a fixed value
import numpy as np
np.random.seed(seed_value)
# 4. Set the `tensorflow` pseudo-random generator at a fixed value
import tensorflow as tf
tf.compat.v1.set_random_seed(seed_value)
# 5. Configure a new global `tensorflow` session
from keras import backend as k
session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1,
device_count={"CPU": 1})
sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)
k.set_session(sess)
from keras.layers import Input, Dense, Reshape, Flatten
from keras.layers import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Sequential, Model, model_from_json
from keras.utils import plot_model
from sklearn.metrics import classification_report
import importData
import logging
import json
# Import logger
logger = logging.getLogger('phishGan')
# Default datasets path
PHIS_PATH_TEST = "data/phish_test.csv"
CLEAN_PATH_TEST = "data/total_test.csv"
class GAN:
"""
GAN Classe used to predict if url is phishing or not
"""
def __init__(self, lr, sample):
"""
:param lr: float (learning rate)
:param sample: int
"""
# ---------------------
# Define attributes
# ---------------------
self.channels = 1
self.countData = 46
self.hiddenLayers = 65
self.data_shape = (self.countData, self.channels)
self.thresHold = None
self.sampleSize = sample
self.dataType = "phish"
self.lr = lr
self.optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=self.lr)
# Build and compile the discriminator
self.discriminator = self.build_discriminator()
self.discriminator.compile(loss='binary_crossentropy',
optimizer=self.optimizer,
metrics=['accuracy'])
# Build the generator
self.generator = self.build_generator()
# The generator takes noise as input and generates data
z = Input(shape=(self.countData,))
img = self.generator(z)
# The discriminator takes generated data as input and determines validity
validity = self.discriminator(img)
# The combined model (stacked generator and discriminator)
# Trains the generator to fool the discriminator
self.combined = Model(z, validity)
self.combined.compile(loss='binary_crossentropy', optimizer=self.optimizer)
del validity, img, z, self.optimizer
return
def build_generator(self, plot=False):
"""
Create the generator and plot the neural network configuration if plot == True
:param plot: int
:return: Model or nothing if plot == True
"""
# ---------------------
# Define model of generator
# ---------------------
model = Sequential()
model.add(Dense(self.hiddenLayers, input_dim=self.countData))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(self.hiddenLayers))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(self.hiddenLayers))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(np.prod(self.data_shape), activation='tanh'))
model.add(Reshape(self.data_shape))
# Saving the model diagram
if plot:
plot_model(model, to_file="generator.png", show_layer_names=True, rankdir="LR")
return
model.summary()
noise = Input(shape=(self.countData,))
img = model(noise)
return Model(noise, img)
def build_discriminator(self, plot=False):
"""
Create the discriminator and plot the neural network configuration if plot == True
:return: Model or nothing if plot == True
"""
# ---------------------
# Define model of discriminator
# ---------------------
model = Sequential()
model.add(Flatten(input_shape=self.data_shape))
model.add(Dense(self.hiddenLayers))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(self.hiddenLayers))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(self.hiddenLayers))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(1, activation='sigmoid'))
# Saving the model diagram
if plot:
plot_model(model, to_file="discriminator.png", rankdir="LR", show_layer_names=True)
return
model.summary()
img = Input(shape=self.data_shape)
validity = model(img)
return Model(img, validity)
def save(self, prefix, path):
"""
Save the GAN model in path/prefix+suffix
:param prefix: string
:param path: string
:return: nothing
"""
# ---------------------
# Save Models
# ---------------------
# Combined
combined_model_json = self.combined.to_json()
with open(path + "/" + prefix + "combined_model.json", "w") as json_file:
json_file.write(combined_model_json)
# Discriminator
discriminator_model_json = self.discriminator.to_json()
with open(path + "/" + prefix + "discriminator_model.json", "w") as json_file:
json_file.write(discriminator_model_json)
# Generator
generator_model_json = self.generator.to_json()
with open(path + "/" + prefix + "generator_model.json", "w") as json_file:
json_file.write(generator_model_json)
# ---------------------
# Save weights
# ---------------------
self.combined.save_weights(path + "/" + prefix + "combined_model.h5")
self.discriminator.save_weights(path + "/" + prefix + "discriminator_model.h5")
self.generator.save_weights(path + "/" + prefix + "generator_model.h5")
# ---------------------
# Save object
# ---------------------
with open(path + "/" + prefix + "object.json", "w") as json_file:
tmp = self.__dict__
tmp["generator"] = None
tmp["discriminator"] = None
tmp["combined"] = None
tmp["optimizer"] = None
print(tmp)
json_file.write(json.dumps(tmp))
del generator_model_json, discriminator_model_json, combined_model_json
def load(self, prefix, path):
"""
Load the GAN model in path/prefix+suffix
:param prefix: string
:param path: string
:return: nothing
"""
# ---------------------
# Load object
# ---------------------
with open(path + "/" + prefix + "object.json", "r") as json_file:
tmp = json.loads(json_file.read())
self.__dict__.update(tmp)
# ---------------------
# Load models
# ---------------------
# Combined
json_file = open(path + "/" + prefix + "combined_model.json", 'r')
loaded_model_json = json_file.read()
json_file.close()
self.combined = model_from_json(loaded_model_json)
# Discriminator
json_file = open(path + "/" + prefix + "discriminator_model.json", 'r')
loaded_model_json = json_file.read()
json_file.close()
self.discriminator = model_from_json(loaded_model_json)
# Generator
json_file = open(path + "/" + prefix + "generator_model.json", 'r')
loaded_model_json = json_file.read()
json_file.close()
self.generator = model_from_json(loaded_model_json)
# ---------------------
# Load weights
# ---------------------
self.combined.load_weights(path + "/" + prefix + "combined_model.h5")
self.discriminator.load_weights(path + "/" + prefix + "discriminator_model.h5")
self.generator.load_weights(path + "/" + prefix + "generator_model.h5")
# ---------------------
# Load optimizer
# ---------------------
self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr)
del json_file, loaded_model_json
def class_report(self, cleanTestDataset, phishTestDataset, calculate=True, determineThreshold=True):
"""
Classification report for the GAN after training
:param cleanTestDataset: list of list
:param phishTestDataset: list of list
:param calculate: bool
:param determineThreshold: bool (used to determine if the threshold is used or calculated)
:return: print
"""
# ---------------------
# Construct the true results
# ---------------------
true = ["clean"] * len(cleanTestDataset) + ["phish"] * len(phishTestDataset)
predict = []
prediction = []
# ---------------------
# Make predicition
# ---------------------
for i in cleanTestDataset + phishTestDataset:
prediction.append(
self.discriminator.predict_on_batch(np.array(i).astype(np.float)[:].reshape(1, self.countData, 1)))
if determineThreshold:
# Calculate the best threshold
self.thresHold = float(((sum(prediction[:len(cleanTestDataset)]) / len(cleanTestDataset)) + (
sum(prediction[len(cleanTestDataset):]) / len(phishTestDataset))) / 2)
if calculate:
# Generate the predict results
for i in prediction:
if self.dataType == "phish" and i[0][0] > self.thresHold:
predict.append("phish")
elif self.dataType != "phish" and i[0][0] < self.thresHold:
predict.append("phish")
else:
predict.append("clean")
return classification_report(np.array(true), np.array(predict), output_dict=True)
return
def best_threshold_calculate(self, cleanTestPath, phishTestPath, step, return_report=True):
"""
Use to determine the best threshold for prediction
:param cleanTestPath: str
:param phishTestPath: str
:param step: float
:param return_report: bool
:return:
"""
phisTest = list(importData.csv_to_list(phishTestPath)[1].values())
cleanTest = list(importData.csv_to_list(cleanTestPath)[1].values())
if len(cleanTest) > len(phisTest):
cleanTest = cleanTest[:len(phisTest)]
else:
phisTest = phisTest[len(cleanTest)]
# Construct the true results
true = ["clean"] * len(cleanTest) + ["phish"] * len(phisTest)
prediction = []
# ---------------------
# Make prediction
# ---------------------
for i in cleanTest + phisTest:
prediction.append(
self.discriminator.predict_on_batch(np.array(i).astype(np.float)[:].reshape(1, self.countData, 1)))
averages = (
(sum(prediction[:len(cleanTest)]) / len(cleanTest)), (sum(prediction[len(cleanTest):]) / len(phisTest)))
mini = min(averages)
maxi = max(averages)
bestClass = {"accuracy": 0}
print("Total of iteration :{}".format(len(np.arange(mini, maxi, step))))
for threshold in np.arange(mini, maxi, step):
predict = []
for i in prediction:
if self.dataType == "phish" and i[0][0] > threshold:
predict.append("phish")
elif self.dataType != "phish" and i[0][0] < threshold:
predict.append("phish")
else:
predict.append("clean")
report = classification_report(np.array(true), np.array(predict), output_dict=True)
if report["accuracy"] > bestClass["accuracy"]:
bestClass = report
self.thresHold = threshold
if return_report:
return bestClass
def train(self, epochs, data, plotFrequency=20, predict=False, phishData=None, cleanData=None):
"""
Train the GAN
:param epochs: int
:param data: string (path to the dataset used to train the GAN)
:param plotFrequency: int
:param predict bool (if the training include prediction on test datasets)
:param phishData: list of lists
:param cleanData: list of lists
:return: list of 7 list (to plot training/validation accuracy/loss of generator/discriminator)
"""
# Load the training dataset
X_train = list(data)
# Load testing datasets
if phishData is None or cleanData is None:
phisTest = list(importData.csv_to_list(PHIS_PATH_TEST)[1].values())
cleanTest = list(importData.csv_to_list(CLEAN_PATH_TEST)[1].values())
else:
phisTest = list(phishData)
cleanTest = list(cleanData)
if len(cleanTest) > len(phisTest):
cleanTest = cleanTest[:len(phisTest)]
else:
phisTest = phisTest[len(cleanTest)]
# Adversarial ground truths
valid = np.ones((self.sampleSize, 1))
fake = np.zeros((self.sampleSize, 1))
# Initialize list for the return values
accuracy = []
Dloss = []
Gloss = []
vaccuracy = []
vDloss = []
vGloss = []
X = []
bestEpoch = -1
bestClass = {"accuracy": 0}
for epoch in range(1, epochs + 1):
# Select a random batch of images
# for training
idxt = np.random.randint(1, int(len(X_train) * 0.9), self.sampleSize)
imgst = np.vstack(np.array(X_train)[idxt])
# for validation
idxv = np.random.randint(int(len(X_train) * 0.9), len(X_train), self.sampleSize)
imgsv = np.vstack(np.array(X_train)[idxv])
# ---------------------
# Training
# ---------------------
noise = np.random.normal(0, 1, (self.sampleSize, self.countData))
# Generate a batch of new data for training
gen_data = self.generator.predict(noise)
# ---------------------
# Train Discriminator
# ---------------------
d_loss_real = self.discriminator.train_on_batch(imgst.reshape(self.sampleSize, self.countData, 1), valid)
d_loss_fake = self.discriminator.train_on_batch(gen_data, fake)
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
# ---------------------
# Train Generator
# ---------------------
noise = np.random.normal(0, 1, (self.sampleSize, self.countData))
# Train the generator (to have the discriminator label samples as valid)
g_loss = self.combined.train_on_batch(noise, valid)
# ---------------------
# Validation
# ---------------------
noise = np.random.normal(0, 1, (self.sampleSize, self.countData))
# Generate a batch of new data for validation
gen_data = self.generator.predict(noise)
# ---------------------
# Validate Discriminator
# ---------------------
vd_loss_real = self.discriminator.test_on_batch(imgsv.reshape(self.sampleSize, self.countData, 1), valid)
vd_loss_fake = self.discriminator.test_on_batch(gen_data, fake)
vd_loss = 0.5 * np.add(vd_loss_real, vd_loss_fake)
# ---------------------
# Validate Generator
# ---------------------
noise = np.random.normal(0, 1, (self.sampleSize, self.countData))
vg_loss = self.combined.test_on_batch(noise, valid)
# Plot the progress
if epoch % plotFrequency == 0:
logger.info("%d [D loss: %f, acc.: %.2f%%] [G loss: %f] [D vloss: %f, vacc.: %.2f%%] [G vloss: %f]" % (
epoch, d_loss[0], 100 * d_loss[1], g_loss, vd_loss[0], 100 * vd_loss[1], vg_loss))
accuracy.append(d_loss[1])
X.append(epoch)
Dloss.append(d_loss[0])
Gloss.append(g_loss)
vaccuracy.append(vd_loss[1])
vDloss.append(vd_loss[0])
vGloss.append(vg_loss)
# Generate the classificaiton report if necessary
if predict:
report = self.class_report(cleanTest, phisTest)
if "accuracy" in report:
if report["accuracy"] > bestClass["accuracy"]:
bestClass = report
bestEpoch = epoch
del report
del idxt, imgst, idxv, imgsv, noise, g_loss, gen_data, d_loss, d_loss_real, d_loss_fake, vd_loss_real, \
vd_loss, vd_loss_fake, vg_loss
del X_train
if not predict:
self.class_report(cleanTest, phisTest, calculate=False)
return X, accuracy, Dloss, Gloss, vaccuracy, vDloss, vGloss, bestClass, bestEpoch