forked from marco-rudolph/cs-flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
88 lines (70 loc) · 3.06 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
import numpy as np
import torch
from sklearn.metrics import roc_auc_score
from tqdm import tqdm
import config as c
from model import get_cs_flow_model, save_model, FeatureExtractor, nf_forward
from utils import *
def train(train_loader, test_loader):
model = get_cs_flow_model()
optimizer = torch.optim.Adam(model.parameters(), lr=c.lr_init, eps=1e-04, weight_decay=1e-5)
model.to(c.device)
if not c.pre_extracted:
fe = FeatureExtractor()
fe.eval()
fe.to(c.device)
for param in fe.parameters():
param.requires_grad = False
z_obs = Score_Observer('AUROC')
for epoch in range(c.meta_epochs):
# train some epochs
model.train()
if c.verbose:
print(F'\nTrain epoch {epoch}')
for sub_epoch in range(c.sub_epochs):
train_loss = list()
for i, data in enumerate(tqdm(train_loader, disable=c.hide_tqdm_bar)):
optimizer.zero_grad()
inputs, labels = preprocess_batch(data) # move to device and reshape
if not c.pre_extracted:
inputs = fe(inputs)
z, jac = nf_forward(model, inputs)
loss = get_loss(z, jac)
train_loss.append(t2np(loss))
loss.backward()
norm = torch.nn.utils.clip_grad_norm_(model.parameters(), c.max_grad_norm)
optimizer.step()
mean_train_loss = np.mean(train_loss)
if c.verbose and epoch == 0 and sub_epoch % 4 == 0:
print('Epoch: {:d}.{:d} \t train loss: {:.4f}'.format(epoch, sub_epoch, mean_train_loss))
# evaluate
model.eval()
if c.verbose:
print('\nCompute loss and scores on test set:')
test_loss = list()
test_z = list()
test_labels = list()
with torch.no_grad():
for i, data in enumerate(tqdm(test_loader, disable=c.hide_tqdm_bar)):
inputs, labels = preprocess_batch(data)
if not c.pre_extracted:
inputs = fe(inputs)
z, jac = nf_forward(model, inputs)
loss = get_loss(z, jac)
z_concat = t2np(concat_maps(z))
score = np.mean(z_concat ** 2, axis=(1, 2))
test_z.append(score)
test_loss.append(t2np(loss))
test_labels.append(t2np(labels))
test_loss = np.mean(np.array(test_loss))
if c.verbose:
print('Epoch: {:d} \t test_loss: {:.4f}'.format(epoch, test_loss))
test_labels = np.concatenate(test_labels)
is_anomaly = np.array([0 if l == 0 else 1 for l in test_labels])
anomaly_score = np.concatenate(test_z, axis=0)
is_best = z_obs.update(roc_auc_score(is_anomaly, anomaly_score), epoch,
print_score=c.verbose or epoch == c.meta_epochs - 1)
if c.save_model and is_best:
print("Best AUROC achieved. Saving new checkpoint.")
save_model(model, c.modelname)
return z_obs.max_score, z_obs.last, z_obs.min_loss_score