|
a |
|
b/Generation/utils/tools.py |
|
|
1 |
import os |
|
|
2 |
|
|
|
3 |
import numpy as np |
|
|
4 |
import torch |
|
|
5 |
import matplotlib.pyplot as plt |
|
|
6 |
import pandas as pd |
|
|
7 |
|
|
|
8 |
plt.switch_backend('agg') |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
def adjust_learning_rate(optimizer, epoch, args): |
|
|
12 |
# lr = args.learning_rate * (0.2 ** (epoch // 2)) |
|
|
13 |
if args.lradj == 'type1': |
|
|
14 |
lr_adjust = {epoch: args.learning_rate * (0.5 ** ((epoch - 1) // 1))} |
|
|
15 |
elif args.lradj == 'type2': |
|
|
16 |
lr_adjust = { |
|
|
17 |
2: 5e-5, 4: 1e-5, 6: 5e-6, 8: 1e-6, |
|
|
18 |
10: 5e-7, 15: 1e-7, 20: 5e-8 |
|
|
19 |
} |
|
|
20 |
if epoch in lr_adjust.keys(): |
|
|
21 |
lr = lr_adjust[epoch] |
|
|
22 |
for param_group in optimizer.param_groups: |
|
|
23 |
param_group['lr'] = lr |
|
|
24 |
print('Updating learning rate to {}'.format(lr)) |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
class EarlyStopping: |
|
|
28 |
def __init__(self, patience=7, verbose=False, delta=0): |
|
|
29 |
self.patience = patience |
|
|
30 |
self.verbose = verbose |
|
|
31 |
self.counter = 0 |
|
|
32 |
self.best_score = None |
|
|
33 |
self.early_stop = False |
|
|
34 |
self.val_loss_min = np.Inf |
|
|
35 |
self.delta = delta |
|
|
36 |
|
|
|
37 |
def __call__(self, val_loss, model, path): |
|
|
38 |
score = -val_loss |
|
|
39 |
if self.best_score is None: |
|
|
40 |
self.best_score = score |
|
|
41 |
self.save_checkpoint(val_loss, model, path) |
|
|
42 |
elif score < self.best_score + self.delta: |
|
|
43 |
self.counter += 1 |
|
|
44 |
print(f'EarlyStopping counter: {self.counter} out of {self.patience}') |
|
|
45 |
if self.counter >= self.patience: |
|
|
46 |
self.early_stop = True |
|
|
47 |
else: |
|
|
48 |
self.best_score = score |
|
|
49 |
self.save_checkpoint(val_loss, model, path) |
|
|
50 |
self.counter = 0 |
|
|
51 |
|
|
|
52 |
def save_checkpoint(self, val_loss, model, path): |
|
|
53 |
if self.verbose: |
|
|
54 |
print(f'Validation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f}). Saving model ...') |
|
|
55 |
torch.save(model.state_dict(), path + '/' + 'checkpoint.pth') |
|
|
56 |
self.val_loss_min = val_loss |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
class dotdict(dict): |
|
|
60 |
"""dot.notation access to dictionary attributes""" |
|
|
61 |
__getattr__ = dict.get |
|
|
62 |
__setattr__ = dict.__setitem__ |
|
|
63 |
__delattr__ = dict.__delitem__ |
|
|
64 |
|
|
|
65 |
|
|
|
66 |
class StandardScaler(): |
|
|
67 |
def __init__(self, mean, std): |
|
|
68 |
self.mean = mean |
|
|
69 |
self.std = std |
|
|
70 |
|
|
|
71 |
def transform(self, data): |
|
|
72 |
return (data - self.mean) / self.std |
|
|
73 |
|
|
|
74 |
def inverse_transform(self, data): |
|
|
75 |
return (data * self.std) + self.mean |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
def visual(true, preds=None, name='./pic/test.pdf'): |
|
|
79 |
""" |
|
|
80 |
Results visualization |
|
|
81 |
""" |
|
|
82 |
plt.figure() |
|
|
83 |
plt.plot(true, label='GroundTruth', linewidth=2) |
|
|
84 |
if preds is not None: |
|
|
85 |
plt.plot(preds, label='Prediction', linewidth=2) |
|
|
86 |
plt.legend() |
|
|
87 |
plt.savefig(name, bbox_inches='tight') |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
def adjustment(gt, pred): |
|
|
91 |
anomaly_state = False |
|
|
92 |
for i in range(len(gt)): |
|
|
93 |
if gt[i] == 1 and pred[i] == 1 and not anomaly_state: |
|
|
94 |
anomaly_state = True |
|
|
95 |
for j in range(i, 0, -1): |
|
|
96 |
if gt[j] == 0: |
|
|
97 |
break |
|
|
98 |
else: |
|
|
99 |
if pred[j] == 0: |
|
|
100 |
pred[j] = 1 |
|
|
101 |
for j in range(i, len(gt)): |
|
|
102 |
if gt[j] == 0: |
|
|
103 |
break |
|
|
104 |
else: |
|
|
105 |
if pred[j] == 0: |
|
|
106 |
pred[j] = 1 |
|
|
107 |
elif gt[i] == 0: |
|
|
108 |
anomaly_state = False |
|
|
109 |
if anomaly_state: |
|
|
110 |
pred[i] = 1 |
|
|
111 |
return gt, pred |
|
|
112 |
|
|
|
113 |
|
|
|
114 |
def cal_accuracy(y_pred, y_true): |
|
|
115 |
return np.mean(y_pred == y_true) |