forked from imperial-qore/TranAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
346 lines (335 loc) · 12.9 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import pickle
import os
import pandas as pd
from tqdm import tqdm
from src.models import *
from src.constants import *
from src.plotting import *
from src.pot import *
from src.utils import *
from src.diagnosis import *
from src.merlin import *
from torch.utils.data import Dataset, DataLoader, TensorDataset
import torch.nn as nn
from time import time
from pprint import pprint
# from beepy import beep
def convert_to_windows(data, model):
windows = []; w_size = model.n_window
for i, g in enumerate(data):
if i >= w_size: w = data[i-w_size:i]
else: w = torch.cat([data[0].repeat(w_size-i, 1), data[0:i]])
windows.append(w if 'TranAD' in args.model or 'Attention' in args.model else w.view(-1))
return torch.stack(windows)
def load_dataset(dataset):
folder = os.path.join(output_folder, dataset)
if not os.path.exists(folder):
raise Exception('Processed Data not found.')
loader = []
for file in ['train', 'test', 'labels']:
if dataset == 'SMD': file = 'machine-1-1_' + file
if dataset == 'SMAP': file = 'P-1_' + file
if dataset == 'MSL': file = 'C-1_' + file
if dataset == 'UCR': file = '136_' + file
if dataset == 'NAB': file = 'ec2_request_latency_system_failure_' + file
loader.append(np.load(os.path.join(folder, f'{file}.npy')))
# loader = [i[:, debug:debug+1] for i in loader]
if args.less: loader[0] = cut_array(0.2, loader[0])
train_loader = DataLoader(loader[0], batch_size=loader[0].shape[0])
test_loader = DataLoader(loader[1], batch_size=loader[1].shape[0])
labels = loader[2]
return train_loader, test_loader, labels
def save_model(model, optimizer, scheduler, epoch, accuracy_list):
folder = f'checkpoints/{args.model}_{args.dataset}/'
os.makedirs(folder, exist_ok=True)
file_path = f'{folder}/model.ckpt'
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': scheduler.state_dict(),
'accuracy_list': accuracy_list}, file_path)
def load_model(modelname, dims):
import src.models
model_class = getattr(src.models, modelname)
model = model_class(dims).double()
optimizer = torch.optim.AdamW(model.parameters() , lr=model.lr, weight_decay=1e-5)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 5, 0.9)
fname = f'checkpoints/{args.model}_{args.dataset}/model.ckpt'
if os.path.exists(fname) and (not args.retrain or args.test):
print(f"{color.GREEN}Loading pre-trained model: {model.name}{color.ENDC}")
checkpoint = torch.load(fname)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
epoch = checkpoint['epoch']
accuracy_list = checkpoint['accuracy_list']
else:
print(f"{color.GREEN}Creating new model: {model.name}{color.ENDC}")
epoch = -1; accuracy_list = []
return model, optimizer, scheduler, epoch, accuracy_list
def backprop(epoch, model, data, dataO, optimizer, scheduler, training = True):
l = nn.MSELoss(reduction = 'mean' if training else 'none')
feats = dataO.shape[1]
if 'DAGMM' in model.name:
l = nn.MSELoss(reduction = 'none')
compute = ComputeLoss(model, 0.1, 0.005, 'cpu', model.n_gmm)
n = epoch + 1; w_size = model.n_window
l1s = []; l2s = []
if training:
for d in data:
_, x_hat, z, gamma = model(d)
l1, l2 = l(x_hat, d), l(gamma, d)
l1s.append(torch.mean(l1).item()); l2s.append(torch.mean(l2).item())
loss = torch.mean(l1) + torch.mean(l2)
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
tqdm.write(f'Epoch {epoch},\tL1 = {np.mean(l1s)},\tL2 = {np.mean(l2s)}')
return np.mean(l1s)+np.mean(l2s), optimizer.param_groups[0]['lr']
else:
ae1s = []
for d in data:
_, x_hat, _, _ = model(d)
ae1s.append(x_hat)
ae1s = torch.stack(ae1s)
y_pred = ae1s[:, data.shape[1]-feats:data.shape[1]].view(-1, feats)
loss = l(ae1s, data)[:, data.shape[1]-feats:data.shape[1]].view(-1, feats)
return loss.detach().numpy(), y_pred.detach().numpy()
if 'Attention' in model.name:
l = nn.MSELoss(reduction = 'none')
n = epoch + 1; w_size = model.n_window
l1s = []; res = []
if training:
for d in data:
ae, ats = model(d)
# res.append(torch.mean(ats, axis=0).view(-1))
l1 = l(ae, d)
l1s.append(torch.mean(l1).item())
loss = torch.mean(l1)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# res = torch.stack(res); np.save('ascores.npy', res.detach().numpy())
scheduler.step()
tqdm.write(f'Epoch {epoch},\tL1 = {np.mean(l1s)}')
return np.mean(l1s), optimizer.param_groups[0]['lr']
else:
ae1s, y_pred = [], []
for d in data:
ae1 = model(d)
y_pred.append(ae1[-1])
ae1s.append(ae1)
ae1s, y_pred = torch.stack(ae1s), torch.stack(y_pred)
loss = torch.mean(l(ae1s, data), axis=1)
return loss.detach().numpy(), y_pred.detach().numpy()
elif 'OmniAnomaly' in model.name:
if training:
mses, klds = [], []
for i, d in enumerate(data):
y_pred, mu, logvar, hidden = model(d, hidden if i else None)
MSE = l(y_pred, d)
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp(), dim=0)
loss = MSE + model.beta * KLD
mses.append(torch.mean(MSE).item()); klds.append(model.beta * torch.mean(KLD).item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
tqdm.write(f'Epoch {epoch},\tMSE = {np.mean(mses)},\tKLD = {np.mean(klds)}')
scheduler.step()
return loss.item(), optimizer.param_groups[0]['lr']
else:
y_preds = []
for i, d in enumerate(data):
y_pred, _, _, hidden = model(d, hidden if i else None)
y_preds.append(y_pred)
y_pred = torch.stack(y_preds)
MSE = l(y_pred, data)
return MSE.detach().numpy(), y_pred.detach().numpy()
elif 'USAD' in model.name:
l = nn.MSELoss(reduction = 'none')
n = epoch + 1; w_size = model.n_window
l1s, l2s = [], []
if training:
for d in data:
ae1s, ae2s, ae2ae1s = model(d)
l1 = (1 / n) * l(ae1s, d) + (1 - 1/n) * l(ae2ae1s, d)
l2 = (1 / n) * l(ae2s, d) - (1 - 1/n) * l(ae2ae1s, d)
l1s.append(torch.mean(l1).item()); l2s.append(torch.mean(l2).item())
loss = torch.mean(l1 + l2)
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
tqdm.write(f'Epoch {epoch},\tL1 = {np.mean(l1s)},\tL2 = {np.mean(l2s)}')
return np.mean(l1s)+np.mean(l2s), optimizer.param_groups[0]['lr']
else:
ae1s, ae2s, ae2ae1s = [], [], []
for d in data:
ae1, ae2, ae2ae1 = model(d)
ae1s.append(ae1); ae2s.append(ae2); ae2ae1s.append(ae2ae1)
ae1s, ae2s, ae2ae1s = torch.stack(ae1s), torch.stack(ae2s), torch.stack(ae2ae1s)
y_pred = ae1s[:, data.shape[1]-feats:data.shape[1]].view(-1, feats)
loss = 0.1 * l(ae1s, data) + 0.9 * l(ae2ae1s, data)
loss = loss[:, data.shape[1]-feats:data.shape[1]].view(-1, feats)
return loss.detach().numpy(), y_pred.detach().numpy()
elif model.name in ['GDN', 'MTAD_GAT', 'MSCRED', 'CAE_M']:
l = nn.MSELoss(reduction = 'none')
n = epoch + 1; w_size = model.n_window
l1s = []
if training:
for i, d in enumerate(data):
if 'MTAD_GAT' in model.name:
x, h = model(d, h if i else None)
else:
x = model(d)
loss = torch.mean(l(x, d))
l1s.append(torch.mean(loss).item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
tqdm.write(f'Epoch {epoch},\tMSE = {np.mean(l1s)}')
return np.mean(l1s), optimizer.param_groups[0]['lr']
else:
xs = []
for d in data:
if 'MTAD_GAT' in model.name:
x, h = model(d, None)
else:
x = model(d)
xs.append(x)
xs = torch.stack(xs)
y_pred = xs[:, data.shape[1]-feats:data.shape[1]].view(-1, feats)
loss = l(xs, data)
loss = loss[:, data.shape[1]-feats:data.shape[1]].view(-1, feats)
return loss.detach().numpy(), y_pred.detach().numpy()
elif 'GAN' in model.name:
l = nn.MSELoss(reduction = 'none')
bcel = nn.BCELoss(reduction = 'mean')
msel = nn.MSELoss(reduction = 'mean')
real_label, fake_label = torch.tensor([0.9]), torch.tensor([0.1]) # label smoothing
real_label, fake_label = real_label.type(torch.DoubleTensor), fake_label.type(torch.DoubleTensor)
n = epoch + 1; w_size = model.n_window
mses, gls, dls = [], [], []
if training:
for d in data:
# training discriminator
model.discriminator.zero_grad()
_, real, fake = model(d)
dl = bcel(real, real_label) + bcel(fake, fake_label)
dl.backward()
model.generator.zero_grad()
optimizer.step()
# training generator
z, _, fake = model(d)
mse = msel(z, d)
gl = bcel(fake, real_label)
tl = gl + mse
tl.backward()
model.discriminator.zero_grad()
optimizer.step()
mses.append(mse.item()); gls.append(gl.item()); dls.append(dl.item())
# tqdm.write(f'Epoch {epoch},\tMSE = {mse},\tG = {gl},\tD = {dl}')
tqdm.write(f'Epoch {epoch},\tMSE = {np.mean(mses)},\tG = {np.mean(gls)},\tD = {np.mean(dls)}')
return np.mean(gls)+np.mean(dls), optimizer.param_groups[0]['lr']
else:
outputs = []
for d in data:
z, _, _ = model(d)
outputs.append(z)
outputs = torch.stack(outputs)
y_pred = outputs[:, data.shape[1]-feats:data.shape[1]].view(-1, feats)
loss = l(outputs, data)
loss = loss[:, data.shape[1]-feats:data.shape[1]].view(-1, feats)
return loss.detach().numpy(), y_pred.detach().numpy()
elif 'TranAD' in model.name:
l = nn.MSELoss(reduction = 'none')
data_x = torch.DoubleTensor(data); dataset = TensorDataset(data_x, data_x)
bs = model.batch if training else len(data)
dataloader = DataLoader(dataset, batch_size = bs)
n = epoch + 1; w_size = model.n_window
l1s, l2s = [], []
if training:
for d, _ in dataloader:
local_bs = d.shape[0]
window = d.permute(1, 0, 2)
elem = window[-1, :, :].view(1, local_bs, feats)
z = model(window, elem)
l1 = l(z, elem) if not isinstance(z, tuple) else (1 / n) * l(z[0], elem) + (1 - 1/n) * l(z[1], elem)
if isinstance(z, tuple): z = z[1]
l1s.append(torch.mean(l1).item())
loss = torch.mean(l1)
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
scheduler.step()
tqdm.write(f'Epoch {epoch},\tL1 = {np.mean(l1s)}')
return np.mean(l1s), optimizer.param_groups[0]['lr']
else:
for d, _ in dataloader:
window = d.permute(1, 0, 2)
elem = window[-1, :, :].view(1, bs, feats)
z = model(window, elem)
if isinstance(z, tuple): z = z[1]
loss = l(z, elem)[0]
return loss.detach().numpy(), z.detach().numpy()[0]
else:
y_pred = model(data)
loss = l(y_pred, data)
if training:
tqdm.write(f'Epoch {epoch},\tMSE = {loss}')
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
return loss.item(), optimizer.param_groups[0]['lr']
else:
return loss.detach().numpy(), y_pred.detach().numpy()
if __name__ == '__main__':
train_loader, test_loader, labels = load_dataset(args.dataset)
if args.model in ['MERLIN']:
eval(f'run_{args.model.lower()}(test_loader, labels, args.dataset)')
model, optimizer, scheduler, epoch, accuracy_list = load_model(args.model, labels.shape[1])
## Prepare data
trainD, testD = next(iter(train_loader)), next(iter(test_loader))
trainO, testO = trainD, testD
if model.name in ['Attention', 'DAGMM', 'USAD', 'MSCRED', 'CAE_M', 'GDN', 'MTAD_GAT', 'MAD_GAN'] or 'TranAD' in model.name:
trainD, testD = convert_to_windows(trainD, model), convert_to_windows(testD, model)
### Training phase
if not args.test:
print(f'{color.HEADER}Training {args.model} on {args.dataset}{color.ENDC}')
num_epochs = 5; e = epoch + 1; start = time()
for e in tqdm(list(range(epoch+1, epoch+num_epochs+1))):
lossT, lr = backprop(e, model, trainD, trainO, optimizer, scheduler)
accuracy_list.append((lossT, lr))
print(color.BOLD+'Training time: '+"{:10.4f}".format(time()-start)+' s'+color.ENDC)
save_model(model, optimizer, scheduler, e, accuracy_list)
plot_accuracies(accuracy_list, f'{args.model}_{args.dataset}')
### Testing phase
torch.zero_grad = True
model.eval()
print(f'{color.HEADER}Testing {args.model} on {args.dataset}{color.ENDC}')
loss, y_pred = backprop(0, model, testD, testO, optimizer, scheduler, training=False)
### Plot curves
if not args.test:
if 'TranAD' in model.name: testO = torch.roll(testO, 1, 0)
plotter(f'{args.model}_{args.dataset}', testO, y_pred, loss, labels)
### Scores
df = pd.DataFrame()
lossT, _ = backprop(0, model, trainD, trainO, optimizer, scheduler, training=False)
for i in range(loss.shape[1]):
lt, l, ls = lossT[:, i], loss[:, i], labels[:, i]
result, pred = pot_eval(lt, l, ls); preds.append(pred)
df = df.append(result, ignore_index=True)
# preds = np.concatenate([i.reshape(-1, 1) + 0 for i in preds], axis=1)
# pd.DataFrame(preds, columns=[str(i) for i in range(10)]).to_csv('labels.csv')
lossTfinal, lossFinal = np.mean(lossT, axis=1), np.mean(loss, axis=1)
labelsFinal = (np.sum(labels, axis=1) >= 1) + 0
result, _ = pot_eval(lossTfinal, lossFinal, labelsFinal)
result.update(hit_att(loss, labels))
result.update(ndcg(loss, labels))
print(df)
pprint(result)
# pprint(getresults2(df, result))
# beep(4)