Repository navigation
Expand file tree
/
Copy pathevaluate.py
More file actions
131 lines (120 loc) · 4.72 KB
/
Copy pathevaluate.py
File metadata and controls
131 lines (120 loc) · 4.72 KB
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
#We shall use test set here. Generator G will take test sample as input and give fake malicious sample as output.
#IDS would try to classify this fake sample
import transform
import torch
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, LSTM
from keras.layers import Conv1D, MaxPooling1D
from torch.autograd import Variable
data = transform.data_importer_IDS(Evaluate=False)
train_data= data.train.samples.values.reshape(data.train.samples.shape[0], data.train.samples.shape[1], 1)
test_data= data.test.samples.values.reshape(data.test.samples.shape[0], data.test.samples.shape[1], 1)
data_partitioned = transform.data_importer_IDS(Evaluate=True)
test_data_attack= data_partitioned.validation.samples.values.reshape(data_partitioned.validation.samples.shape[0], data_partitioned.validation.samples.shape[1], 1)
test_data_normal= data_partitioned.test.samples.values.reshape(data_partitioned.test.samples.shape[0], data_partitioned.test.samples.shape[1], 1)
# print('normal data attack cat', data.test.samples[0]['attack_cat'])
IDS=Sequential()
IDS.add(Conv1D(32, 3, input_shape=train_data.shape[1:]))
IDS.add(Activation('relu'))
IDS.add(Conv1D(64, 3))
IDS.add(Activation('relu'))
IDS.add(MaxPooling1D(pool_size=2))
IDS.add(LSTM(70, dropout=0.1))
IDS.add(Dropout(0.1))
IDS.add(Dense(10))
# IDS.add(Dense(2))
IDS.add(Activation('softmax'))
# IDS.load_weights("models/ids_binary/weight.h5")
IDS.load_weights("models/ids/weight.h5")
result=IDS.predict(test_data)
result_normal=IDS.predict(test_data_normal)
result_attack=IDS.predict(test_data_attack)
# result=IDS.predict(train_data)
# print("testLabel before processing:", data.test.labels[0:5])
TP = TP_NORMAL = TP_ATTACK = 0
TN = TN_NORMAL = TN_ATTACK = 0
FP = FP_NORMAL = FP_ATTACK = 0
FN = FN_NORMAL = FN_ATTACK = 0
classResult=result.argmax(axis=1)
classResultNormal=result_normal.argmax(axis=1)
classResultAttack=result_attack.argmax(axis=1)
# print("classResult before processing:",result[0:5])
# classResult =(result>0.5)
testLabels=data.test.labels.argmax(axis=1)
testLabelsNormal=data_partitioned.test.labels.argmax(axis=1)
testLabelsAttack=data_partitioned.validation.labels.argmax(axis=1)
# testLabels=data.train.labels.argmax(axis=1)
# print("testLabels", testLabels[0:5])
# print("classResult",classResult[0:5])
numTestSample=classResult.shape[0]
# print("total test sample", numTestSample)
for i in range(numTestSample):
temp_result = classResult[i]
temp_test = testLabels[i]
if temp_result == temp_test:
if temp_result != 0:
TP = TP + 1
else:
TN = TN + 1
else:
if temp_result != 0:
FP = FP + 1
else:
FN = FN + 1
print('Accuracy: ', (TP+TN)/(TP+TN+FP+FN))
precision = TP/(TP+FP)
recall = TP/(TP+FN)
print('Precision: ', precision)
print('Recall: ', recall)
print('F1: ', 2*(precision*recall)/(precision+recall))
numTestSampleNormal=classResultNormal.shape[0]
for i in range(numTestSampleNormal):
temp_result = classResultNormal[i]
temp_test = testLabelsNormal[i]
if temp_result == temp_test:
if temp_result == 1:
TP_NORMAL = TP_NORMAL + 1
else:
TN_NORMAL = TN_NORMAL + 1
else:
if temp_result == 1:
FP_NORMAL = FP_NORMAL + 1
else:
FN_NORMAL = FN_NORMAL + 1
print('Accuracy Normal: ', (TP_NORMAL+TN_NORMAL)/(TP_NORMAL+TN_NORMAL+FP_NORMAL+FN_NORMAL))
# precision = TP_NORMAL/(TP_NORMAL+FP_NORMAL)
# recall = TP_NORMAL/(TP_NORMAL+FN_NORMAL)
# print('Precision Normal: ', precision)
# print('Recall Normal: ', recall)
# print('F1: ', 2*(precision*recall)/(precision+recall))
numTestSampleAttack=classResultAttack.shape[0]
# print("numTestSample", numTestSample)
for i in range(numTestSampleAttack):
temp_result = classResultAttack[i]
temp_test = testLabelsAttack[i]
if temp_result == temp_test:
if temp_result == 1:
TP_ATTACK = TP_ATTACK + 1
else:
TN_ATTACK = TN_ATTACK + 1
else:
if temp_result == 1:
FP_ATTACK = FP_ATTACK + 1
else:
FN_ATTACK = FN_ATTACK + 1
print('Accuracy Attack: ', (TP_ATTACK+TN_ATTACK)/(TP_ATTACK+TN_ATTACK+FP_ATTACK+FN_ATTACK))
# precision = TP_ATTACK/(TP_ATTACK+FP_ATTACK)
# recall = TP_ATTACK/(TP_ATTACK+FN_ATTACK)
# print('Precision Attack: ', precision)
# print('Recall Attack: ', recall)
# print('F1: ', 2*(precision*recall)/(precision+recall))
G=torch.load('G_model.pth')
z = Variable(torch.randn(512,9))
G_sample = G(z)
G_sample = G_sample.detach().numpy()
sample_data = G_sample.reshape(G_sample.shape[0], G_sample.shape[1], 1)
I_sample=IDS.predict(sample_data).argmax(axis=1)
correct=I_sample[I_sample!=0]
totalG=I_sample.shape[0]
correctG=correct.shape[0]
print("Accuracy under GAN:", correctG/totalG)