-
Notifications
You must be signed in to change notification settings - Fork 68
/
train.py
100 lines (81 loc) · 3.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
import numpy as np
import torch
from sklearn.metrics import roc_auc_score
from tqdm import tqdm
import config as c
from localization import export_gradient_maps
from model import DifferNet, save_model, save_weights
from utils import *
class Score_Observer:
'''Keeps an eye on the current and highest score so far'''
def __init__(self, name):
self.name = name
self.max_epoch = 0
self.max_score = None
self.last = None
def update(self, score, epoch, print_score=False):
self.last = score
if epoch == 0 or score > self.max_score:
self.max_score = score
self.max_epoch = epoch
if print_score:
self.print_score()
def print_score(self):
print('{:s}: \t last: {:.4f} \t max: {:.4f} \t epoch_max: {:d}'.format(self.name, self.last, self.max_score,
self.max_epoch))
def train(train_loader, test_loader):
model = DifferNet()
optimizer = torch.optim.Adam(model.nf.parameters(), lr=c.lr_init, betas=(0.8, 0.8), eps=1e-04, weight_decay=1e-5)
model.to(c.device)
score_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
# TODO inspect
# inputs += torch.randn(*inputs.shape).cuda() * c.add_img_noise
z = model(inputs)
loss = get_loss(z, model.nf.jacobian(run_forward=False))
train_loss.append(t2np(loss))
loss.backward()
optimizer.step()
mean_train_loss = np.mean(train_loss)
if c.verbose:
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)
z = model(inputs)
loss = get_loss(z, model.nf.jacobian(run_forward=False))
test_z.append(z)
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])
z_grouped = torch.cat(test_z, dim=0).view(-1, c.n_transforms_test, c.n_feat)
anomaly_score = t2np(torch.mean(z_grouped ** 2, dim=(-2, -1)))
score_obs.update(roc_auc_score(is_anomaly, anomaly_score), epoch,
print_score=c.verbose or epoch == c.meta_epochs - 1)
if c.grad_map_viz:
export_gradient_maps(model, test_loader, optimizer, -1)
if c.save_model:
model.to('cpu')
save_model(model, c.modelname)
save_weights(model, c.modelname)
return model