-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_classifier.py
181 lines (166 loc) · 8.63 KB
/
train_classifier.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
import numpy as np
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, f1_score
import torch
import torch.nn as nn
import os
from data_utils import read_freq_data, get_all_sets
from torch.utils.data import random_split
import torchvision.transforms as transforms
import argparse
import matplotlib.pyplot as plt
from joblib import dump
from datetime import datetime
import pandas as pd
import json
import random as python_random
from torch.utils.data import random_split, DataLoader
import torch.optim as optim
from networks import classifier_circular
import copy
today = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
import torch.nn.functional as F
import shutil
from mpl_toolkits.axes_grid1 import make_axes_locatable
activations = dict()
def conv_inp_op(self, inp, outp):
activations["input"] = inp[0].detach()
activations["output"] = outp.detach()
class AnomalyClassifier:
def __init__(self, config_path):
# load data
config=json.load(open(config_path, 'r'))
self.config_path = config_path
torch.manual_seed(config["random_seed"])
np.random.seed(config["random_seed"])
python_random.seed(config["random_seed"])
self.save_path = config["save_path"]
input_signal, output_signal, self.labels = read_freq_data(config["folder_path"])
train_split, val_split, test_split = get_all_sets(input_signal, output_signal, self.labels)
ann_upsampler = torch.load(config["upsampler_path"])
get_input = lambda x: torch.from_numpy(np.array([ann_upsampler(inp.float()).detach().numpy() for inp, op, label in x]))
get_label = lambda x: np.array([label for inp, op, label in x])
self.train_input, self.train_labels = get_input(train_split), get_label(train_split)
self.train_set = list(zip(self.train_input, self.train_labels))
self.val_input, self.val_labels = get_input(val_split), get_label(val_split)
self.val_set = list(zip(self.val_input, self.val_labels))
self.test_input, self.test_labels = get_input(test_split), get_label(test_split)
self.test_set = list(zip(self.test_input, self.test_labels))
print(f"Train Normal Signals - {np.sum(self.train_labels==0)}")
print(f"Train Abnormal Signals - {np.sum(self.train_labels==1)}")
print(f"Test Normal Signals - {np.sum(self.test_labels==0)}")
print(f"Test Abnormal Signals - {np.sum(self.test_labels==1)}")
self.train_loader = DataLoader(self.train_set, shuffle=True, batch_size = config["batch_size"])
self.val_loader = DataLoader(self.val_set, shuffle=True, batch_size = config["batch_size"])
self.test_loader = DataLoader(self.test_set, batch_size = len(self.test_set))
self.device = torch.device(
"cuda:0" if torch.cuda.is_available() else "cpu")
# Loss Function
if config["pos_weight"]:
self.loss_func = torch.nn.BCELoss(pos_weight=torch.tensor(np.sum(self.train_labels==0)/np.sum(self.train_labels==1)))
else:
self.loss_func = torch.nn.BCELoss()
if config["conv1d"]:
self.classifier = classifier_circular.CirConvNetClassifier()
else:
self.classifier = classifier_circular.FCNClassifier(69, 1, [8, 8])
print(self.classifier)
self.classifier = self.classifier.to(self.device)
# Register hook
# self.classifier.layer1.register_forward_hook(conv_inp_op)
pytorch_total_params = sum(p.numel() for p in self.classifier.parameters() if p.requires_grad)
print(pytorch_total_params)
if config.get("optimizer", None) == "Adam":
self.optimizer = optim.Adam(self.classifier.parameters(), lr=config["lr"], weight_decay=config["weight_decay"])
else:
self.optimizer = optim.SGD(self.classifier.parameters(), lr=config["lr"], weight_decay=config["weight_decay"])
print(self.optimizer)
self.epochs = config["epochs"]
self.save_path = config["save_path"]
self.best_model = None
self.save_model = config["save_model"]
def accuracy(self, loader, model=None, show_confusion=False, save_confusion_path = None, loader_labels=None):
if model is None:
model = self.classifier
acc = 0.0
normal_acc = 0
abnormal_acc = 0
all_predictions = list()
all_labels = list()
with torch.no_grad():
for inp, labels in loader:
inp = inp.to(self.device)
labels = labels.to(self.device)
pred = model(inp.float())
pred[pred > 0.5] = 1
pred[pred < 0.5] = 0
pred = pred.squeeze(1)
acc += torch.sum(pred == labels)
all_predictions.append(pred)
all_labels.append(labels)
normal_acc += torch.sum(pred[labels == 0] == 0)
abnormal_acc += torch.sum(pred[labels == 1])
all_predictions_tensor = torch.cat(all_predictions)
all_labels_tensor = torch.cat(all_labels)
if show_confusion:
pred = all_predictions_tensor
cm = confusion_matrix(loader_labels, pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm,
display_labels=["Normal", 'Abnormal'])
disp.plot()
if save_confusion_path:
plt.savefig(save_confusion_path)
plt.show()
total_accuracy = acc/len(loader.dataset)
abnormal_accuracy = abnormal_acc/np.sum(loader_labels==1)
normal_accuracy = normal_acc/np.sum(loader_labels == 0)
f1 = f1_score(all_labels_tensor.numpy(), all_predictions_tensor.numpy())
return total_accuracy, abnormal_accuracy, normal_accuracy, f1
def train(self):
best_val_acc = -1
best_val_f1 = -1
best_model = None
best_epoch = None
for epoch in range(0, self.epochs):
train_loss = 0.0
for inp, label in self.train_loader:
inp = inp.to(self.device)
label = label.to(self.device)
self.optimizer.zero_grad()
pred = self.classifier(inp.float())
loss = self.loss_func(pred.squeeze(1), label.float()) # + 0.5*l1_regularization
loss.backward()
self.optimizer.step()
train_loss += loss.item()
val_loss = 0.0
val_accuracy, val_adnormal, val_normal, f1_val = self.accuracy(self.val_loader, loader_labels=self.val_labels)
train_accuracy, train_adnormal, train_normal, _ = self.accuracy(self.train_loader, loader_labels=self.train_labels)
if f1_val > best_val_f1:
best_val_f1 = f1_val
best_val_acc = val_accuracy
best_model = copy.deepcopy(self.classifier)
best_epoch = epoch
if not epoch % 100:
print(f"Epoch - {epoch}, train loss - {train_loss/len(self.train_loader)*1.0:.5f}, Train accuracy - {train_accuracy:.5f}, val accuracy - {val_accuracy:.5f}, val abnormal - {val_adnormal}, val normal - {val_normal}, val F1 - {f1_val}")
cnf_matrix_path = None
if self.save_model:
print("Saving Model")
with torch.no_grad():
model_info_path = os.path.join(self.save_path, str(today))
print(f"Saving Model to {model_info_path}")
cnf_matrix_path = os.path.join(model_info_path, "test_confusion.png")
if os.path.exists(model_info_path):
shutil.rmtree(model_info_path)
os.makedirs(model_info_path)
torch.save(best_model.cpu(), os.path.join(model_info_path, f"classifier_{today}.pt"))
shutil.copyfile(self.config_path, os.path.join(model_info_path, "config.json"))
print(f"Validation best Epoch: {best_epoch}, Acc: {best_val_acc}, F1: {best_val_f1}")
total, abnrmal, nrmal, f1 = self.accuracy(self.test_loader, best_model, show_confusion=True, save_confusion_path=cnf_matrix_path, loader_labels=self.test_labels)
print(f"Test Set - Accuracy: {total}, Abnormal: {abnrmal}, Normal: {nrmal}, F1: {f1}")
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='HRV Regressor Training')
parser.add_argument('--config', help="path to config with training params", default="config.json")
parser.add_argument('--signal_type', help="Signal type", default="original", choices=["original", "reconstructed"])
args = parser.parse_args()
trainer = AnomalyClassifier(config_path=args.config)
trainer.train()