forked from oshindow/Transformer-Transducer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
322 lines (255 loc) · 12.4 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
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
319
320
321
322
import os
import shutil
import argparse
import yaml
import time
import torch
import torch.nn as nn
import torch.utils.data
from rnnt.model import Transducer, Pre_encoder
from rnnt.optim import Optimizer
from rnnt.dataset import AudioDataset
from tensorboardX import SummaryWriter
from rnnt.utils import AttrDict, init_logger, count_parameters, save_model, save_pre_model, computer_cer
def pre_train(epoch, config, pre_model, training_data, optimizer, logger, visualizer=None, mems=None):
pre_model.train()
start_epoch = time.process_time()
total_loss = 0
optimizer.epoch()
batch_steps = len(training_data)
for step, (inputs, inputs_length, targets, targets_length) in enumerate(training_data):
if config.training.num_gpu > 0:
inputs, inputs_length = inputs.cuda(), inputs_length.cuda()
targets, targets_length = targets.cuda(), targets_length.cuda()
max_inputs_length = inputs_length.max().item()
max_targets_length = targets_length.max().item()
inputs = inputs[:, :max_inputs_length, :]
targets = targets[:, :max_targets_length]
if config.optim.step_wise_update:
optimizer.step_decay_lr()
optimizer.zero_grad()
start = time.process_time()
loss = pre_model(inputs, inputs_length, targets, targets_length, mems=mems)
if config.training.num_gpu > 1:
loss = torch.mean(loss)
loss.backward()
total_loss += loss.item()
# nn.utils.clip_grad_value_(
# model.parameters(), config.training.max_grad_norm)
# clip_grade_value
clip_value = float(config.training.max_grad_norm)
total_norm = 0
for p in list(filter(lambda p: p.grad is not None, pre_model.parameters())):
p.grad.data.clamp_(min=-clip_value, max=clip_value)
param_norm = p.grad.data.norm(2.)
total_norm += param_norm.item() ** 2.
total_norm = total_norm ** (1./2.)
optimizer.step()
if visualizer is not None:
visualizer.add_scalar(
'train_loss', loss.item(), optimizer.global_step)
visualizer.add_scalar(
'learn_rate', optimizer.lr, optimizer.global_step)
avg_loss = total_loss / (step + 1)
if optimizer.global_step % config.training.show_interval == 0:
end = time.process_time()
process = step / batch_steps * 100
logger.info('-Training-Epoch:%d(%.5f%%), Global Step:%d, Learning Rate:%.6f, Grad Norm:%.5f, Loss:%.5f, '
'AverageLoss: %.5f, Run Time:%.3f' % (epoch, process, optimizer.global_step, optimizer.lr,
total_norm, loss.item(), avg_loss, end-start))
# break
end_epoch = time.process_time()
logger.info('-Training-Epoch:%d, Average Loss: %.5f, Epoch Time: %.3f' %
(epoch, total_loss / (step+1), end_epoch-start_epoch))
def train(epoch, config, model, training_data, optimizer, logger, visualizer=None, mems=None):
model.train()
start_epoch = time.process_time()
total_loss = 0
optimizer.epoch()
batch_steps = len(training_data)
for step, (inputs, inputs_length, targets, targets_length) in enumerate(training_data):
if config.training.num_gpu > 0:
inputs, inputs_length = inputs.cuda(), inputs_length.cuda()
targets, targets_length = targets.cuda(), targets_length.cuda()
max_inputs_length = inputs_length.max().item()
max_targets_length = targets_length.max().item()
inputs = inputs[:, :max_inputs_length, :]
targets = targets[:, :max_targets_length]
if config.optim.step_wise_update:
optimizer.step_decay_lr()
optimizer.zero_grad()
start = time.process_time()
loss = model(inputs, inputs_length, targets, targets_length, mems=mems)
if config.training.num_gpu > 1:
loss = torch.mean(loss)
loss.backward()
total_loss += loss.item()
# nn.utils.clip_grad_value_(
# model.parameters(), config.training.max_grad_norm)
# clip_grade_value
clip_value = float(config.training.max_grad_norm)
total_norm = 0
for p in list(filter(lambda p: p.grad is not None, model.parameters())):
p.grad.data.clamp_(min=-clip_value, max=clip_value)
param_norm = p.grad.data.norm(2.)
total_norm += param_norm.item() ** 2.
total_norm = total_norm ** (1./2.)
optimizer.step()
if visualizer is not None:
visualizer.add_scalar(
'train_loss', loss.item(), optimizer.global_step)
visualizer.add_scalar(
'learn_rate', optimizer.lr, optimizer.global_step)
avg_loss = total_loss / (step + 1)
if optimizer.global_step % config.training.show_interval == 0:
end = time.process_time()
process = step / batch_steps * 100
logger.info('-Training-Epoch:%d(%.5f%%), Global Step:%d, Learning Rate:%.6f, Grad Norm:%.5f, Loss:%.5f, '
'AverageLoss: %.5f, Run Time:%.3f' % (epoch, process, optimizer.global_step, optimizer.lr,
total_norm, loss.item(), avg_loss, end-start))
# break
end_epoch = time.process_time()
logger.info('-Training-Epoch:%d, Average Loss: %.5f, Epoch Time: %.3f' %
(epoch, total_loss / (step+1), end_epoch-start_epoch))
def eval(epoch, config, model, validating_data, logger, visualizer=None):
model.eval()
total_loss = 0
total_dist = 0
total_word = 0
batch_steps = len(validating_data)
for step, (inputs, inputs_length, targets, targets_length) in enumerate(validating_data):
if config.training.num_gpu > 0:
inputs, inputs_length = inputs.cuda(), inputs_length.cuda()
targets, targets_length = targets.cuda(), targets_length.cuda()
max_inputs_length = inputs_length.max().item()
max_targets_length = targets_length.max().item()
inputs = inputs[:, :max_inputs_length, :]
targets = targets[:, :max_targets_length]
preds = model.recognize(inputs, inputs_length)
transcripts = [targets.cpu().numpy()[i][:targets_length[i].item()]
for i in range(targets.size(0))]
dist, num_words = computer_cer(preds, transcripts)
total_dist += dist
total_word += num_words
cer = total_dist / total_word * 100
if step % config.training.show_interval == 0:
process = step / batch_steps * 100
logger.info('-Validation-Epoch:%d(%.5f%%), CER: %.5f %%' % (epoch, process, cer))
val_loss = total_loss/(step+1)
logger.info('-Validation-Epoch:%4d, AverageLoss:%.5f , AverageCER: %.5f %%' %
(epoch, val_loss, cer))
if visualizer is not None:
visualizer.add_scalar('cer', cer, epoch)
return cer
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-config', type=str, default='config/thchs30.yaml')
parser.add_argument('-log', type=str, default='train.log')
parser.add_argument('-mode', type=str, default='retrain')
opt = parser.parse_args()
configfile = open(opt.config)
config = AttrDict(yaml.load(configfile, Loader=yaml.FullLoader))
exp_name = os.path.join('egs', config.data.name, 'exp', config.training.save_model)
if not os.path.isdir(exp_name):
os.makedirs(exp_name)
logger = init_logger(os.path.join(exp_name, opt.log))
shutil.copyfile(opt.config, os.path.join(exp_name, 'config.yaml'))
logger.info('Save config info.')
num_workers = config.training.num_gpu * 2
train_dataset = AudioDataset(config.data, 'train')
training_data = torch.utils.data.DataLoader(
train_dataset, batch_size=config.data.batch_size * config.training.num_gpu,
shuffle=config.data.shuffle, num_workers=num_workers)
logger.info('Load Train Set!')
dev_dataset = AudioDataset(config.data, 'dev')
validate_data = torch.utils.data.DataLoader(
dev_dataset, batch_size=config.data.batch_size * config.training.num_gpu,
shuffle=False, num_workers=num_workers)
logger.info('Load Dev Set!')
if config.training.num_gpu > 0:
torch.cuda.manual_seed(config.training.seed)
torch.backends.cudnn.deterministic = True
else:
torch.manual_seed(config.training.seed)
logger.info('Set random seed: %d' % config.training.seed)
if config.training.pre_train:
pre_model =Pre_encoder(config.model)
else:
model = Transducer(config.model)
if config.training.load_model:
checkpoint = torch.load(config.training.load_model)
model.encoder.load_state_dict(checkpoint['encoder'])
model.decoder.load_state_dict(checkpoint['decoder'])
model.joint.load_state_dict(checkpoint['joint'])
logger.info('Loaded model from %s' % config.training.load_model)
elif config.training.load_encoder or config.training.load_decoder:
if config.training.load_encoder:
checkpoint = torch.load(config.training.load_encoder)
model.encoder.load_state_dict(checkpoint['encoder'])
logger.info('Loaded encoder from %s' %
config.training.load_encoder)
if config.training.load_decoder:
checkpoint = torch.load(config.training.load_decoder)
model.decoder.load_state_dict(checkpoint['decoder'])
logger.info('Loaded decoder from %s' %
config.training.load_decoder)
if config.training.num_gpu > 0:
if config.training.pre_train:
pre_model = pre_model.cuda()
else:
model = model.cuda()
if config.training.num_gpu > 1:
device_ids = list(range(config.training.num_gpu))
model = torch.nn.DataParallel(model, device_ids=device_ids)
logger.info('Loaded the model to %d GPUs' % config.training.num_gpu)
if config.training.pre_train:
n_params, _, _ = count_parameters(pre_model)
logger.info('# the number of parameters in the pre model: %d' % n_params)
optimizer = Optimizer(pre_model.parameters(), config.optim)
else:
n_params, enc, dec = count_parameters(model)
logger.info('# the number of parameters in the whole model: %d' % n_params)
logger.info('# the number of parameters in the Encoder: %d' % enc)
logger.info('# the number of parameters in the Decoder: %d' % dec)
logger.info('# the number of parameters in the JointNet: %d' %
(n_params - dec - enc))
optimizer = Optimizer(model.parameters(), config.optim)
logger.info('Created a %s optimizer.' % config.optim.type)
if opt.mode == 'continue':
optimizer.load_state_dict(checkpoint['optimizer'])
start_epoch = checkpoint['epoch']
logger.info('Load Optimizer State!')
else:
start_epoch = 0
# create a visualizer
if config.training.visualization:
visualizer = SummaryWriter(os.path.join(exp_name, 'log'))
logger.info('Created a visualizer.')
else:
visualizer = None
for epoch in range(start_epoch, config.training.epochs):
if config.training.pre_train:
pre_train(epoch, config, pre_model, training_data,
optimizer, logger, visualizer)
save_name = os.path.join(exp_name, '%s.epoch%d.ckpt' % (config.training.save_pre_model, epoch))
save_pre_model(pre_model, optimizer, config, save_name)
logger.info('Epoch %d pre_model has been saved.' % epoch)
else:
train(epoch, config, model, training_data,
optimizer, logger, visualizer)
save_name = os.path.join(exp_name, '%s.epoch%d.ckpt' % (config.training.save_model, epoch))
save_model(model, optimizer, config, save_name)
logger.info('Epoch %d model has been saved.' % epoch)
if config.training.eval_or_not:
_ = eval(epoch, config, model, validate_data, logger, visualizer)
if epoch >= config.optim.begin_to_adjust_lr:
optimizer.decay_lr()
# early stop
if optimizer.lr < 1e-6:
logger.info('The learning rate is too low to train.')
break
logger.info('Epoch %d update learning rate: %.6f' %
(epoch, optimizer.lr))
logger.info('The training process is OVER!')
if __name__ == '__main__':
main()