-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanomaly_detection.py
263 lines (232 loc) · 15.8 KB
/
anomaly_detection.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
import argparse
import os, sys
import torch
import pickle
import preprocess_data
import pandas as pd
from model import model as imported_model
from torch import optim
from torch import nn
from pathlib import Path
from matplotlib import pyplot as plt
import numpy as np
import pdb
from sklearn.svm import SVR, LinearSVR
from sklearn.model_selection import GridSearchCV
from anomalyDetector import fit_norm_distribution_param
from anomalyDetector import anomalyScore
from anomalyDetector import get_precision_recall
def main(args_):
print('-' * 89)
print("=> loading checkpoint ")
checkpoint = torch.load(str(Path(args_.save_path,args_.model,args_.data,'model_best',args_.filename).with_suffix('.pth'))) #using model with lowest val error
args = checkpoint['args']
args.prediction_window_size= args_.prediction_window_size
args.beta = args_.beta
args.save_fig = False #args_.save_fig
args.compensate = args_.compensate
args.use_SVR = args_.use_SVR
args.device = args_.device
args.dropout = 0.0
args.result_path = args_.result_path
print("=> loaded checkpoint")
os.makedirs(os.path.join(args.result_path, args.model, args_.data, args_.filename[:-4]), exist_ok=True)
# Set the random seed manually for reproducibility.
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
###############################################################################
# Load data
###############################################################################
TimeseriesData = preprocess_data.PickleDataLoad(data_type=args.data,filename=args.filename, augment_test_data=False)
train_dataset = TimeseriesData.batchify(args,TimeseriesData.trainData[:TimeseriesData.length], bsz=1)
test_dataset = TimeseriesData.batchify(args,TimeseriesData.testData, bsz=1)
###############################################################################
# Build the model
###############################################################################
nfeatures = TimeseriesData.trainData.size(-1)
model = imported_model.RNNPredictor(rnn_type = args.model,
enc_inp_size=nfeatures,
rnn_inp_size = args.emsize,
rnn_hid_size = args.nhid,
dec_out_size=nfeatures,
nlayers = args.nlayers,
dropout=0.0,
res_connection=args.res_connection).to(torch.device(args.device))
model.load_state_dict(checkpoint['state_dict'])
optimizer = optim.Adam(model.parameters(), lr= args.lr,weight_decay=args.weight_decay)
criterion = nn.MSELoss()
scores, predicted_scores, precisions, recalls, f_betas = list(), list(), list(), list(), list()
targets, mean_predictions, oneStep_predictions, Nstep_predictions = list(), list(), list(), list()
try:
# For each channel in the dataset
for channel_idx in range(nfeatures):
if args.use_SVR:
save_dir = Path(args.result_path, args.model, args.data, args.filename).with_suffix('')
save_dir.mkdir(parents=True, exist_ok=True)
print("replacing model with an svr predictor.")
model = LinearSVR(max_iter=5000)#GridSearchCV(LinearSVR(), cv=5,param_grid={"C": [1e0, 1e1, 1e2], "loss":["epsilon_insensitive", "squared_epsilon_insensitive"], "max_iter":['2000']}, n_jobs=1)
td = train_dataset.squeeze(-1).cpu()
model.fit(td, torch.roll(td.squeeze(-1), -1))
print('=> calculating anomaly scores')
score = model.predict(test_dataset.squeeze(-1).cpu())
scoreframe = pd.DataFrame(score)
scoreframe = scoreframe / scoreframe.max()
scoreframe.to_csv(save_dir.joinpath('scores.csv'))
break
''' 1. Load mean and covariance if they are pre-calculated, if not calculate them. '''
# Mean and covariance are calculated on train dataset.
if 'means' in checkpoint.keys() and 'covs' in checkpoint.keys():
print('=> loading pre-calculated mean and covariance')
mean, cov = checkpoint['means'][channel_idx], checkpoint['covs'][channel_idx]
else:
print('=> calculating mean and covariance')
mean, cov = fit_norm_distribution_param(args, model, train_dataset, channel_idx=channel_idx)
''' 2. Train anomaly score predictor using support vector regression (SVR). (Optional) '''
# An anomaly score predictor is trained
# given hidden layer output and the corresponding anomaly score on train dataset.
# Predicted anomaly scores on test dataset can be used for the baseline of the adaptive threshold.
if args.compensate:
print('=> training an SVR as anomaly score predictor')
train_score, _, _, hiddens, _, exectime = anomalyScore(args, model, train_dataset, mean, cov, channel_idx=channel_idx)
#score_predictor = GridSearchCV(SVR(), cv=5,param_grid={"C": [1e0, 1e1, 1e2],"gamma": np.logspace(-1, 1, 3)}, n_jobs=8)
#score_predictor = GridSearchCV(LinearSVR(), cv=5,param_grid={"C": [1e0, 1e1, 1e2], "loss":["epsilon_insensitive", "squared_epsilon_insensitive"], "max_iter":['2000']}, n_jobs=8)
score_predictor.fit(torch.cat(hiddens,dim=0).numpy(), train_score.cpu().numpy())
else:
score_predictor=None
''' 3. Calculate anomaly scores'''
# Anomaly scores are calculated on the test dataset
# given the mean and the covariance calculated on the train dataset
print('=> calculating anomaly scores')
score, sorted_prediction, sorted_error, _, predicted_score, exectime = anomalyScore(args, model, test_dataset, mean, cov, optimizer, criterion,
online=True,
score_predictor=score_predictor,
channel_idx=channel_idx)
''' 4. Evaluate the result '''
# The obtained anomaly scores are evaluated by measuring precision, recall, and f_beta scores
# The precision, recall, f_beta scores are are calculated repeatedly,
# sampling the threshold from 1 to the maximum anomaly score value, either equidistantly or logarithmically.
print('=> calculating precision, recall, and f_beta')
precision, recall, f_beta = get_precision_recall(args, score, num_samples=10, beta=args.beta,
label=TimeseriesData.testLabel.to(args.device))
#print(f_beta)
#print('data: ',args.data,' filename: ',args.filename,
# ' f-beta (no compensation): ', f_beta.max().item(),' beta: ',args.beta)
if args.compensate:
precision, recall, f_beta = get_precision_recall(args, score, num_samples=1000, beta=args.beta,
label=TimeseriesData.testLabel.to(args.device),
predicted_score=predicted_score)
print(f_beta)
#print('data: ',args.data,' filename: ',args.filename,
# ' f-beta (compensation): ', f_beta.max().item(),' beta: ',args.beta)
with open(os.path.join(args.result_path, args.model, args_.data, args_.filename[:-4], "exectime.txt"), mode='w') as f:
f.write(str(exectime))
target = preprocess_data.reconstruct(test_dataset.cpu()[:, 0, channel_idx],
TimeseriesData.mean[channel_idx],
TimeseriesData.std[channel_idx]).numpy()
mean_prediction = preprocess_data.reconstruct(sorted_prediction.mean(dim=1).cpu(),
TimeseriesData.mean[channel_idx],
TimeseriesData.std[channel_idx]).numpy()
oneStep_prediction = preprocess_data.reconstruct(sorted_prediction[:, -1].cpu(),
TimeseriesData.mean[channel_idx],
TimeseriesData.std[channel_idx]).numpy()
Nstep_prediction = preprocess_data.reconstruct(sorted_prediction[:, 0].cpu(),
TimeseriesData.mean[channel_idx],
TimeseriesData.std[channel_idx]).numpy()
sorted_errors_mean = sorted_error.abs().mean(dim=1).cpu()
sorted_errors_mean *= TimeseriesData.std[channel_idx]
sorted_errors_mean = sorted_errors_mean.numpy()
score = score.cpu()
scores.append(score), targets.append(target), predicted_scores.append(predicted_score)
mean_predictions.append(mean_prediction), oneStep_predictions.append(oneStep_prediction)
Nstep_predictions.append(Nstep_prediction)
precisions.append(precision), recalls.append(recall), f_betas.append(f_beta)
if args.save_fig:
save_dir = Path(args.result_path, args.model, args.data,args.filename).with_suffix('').joinpath('fig_detection')
save_dir.mkdir(parents=True,exist_ok=True)
plt.plot(precision.cpu().numpy(),label='precision')
plt.plot(recall.cpu().numpy(),label='recall')
plt.plot(f_beta.cpu().numpy(), label='f1')
plt.legend()
plt.xlabel('Threshold (log scale)')
plt.ylabel('Value')
plt.title('Anomaly Detection on ' + args.data + ' Dataset', fontsize=18, fontweight='bold')
plt.savefig(str(save_dir.joinpath('fig_f_beta_channel'+str(channel_idx)).with_suffix('.png')))
plt.close()
fig, ax1 = plt.subplots(figsize=(15,5))
ax1.plot(target,label='Target',
color='black', marker='.', linestyle='--', markersize=1, linewidth=0.5)
ax1.plot(mean_prediction, label='Mean predictions',
color='purple', marker='.', linestyle='--', markersize=1, linewidth=0.5)
ax1.plot(oneStep_prediction, label='1-step predictions',
color='green', marker='.', linestyle='--', markersize=1, linewidth=0.5)
ax1.plot(Nstep_prediction, label=str(args.prediction_window_size) + '-step predictions',
color='blue', marker='.', linestyle='--', markersize=1, linewidth=0.5)
ax1.plot(sorted_errors_mean,label='Absolute mean prediction errors',
color='orange', marker='.', linestyle='--', markersize=1, linewidth=1.0)
ax1.legend(loc='upper left')
ax1.set_ylabel('Value',fontsize=15)
ax1.set_xlabel('Index',fontsize=15)
ax2 = ax1.twinx()
ax2.plot(score.numpy().reshape(-1, 1), label='Anomaly scores from \nmultivariate normal distribution',
color='red', marker='.', linestyle='--', markersize=1, linewidth=1)
if args.compensate:
ax2.plot(predicted_score, label='Predicted anomaly scores from SVR',
color='cyan', marker='.', linestyle='--', markersize=1, linewidth=1)
#ax2.plot(score.reshape(-1,1)/(predicted_score+1),label='Anomaly scores from \nmultivariate normal distribution',
# color='hotpink', marker='.', linestyle='--', markersize=1, linewidth=1)
ax2.legend(loc='upper right')
ax2.set_ylabel('anomaly score',fontsize=15)
#plt.axvspan(2830,2900 , color='yellow', alpha=0.3)
plt.title('Anomaly Detection on ' + args.data + ' Dataset', fontsize=18, fontweight='bold')
plt.tight_layout()
plt.xlim([0,len(test_dataset)])
#plt.savefig(str(save_dir.joinpath('fig_scores_channel'+str(channel_idx)).with_suffix('.svg')))
#plt.show()
plt.close()
except KeyboardInterrupt:
print('-' * 89)
print('Exiting from training early')
if not args.use_SVR:
print('=> saving the results as pickle extensions')
save_dir = Path(args.result_path, args.model, args.data, args.filename).with_suffix('')
save_dir.mkdir(parents=True, exist_ok=True)
# pickle.dump(targets, open(str(save_dir.joinpath('target.pkl')),'wb'))
# pickle.dump(mean_predictions, open(str(save_dir.joinpath('mean_predictions.pkl')),'wb'))
# pickle.dump(oneStep_predictions, open(str(save_dir.joinpath('oneStep_predictions.pkl')),'wb'))
# pickle.dump(Nstep_predictions, open(str(save_dir.joinpath('Nstep_predictions.pkl')),'wb'))
# pickle.dump(scores, open(str(save_dir.joinpath('score.pkl')),'wb'))
# pickle.dump(predicted_scores, open(str(save_dir.joinpath('predicted_scores.pkl')),'wb'))
# pickle.dump(precisions, open(str(save_dir.joinpath('precision.pkl')),'wb'))
# pickle.dump(recalls, open(str(save_dir.joinpath('recall.pkl')),'wb'))
# pickle.dump(f_betas, open(str(save_dir.joinpath('f_beta.pkl')),'wb'))
# pd.DataFrame(targets).T.to_csv(save_dir.joinpath('targets.csv'))
# pd.DataFrame(mean_predictions).T.to_csv(save_dir.joinpath('mean_predictions.csv'))
# pd.DataFrame(oneStep_predictions).T.to_csv(save_dir.joinpath('oneStep_predictions.csv'))
# pd.DataFrame(Nstep_predictions).T.to_csv(save_dir.joinpath('Nstep_predictions.csv'))
scoreframe = pd.DataFrame(scores[0].numpy())
scoreframe = scoreframe / scoreframe.max()
scoreframe.to_csv(save_dir.joinpath('scores.csv'))
# pd.DataFrame(precisions).T.to_csv(save_dir.joinpath('precisions.csv'))
# pd.DataFrame(recalls).T.to_csv(save_dir.joinpath('recalls.csv'))
#pd.DataFrame(f_betas).T.to_csv(save_dir.joinpath('f_betas.csv'))
#pd.DataFrame(predicted_scores).T.to_csv(save_dir.joinpath('predicted_scores.csv'))
print('-' * 89)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='PyTorch RNN Anomaly Detection Model')
parser.add_argument('--prediction_window_size', type=int, default=1,
help='prediction_window_size')
parser.add_argument('--data', type=str, default='IMS_bearings_test2',
help='type of the dataset (ecg, gesture, power_demand, space_shuttle, respiration, nyc_taxi')
parser.add_argument('--filename', type=str, default='combinedfiles_s0.pkl',
help='filename of the dataset')
parser.add_argument('--save_fig', action='store_true',
help='save results as figures')
parser.add_argument('--compensate', action='store_true',
help='compensate anomaly score using anomaly score esimation')
parser.add_argument('--beta', type=float, default=1.0,
help='beta value for f-beta score')
parser.add_argument('--model', type=str, default='GRU',
help='type of recurrent net (RNN_TANH, RNN_RELU, LSTM, GRU, SRU)')
parser.add_argument('--save_path', type=str, default='save')
args_ = parser.parse_args()
main(args_)