-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·211 lines (200 loc) · 9.45 KB
/
main.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
#! /usr/bin/env python3
from lib.utils import *
from lib.pcapAE import PcapAE
from lib.earlystopping import EarlyStopping
from ad.ad import *
try:
blob = prolog()
except Exception as error:
exit(error)
else:
ARGS = blob['ARGS']
PARSER = blob['PARSER']
EXP_NAME = blob['EXP_NAME']
EXTRA = blob['EXTRA']
DEVICE = blob['DEVICE']
WRITER = blob['WRITER']
CALL_STRING = blob['CALL_STRING']
del blob
# pcapAE
if ARGS.train is not None:
encoder_params, decoder_params = get_net(cell='GRU' if ARGS.cell.upper() == 'GRU' else 'LSTM',
device=DEVICE,
size=infer_size(ARGS.train),
no_bn=ARGS.no_bn,
dropout=ARGS.dropout,
verbose=ARGS.verbose)
if ARGS.retrain:
compressor = PcapAE(model_path=ARGS.model,
device=DEVICE,
n_frames_input=ARGS.finput,
n_frames_output=ARGS.foutput,
log_dir=ARGS.dir,
verbose=ARGS.verbose,
name=EXP_NAME)
else:
compressor = PcapAE(encoder_params=encoder_params,
decoder_params=decoder_params,
n_frames_input=ARGS.finput,
n_frames_output=ARGS.foutput,
device=DEVICE,
log_dir=ARGS.dir,
verbose=ARGS.verbose,
name=EXP_NAME)
savelog(ARGS.dir,
EXP_NAME,
PARSER.parse_args(),
compressor.model.__str__(),
encoder_params,
decoder_params,
ARGS.dropout,
CALL_STRING,
ARGS.loss,
ARGS.optim,
ARGS.scheduler,
ARGS.clipping,
show_print=ARGS.verbose)
compressor.fit(train_set=ARGS.train,
vali_set=ARGS.vali,
criterion=ARGS.loss,
optimizer=ARGS.optim,
scheduler=ARGS.scheduler,
early_stopping=EarlyStopping(ARGS.dir,
patience=7,
model=compressor,
verbose=ARGS.verbose,
exp_tag=EXP_NAME),
log_dir=ARGS.dir,
epochs=ARGS.epochs,
batch_size=ARGS.batch_size,
learn_rate=ARGS.learn_rate,
gradient_clip_value=ARGS.clipping,
num_workers=ARGS.workers,
fraction=ARGS.fraction,
no_cache=ARGS.noCache,
writer=WRITER,
return_net=True)
if ARGS.fit != '' and 'redu_data' not in ARGS.fit and not ARGS.baseline:
try:
compressor
except NameError:
compressor = PcapAE(model_path=ARGS.model,
device=DEVICE,
log_dir=ARGS.dir,
verbose=ARGS.verbose,
name=EXP_NAME)
return_data = compressor.transform(data=ARGS.fit,
num_workers=ARGS.workers,
save_path=f"{ARGS.dir}/redu_data/{EXP_NAME}/",
return_data=ARGS.AD)
if ARGS.AD:
train_data = return_data[0]
if ARGS.predict:
compressor.transform(data=ARGS.predict,
num_workers=ARGS.workers,
save_path=f"{ARGS.dir}/redu_data/{EXP_NAME}/")
if ARGS.fit == '' and ARGS.predict != '' and ARGS.model and 'AD' not in ARGS.model and ARGS.baseline != 'pcapAE':
compressor = PcapAE(model_path=ARGS.model,
device=DEVICE,
n_frames_input=ARGS.finput,
n_frames_output=ARGS.foutput,
log_dir=ARGS.dir,
verbose=ARGS.verbose,
name=EXP_NAME)
compressor.transform(data=ARGS.predict,
num_workers=ARGS.workers,
save_path=f"{ARGS.dir}/redu_data/{EXP_NAME}/")
# AD
if ARGS.AD or all([x in ARGS.model for x in ['save_model', 'AD']]) or ARGS.baseline == 'pcapAE':
classifier = AD(blueprint=None if ARGS.baseline == 'pcapAE' else ARGS.model,
EXP_NAME=EXP_NAME,
n_jobs=ARGS.n_jobs,
verbose=ARGS.verbose)
start_time = time.now()
if ARGS.baseline == 'noDL':
if ARGS.fit == 'skip':
train_data = None
else:
train_data, _ = get_raw_data(ARGS.fit, ARGS)
else:
try:
train_data
except NameError:
if ARGS.fit != '':
train_data = load_compressed_data(ARGS.fit)
else:
train_data = None
if train_data is not None or ARGS.baseline == 'pcapAE' or ARGS.fit == 'skip':
if ARGS.grid_search:
with console.status("[bold green]grid search...", spinner='dots') as status:
classifier.grid_search(data=train_data,
truth=ARGS.fit)
if ARGS.baseline == 'pcapAE':
compressor = PcapAE(model_path=ARGS.model,
device=DEVICE,
n_frames_input=ARGS.finput,
n_frames_output=ARGS.foutput,
log_dir=ARGS.dir,
verbose=ARGS.verbose,
name=EXP_NAME)
threshold = ARGS.threshold
# returns (threshold, [(preds_predict, ground_predict), (preds, grounds)])
datas = [ARGS.predict, ARGS.vali, ARGS.eval] if ARGS.eval != '' else [ARGS.predict]
threshold, pred_gt_list = compressor.predict(datas=datas,
num_workers=ARGS.workers,
NORMAL_LABEL=NORMAL,
ANOMALY_LABEL=ANOMALY,
threshold=None if (threshold == '') else sani(threshold),
WRITER=WRITER,
log_dir=ARGS.dir,
extra_lable=EXTRA,
name=EXP_NAME)
for ds_name, (preds, truth) in zip(datas, pred_gt_list):
classifier.calc_metrics(predicted=preds,
truth=truth,
show=True,
save=ds_name,
WRITER=WRITER,
log_dir=ARGS.dir,
threshold=(0, threshold),
in_file=ARGS.predict,
time=time.now()-start_time,
save_ano_pids='y',)
classifier.clean(ARGS.dir)
else:
if train_data is not None:
classifier.fit_data(train_data, ARGS.dir)
del train_data
if ARGS.baseline == 'noDL':
if train_data is not None:
predict_data, truth = get_raw_data(ARGS.vali, ARGS)
classifier.calc_metrics(predicted=classifier.predict_data(data=predict_data),
truth=truth,
show=True,
save=ARGS.vali,
WRITER=WRITER,
log_dir=ARGS.dir,
in_file=ARGS.vali,
time=time.now()-start_time,
save_ano_pids='y',)
# eval
eval_data, truth = get_raw_data(ARGS.predict, ARGS)
classifier.calc_metrics(predicted=classifier.predict_data(data=eval_data),
truth=truth,
show=True,
save=ARGS.predict,
WRITER=WRITER,
log_dir=ARGS.dir,
in_file=ARGS.predict,
time=time.now()-start_time,
save_ano_pids='y',)
if ARGS.predict != '' and 'redu_data' in ARGS.predict:
classifier.calc_metrics(predicted=classifier.predict_data(data=load_compressed_data(ARGS.predict)),
truth=ARGS.predict,
show=True,
save=ARGS.predict,
WRITER=WRITER,
log_dir=ARGS.dir,
in_file=ARGS.predict,
time=time.now()-start_time,
save_ano_pids='y',)