-
Notifications
You must be signed in to change notification settings - Fork 7
/
trainer.py
executable file
·250 lines (219 loc) · 9.34 KB
/
trainer.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
import torch
import torch.nn.functional as F
import numpy as np
from tqdm import tqdm
from sklearn.metrics import roc_curve
from scipy.optimize import brentq
from scipy.interpolate import interp1d
from sklearn.metrics import confusion_matrix
from collections import OrderedDict
from utils import *
from metric import compute_sed_scores, calculate_per_class_lwlrap
#####
# ASC & SED & TAG Joint Training
#####
def train_joint_3task(model, trnset_gen_ASC, trnset_gen_SED, trnset_gen_TAG, epoch, args, device, criterion, optimizer, lr_scheduler):
#train phase
model.train()
idx_ct_start = int(args.nb_iter_per_epoch*epoch)
mixup = args.do_mixup and epoch > args.mixup_start
loss = 0.
loss_SED = 0.
loss_ASC = 0.
loss_TAG = 0.
with tqdm(total = args.nb_iter_per_epoch, ncols = 100) as pbar:
for idx in range(args.nb_iter_per_epoch):
_loss = 0.
#####
# ASC feed forward
#####
if 'ASC' in args.task:
m_batch, m_label = next(trnset_gen_ASC)
m_batch, m_label = m_batch.to(device), m_label.to(device)
#mixup data if condition is met
if mixup:
m_batch, m_label_a, m_label_b, lam = mixup_data(m_batch, m_label, alpha=args.mixup_alpha)
m_batch, m_label_a, m_label_b = map(torch.autograd.Variable, [m_batch, m_label_a, m_label_b])
out_ASC = model(m_batch, mode = ['ASC'])['ASC']
_loss_ASC = mixup_criterion(criterion['cce_ASC'], out_ASC, m_label_a, m_label_b, lam)
else:
out_ASC = model(m_batch, mode = ['ASC'])['ASC']
_loss_ASC = criterion['cce_ASC'](out_ASC, m_label)
_loss += args.loss_weight_ASC * _loss_ASC
loss_ASC += _loss_ASC.detach().cpu().numpy()
#####
# SED feed forward
#####
if 'SED' in args.task:
m_batch, m_label = next(trnset_gen_SED)
m_batch, m_label = m_batch.to(device), m_label.to(device, dtype=torch.float)
#mixup data if condition is met
if mixup:
m_batch, m_label_a, m_label_b, lam = mixup_data(m_batch, m_label, alpha=args.mixup_alpha)
m_batch, m_label_a, m_label_b = map(torch.autograd.Variable, [m_batch, m_label_a, m_label_b])
out_SED = model(m_batch, mode = ['SED'])['SED']
_loss_SED = mixup_criterion(criterion['bce_SED'], out_SED, m_label_a, m_label_b, lam)
else:
out_SED = model(m_batch, mode = ['SED'])['SED']
_loss_SED = criterion['bce_SED'](out_SED, m_label)
_loss += args.loss_weight_SED * _loss_SED
loss_SED += _loss_SED.detach().cpu().numpy()
#####
# TAG feed forward
#####
if 'TAG' in args.task:
m_batch, m_label = next(trnset_gen_TAG)
m_batch, m_label = m_batch.to(device), m_label.to(device, dtype=torch.float)
out_TAG = model(m_batch, mode = ['TAG'])['TAG']
#_loss_TAG = criterion['bce_TAG'](out_TAG, m_label)
if mixup:
m_batch, m_label_a, m_label_b, lam = mixup_data(m_batch, m_label, alpha=args.mixup_alpha)
m_batch, m_label_a, m_label_b = map(torch.autograd.Variable, [m_batch, m_label_a, m_label_b])
out_TAG = model(m_batch, mode = ['TAG'])['TAG']
_loss_TAG = mixup_criterion(criterion['bce_TAG'], out_TAG, m_label_a, m_label_b, lam)
else:
out_TAG = model(m_batch, mode = ['TAG'])['TAG']
_loss_TAG = criterion['bce_TAG'](out_TAG, m_label)
_loss += args.loss_weight_TAG * _loss_TAG
loss_TAG += _loss_TAG.detach().cpu().numpy()
loss += _loss.detach().cpu().numpy()
#####
# Joint back-prop
#####
optimizer.zero_grad()
_loss.backward()
optimizer.step()
if idx % args.nb_iter_per_log == 0:
description = '%s\tepoch: %d '%(args.name, epoch)
if idx != 0:
if 'SED' in args.task:
loss_SED /= args.nb_iter_per_log
description += 'SED: %.3f '%(loss_SED)
loss_SED = 0.
if 'ASC' in args.task:
loss_ASC /= args.nb_iter_per_log
description += 'ASC: %.3f '%(loss_ASC)
loss_ASC = 0.
if 'TAG' in args.task:
loss_TAG /= args.nb_iter_per_log
description += 'TAG: %.3f '%(loss_TAG)
loss_TAG = 0.
loss /= args.nb_iter_per_log
description += 'TOT: %.4f'%(loss)
loss = 0.
pbar.set_description(description)
if idx != 0:
pbar.update(args.nb_iter_per_log)
else:
pbar.update(1)
############
## delete ##
############
# XXX: where variable 'lr' using?
########################################################
"""
for p_group in optimizer.param_groups:
lr = p_group['lr']
break
"""
########################################################
if args.do_lr_decay:
if args.lr_decay == 'cosine':
lr_scheduler.step()
else:
raise NotImplementedError('Not just yet..')
#####
# SED
#####
def evaluate_SED(model, evlset_gen, device, args):
y_true = []
y_pred = []
y_keys = []
model.eval()
with torch.set_grad_enabled(False):
for m_batch, m_label, m_keys in evlset_gen:
m_batch = m_batch.to(device)
out = model(m_batch, mode = ['SED'])['SED']
if args.verbose >= 3: print('out shape: ', out.size()) #should be (bs, 600, 14)
y_true.append(m_label.numpy())
y_keys.append(m_keys)
y_pred.append(out.detach().cpu().numpy())
y_true = np.concatenate(y_true, axis=0)
y_pred = np.concatenate(y_pred, axis=0)
y_keys = np.concatenate(y_keys, axis=0)
if args.verbose >=2:
print('y_true:',y_true.shape)
print('y_pred:',y_pred.shape)
print('y_keys:',y_keys.shape)
assert len(y_true) == len(y_pred) == len(y_keys)
d_decode = OrderedDict()
d_true = OrderedDict()
for p, k, t in zip(y_pred, y_keys, y_true):
k_seg = k.split(':')[0]
if k_seg not in d_decode:
d_decode[k_seg] = np.zeros((4, 600, 14))
d_true[k_seg] = t
c = int(k.strip().split(':')[-1][2])-1 #channel
d_decode[k_seg][c,:,:] = p
y_pred, y_true = [], []
for k, v in d_decode.items():
y_pred.append(np.mean(v, axis = 0))
y_true.append(d_true[k])
y_pred = np.concatenate(y_pred, axis = 0)
y_true = np.concatenate(y_true, axis = 0)
if args.verbose >=2:
print('y_true:',y_true.shape)
print('y_pred:',y_pred.shape)
er, f1 = compute_sed_scores(
pred = (y_pred>args.sed_threshold).astype(np.float32),
gt = y_true,
nb_frames_1s = 50
)
if args.verbose>=2:
print('y_true: ', y_true.shape)
print('y_pred: ', y_pred.shape)
assert y_true.shape == y_pred.shape
if args.verbose > 0: print('Eval SED success')
return er, f1
#####
# ASC
#####
def evaluate_ASC(model, evlset_gen, device, args):
y_pred = []
y_true = []
model.eval()
with torch.set_grad_enabled(False):
for m_batch, m_label in evlset_gen:
m_batch = m_batch.view(-1, 1, args.nb_mels, args.nb_frames_ASC+1).to(device)
out = model(m_batch, mode = ['ASC'])['ASC']
out = F.softmax(out, dim=-1).view(-1, 3, out.size(1)).mean(dim=1, keepdim=False)
m_label = list(m_label.numpy())
y_pred.extend(list(out.cpu().numpy())) #>>> (16, 64?)
y_true.extend(m_label)
y_pred = np.argmax(np.array(y_pred), axis=1).tolist()
conf_mat = confusion_matrix(y_true = y_true, y_pred = y_pred)
nb_cor = 0
for i in range(len(conf_mat)):
nb_cor += conf_mat[i,i]
conf_mat[i,i] = 0
acc = nb_cor / len(y_true) * 100
if args.verbose > 0: print('Eval ASC success')
return acc, conf_mat
#####
# TAG
#####
def evaluate_TAG(model, evlset_gen, device, args):
model.eval()
y_pred = np.zeros([0, 80], np.float32)
y_true = np.zeros([0, 80], np.float32)
with torch.set_grad_enabled(False):
for m_batch, m_label in evlset_gen:
m_batch = m_batch.to(device)
out = torch.sigmoid(model(m_batch, mode = ['TAG'])['TAG'])
m_label = list(m_label.numpy())
y_pred = np.concatenate([y_pred, out.detach().cpu().numpy()])
y_true = np.concatenate([y_true, m_label])
per_class_lwlrap, weight_per_class = calculate_per_class_lwlrap(y_true, y_pred)
lwlrap = np.sum(per_class_lwlrap * weight_per_class)
if args.verbose > 0: print('Eval TAG success')
return lwlrap