forked from Entromorgan/RepNet-Pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_utils.py
271 lines (213 loc) · 8.98 KB
/
train_utils.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import time
import torch
import numpy as np
from tqdm import tqdm
from torch.utils.data import DataLoader, ConcatDataset
#============metrics ==================
def MAE(y, ypred) :
"""for period"""
batch_size = y.shape[0]
yarr = y.clone().detach().cpu().numpy()
ypredarr = ypred.clone().detach().cpu().numpy()
ae = np.sum(np.absolute(yarr - ypredarr))
mae = ae / yarr.flatten().shape[0]
return mae
def f1score(y, ypred) :
"""for periodicity"""
batch_size = y.shape[0]
yarr = y.clone().detach().cpu().numpy()
ypredarr = ypred.clone().detach().cpu().numpy().astype(bool)
tp = np.logical_and(yarr, ypredarr).sum()
precision = tp / (ypredarr.sum() + 1e-6)
recall = tp / (yarr.sum() + 1e-6)
if precision + recall == 0:
fscore = 0
else :
fscore = 2*precision*recall/(precision + recall)
return fscore
def running_mean(x, N):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / float(N)
def getPeriodicity(periodLength):
periodicity = torch.nn.functional.threshold(periodLength, 2, 0)
periodicity = -torch.nn.functional.threshold(-periodicity, -1, -1)
return periodicity
def getCount(periodLength):
frac = 1/periodLength
frac = torch.nan_to_num(frac, 0, 0, 0)
count = torch.sum(frac, dim = [1])
return count
def getStart(periodLength):
tmp = periodLength.squeeze(2)
idx = torch.arange(tmp.shape[1], 0, -1)
tmp2 = tmp * idx
indices = torch.argmax(tmp2, 1, keepdim=True)
return indices
def training_loop(n_epochs,
device,
model,
train_set,
val_set,
batch_size,
lr = 6e-6,
ckpt_name = 'ckpt',
use_count_error = True,
saveCkpt= True,
train = True,
validate = True,
lastCkptPath = None):
prevEpoch = 0
trainLosses = []
valLosses = []
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr = lr)
if lastCkptPath != None :
print("loading checkpoint")
checkpoint = torch.load(lastCkptPath)
prevEpoch = checkpoint['epoch']
trainLosses = checkpoint['trainLosses']
valLosses = checkpoint['valLosses']
model.load_state_dict(checkpoint['state_dict'], strict = True)
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
del checkpoint
model.to(device)
for state in optimizer.state.values():
for k, v in state.items():
if isinstance(v, torch.Tensor):
state[k] = v.to(device)
lossMAE = torch.nn.SmoothL1Loss()
lossBCE = torch.nn.BCEWithLogitsLoss()
train_loader = DataLoader(train_set,
batch_size=batch_size,
num_workers=1,
shuffle = True)
val_loader = DataLoader(val_set,
batch_size = batch_size,
num_workers=1,
drop_last = False,
shuffle = True)
if validate and not train:
currEpoch = prevEpoch
else :
currEpoch = prevEpoch + 1
for epoch in tqdm(range(currEpoch, n_epochs + currEpoch)):
#train loop
if train :
pbar = tqdm(train_loader, total = len(train_loader))
mae = 0
mae_count = 0
fscore = 0
i = 1
a=0
for X, y in pbar:
torch.cuda.empty_cache()
model.train()
X = X.to(device).float()
y1 = y.to(device).float()
y2 = getPeriodicity(y1).to(device).float()
y1pred, y2pred = model(X)
loss1 = lossMAE(y1pred, y1)
loss2 = lossBCE(y2pred, y2)
loss = loss1 + 5*loss2
countpred = torch.sum((y2pred > 0) / (y1pred + 1e-1), 1)
count = torch.sum((y2 > 0) / (y1 + 1e-1), 1)
loss3 = torch.sum(torch.div(torch.abs(countpred - count), (count + 1e-1)))
if use_count_error:
loss += loss3
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss = loss.item()
trainLosses.append(train_loss)
mae += loss1.item()
mae_count += loss3.item()
del X, y, y1, y2, y1pred, y2pred
i+=1
pbar.set_postfix({'Epoch': epoch,
'MAE_period': (mae/i),
'MAE_count' : (mae_count/i),
'Mean Tr Loss':np.mean(trainLosses[-i+1:])})
if validate:
#validation loop
with torch.no_grad():
mae = 0
mae_count = 0
fscore = 0
i = 1
pbar = tqdm(val_loader, total = len(val_loader))
for X, y in pbar:
torch.cuda.empty_cache()
model.eval()
X = X.to(device).float()
y1 = y.to(device).float()
y2 = getPeriodicity(y1).to(device).float()
y1pred, y2pred = model(X)
loss1 = lossMAE(y1pred, y1)
loss2 = lossBCE(y2pred, y2)
loss = loss1 + loss2
countpred = torch.sum((y2pred > 0) / (y1pred + 1e-1), 1)
count = torch.sum((y2 > 0) / (y1 + 1e-1), 1)
loss3 = lossMAE(countpred, count)
if use_count_error:
loss += loss3
val_loss = loss.item()
valLosses.append(val_loss)
mae += loss1.item()
mae_count += loss3.item()
del X, y, y1, y2, y1pred, y2pred
i += 1
pbar.set_postfix({'Epoch': epoch,
'MAE_period': (mae/i),
'MAE_count' : (mae_count/i),
'Mean Val Loss':np.mean(valLosses[-i+1:])})
#save checkpoint
if saveCkpt:
checkpoint = {
'epoch': epoch,
'state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'trainLosses' : trainLosses,
'valLosses' : valLosses
}
torch.save(checkpoint,
'checkpoint/' + ckpt_name + str(epoch) + '.pt')
#lr_scheduler.step()
return trainLosses, valLosses
def trainTestSplit(dataset, TTR):
trainDataset = torch.utils.data.Subset(dataset, range(0, int(TTR * len(dataset))))
valDataset = torch.utils.data.Subset(dataset, range(int(TTR*len(dataset)), len(dataset)))
return trainDataset, valDataset
def running_mean(x, N):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / float(N)
def plot_grad_flow(named_parameters):
'''Plots the gradients flowing through different layers in the net during training.
Can be used for checking for possible gradient vanishing / exploding problems.
Usage: Plug this function in Trainer class after loss.backwards() as
"plot_grad_flow(self.model.named_parameters())" to visualize the gradient flow'''
import matplotlib.pyplot as plts
from matplotlib.lines import Line2D
ave_grads = []
max_grads= []
median_grads = []
layers = []
for n, p in named_parameters:
if(p.requires_grad) and (p.grad is not None) and ("bias" not in n):
layers.append(n)
ave_grads.append(p.grad.abs().mean())
max_grads.append(p.grad.abs().max())
median_grads.append(p.grad.abs().median())
width = 0.3
plt.bar(np.arange(len(max_grads)), max_grads, width, color="c")
plt.bar(np.arange(len(max_grads)) + width, ave_grads, width, color="b")
plt.bar(np.arange(len(max_grads)) + 2*width, median_grads, width, color='r')
plt.hlines(0, 0, len(ave_grads)+1, lw=2, color="k" )
plt.xticks(range(0,len(ave_grads), 1), layers, rotation="vertical")
plt.xlim(left=0, right=len(ave_grads))
plt.ylim(bottom = -0.001, top=0.02) # zoom in on the lower gradient regions
plt.xlabel("Layers")
plt.ylabel("average gradient")
plt.title("Gradient flow")
plt.grid(True)
plt.legend([Line2D([0], [0], color="c", lw=4),
Line2D([0], [0], color="b", lw=4),
Line2D([0], [0], color="r", lw=4)], ['max-gradient', 'mean-gradient', 'median-gradient'])