forked from WenweiGu/ISOLATE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_rca.py
305 lines (241 loc) · 9.29 KB
/
train_rca.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import pickle
import torch
from CVAE import loss_function, loss_function_positive
import torch.optim as optim
from model import RTAnomaly
from dataloader import load_dataset, get_dataloaders, get_positive_dataloaders
from data_preprocess import normalize, generate_windows, minmax_score
import matplotlib.pyplot as plt
import logging
from tqdm import tqdm
from evaluate import get_anomaly_score
import numpy as np
params = {
'data_root': "./datasets/HW",
'train_postfix': "train.pkl",
'test_postfix': "test.pkl",
'test_label_postfix': "test_label.pkl",
'train_label_postfix': "train_label.pkl",
'positive': True,
'dim': 38,
'entity': ['37f4ceba-f840-4c08-a488-676bce922fcf'],
'valid_ratio': 0,
'normalize': "minmax",
'window_size': 20,
'stride': 10,
'batch_size': 32,
'num_workers': 0,
'device': torch.device("cuda:0" if torch.cuda.is_available() else "cpu"),
'gnn_dim': 128,
'pooling_ratio': 0.5,
'threshold': 0.5,
'dropout': 0.5,
'filters': [256, 256, 256],
'kernels': [8, 5, 3],
'dilation': [1, 2, 4],
'layers': [50, 10],
'gru_dim': 128,
'epoch': 50,
'lr': 1e-4,
'wd': 1e-3,
'recon_filter': 5,
'hidden_size': 100,
'latent_size': 10,
'cof': 0.5
}
def get_positive_label(model, item, threshold=0.9):
model.train()
data_dict = load_dataset(
data_root=params["data_root"],
entities=params["entity"],
dim=params["dim"],
valid_ratio=params["valid_ratio"],
test_label_postfix=params["test_label_postfix"],
test_postfix=params["test_postfix"],
train_postfix=params["train_postfix"],
)
data_dict = normalize(data_dict, method=params["normalize"])
windows = generate_windows(
data_dict,
window_size=params["window_size"],
stride=1 # 确保每个点都有标签
)
train_window = windows[item]['train_windows']
loader_train, _, loader_test = get_dataloaders(
train_window,
train_window,
batch_size=params["batch_size"],
num_workers=params["num_workers"]
)
for epoch in range(params['epoch']):
loss = 0
for n, x in enumerate(tqdm(loader_train)):
if x.shape[0] == 1:
continue
x = x.to(params['device']) # 先放GPU上
x = x.permute(0, 2, 1)
label = torch.zeros((x.shape[0], 1)).to(params['device'])
optimizer.zero_grad()
x_recon, recon_embed, embed, mu, log_var, _ = model(x, label)
# loss 部分可以加入别的部分, 有一定作用
loss_train = loss_function(x, x_recon, recon_embed, embed, mu, log_var, cof=params['cof'])
loss += loss_train
loss_train.backward()
optimizer.step()
model.eval()
score, _, _ = get_anomaly_score(loader_test, encoder, params['device'], 1)
score = np.array(minmax_score(score))
plt.plot(score)
plt.axhline(threshold, color='r')
plt.xticks([])
plt.yticks([])
plt.savefig(f'./PU_{entity}.jpg', bbox_inches='tight', dpi=600)
plt.close()
train_label = np.zeros((score.shape[0] + params['window_size'], 1))
train_label[np.where(score > threshold)] = 1
pickle.dump(train_label, open(str(params['data_root']) + '/' + item + '_train_label.pkl', 'wb'))
def anomaly_segment(label):
# find the change point
tag = [0 for j in range(len(label))]
for i in range(1, len(label)):
if label[i] > label[i - 1]:
tag[i] = 1
elif label[i] < label[i - 1]:
tag[i] = -1
# flag for change
flag = 0
start = []
end = []
for i in range(0, len(label)):
if flag == 0:
if tag[i] == 1:
start.append(i)
flag = 1 # go into anomaly pattern
if flag == 1:
if tag[i] == -1:
end.append(i)
flag = 0 # go out anomaly pattern
if len(start) != len(end):
end.append(len(label))
anomaly_segment = [(start[i], end[i]) for i in range(len(start))]
return anomaly_segment
for entity in params['entity']:
logging.info("Fitting dataset: {}".format(entity))
avg1 = None
avg2 = None
if params['positive']:
train = True
test = True
get_positive = True
t_dict = load_dataset(
data_root=params["data_root"],
entities=params["entity"],
dim=params["dim"],
valid_ratio=params["valid_ratio"],
test_label_postfix=params["test_label_postfix"],
test_postfix=params["test_postfix"],
train_postfix=params["train_postfix"]
)
w = generate_windows(
t_dict,
window_size=params["window_size"],
stride=params["stride"],
positive_label=False
)
dim = w[entity]['train_windows'].shape[-1]
encoder = RTAnomaly(
ndim=dim,
len_window=params['window_size'],
gnn_dim=params['gnn_dim'],
pooling_ratio=params['pooling_ratio'],
threshold=params['threshold'],
dropout=params['dropout'],
filters=params['filters'],
kernels=params['kernels'],
dilation=params['dilation'],
layers=params['layers'],
gru_dim=params['gru_dim'],
device=params['device'],
recon_filter=params['recon_filter'],
hidden_size=params['hidden_size'],
latent_size=params['latent_size']
)
encoder.to(params['device'])
optimizer = optim.Adam(encoder.parameters(),
lr=params['lr'], weight_decay=params['wd'])
if get_positive:
get_positive_label(encoder, entity, threshold=0.9)
train_dict = load_dataset(
data_root=params["data_root"],
entities=params["entity"],
dim=params["dim"],
valid_ratio=params["valid_ratio"],
test_label_postfix=params["test_label_postfix"],
test_postfix=params["test_postfix"],
train_postfix=params["train_postfix"],
train_label_postfix=params["train_label_postfix"]
)
train_dict = normalize(train_dict, method=params["normalize"])
window = generate_windows(
train_dict,
window_size=params["window_size"],
stride=params["stride"],
positive_label=True
)
train_windows = window[entity]['train_windows']
test_windows = window[entity]['test_windows']
test_labels = window[entity]['test_label'][:, -1].reshape(-1, 1)
train_labels = window[entity]['train_label'][:, -1].reshape(-1, 1)
train_loader, _, test_loader = get_positive_dataloaders(
train_windows,
train_labels,
test_windows,
batch_size=params["batch_size"],
num_workers=params["num_workers"]
)
if train:
A_n = []
encoder.train()
for epoch in range(params['epoch']):
loss = 0
for i, (x, y) in enumerate(tqdm(train_loader)):
if x.shape[0] == 1:
continue
x = x.to(params['device']) # 先放GPU上
x = x.permute(0, 2, 1)
y = y.to(params['device'])
optimizer.zero_grad()
x_recon, recon_embed, embed, mu, log_var, a_n = encoder(x, y)
A_n.extend(a_n)
# loss 部分可以加入别的部分, 有一定作用
loss_train = loss_function_positive(x, x_recon, recon_embed, embed, mu, log_var, y,
cof=params['cof'])
loss += loss_train
loss_train.backward()
optimizer.step()
loss /= train_loader.__len__()
print(f'Training loss for epoch {epoch} is: {float(loss)}')
torch.save(encoder.state_dict(), './save/checkpoint_' + entity + '.pth')
A_n = np.array([n.cpu().detach().numpy() for n in A_n])
avg1 = np.mean(np.mean(np.array(A_n), axis=0), axis=0)
if test:
logging.info("Finish dataset: {}".format(entity))
encoder.load_state_dict(torch.load('./save/checkpoint_' + entity + '.pth'))
encoder.eval()
score, A, score_metrics = get_anomaly_score(test_loader, encoder, params['device'], 1)
score = minmax_score(score)
np.save(f'./score/score_{entity}.npy', score)
A = np.array(A)
eval_score = np.load(f'./score/score_{entity}.npy')
test_labels = test_labels.flatten()
segments = anomaly_segment(test_labels)
average = []
avg_anomaly = np.mean(np.array(score_metrics), axis=0)
for seg in segments:
average = np.mean(np.mean(np.array(A[seg[0]:seg[1], :, :]), axis=0), axis=0)
avg2 = np.array(average)
dif = np.abs(avg1 - avg2)
idx_anomaly = np.argsort(-avg_anomaly)
rank_anomaly = np.argsort(idx_anomaly)
np.save(f'./RCA_{entity}.txt', rank_anomaly)
print(f'Metric localization ranking is: {rank_anomaly}')