-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
319 lines (261 loc) · 12.4 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.nn.utils.rnn import PackedSequence
from dataset import logger
from itertools import permutations
import torch.nn.functional as F
import torch as th
import os
import time
import warnings
warnings.filterwarnings('ignore')
def create_optimizer(optimizer, params, **kwargs):
supported_optimizer = {
'sgd': th.optim.SGD, # momentum, weight_decay, lr
'rmsprop': th.optim.RMSprop, # momentum, weight_decay, lr
'adam': th.optim.Adam, # weight_decay, lr
'adadelta': th.optim.Adadelta, # weight_decay, lr
'adagrad': th.optim.Adagrad, # lr, lr_decay, weight_decay
'adamax': th.optim.Adamax # lr, weight_decay
# ...
}
if optimizer not in supported_optimizer:
raise ValueError('Now only support optimizer {}'.format(optimizer))
if optimizer != 'sgd' and optimizer != 'rmsprop':
del kwargs['momentum']
opt = supported_optimizer[optimizer](params, **kwargs)
logger.info('Create optimizer {}: {}'.format(optimizer, kwargs))
return opt
def packed_sequence_cuda(packed_sequence, device):
if th.cuda.is_available():
packed_sequence = packed_sequence.to(device)
return packed_sequence
class PITONNtrainer(object):
def __init__(self,
nnet,
checkpoint="checkpoint",
optimizer="adam",
lr=1e-5,
momentum=0.9,
weight_decay=0,
clip_norm=None,
min_lr=0,
patience=1,
factor=0.5,
disturb_std=0.0,
gpuid=0):
# multi gpu
'''if not th.cuda.is_available():
raise RuntimeError("CUDA device unavailable...exist")
if not isinstance(gpuid, tuple):
gpuid = (gpuid, )'''
self.device = th.device('cuda:{}'.format(gpuid[0]))
self.device = th.device('cpu') if gpuid == - \
1 else th.device('cuda:{}'.format(gpuid[0]))
self.gpuid = gpuid
self.nnet = nnet.to(self.device)
logger.info("Network structure:\n{}".format(self.nnet))
self.optimizer = create_optimizer(
optimizer,
self.nnet.parameters(),
lr=lr,
momentum=momentum,
weight_decay=weight_decay)
self.scheduler = ReduceLROnPlateau(
self.optimizer,
mode='min',
factor=factor,
patience=patience,
min_lr=min_lr,
verbose=True)
self.checkpoint = checkpoint
self.num_spks = nnet.num_spks
self.clip_norm = clip_norm
self.disturb = disturb_std
if self.disturb:
logger.info("Disturb networks with std = {}".format(disturb_std))
if self.clip_norm:
logger.info("Clip gradient by 2-norm {}".format(clip_norm))
if not os.path.exists(checkpoint):
os.makedirs(checkpoint)
self.num_params = sum(
[param.nelement() for param in nnet.parameters()]) / 10.0**6
logger.info("Loading model to GPUs:{}, #param: {:.2f}M".format(
gpuid, self.num_params))
def pre_train(self, dataset):
self.nnet.train()
logger.info("Training...")
dataset.pre_train = False
tot_loss = num_batch = 0
for input_sizes, nnet_input, source_attr, target_attr, status in dataset:
num_batch += 1
nnet_input = packed_sequence_cuda(nnet_input, self.device) if isinstance(
nnet_input, PackedSequence) else nnet_input.to(self.device)
self.optimizer.zero_grad()
if num_batch % 50 == 0:
print("Processed {} batches".format(num_batch))
mix_spec, spk1_spec, spk2_spec, speaker_1, speaker_2, Orth_const = self.nnet(
nnet_input, status,per_train=True)
loss = th.nn.MSELoss()
sparse_const = th.sum(
(speaker_1*speaker_2 + th.finfo(th.float32).eps/10) / (speaker_1 + speaker_2 + th.finfo(th.float32).eps))
cur_loss = th.sum(th.abs(Orth_const)) + \
sparse_const + loss(mix_spec, nnet_input) * 1000
if num_batch % 50 == 0:
print("Processed sparse_const {},Orth_const {},loss {}".format(sparse_const,th.sum(th.abs(Orth_const)),loss(mix_spec, nnet_input)))
tot_loss += cur_loss.item()
cur_loss.backward()
if self.clip_norm:
th.nn.utils.clip_grad_norm_(self.nnet.parameters(),
self.clip_norm)
self.optimizer.step()
return tot_loss / num_batch, num_batch
def train(self, dataset):
self.nnet.train()
logger.info("Training...")
dataset.pre_train = True
tot_loss = num_batch = 0
for input_sizes, nnet_input, source_attr, target_attr, status in dataset:
num_batch += 1
nnet_input = packed_sequence_cuda(nnet_input, self.device) if isinstance(
nnet_input, PackedSequence) else nnet_input.to(self.device)
self.optimizer.zero_grad()
if num_batch % 50 == 0:
print("Processed {} batches".format(num_batch))
mix_spec, spk1_spec, spk2_spec, speaker_1, speaker_2, _ = self.nnet(
nnet_input, status)
cur_loss = self.permutate_loss(
mix_spec, spk1_spec, spk2_spec, input_sizes, source_attr, target_attr)
tot_loss += cur_loss.item()
cur_loss.backward()
if self.clip_norm:
th.nn.utils.clip_grad_norm_(self.nnet.parameters(),
self.clip_norm)
self.optimizer.step()
return tot_loss / num_batch, num_batch
def validate(self, dataset):
self.nnet.eval()
logger.info("Cross Validate...")
tot_loss = num_batch = 0
with th.no_grad():
for input_sizes, nnet_input, source_attr, target_attr, status in dataset:
num_batch += 1
if num_batch % 50 == 0:
print("Processed {} batches".format(num_batch))
nnet_input = packed_sequence_cuda(nnet_input, self.device) if isinstance(
nnet_input, PackedSequence) else nnet_input.to(self.device)
mix_spec, spk1_spec, spk2_spec, speaker_1, speaker_2, _ = self.nnet(
nnet_input,status)
cur_loss = self.permutate_loss(
mix_spec, spk1_spec, spk2_spec, input_sizes, source_attr,target_attr)
tot_loss += cur_loss.item()
return tot_loss / num_batch, num_batch
def permutate_loss(self, mix_spec, spk1_spec, spk2_spec, input_sizes, source_attr, target_attr):
input_sizes = input_sizes.to(self.device)
mixture_spect = source_attr["spectrogram"].to(self.device)
targets_spect = [t.to(self.device) for t in target_attr["spectrogram"]]
if self.num_spks != len(targets_spect):
raise ValueError(
"Number targets do not match known speakers: {} vs {}".format(
self.num_spks, len(targets_spect)))
is_loss_with_psm = "phase" in source_attr
if is_loss_with_psm:
mixture_phase = source_attr["phase"].to(self.device)
targets_phase = [t.to(self.device) for t in target_attr["phase"]]
mask = []
mask.append(spk1_spec/(spk1_spec + spk2_spec +
th.finfo(th.float32).eps))
mask.append(spk2_spec/(spk1_spec + spk2_spec +
th.finfo(th.float32).eps))
mixture_spect_real = mixture_spect.real
mixture_spect_imge = mixture_spect.imag
def loss(permute):
loss_for_permute = []
for s, t in enumerate(permute):
# refer_spect = targets_spect[t] * th.cos(
# mixture_phase -
# targets_phase[t]) if is_loss_with_psm else targets_spect[t]
# TODO: using non-negative psm(add ReLU)?
refer_spect = targets_spect[t] * F.relu(
th.cos(mixture_phase - targets_phase[t])
) if is_loss_with_psm else targets_spect[t]
# N x T x F => N x 1
outsignal_real = mask[s] * mixture_spect_real
outsignal_imge = mask[s] * mixture_spect_imge
refer_spect_real = refer_spect.real
refer_spect_imag = refer_spect.imag
refer_spect = th.cat((refer_spect_real.unsqueeze(-1),refer_spect_imag.unsqueeze(-1)),-1)
outsignal = th.cat((outsignal_real.unsqueeze(-1),outsignal_imge.unsqueeze(-1)),-1)
refer_spect = refer_spect.permute(0,2,1,3)
outsignal = outsignal.permute(0,2,1,3)
refer_spect = th.istft(refer_spect,256,128,256,th.hann_window(256).to(self.device))
outsignal = th.istft(outsignal,256,128,256,th.hann_window(256).to(self.device))
utt_loss = self.calc_sdr_torch(outsignal, refer_spect)
loss_for_permute.append(utt_loss)
loss_perutt = sum(loss_for_permute)
return 0 - loss_perutt
num_utts = input_sizes.shape[0]
# O(N!), could be optimized
# P x N
pscore = th.stack(
[loss(p) for p in permutations(range(self.num_spks))])
min_perutt, _ = th.min(pscore, dim=0)
return th.mean(min_perutt)/2
def calc_sdr_torch(self, estimation, origin, mask=None):
"""
batch-wise SDR caculation for one audio file on pytorch Variables.
estimation: (batch, nsample)
origin: (batch, nsample)
mask: optional, (batch, nsample), binary
"""
if mask is not None:
origin = origin * mask
estimation = estimation * mask
origin_power = th.pow(origin, 2).sum(
1, keepdim=True) + 1e-8 # (batch, 1)
scale = th.sum(origin*estimation, 1, keepdim=True) / \
origin_power # (batch, 1)
est_true = scale * origin # (batch, nsample)
est_res = estimation - est_true # (batch, nsample)
true_power = th.pow(est_true, 2).sum(1) + 1e-8
res_power = th.pow(est_res, 2).sum(1) + 1e-8
# (batch, 1)
return 10*th.log10(true_power) - 10*th.log10(res_power)
def run(self, train_set, dev_set, num_epoches=20):
start_time = time.time()
init_loss, _ = self.validate(dev_set)
end_time = time.time()
logger.info("Epoch {:2d}: dev = {:.4f}({:.2f}s)".format(0, init_loss,end_time-start_time))
th.save(self.nnet.state_dict(), os.path.join(
self.checkpoint, 'epoch.0.pkl'))
for epoch in range(1, num_epoches + 1):
on_train_start = time.time()
train_loss, train_num_batch = self.pre_train(train_set)
on_valid_start = time.time()
valid_loss, valid_num_batch = self.validate(dev_set)
on_valid_end = time.time()
# scheduler learning rate
self.scheduler.step(valid_loss)
logger.info(
"Loss(time/mini-batch) - Epoch {:2d}: train = {:.4f}({:.2f}s/{:d}) |"
" dev = {:.4f}({:.2f}s/{:d})".format(
epoch, train_loss, on_valid_start - on_train_start,
train_num_batch, valid_loss, on_valid_end - on_valid_start,
valid_num_batch))
for epoch in range(1, 2*num_epoches + 1):
on_train_start = time.time()
train_loss, train_num_batch = self.train(train_set)
on_valid_start = time.time()
valid_loss, valid_num_batch = self.validate(dev_set)
on_valid_end = time.time()
# scheduler learning rate
self.scheduler.step(valid_loss)
logger.info(
"Loss(time/mini-batch) - Epoch {:2d}: train = {:.4f}({:.2f}s/{:d}) |"
" dev = {:.4f}({:.2f}s/{:d})".format(
epoch, train_loss, on_valid_start - on_train_start,
train_num_batch, valid_loss, on_valid_end - on_valid_start,
valid_num_batch))
save_path = os.path.join(self.checkpoint,
'epoch.{:d}.pkl'.format(epoch))
th.save(self.nnet.state_dict(), save_path)
logger.info("Training for {} epoches done!".format(num_epoches))