-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
214 lines (189 loc) · 12.5 KB
/
train.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
#%%
import os
import pandas as pd
import numpy as np
import copy
from tqdm import tqdm
from plot import plot
from utils.evaluator import evaluate, set_thresholds
from utils.evaluator_seg import compute_anomaly_scores, compute_metrics
# Univariate
from utils.data_loader import load_kpi, load_IoT_fridge
# Multivariate
from utils.data_loader import load_samsung, load_energy, load_unsw, load_IoT_modbus
def _elements(array):
return array.ndim and array.size
def train(AE_model, Temporal_AE_model, model_name, window_size, stride, lamda_t, wavelet_num, seed, dataset, temporal=False, decomposition=False, segmentation=False):
ts_scores = {'dataset': [], 'f1': [], 'precision': [], 'recall': [], 'pr_auc': [], 'roc_auc': [], 'th_index': [], 'predicted_index': []}
seg_scores = {'dataset': [], 'avg_f1': [], 'avg_p': [], 'avg_r': [], 'max_p': [], 'max_r': [], 'max_f1': [], 'correct_count': [], 'correct_ratio': []}
if temporal == True:
datasets_auxiliary = globals()[f'load_{dataset}'](window_size, stride, lamda_t, wavelet_num, temporal=temporal)
ax_trains, ax_tests = datasets_auxiliary['x_train'], datasets_auxiliary['x_test']
# There are eight cases #1-1~#1-4 & #2-1~#2-4
# 1) decomposition==True: Decompose time series and evaluate through traditional metrics (Temporal)
# 4) decomposition==False: Evaluate through traditional metrics with common methods
if segmentation == False:
datasets = globals()[f'load_{dataset}'](window_size, stride, lamda_t, wavelet_num, decomposition=decomposition, segmentation=segmentation)
x_trains, x_tests, y_tests = datasets['x_train'], datasets['x_test'], datasets['y_test']
test_seq, label_seq = datasets['test_seq'], datasets['label_seq']
if decomposition == True:
train_residual, test_residual = datasets['x_train_resid'], datasets['x_test_resid']
per_window_idx = []
for data_num in tqdm(range(len(x_trains))):
# 1) if decomposition == True
if decomposition == True:
X_test = x_tests[data_num]
residual_X_train = train_residual[data_num]
residual_X_test = test_residual[data_num]
# 1-1) temporal=True, decomposition=True, Segmentation=False
if temporal == True:
X_train_ax = ax_trains[data_num]
X_test_ax = ax_tests[data_num]
model = Temporal_AE_model(X_train_ax, residual_X_train)
rec_x = model.predict([X_test_ax, residual_X_test])
thresholds = set_thresholds(residual_X_test, rec_x, is_reconstructed=True)
test_scores = evaluate(thresholds, residual_X_test, rec_x, y_tests[data_num], is_reconstructed=True)
# 2-1) temporal=False, decomposition=True, Segmentation=False
else:
if model_name == "MS-RNN":
model = AE_model(residual_X_train)
rec_x = [np.flip(rec, axis=1) for rec in model.predict(residual_X_test)]
thresholds = set_thresholds(residual_X_test, rec_x, is_reconstructed=True, scoring='square_median')
test_scores = evaluate(thresholds, residual_X_test, rec_x, y_tests[data_num], is_reconstructed=True, scoring='square_median')
else:
model = AE_model(residual_X_train)
rec_x = model.predict(residual_X_test)
thresholds = set_thresholds(residual_X_test, rec_x, is_reconstructed=True)
test_scores = evaluate(thresholds, residual_X_test, rec_x, y_tests[data_num], is_reconstructed=True)
# 4) if decomposition == False
else:
X_train = x_trains[data_num]
X_test = x_tests[data_num]
# 1-4) temporal=True, decomposition=False, segmentation=False
if temporal == True:
X_train_ax = ax_trains[data_num]
X_test_ax = ax_tests[data_num]
model = Temporal_AE_model(X_train_ax, X_train)
rec_x = model.predict([X_test_ax, X_test])
thresholds = set_thresholds(X_test, rec_x, is_reconstructed=True)
test_scores = evaluate(thresholds, X_test, rec_x, y_tests[data_num], is_reconstructed=True)
# 2-4) temporal=False, decomposition=False, segmentation:False
else:
if model_name == "MS-RNN":
model = AE_model(X_train)
rec_x = [np.flip(rec, axis=1) for rec in model.predict(X_test)]
thresholds = set_thresholds(X_test, rec_x, is_reconstructed=True, scoring='square_median')
test_scores = evaluate(thresholds, X_test, rec_x, y_tests[data_num], is_reconstructed=True, scoring='square_median')
else:
model = AE_model(X_train)
rec_x = model.predict(X_test)
thresholds = set_thresholds(X_test, rec_x, is_reconstructed=True)
test_scores = evaluate(thresholds, X_test, rec_x, y_tests[data_num], is_reconstructed=True)
ts_scores['dataset'].append(f'Data{data_num+1}')
ts_scores['f1'].append(np.max(test_scores['f1']))
ts_scores['precision'].append(np.mean(test_scores['precision']))
ts_scores['recall'].append(np.mean(test_scores['recall']))
ts_scores['pr_auc'].append(test_scores['pr_auc'])
ts_scores['roc_auc'].append(test_scores['roc_auc'])
th_index = int(np.median(np.where(test_scores['f1']==np.max(test_scores['f1']))[0]))
ts_scores['th_index'].append(th_index)
print(f'{seed}th {model_name} Data{data_num+1}', np.max(test_scores['f1']), np.mean(test_scores['precision']), np.mean(test_scores['recall']), test_scores['pr_auc'], test_scores['roc_auc'])
pred_anomal_idx = []
for t in range(len(X_test)):
pred_anomalies = np.where(test_scores['rec_errors'][t] > thresholds[th_index])[0]
isEmpty = (_elements(pred_anomalies) == 0)
if isEmpty:
pass
else:
if pred_anomalies[0] == 0:
pred_anomal_idx.append(t)
per_window_idx.append(pred_anomal_idx)
ts_scores['predicted_index'].extend(per_window_idx)
scores_all = copy.deepcopy(ts_scores)
del ts_scores['th_index']
results_df = pd.DataFrame(ts_scores)
print("@"*5, f'{seed}th Seed {model_name} R{decomposition}_T{temporal}_Ts', "@"*5)
print(results_df.groupby('dataset').mean())
save_results_path = f'./results/{dataset}/Ts'
try:
if not(os.path.isdir(save_results_path)):
os.makedirs(os.path.join(save_results_path), exist_ok=True)
except OSError as e:
print("Failed to create directory!!!!!")
results_df.to_csv(f'{save_results_path}/{model_name}_R{decomposition}_T{temporal}_ts_seed{seed}.csv', index=False)
plot(model_name, ts_scores, test_seq, label_seq, seed, save_results_path, decomposition, temporal)
# 2) decomposition==True: Decompose time series and evalutate new metrics (Temporal+Seg_evaluation)
# 3) decomposition==False: Evaluate through new metrics with common methods (Seg_evaluation)
elif segmentation == True:
datasets = globals()[f'load_{dataset}'](window_size, stride, lamda_t, wavelet_num, decomposition=decomposition, segmentation=segmentation)
x_trains, x_tests = datasets['x_train'], datasets['x_test']
y_tests, y_segment_tests = datasets['y_test'], datasets['y_segment_test']
if decomposition == True:
train_residual, test_residual = datasets['x_train_resid'], datasets['x_test_resid']
per_window_idx = []
for data_num in tqdm(range(len(x_trains))):
# 2) if decomposition == True
if decomposition == True:
residual_X_train = train_residual[data_num]
residual_X_test = test_residual[data_num]
# 1-2) temporal=True, decomposition=True, segmentation=True
if temporal == True:
X_train_ax = ax_trains[data_num]
X_test_ax = ax_tests[data_num]
model = Temporal_AE_model(X_train_ax, residual_X_train)
scores = compute_anomaly_scores(residual_X_test, model.predict([X_test_ax, residual_X_test]))
test_scores = compute_metrics(scores, y_tests[data_num], y_segment_tests[data_num])
else:
# 2-2) temporal=False, decomposition=True, segmentation=True
if model_name == "MS-RNN":
model = AE_model(residual_X_train)
rec_x = np.mean([np.flip(rec, axis=1) for rec in model.predict(residual_X_test)], axis=0)
scores = compute_anomaly_scores(residual_X_test, rec_x, scoring='square_median')
test_scores = compute_metrics(scores, y_tests[data_num], y_segment_tests[data_num])
else:
model = AE_model(residual_X_train)
scores = compute_anomaly_scores(residual_X_test, model.predict(residual_X_test))
test_scores = compute_metrics(scores, y_tests[data_num], y_segment_tests[data_num])
# 3) if decomposition == False
else:
X_train = x_trains[data_num]
X_test = x_tests[data_num]
# 1-3) temporal=True, decomposition=False, segmentation=True
if temporal == True:
X_train_ax = ax_trains[data_num]
X_test_ax = ax_tests[data_num]
model = Temporal_AE_model(X_train_ax, X_train)
scores = compute_anomaly_scores(X_test, model.predict([X_test_ax, X_test]))
test_scores = compute_metrics(scores, y_tests[data_num], y_segment_tests[data_num])
# 2-3) temporal=False, decomposition=False, segmentation=True
else:
if model_name == "MS-RNN":
model = AE_model(X_train)
rec_x = np.mean([np.flip(rec, axis=1) for rec in model.predict(X_test)], axis=0)
scores = compute_anomaly_scores(X_test, rec_x, scoring='square_median')
test_scores = compute_metrics(scores, y_tests[data_num], y_segment_tests[data_num])
else:
model = AE_model(X_train)
scores = compute_anomaly_scores(X_test, model.predict(X_test))
test_scores = compute_metrics(scores, y_tests[data_num], y_segment_tests[data_num])
seg_scores['dataset'].append(f'Data{data_num+1}')
seg_scores['max_f1'].append(np.max(test_scores['f1']))
seg_scores['max_p'].append(np.max(test_scores['precision']))
seg_scores['max_r'].append(np.max(test_scores['recall']))
seg_scores['avg_f1'].append(np.average(test_scores['f1']))
seg_scores['avg_p'].append(np.average(test_scores['precision']))
seg_scores['avg_r'].append(np.average(test_scores['recall']))
seg_scores['correct_count'].append(np.average(test_scores['count']))
seg_scores['correct_ratio'].append(np.average(test_scores['ratio']))
print(f'{seed}th {model_name} Data{data_num+1}', np.max(test_scores['f1']), np.mean(test_scores['precision']), np.mean(test_scores['recall']), np.mean(test_scores['count']), np.mean(test_scores['ratio']))
results_df = pd.DataFrame(seg_scores)
print("@"*5, f'{seed}th Seed {model_name} R{decomposition}_T{temporal}_Seg', "@"*5)
print(results_df.groupby('dataset').mean())
save_results_path = f'./results/{dataset}/Seg'
try:
if not(os.path.isdir(save_results_path)):
os.makedirs(os.path.join(save_results_path), exist_ok=True)
except OSError as e:
print("Failed to create directory!!!!!")
results_df.to_csv(f'{save_results_path}/{model_name}_R{decomposition}_T{temporal}_seg_seed{seed}.csv', index=False)
# %%