-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
131 lines (104 loc) · 4.96 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
import time
from data import get_loader
from model import DAGMM
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score, confusion_matrix, precision_recall_fscore_support
from util import get_classification_report, plot_confusion_matrix, plot_loss_curve, init_weights
class Trainer():
def __init__(self, hyp):
# using cuda if available
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.model = DAGMM(hyp).to(self.device)
self.train_loader, self.test_loader = get_loader(hyp)
# AdamW Optimizer
self.optimizer = optim.AdamW(self.model.parameters(), hyp['lr'], amsgrad=True)
# change the learning rate amid learning to get better performance
self.scheduler = optim.lr_scheduler.ExponentialLR(optimizer=self.optimizer, gamma=hyp['decay_rate'])
def train(self, hyp):
self.model.train()
self.model.apply(init_weights)
loss_list, recon_list, energy_list = [], [], []
for epoch in range(hyp['epochs']):
#loss_total = 0
for data, target in self.train_loader:
data, target = data.to(self.device), target.to(self.device)
self.optimizer.zero_grad()
zc, xp, z, gamma = self.model(data)
data, xp, z, gamma = data.cpu(), xp.cpu(), z.cpu(), gamma.cpu()
loss, reconstruct_error, sample_energy, P = self.model.criterion(data, xp, gamma, z)
#loss_total += loss.item()
loss.backward()
self.optimizer.step()
self.scheduler.step()
#plot_loss_curve(loss_list)
torch.save(self.model.state_dict(), 'results/dagmm.pth')
return loss_list, recon_list, energy_list
def load_model(self, hyp):
model = DAGMM(hyp)
try:
model.load_state_dict(torch.load('results/dagmm.pth'))
print('Successfully loaded model')
except IOError as e:
print('An IOError occurred. {}'.format(e.args[-1]))
return model
def compute_threshold(self, model):
energy_list = []
with torch.no_grad():
for data, labels in self.train_loader:
data, labels = data.to(self.device), labels.to(self.device)
zc,xp,z,gamma = model(data)
_, _, _, _, E = model.sample_energy(gamma, z)
energy_list.extend(E.detach().cpu().tolist())
threshold = np.percentile(energy_list, 80.31)
print('threshold: %.4f' %(threshold))
return threshold
def test(self, hyp):
model = self.load_model(hyp)
model.eval()
if hyp['is_threshold'] == True:
threshold = hyp['threshold']
else:
threshold = self.compute_threshold(model)
true_list, pred_list = [], []
index = 1
with torch.no_grad():
for x, y in self.test_loader:
x = x.to(self.device)
zc,xp,z,gamma = model(x)
_, _, _, _, E = model.sample_energy(gamma, z)
true_list.extend(y.cpu().tolist())
pred_list.extend((E > threshold).cpu().tolist())
# plot 3D scatter plot to show the difference between normal and abnormal samples
if index <= 4:
fig = plt.figure(figsize = (10, 7))
ax = plt.axes(projection ="3d")
normal_list, abnormal_list = [], []
z = z.detach().cpu().tolist()
for i in range(hyp['batch_size']*10):
if y[i] == 1:
normal_list.append(z[i])
else:
abnormal_list.append(z[i])
print(normal_list)
normal_list, abnormal_list = np.array(normal_list), np.array(abnormal_list)
ax.scatter3D(normal_list[:,0], normal_list[:,1], normal_list[:,2], color='green', marker='o', label='normal')
ax.scatter3D(abnormal_list[:,0], abnormal_list[:,1], abnormal_list[:,2], color='red', marker='x', label='abnormal')
ax.set_xlabel('z1')
ax.set_ylabel('z2')
ax.set_zlabel('z3')
ax.legend()
plt.title("3D scatter plot")
plt.savefig('results/3d_scatter_plot_{}.png'.format(str(index)), dpi=600)
index += 1
accuracy = accuracy_score(true_list, pred_list) * 100
print('Accuracy: {:.2f}%'.format(accuracy))
# get and plot confusion matrix
cm = confusion_matrix(true_list, pred_list)
plot_confusion_matrix(cm, 'test')
get_classification_report(true_list, pred_list)
return accuracy