-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathaverage_all_detection_results.py
139 lines (115 loc) · 5.66 KB
/
average_all_detection_results.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
import os
import sys
import re
import numpy as np
from collections import Counter
import cade.data as data
def main(dataset, use_pure_ae, families_cnt, last_label, margin, mad, cae_lambda):
if use_pure_ae == 0:
REPORT_FOLDER = 'reports'
else:
REPORT_FOLDER = 'pure_ae_reports'
if dataset == 'drebin':
families = range(families_cnt)
else:
families = range(1, families_cnt)
name_dict = {1: 'SSH', 2: 'Hulk', 3: 'Infilteration'}
p1 = re.compile('precision: \d+\.\d+')
p2 = re.compile('recall: \d+\.\d+')
p3 = re.compile('f1: \d+\.\d+')
p4 = re.compile('best inspection count: \d+')
precision_list, recall_list, f1_list, inspect_cnt_list = [], [], [], []
involved_families_list = []
normalized_inspect_cnt_list = []
for i in families:
'''calc how many new family samples in the testing set'''
if dataset == 'drebin':
single_dataset = f'drebin_new_{i}'
name = i
else:
single_dataset = f'IDS_new_{name_dict[i]}'
name = name_dict[i]
X_train, y_train, X_test, y_test = data.load_features(single_dataset, i)
total_new_family = len(np.where(y_test == last_label)[0])
'''record results for each family'''
result_path = os.path.join(f'{REPORT_FOLDER}', single_dataset, f'dist_mlp_one_by_one_check_pr_value_m{margin}_mad{mad}_lambda{cae_lambda}.csv')
with open(result_path, 'r') as f:
content = f.read()
precision = float(re.findall(p1, content)[0].replace('precision: ', '')) / 100
recall = float(re.findall(p2, content)[0].replace('recall: ', '')) / 100
f1 = float(re.findall(p3, content)[0].replace('f1: ', '')) / 100
inspect_cnt = int(re.findall(p4, content)[0].replace('best inspection count: ', ''))
print(f'family {name}: precision: {precision * 100}%, recall: {recall * 100}%, f1: {f1 * 100}%, inspect: {inspect_cnt}')
precision_list.append(precision)
recall_list.append(recall)
f1_list.append(f1)
inspect_cnt_list.append(inspect_cnt)
normalized_inspect_cnt_list.append(inspect_cnt / total_new_family)
# check the involved families in the drifting samples when we get the best results
involved_families = []
with open(result_path, 'r') as f:
next(f)
for idx, line in enumerate(f):
if idx < inspect_cnt:
line = line.strip().split(',')
real = line[1]
involved_families.append(real)
involved_families_list.append(involved_families)
print('============================================')
print('avg +/- std (final result in Table 3): ')
print(f'precision: {np.average(precision_list) * 100:.2f}% +/- {np.std(precision_list):.2f}')
print(f'recall: {np.average(recall_list) * 100:.2f}% +/- {np.std(recall_list):.2f}')
print(f'f1: {np.average(f1_list) * 100:.2f}% +/- {np.std(f1_list):.2f}')
print(f'inspect_cnt: {np.average(inspect_cnt_list):.2f} +/- {np.std(inspect_cnt_list):.2f}')
print(f'normalized inspect_cnt: {np.average(normalized_inspect_cnt_list):.2f} ' +
f'+/- {np.std(normalized_inspect_cnt_list):.2f}')
print('============================================')
saved_report_folder = f'{REPORT_FOLDER}/average_{dataset}'
os.makedirs(saved_report_folder, exist_ok=True)
with open(f'{saved_report_folder}/average_{dataset}_result_margin{margin}_mad{mad}_lambda{cae_lambda}.txt', 'w') as f:
f.write('family_idx,precision,recall,f1,insepct_cnt,normalized_inspect_cnt\n')
for i in range(len(precision_list)):
if dataset == 'drebin':
name = i
else:
name = name_dict[i+1]
f.write(f'{name},{precision_list[i]:.4f},{recall_list[i]:.4f},{f1_list[i]:.4f},' + \
f'{inspect_cnt_list[i]:.2f},{normalized_inspect_cnt_list[i]:.2f}\n')
f.write('============================================\n')
f.write('avg +/- std (final result in Table 3): \n')
f.write(f'precision: {np.average(precision_list) * 100:.2f}% +/- {np.std(precision_list):.2f}\n')
f.write(f'recall: {np.average(recall_list) * 100:.2f}% +/- {np.std(recall_list):.2f}\n')
f.write(f'f1: {np.average(f1_list) * 100:.2f}% +/- {np.std(f1_list):.2f} \n')
f.write(f'inspect_cnt: {np.average(inspect_cnt_list):.2f} +/- {np.std(inspect_cnt_list):.2f}\n')
f.write(f'normalized inspect_cnt: {np.average(normalized_inspect_cnt_list):.2f} ' +
f'+/- {np.std(normalized_inspect_cnt_list):.2f}\n')
f.write('============================================\n')
for i in range(len(involved_families_list)):
if dataset == 'drebin':
name = i
else:
name = name_dict[i+1]
f.write(f'family {name}:\t families detected as drifting: {Counter(involved_families_list[i])}\n')
if __name__ == "__main__":
if len(sys.argv) != 3:
print(f'usage: "python -u average_all_detection_results.py drebin 0", ' +
'where 0 for CADE, 1 for vanilla autoencoder. You may also specify to use drebin or IDS.')
sys.exit(-1)
dataset = sys.argv[1]
use_pure_ae = int(sys.argv[2])
if dataset == 'drebin':
families_cnt = 8
last_label = 7
elif dataset == 'IDS':
families_cnt = 4
last_label = 3
else:
print('dataset could only be "drebin" or "IDS"')
sys.exit(-1)
if use_pure_ae:
mad = 0.0
else:
mad = 3.5
margin = 10.0
cae_lambda = 0.1
main(dataset, use_pure_ae, families_cnt, last_label, margin, mad, cae_lambda)