-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathutils.py
260 lines (204 loc) · 8.07 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
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
import argparse
import logging
import cv2 as cv
import librosa
import matplotlib.pylab as plt
import numpy as np
import pinyin
import torch
# from scipy.io.wavfile import read
from config import sampling_rate, VOCAB, IVOCAB
# from text.cleaners import chinese_cleaners
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='Tacotron2')
parser.add_argument('--epochs', default=10000, type=int)
parser.add_argument('--max_norm', default=1, type=float, help='Gradient norm threshold to clip')
# minibatch
parser.add_argument('--batch_size', default=64, type=int)
parser.add_argument('--num-workers', default=4, type=int, help='Number of workers to generate minibatch')
# logging
parser.add_argument('--print_freq', default=10, type=int, help='Frequency of printing training information')
# optimizer
parser.add_argument('--lr', default=1e-3, type=float, help='Init learning rate')
parser.add_argument('--l2', default=1e-6, type=float, help='weight decay (L2)')
parser.add_argument('--checkpoint', type=str, default=None, help='checkpoint')
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.INFO)
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
def get_mask_from_lengths(lengths):
max_len = torch.max(lengths).item()
ids = torch.arange(0, max_len, out=torch.cuda.LongTensor(max_len))
mask = (ids < lengths.unsqueeze(1)).bool()
return mask
def load_wav_to_torch(full_path):
# sampling_rate, data = read(full_path)
y, sr = librosa.core.load(full_path, sampling_rate)
yt, _ = librosa.effects.trim(y)
return torch.FloatTensor(yt.astype(np.float32)), sr
def load_filepaths_and_text(filename, split="|"):
with open(filename, encoding='utf-8') as f:
filepaths_and_text = [line.strip().split(split) for line in f]
return filepaths_and_text
def to_gpu(x):
x = x.contiguous()
if torch.cuda.is_available():
x = x.cuda(non_blocking=True)
return torch.autograd.Variable(x)
def text_to_sequence(text):
# text = chinese_cleaners(text)
result = [VOCAB[ch] for ch in text]
return result
def sequence_to_text(seq):
result = [IVOCAB[str(idx)] for idx in seq]
return result
def plot_data(data, figsize=(16, 4)):
fig, axes = plt.subplots(1, len(data), figsize=figsize)
for i in range(len(data)):
axes[i].imshow(data[i], aspect='auto', origin='lower',
interpolation='none')
def test(model, step_num, loss):
model.eval()
text = "相对论直接和间接的催生了量子力学的诞生 也为研究微观世界的高速运动确立了全新的数学模型"
text = pinyin.get(text, format="numerical", delimiter=" ")
sequence = np.array(text_to_sequence(text))[None, :]
sequence = torch.autograd.Variable(torch.from_numpy(sequence)).cuda().long()
with torch.no_grad():
mel_outputs, mel_outputs_postnet, _, alignments = model.inference(sequence)
plot_data((mel_outputs.float().data.cpu().numpy()[0],
mel_outputs_postnet.float().data.cpu().numpy()[0],
alignments.float().data.cpu().numpy()[0].T))
title = 'step={0}, loss={1:.5f}'.format(step_num, loss)
plt.title(title)
filename = 'images/temp.jpg'
plt.savefig(filename)
img = cv.imread(filename)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img = img / 255.
return img
class HParams:
def __init__(self):
self.n_mel_channels = None
self.dynamic_loss_scaling = True
self.fp16_run = False
self.distributed_run = False
################################
# Data Parameters #
################################
self.load_mel_from_disk = False
################################
# Audio Parameters #
################################
self.max_wav_value = 32768.0
self.sampling_rate = 22050
self.filter_length = 1024
self.hop_length = 256
self.win_length = 1024
self.n_mel_channels = 80
self.mel_fmin = 0.0
self.mel_fmax = 8000.0
################################
# Model Parameters #
################################
self.n_symbols = 35
self.symbols_embedding_dim = 512
# Encoder parameters
self.encoder_kernel_size = 5
self.encoder_n_convolutions = 3
self.encoder_embedding_dim = 512
# Decoder parameters
self.n_frames_per_step = 1 # currently only 1 is supported
self.decoder_rnn_dim = 1024
self.prenet_dim = 256
self.max_decoder_steps = 1000
self.gate_threshold = 0.5
self.p_attention_dropout = 0.1
self.p_decoder_dropout = 0.1
# Attention parameters
self.attention_rnn_dim = 1024
self.attention_dim = 128
# Location Layer parameters
self.attention_location_n_filters = 32
self.attention_location_kernel_size = 31
# Mel-post processing network parameters
self.postnet_embedding_dim = 512
self.postnet_kernel_size = 5
self.postnet_n_convolutions = 5
################################
# Optimization Hyperparameters #
################################
self.learning_rate = 1e-3
self.weight_decay = 1e-6
self.batch_size = 64
self.mask_padding = True # set model's padded outputs to padded values