-
Notifications
You must be signed in to change notification settings - Fork 35
/
utils.py
231 lines (195 loc) · 8.57 KB
/
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
import argparse
import logging
import librosa
import numpy as np
import torch
from config import sample_rate
def clip_gradient(optimizer, grad_clip):
"""
Clips gradients computed during backpropagation to avoid explosion of gradients.
:param optimizer: optimizer with the gradients to be clipped
:param grad_clip: clip value
"""
for group in optimizer.param_groups:
for param in group['params']:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)
def save_checkpoint(epoch, epochs_since_improvement, model, optimizer, loss, is_best):
state = {'epoch': epoch,
'epochs_since_improvement': epochs_since_improvement,
'loss': loss,
'model': model,
'optimizer': optimizer}
filename = 'checkpoint.tar'
torch.save(state, filename)
# If this checkpoint is the best so far, store a copy so it doesn't get overwritten by a worse checkpoint
if is_best:
torch.save(state, 'BEST_checkpoint.tar')
class AverageMeter(object):
"""
Keeps track of most recent, average, sum, and count of a metric.
"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def adjust_learning_rate(optimizer, shrink_factor):
"""
Shrinks learning rate by a specified factor.
:param optimizer: optimizer whose learning rate must be shrunk.
:param shrink_factor: factor in interval (0, 1) to multiply learning rate with.
"""
print("\nDECAYING learning rate.")
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr'] * shrink_factor
print("The new learning rate is %f\n" % (optimizer.param_groups[0]['lr'],))
def accuracy(scores, targets, k=1):
batch_size = targets.size(0)
_, ind = scores.topk(k, 1, True, True)
correct = ind.eq(targets.view(-1, 1).expand_as(ind))
correct_total = correct.view(-1).float().sum() # 0D tensor
return correct_total.item() * (100.0 / batch_size)
def parse_args():
parser = argparse.ArgumentParser(description='Speech Transformer')
# Low Frame Rate (stacking and skipping frames)
parser.add_argument('--LFR_m', default=4, type=int,
help='Low Frame Rate: number of frames to stack')
parser.add_argument('--LFR_n', default=3, type=int,
help='Low Frame Rate: number of frames to skip')
# Network architecture
# encoder
# TODO: automatically infer input dim
parser.add_argument('--d_input', default=80, type=int,
help='Dim of encoder input (before LFR)')
parser.add_argument('--n_layers_enc', default=6, type=int,
help='Number of encoder stacks')
parser.add_argument('--n_head', default=8, type=int,
help='Number of Multi Head Attention (MHA)')
parser.add_argument('--d_k', default=64, type=int,
help='Dimension of key')
parser.add_argument('--d_v', default=64, type=int,
help='Dimension of value')
parser.add_argument('--d_model', default=512, type=int,
help='Dimension of model')
parser.add_argument('--d_inner', default=2048, type=int,
help='Dimension of inner')
parser.add_argument('--dropout', default=0.1, type=float,
help='Dropout rate')
parser.add_argument('--pe_maxlen', default=5000, type=int,
help='Positional Encoding max len')
# decoder
parser.add_argument('--d_word_vec', default=512, type=int,
help='Dim of decoder embedding')
parser.add_argument('--n_layers_dec', default=6, type=int,
help='Number of decoder stacks')
parser.add_argument('--tgt_emb_prj_weight_sharing', default=1, type=int,
help='share decoder embedding with decoder projection')
# Loss
parser.add_argument('--label_smoothing', default=0.1, type=float,
help='label smoothing')
# Training config
parser.add_argument('--epochs', default=150, type=int,
help='Number of maximum epochs')
# minibatch
parser.add_argument('--shuffle', default=1, type=int,
help='reshuffle the data at every epoch')
parser.add_argument('--batch-size', default=32, type=int,
help='Batch size')
parser.add_argument('--batch_frames', default=0, type=int,
help='Batch frames. If this is not 0, batch size will make no sense')
parser.add_argument('--maxlen-in', default=800, type=int, metavar='ML',
help='Batch size is reduced if the input sequence length > ML')
parser.add_argument('--maxlen-out', default=150, type=int, metavar='ML',
help='Batch size is reduced if the output sequence length > ML')
parser.add_argument('--num-workers', default=4, type=int,
help='Number of workers to generate minibatch')
# optimizer
parser.add_argument('--lr', default=0.001, type=float,
help='learning rate')
parser.add_argument('--k', default=0.2, type=float,
help='tunable scalar multiply to learning rate')
parser.add_argument('--warmup_steps', default=4000, type=int,
help='warmup steps')
parser.add_argument('--checkpoint', type=str, default=None, help='checkpoint')
parser.add_argument('--n_samples', default="train:-1,dev:-1,test:-1", type=str,
help='choose the number of examples to use')
args = parser.parse_args()
return args
def get_logger():
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s %(levelname)s \t%(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
def ensure_folder(folder):
import os
if not os.path.isdir(folder):
os.mkdir(folder)
def pad_list(xs, pad_value):
# From: espnet/src/nets/e2e_asr_th.py: pad_list()
n_batch = len(xs)
max_len = max(x.size(0) for x in xs)
pad = xs[0].new(n_batch, max_len, *xs[0].size()[1:]).fill_(pad_value)
for i in range(n_batch):
pad[i, :xs[i].size(0)] = xs[i]
return pad
# [-0.5, 0.5]
def normalize(yt):
yt_max = np.max(yt)
yt_min = np.min(yt)
a = 1.0 / (yt_max - yt_min)
b = -(yt_max + yt_min) / (2 * (yt_max - yt_min))
yt = yt * a + b
return yt
# Acoustic Feature Extraction
# Parameters
# - input file : str, audio file path
# - feature : str, fbank or mfcc
# - dim : int, dimension of feature
# - cmvn : bool, apply CMVN on feature
# - window_size : int, window size for FFT (ms)
# - stride : int, window stride for FFT
# - save_feature: str, if given, store feature to the path and return len(feature)
# Return
# acoustic features with shape (time step, dim)
def extract_feature(input_file, feature='fbank', dim=80, cmvn=True, delta=False, delta_delta=False,
window_size=25, stride=10, save_feature=None):
y, sr = librosa.load(input_file, sr=sample_rate)
yt, _ = librosa.effects.trim(y, top_db=20)
yt = normalize(yt)
ws = int(sr * 0.001 * window_size)
st = int(sr * 0.001 * stride)
if feature == 'fbank': # log-scaled
feat = librosa.feature.melspectrogram(y=yt, sr=sr, n_mels=dim,
n_fft=ws, hop_length=st)
feat = np.log(feat + 1e-6)
elif feature == 'mfcc':
feat = librosa.feature.mfcc(y=yt, sr=sr, n_mfcc=dim, n_mels=26,
n_fft=ws, hop_length=st)
feat[0] = librosa.feature.rmse(yt, hop_length=st, frame_length=ws)
else:
raise ValueError('Unsupported Acoustic Feature: ' + feature)
feat = [feat]
if delta:
feat.append(librosa.feature.delta(feat[0]))
if delta_delta:
feat.append(librosa.feature.delta(feat[0], order=2))
feat = np.concatenate(feat, axis=0)
if cmvn:
feat = (feat - feat.mean(axis=1)[:, np.newaxis]) / (feat.std(axis=1) + 1e-16)[:, np.newaxis]
if save_feature is not None:
tmp = np.swapaxes(feat, 0, 1).astype('float32')
np.save(save_feature, tmp)
return len(tmp)
else:
return np.swapaxes(feat, 0, 1).astype('float32')