mirror of
https://github.com/wassname/TTS.git
synced 2026-09-12 12:11:47 +08:00
Beginning
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+110
@@ -0,0 +1,110 @@
|
||||
import librosa
|
||||
import numpy as np
|
||||
from scipy import signal
|
||||
import Tacotron.train_config as c
|
||||
|
||||
|
||||
_mel_basis = None
|
||||
|
||||
|
||||
def save_wav(wav, path):
|
||||
wav *= 32767 / max(0.01, np.max(np.abs(wav)))
|
||||
librosa.output.write_wav(path, wav.astype(np.int16), c.sample_rate)
|
||||
|
||||
|
||||
def _linear_to_mel(spectrogram):
|
||||
global _mel_basis
|
||||
if _mel_basis is None:
|
||||
_mel_basis = _build_mel_basis()
|
||||
return np.dot(_mel_basis, spectrogram)
|
||||
|
||||
|
||||
def _build_mel_basis():
|
||||
n_fft = (c.num_freq - 1) * 2
|
||||
return librosa.filters.mel(c.sample_rate, n_fft, n_mels=c.num_mels)
|
||||
|
||||
|
||||
def _normalize(S):
|
||||
return np.clip((S - c.min_level_db) / -c.min_level_db, 0, 1)
|
||||
|
||||
|
||||
def _denormalize(S):
|
||||
return (np.clip(S, 0, 1) * -c.min_level_db) + c.min_level_db
|
||||
|
||||
|
||||
def _stft_parameters():
|
||||
n_fft = (c.num_freq - 1) * 2
|
||||
hop_length = int(c.frame_shift_ms / 1000 * c.sample_rate)
|
||||
win_length = int(c.frame_length_ms / 1000 * c.sample_rate)
|
||||
return n_fft, hop_length, win_length
|
||||
|
||||
|
||||
def _amp_to_db(x):
|
||||
return 20 * np.log10(np.maximum(1e-5, x))
|
||||
|
||||
|
||||
def _db_to_amp(x):
|
||||
return np.power(10.0, x * 0.05)
|
||||
|
||||
|
||||
def preemphasis(x):
|
||||
return signal.lfilter([1, -c.preemphasis], [1], x)
|
||||
|
||||
|
||||
def inv_preemphasis(x):
|
||||
return signal.lfilter([1], [1, -c.preemphasis], x)
|
||||
|
||||
|
||||
def spectrogram(y):
|
||||
D = _stft(preemphasis(y))
|
||||
S = _amp_to_db(np.abs(D)) - c.ref_level_db
|
||||
return _normalize(S)
|
||||
|
||||
|
||||
def inv_spectrogram(spectrogram):
|
||||
'''Converts spectrogram to waveform using librosa'''
|
||||
|
||||
S = _denormalize(spectrogram)
|
||||
S = _db_to_amp(S + c.ref_level_db) # Convert back to linear
|
||||
|
||||
# Reconstruct phase
|
||||
return inv_preemphasis(_griffin_lim(S ** c.power))
|
||||
|
||||
|
||||
def _griffin_lim(S):
|
||||
'''librosa implementation of Griffin-Lim
|
||||
Based on https://github.com/librosa/librosa/issues/434
|
||||
'''
|
||||
angles = np.exp(2j * np.pi * np.random.rand(*S.shape))
|
||||
S_complex = np.abs(S).astype(np.complex)
|
||||
y = _istft(S_complex * angles)
|
||||
for i in range(c.griffin_lim_iters):
|
||||
angles = np.exp(1j * np.angle(_stft(y)))
|
||||
y = _istft(S_complex * angles)
|
||||
return y
|
||||
|
||||
|
||||
def _istft(y):
|
||||
_, hop_length, win_length = _stft_parameters()
|
||||
return librosa.istft(y, hop_length=hop_length, win_length=win_length)
|
||||
|
||||
|
||||
def melspectrogram(y):
|
||||
D = _stft(preemphasis(y))
|
||||
S = _amp_to_db(_linear_to_mel(np.abs(D)))
|
||||
return _normalize(S)
|
||||
|
||||
|
||||
def _stft(y):
|
||||
n_fft, hop_length, win_length = _stft_parameters()
|
||||
return librosa.stft(y=y, n_fft=n_fft, hop_length=hop_length, win_length=win_length)
|
||||
|
||||
|
||||
def find_endpoint(wav, threshold_db=-40, min_silence_sec=0.8):
|
||||
window_length = int(c.sample_rate * min_silence_sec)
|
||||
hop_length = int(window_length / 4)
|
||||
threshold = _db_to_amp(threshold_db)
|
||||
for x in range(hop_length, len(wav) - window_length, hop_length):
|
||||
if np.max(wav[x:x + window_length]) < threshold:
|
||||
return x + hop_length
|
||||
return len(wav)
|
||||
@@ -0,0 +1,18 @@
|
||||
import numpy as np
|
||||
|
||||
|
||||
def pad_data(x, length):
|
||||
_pad = 0
|
||||
return np.pad(x, (0, length - x.shape[0]), mode='constant', constant_values=_pad)
|
||||
|
||||
|
||||
def prepare_data(inputs):
|
||||
max_len = max((len(x) for x in inputs))
|
||||
return np.stack([pad_data(x, max_len) for x in inputs])
|
||||
|
||||
|
||||
def pad_per_step(inputs, outputs_per_step):
|
||||
timesteps = inputs.shape[-1]
|
||||
return np.pad(inputs, [[0, 0], [0, 0],
|
||||
[0, outputs_per_step - (timesteps % outputs_per_step)]],
|
||||
mode='constant', constant_values=0.0)
|
||||
@@ -0,0 +1,178 @@
|
||||
import os
|
||||
import sys
|
||||
import glob
|
||||
import time
|
||||
import shutil
|
||||
import datetime
|
||||
import numpy as np
|
||||
|
||||
|
||||
def create_experiment_folder(root_path):
|
||||
""" Create a folder with the current date and time """
|
||||
date_str = datetime.datetime.now().strftime("%B-%d-%Y_%I:%M%p")
|
||||
output_folder = os.path.join(root_path, date_str)
|
||||
os.makedirs(output_folder, exist_ok=True)
|
||||
print(" > Experiment folder: {}".format(output_folder))
|
||||
return output_folder
|
||||
|
||||
|
||||
def remove_experiment_folder(experiment_path):
|
||||
"""Check folder if there is a checkpoint, otherwise remove the folder"""
|
||||
|
||||
checkpoint_files = glob.glob(experiment_path+"/*.pth.tar")
|
||||
if len(checkpoint_files) == 0:
|
||||
shutil.rmtree(experiment_path)
|
||||
print(" ! Run is removed from {}".format(experiment_path))
|
||||
else:
|
||||
print(" ! Run is kept in {}".format(experiment_path))
|
||||
|
||||
|
||||
def copy_config_file(config_file, path):
|
||||
config_name = os.path.basename(config_file)
|
||||
out_path = os.path.join(path, config_name)
|
||||
shutil.copyfile(config_file, out_path)
|
||||
|
||||
|
||||
def save_checkpoint(state, filename='checkpoint.pth.tar'):
|
||||
torch.save(state, filename)
|
||||
|
||||
|
||||
class Progbar(object):
|
||||
"""Displays a progress bar.
|
||||
# Arguments
|
||||
target: Total number of steps expected, None if unknown.
|
||||
interval: Minimum visual progress update interval (in seconds).
|
||||
"""
|
||||
|
||||
def __init__(self, target, width=30, verbose=1, interval=0.05):
|
||||
self.width = width
|
||||
self.target = target
|
||||
self.sum_values = {}
|
||||
self.unique_values = []
|
||||
self.start = time.time()
|
||||
self.last_update = 0
|
||||
self.interval = interval
|
||||
self.total_width = 0
|
||||
self.seen_so_far = 0
|
||||
self.verbose = verbose
|
||||
self._dynamic_display = ((hasattr(sys.stdout, 'isatty') and
|
||||
sys.stdout.isatty()) or
|
||||
'ipykernel' in sys.modules)
|
||||
|
||||
def update(self, current, values=None, force=False):
|
||||
"""Updates the progress bar.
|
||||
# Arguments
|
||||
current: Index of current step.
|
||||
values: List of tuples (name, value_for_last_step).
|
||||
The progress bar will display averages for these values.
|
||||
force: Whether to force visual progress update.
|
||||
"""
|
||||
values = values or []
|
||||
for k, v in values:
|
||||
if k not in self.sum_values:
|
||||
self.sum_values[k] = [v * (current - self.seen_so_far),
|
||||
current - self.seen_so_far]
|
||||
self.unique_values.append(k)
|
||||
else:
|
||||
self.sum_values[k][0] += v * (current - self.seen_so_far)
|
||||
self.sum_values[k][1] += (current - self.seen_so_far)
|
||||
self.seen_so_far = current
|
||||
|
||||
now = time.time()
|
||||
info = ' - %.0fs' % (now - self.start)
|
||||
if self.verbose == 1:
|
||||
if (not force and (now - self.last_update) < self.interval and
|
||||
self.target is not None and current < self.target):
|
||||
return
|
||||
|
||||
prev_total_width = self.total_width
|
||||
if self._dynamic_display:
|
||||
sys.stdout.write('\b' * prev_total_width)
|
||||
sys.stdout.write('\r')
|
||||
else:
|
||||
sys.stdout.write('\n')
|
||||
|
||||
if self.target is not None:
|
||||
numdigits = int(np.floor(np.log10(self.target))) + 1
|
||||
barstr = '%%%dd/%d [' % (numdigits, self.target)
|
||||
bar = barstr % current
|
||||
prog = float(current) / self.target
|
||||
prog_width = int(self.width * prog)
|
||||
if prog_width > 0:
|
||||
bar += ('=' * (prog_width - 1))
|
||||
if current < self.target:
|
||||
bar += '>'
|
||||
else:
|
||||
bar += '='
|
||||
bar += ('.' * (self.width - prog_width))
|
||||
bar += ']'
|
||||
else:
|
||||
bar = '%7d/Unknown' % current
|
||||
|
||||
self.total_width = len(bar)
|
||||
sys.stdout.write(bar)
|
||||
|
||||
if current:
|
||||
time_per_unit = (now - self.start) / current
|
||||
else:
|
||||
time_per_unit = 0
|
||||
if self.target is not None and current < self.target:
|
||||
eta = time_per_unit * (self.target - current)
|
||||
if eta > 3600:
|
||||
eta_format = '%d:%02d:%02d' % (
|
||||
eta // 3600, (eta % 3600) // 60, eta % 60)
|
||||
elif eta > 60:
|
||||
eta_format = '%d:%02d' % (eta // 60, eta % 60)
|
||||
else:
|
||||
eta_format = '%ds' % eta
|
||||
|
||||
info = ' - ETA: %s' % eta_format
|
||||
else:
|
||||
if time_per_unit >= 1:
|
||||
info += ' %.0fs/step' % time_per_unit
|
||||
elif time_per_unit >= 1e-3:
|
||||
info += ' %.0fms/step' % (time_per_unit * 1e3)
|
||||
else:
|
||||
info += ' %.0fus/step' % (time_per_unit * 1e6)
|
||||
|
||||
for k in self.unique_values:
|
||||
info += ' - %s:' % k
|
||||
if isinstance(self.sum_values[k], list):
|
||||
avg = np.mean(
|
||||
self.sum_values[k][0] / max(1, self.sum_values[k][1]))
|
||||
if abs(avg) > 1e-3:
|
||||
info += ' %.4f' % avg
|
||||
else:
|
||||
info += ' %.4e' % avg
|
||||
else:
|
||||
info += ' %s' % self.sum_values[k]
|
||||
|
||||
self.total_width += len(info)
|
||||
if prev_total_width > self.total_width:
|
||||
info += (' ' * (prev_total_width - self.total_width))
|
||||
|
||||
if self.target is not None and current >= self.target:
|
||||
info += '\n'
|
||||
|
||||
sys.stdout.write(info)
|
||||
sys.stdout.flush()
|
||||
|
||||
elif self.verbose == 2:
|
||||
if self.target is None or current >= self.target:
|
||||
for k in self.unique_values:
|
||||
info += ' - %s:' % k
|
||||
avg = np.mean(
|
||||
self.sum_values[k][0] / max(1, self.sum_values[k][1]))
|
||||
if avg > 1e-3:
|
||||
info += ' %.4f' % avg
|
||||
else:
|
||||
info += ' %.4e' % avg
|
||||
info += '\n'
|
||||
|
||||
sys.stdout.write(info)
|
||||
sys.stdout.flush()
|
||||
|
||||
self.last_update = now
|
||||
|
||||
def add(self, n, values=None):
|
||||
self.update(self.seen_so_far + n, values)
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
def get_param_size(model):
|
||||
params = 0
|
||||
for p in model.parameters():
|
||||
tmp = 1
|
||||
for x in p.size():
|
||||
tmp *= x
|
||||
params += tmp
|
||||
return params
|
||||
Reference in New Issue
Block a user