-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcidds_detectors.py
More file actions
166 lines (146 loc) · 7.14 KB
/
cidds_detectors.py
File metadata and controls
166 lines (146 loc) · 7.14 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
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
import os
import sys
from contextlib import nullcontext
import joblib
from anomaly_detection.util import TorchRandomSeed
from argparse import ArgumentParser
from pathlib import Path
from collections import ChainMap
import datetime
import numpy as np
import pandas as pd
import dask.array as da
from dask_ml.wrappers import ParallelPostFit
from sklearn.svm import OneClassSVM
from sklearn.ensemble import IsolationForest
from sklearn.metrics import average_precision_score, roc_auc_score
import torch.utils.data
from data.cidds.util import get_cols_and_dtypes
from anomaly_detection.autoencoder_torch import Autoencoder
def detect_anomalies(algorithm, params, seed, evaluation_save_path, model_load_path=None, job_name=None, device='cpu'):
# setup args
source_path = './'
num_encoding = 'quantized'
data_folder = 'onehot_quantized'
if 'device' in params:
device = params.pop('device') # 'cuda', 'cpu'
print(f'device: {device}')
cols, dtypes = get_cols_and_dtypes(cat_encoding='onehot', num_encoding=num_encoding)
if job_name is not None and 'rashomon' in job_name:
np.random.seed(42) # rashomon: same seed for everything but model initialization
else:
np.random.seed(seed)
# Load model
if algorithm == 'IsolationForest':
detector_class = IsolationForest
params['random_state'] = seed
elif algorithm == 'OneClassSVM':
detector_class = OneClassSVM
params.pop('seed')
elif algorithm == 'Autoencoder':
detector_class = Autoencoder
params['n_inputs'] = len(cols)
if job_name is not None and 'rashomon' in job_name:
try:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
except NameError or ModuleNotFoundError:
pass
else:
raise ValueError(f"Variable algorithm was: {algorithm}")
# Training
with TorchRandomSeed(seed) if algorithm == 'Autoencoder' and job_name is not None and 'rashomon' in job_name else nullcontext:
detector = detector_class(**params)
if model_load_path is not None:
detector.load(model_load_path)
else:
# Load data
print(f'Loading train ...')
train = pd.read_csv(Path(source_path) / 'data' / 'cidds' / 'data_prep' / data_folder / 'train.csv.gz',
index_col=None, usecols=cols, header=0, dtype=dtypes, compression='gzip')[cols]
print('Training ...')
if algorithm == 'IsolationForest':
detector = detector.fit(train)
elif algorithm == 'OneClassSVM':
train = train.sample(frac=0.001, random_state=seed)
detector = detector.fit(train)
elif algorithm == 'Autoencoder':
with TorchRandomSeed(42) if job_name is not None and 'rashomon' in job_name else nullcontext:
detector = detector.fit(train, device=device)
del train
if job_name is not None:
out_path = Path('./outputs/models/cidds/') / algorithm
if not out_path.exists():
out_path.mkdir(parents=True)
if algorithm in ['IsolationForest', 'OneClassSVM']:
joblib.dump(detector, out_path / f'{job_name}.pkl')
elif algorithm == 'Autoencoder':
detector.save(out_path / f'{job_name}_state_dict.pt')
# Evaluation
eval_out = []
for split in ['valid', 'test']:
print(f'Loading {split} ...')
eval_data = pd.read_csv(Path(source_path) / 'data' / 'cidds' / 'data_prep' / data_folder / f'{split}.csv.gz',
index_col=None, usecols=cols + ['isNormal'], header=0,
dtype={'isNormal': np.int8, **dtypes}, compression='gzip')[cols + ['isNormal']]
y = 1 - eval_data.pop('isNormal')
print(f'Evaluating {split} ...')
if algorithm == 'IsolationForest':
scores = -1 * pd.Series(detector.score_samples(eval_data), index=eval_data.index)
out_dict = {f'auc_pr_{split}': average_precision_score(y_true=y, y_score=scores),
f'auc_roc_{split}': roc_auc_score(y_true=y, y_score=scores)}
elif algorithm == 'OneClassSVM':
scores = -1 * pd.Series(detector.predict(da.from_array(eval_data.values, chunks=(100, -1))).compute(),
index=eval_data.index)
out_dict = {f'auc_pr_{split}': average_precision_score(y_true=y, y_score=scores),
f'auc_roc_{split}': roc_auc_score(y_true=y, y_score=scores)}
elif algorithm == 'Autoencoder':
y_eval = torch.tensor(y)
x = torch.Tensor(eval_data[cols].values)
eval_data = torch.utils.data.TensorDataset(x, y_eval)
eval_loader = torch.utils.data.DataLoader(dataset=eval_data, batch_size=params['batch_size'],
num_workers=0, shuffle=False)
out_dict = detector.test(eval_loader, device=device)
out_dict = {key + f'_{split}': val for key, val in out_dict.items()}
print(out_dict)
eval_out.append(out_dict)
# Outputs
if evaluation_save_path is not None:
out_dict = dict(ChainMap(*eval_out))
out_df = pd.DataFrame()
out_df = out_df.append({**params, **out_dict, 'seed': seed, 'job_name': job_name}, ignore_index=True)
out_name = job_name if job_name is not None else datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S-%f")
out_path = Path(evaluation_save_path) / algorithm
if not out_path.exists():
out_path.mkdir(parents=True)
out_df.to_csv(out_path / f'{out_name}.csv', index=False)
if __name__ == '__main__':
"""
Argparser needs to accept all possible param_search arguments, but only passes given args to params.
"""
str_args = ('algorithm', 'evaluation_save_path', 'job_name', 'model_load_path', 'device')
float_args = ('learning_rate', 'max_samples', 'max_features', 'kernel', 'gamma', 'nu', 'tol')
int_args = ('cpus', 'n_layers', 'n_bottleneck', 'epochs', 'batch_size', 'verbose', 'seed', 'n_estimators',
'random_state', 'bootstrap', 'n_jobs', 'shrinking', 'cache_size', 'max_iter')
bool_args = ['batch_norm']
parser = ArgumentParser()
for arg in str_args:
parser.add_argument(f'--{arg}')
for arg in int_args:
parser.add_argument(f'--{arg}', type=int)
for arg in float_args:
parser.add_argument(f'--{arg}', type=float)
for arg in bool_args:
parser.add_argument(f'--{arg}', action='store_true')
args_dict = vars(parser.parse_args())
evaluation_save_path = args_dict.pop('evaluation_save_path')
model_load_path = args_dict.pop('model_load_path')
job_name = args_dict.pop('job_name')
algorithm = args_dict.pop('algorithm')
params = {key: val for key, val in args_dict.items() if val} # remove entries with None values
detect_anomalies(algorithm=algorithm,
params=params,
seed=args_dict['seed'],
evaluation_save_path=evaluation_save_path,
model_load_path=model_load_path,
job_name=job_name)