-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
192 lines (164 loc) · 6.64 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
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
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
"""
Created on Wed May 3 10:52:22 2023
@author: Revlis_user
"""
#%%
'Import Libraries'
from datetime import datetime
import math
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import f1_score, confusion_matrix
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
from params import *
from preprocessing import *
from utils import *
from xresnet1d import *
#%%
'Data Loader'
train_set = Dataset(train_x, train_y.values, int(w_size * model_sr),)
val_set = Dataset(val_x, val_y.values, int(w_size * model_sr))
train_loader = DataLoader(dataset=train_set,
batch_size = batch_size,
shuffle=True,)
val_loader = DataLoader(dataset=val_set,
batch_size = batch_size,
shuffle=True)
test_set = StepWiseTestset(test_x, test_y.values,
int(w_size * model_sr), int(.5 * model_sr))
test_loader = DataLoader(dataset=test_set,
batch_size = 1,
shuffle=False)
#%%
'Training the Model'
model = xresnet1d152(model_drop_r=model_dropout,
original_f_number=False,
# Original xResnet's Resblock Filter Dim if True
# PTB-XL Benchmark Article's Filter Dim if False
fc_drop=fc_drop,)
model.to(device)
opt = torch.optim.AdamW(model.parameters(), lr =.01)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
opt, mode='min', factor=0.1, patience=5)
step_per_epoch = math.ceil(len(train_set)/batch_size)
train_loss = []
val_loss = []
train_f1 = []
val_f1 = []
for epoch in range(n_epochs):
t0 = datetime.now()
loss_per_step = []
f1_per_step = []
model.train()
with tqdm(train_loader, unit='batch',) as per_epoch:
for x,y in per_epoch:
opt.zero_grad()
per_epoch.set_description(f"Epoch: {epoch+1}/{n_epochs}")
x,y = x.to(device, torch.float32), y.to(device, torch.float32)
y_hat = model(x)
loss = loss_fn(y_hat, y)
y_hat = (out_activation(y_hat).cpu().detach().numpy()>=.5
).astype(int)
loss_per_step.append(loss.item())
score = f1_score(y.cpu().numpy().astype('int32'), y_hat,
average='samples', zero_division = 0)
f1_per_step.append(score)
loss.backward()
opt.step()
per_epoch.set_postfix(train_loss=loss.item(),
f1 = score)
if scheduler.__module__ == 'torch.optim.lr_scheduler':
scheduler.step(1)
train_loss.append(sum(loss_per_step)/len(loss_per_step))
train_f1.append(np.average(f1_per_step))
val_loss_per_step = []
val_f1_per_step = []
model.eval()
with torch.no_grad():
with tqdm(val_loader, unit='batch',) as per_val_epoch:
for x_val,y_val in per_val_epoch:
per_val_epoch.set_description("Model Evaluation: ")
x_val,y_val = x_val.to(device, torch.float32
), y_val.to(device,
torch.float32)
y_hat_val = model(x_val)
loss_val = loss_fn(y_hat_val, y_val)
y_hat_val = (out_activation(
y_hat_val).cpu().detach().numpy()>=.5).astype(int)
val_loss_per_step.append(loss_val.item())
score = f1_score(y_val.cpu().numpy().astype('int32'),
y_hat_val,average='samples',
zero_division = 0)
val_f1_per_step.append(score)
per_val_epoch.set_postfix(val_loss=loss_val.item(),
f1 = score)
val_loss.append(sum(val_loss_per_step)/len(val_loss_per_step))
val_f1.append(np.average(val_f1_per_step))
torch.save(model.state_dict(),
f'results/model_weight_{epoch+1:02d}.pt')
dt = datetime.now() - t0
with open('results/log.txt',
"a+") as external_file:
print(f'''train_loss: {train_loss[-1]:.6f},
train_f1: {train_f1[-1]:.6f}''',
f"val_loss: {val_loss[-1]:.6f}, val_f1: {val_f1[-1]:.6f}",
f'Time Duration: {dt}',
file=external_file)
print(f'train_loss: {train_loss[-1]:.6f}, train_f1: {train_f1[-1]:.6f}',
f"val_loss: {val_loss[-1]:.6f}, val_f1: {val_f1[-1]:.6f}",
f'Time Duration: {dt}')
#%%
'Results Visualization'
plt.figure()
plt.plot(train_loss, label='train loss')
plt.plot(val_loss, label='val loss')
plt.legend()
plt.show()
plt.savefig('results/Loss.png', )
plt.figure()
plt.plot(train_f1, label='train F1')
plt.plot(val_f1, label='val F1')
plt.legend()
plt.show()
plt.savefig('results/F1_Score.png', )
predictions = []
model.eval()
with torch.no_grad():
for x_test, y_test in test_loader:
windows_predictions = []
for segments in x_test:
segments = segments.to(device, torch.float32)
output = out_activation(model(segments))
windows_predictions.append(output)
windows_predictions = torch.stack(windows_predictions)
aggregated_prediction = torch.max(windows_predictions, dim=0)[0]
predictions.append(aggregated_prediction)
predictions = torch.cat(predictions, dim=0)
predictions = predictions.cpu().detach().numpy()
'Compute confusion matrix with opt_threshold'
threshold = get_optimal_precision_recall(test_y.values,
predictions)[-1]
threshold = dict(zip(output_label, threshold))
with open('results/log.txt',
"a+") as external_file:
print(f'threshold: {threshold}',
file=external_file)
external_file.close()
for i in range(len(output_label)):
cm = confusion_matrix(test_y.iloc[:,i].values,
predictions[:,i]>=threshold[output_label[i]])
plt.figure()
plot_cm(cm, classes = list(range(cm.shape[0])),
title = f'CM_{output_label[i]}')
plt.savefig(f'results/CM_{output_label[i]}.png')
score = f1_score(test_y.iloc[:,i].values,
predictions[:,i]>=threshold[output_label[i]],
zero_division = 0)
with open('results/log.txt',
"a+") as external_file:
print(f'{output_label[i]}_F1_Score: {score}',
file=external_file)
external_file.close()