diff --git a/.gitattributes b/.gitattributes index c7d9f3332a950355d5a77d85000f05e6f45435ea..5be91f972c7b8865ddced78b6e12ea3453ba2daf 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,34 +1 @@ -*.7z filter=lfs diff=lfs merge=lfs -text -*.arrow filter=lfs diff=lfs merge=lfs -text -*.bin filter=lfs diff=lfs merge=lfs -text -*.bz2 filter=lfs diff=lfs merge=lfs -text -*.ckpt filter=lfs diff=lfs merge=lfs -text -*.ftz filter=lfs diff=lfs merge=lfs -text -*.gz filter=lfs diff=lfs merge=lfs -text -*.h5 filter=lfs diff=lfs merge=lfs -text -*.joblib filter=lfs diff=lfs merge=lfs -text -*.lfs.* filter=lfs diff=lfs merge=lfs -text -*.mlmodel filter=lfs diff=lfs merge=lfs -text -*.model filter=lfs diff=lfs merge=lfs -text -*.msgpack filter=lfs diff=lfs merge=lfs -text -*.npy filter=lfs diff=lfs merge=lfs -text -*.npz filter=lfs diff=lfs merge=lfs -text -*.onnx filter=lfs diff=lfs merge=lfs -text -*.ot filter=lfs diff=lfs merge=lfs -text -*.parquet filter=lfs diff=lfs merge=lfs -text -*.pb filter=lfs diff=lfs merge=lfs -text -*.pickle filter=lfs diff=lfs merge=lfs -text -*.pkl filter=lfs diff=lfs merge=lfs -text -*.pt filter=lfs diff=lfs merge=lfs -text -*.pth filter=lfs diff=lfs merge=lfs -text -*.rar filter=lfs diff=lfs merge=lfs -text -*.safetensors filter=lfs diff=lfs merge=lfs -text -saved_model/**/* filter=lfs diff=lfs merge=lfs -text -*.tar.* filter=lfs diff=lfs merge=lfs -text -*.tflite filter=lfs diff=lfs merge=lfs -text -*.tgz filter=lfs diff=lfs merge=lfs -text -*.wasm filter=lfs diff=lfs merge=lfs -text -*.xz filter=lfs diff=lfs merge=lfs -text -*.zip filter=lfs diff=lfs merge=lfs -text -*.zst filter=lfs diff=lfs merge=lfs -text -*tfevents* filter=lfs diff=lfs merge=lfs -text +*.ipynb linguist-vendored diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..9401d2ebba5b5f79cd139c11374330a0dea4e144 --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +*.pyc +*.aux +*.log +*.out +*.synctex.gz +*.suo +*__pycache__ +*.idea +*.ipynb_checkpoints +*.pickle +*.npy +*.blg +*.bbl +*.bcf +*.toc +*.wav +*.sh +encoder/saved_models/* +synthesizer/saved_models/* +vocoder/saved_models/* diff --git a/1320_00000.mp3 b/1320_00000.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..f0791b04239a75e1eed175403d78fa503772bccf Binary files /dev/null and b/1320_00000.mp3 differ diff --git a/3575_00000.mp3 b/3575_00000.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..545d784f88a0edefee354bba7caff2fadf617261 Binary files /dev/null and b/3575_00000.mp3 differ diff --git a/6829_00000.mp3 b/6829_00000.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..34f0382f198cf408c4d6f19676523b780e295e8c Binary files /dev/null and b/6829_00000.mp3 differ diff --git a/8230_00000.mp3 b/8230_00000.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..b7c5620095db916a11b06864b3bcfc3df3611f21 Binary files /dev/null and b/8230_00000.mp3 differ diff --git a/Kor_preprocess.py b/Kor_preprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..014003ca69d1ebc665be54ae308cc04efbe0c7c2 --- /dev/null +++ b/Kor_preprocess.py @@ -0,0 +1,178 @@ +from multiprocessing.pool import Pool +from synthesizer import audio +from functools import partial +from itertools import chain +from encoder import inference as encoder +from pathlib import Path +from utils import logmmse +from tqdm import tqdm +import numpy as np +import librosa +import os +## + + +def preprocess_KSponSpeech(datasets_root: Path, out_dir: Path, n_processes: int, + skip_existing: bool, hparams): + # Gather the input directories + dataset_root = datasets_root.joinpath("KSponSpeech") + input_dirs = [dataset_root.joinpath("KsponSpeech_01"), + dataset_root.joinpath("KsponSpeech_02"), + dataset_root.joinpath("KsponSpeech_03"), + dataset_root.joinpath("KsponSpeech_04"), + dataset_root.joinpath("KsponSpeech_05")] + + print("\n ".join(map(str, ["Using data from:"] + input_dirs))) + assert all(input_dir.exists() for input_dir in input_dirs) + + # Create the output directories for each output file type + out_dir.joinpath("mels").mkdir(exist_ok=True) + out_dir.joinpath("audio").mkdir(exist_ok=True) + + # Create a metadata file + metadata_fpath = out_dir.joinpath("train.txt") + metadata_file = metadata_fpath.open("a" if skip_existing else "w", encoding="cp949") + + # Preprocess the dataset + speaker_dirs = list(chain.from_iterable(input_dir.glob("*") for input_dir in input_dirs)) # 폴더안의 모든 폴더(Speaker) + func = partial(preprocess_speaker, out_dir=out_dir, skip_existing=skip_existing, + hparams=hparams) + job = Pool(n_processes).imap(func, speaker_dirs) + for speaker_metadata in tqdm(job, "KSponSpeech", len(speaker_dirs), unit="speakers"): + for metadatum in speaker_metadata: + metadata_file.write("|".join(str(x) for x in metadatum) + "\n") + metadata_file.close() + + # Verify the contents of the metadata file + with metadata_fpath.open("r", encoding="cp949") as metadata_file: + metadata = [line.split("|") for line in metadata_file] + mel_frames = sum([int(m[4]) for m in metadata]) + timesteps = sum([int(m[3]) for m in metadata]) + sample_rate = hparams.sample_rate + hours = (timesteps / sample_rate) / 3600 + print("The dataset consists of %d utterances, %d mel frames, %d audio timesteps (%.2f hours)." % + (len(metadata), mel_frames, timesteps, hours)) + print("Max input length (text chars): %d" % max(len(m[5]) for m in metadata)) + print("Max mel frames length: %d" % max(int(m[4]) for m in metadata)) + print("Max audio timesteps length: %d" % max(int(m[3]) for m in metadata)) + + +def preprocess_speaker(speaker_dir, out_dir: Path, skip_existing: bool, hparams): + metadata = [] + check_list = [",01", ",02", ",03", ",04", ",05", ",06", ",07", ",08", ",09"] + # Gather the utterance audios and texts + # try: + files = os.listdir(speaker_dir) + + for file in files: + if file.endswith("alignment.txt"): + with open(os.path.join(speaker_dir, file), "r", encoding='cp949') as alignments_file: + alignments = [line.rstrip().split(" ") for line in alignments_file.readlines()] + # # except StopIteration: + # # # A few alignment files will be missing + # # continue + + # Iterate over each entry in the alignments file + for wav_fname, words in alignments: + + for check in check_list: + if check in words: + print(words) + words = "pass" + + wav_fpath = speaker_dir.joinpath(wav_fname + ".pcm") + assert wav_fpath.exists() + # words = words.replace("\"", "").split(",") + # end_times = list(map(float, end_times.replace("\"", "").split(","))) + # + # # Process each sub-utterance + wavs = normalization(wav_fpath, hparams) + + if wavs is not None and words is not "pass": + sub_basename = "%s" % (wav_fname) + metadata.append(process_utterance(wavs, words, out_dir, sub_basename, + skip_existing, hparams)) + + return [m for m in metadata if m is not None] + + +def normalization(wav_fpath, hparams): + try: + wav = np.memmap(wav_fpath, dtype='h', mode='r') + if hparams.rescale: + wav = wav / np.abs(wav).max() * hparams.rescaling_max + except EOFError: + print(wav_fpath) + return None + return wav + + +def process_utterance(wav: np.ndarray, text: str, out_dir: Path, basename: str, + skip_existing: bool, hparams): + ## FOR REFERENCE: + # For you not to lose your head if you ever wish to change things here or implement your own + # synthesizer. + # - Both the audios and the mel spectrograms are saved as numpy arrays + # - There is no processing done to the audios that will be saved to disk beyond volume + # normalization (in split_on_silences) + # - However, pre-emphasis is applied to the audios before computing the mel spectrogram. This + # is why we re-apply it on the audio on the side of the vocoder. + # - Librosa pads the waveform before computing the mel spectrogram. Here, the waveform is saved + # without extra padding. This means that you won't have an exact relation between the length + # of the wav and of the mel spectrogram. See the vocoder data loader. + + # Skip existing utterances if needed + mel_fpath = out_dir.joinpath("mels", "mel-%s.npy" % basename) + wav_fpath = out_dir.joinpath("audio", "audio-%s.npy" % basename) + if skip_existing and mel_fpath.exists() and wav_fpath.exists(): + return None + + # Skip utterances that are too short + if len(wav) < hparams.utterance_min_duration * hparams.sample_rate: + return None + + # Compute the mel spectrogram + mel_spectrogram = audio.melspectrogram(wav, hparams).astype(np.float32) + mel_frames = mel_spectrogram.shape[1] + + # Skip utterances that are too long + if mel_frames > hparams.max_mel_frames and hparams.clip_mels_length: + return None + + # Write the spectrogram, embed and audio to disk + np.save(mel_fpath, mel_spectrogram.T, allow_pickle=False) + np.save(wav_fpath, wav, allow_pickle=False) + + # Return a tuple describing this training example + return wav_fpath.name, mel_fpath.name, "embed-%s.npy" % basename, len(wav), mel_frames, text + + +def embed_utterance(fpaths, encoder_model_fpath): + if not encoder.is_loaded(): + encoder.load_model(encoder_model_fpath) + + # Compute the speaker embedding of the utterance + wav_fpath, embed_fpath = fpaths + wav = np.load(wav_fpath) + wav = encoder.preprocess_wav(wav) + embed = encoder.embed_utterance(wav) + np.save(embed_fpath, embed, allow_pickle=False) + + +def create_embeddings(synthesizer_root: Path, encoder_model_fpath: Path, n_processes: int): + wav_dir = synthesizer_root.joinpath("audio") + metadata_fpath = synthesizer_root.joinpath("train.txt") + assert wav_dir.exists() and metadata_fpath.exists() + embed_dir = synthesizer_root.joinpath("embeds") + embed_dir.mkdir(exist_ok=True) + + # Gather the input wave filepath and the target output embed filepath + with metadata_fpath.open("r") as metadata_file: + metadata = [line.split("|") for line in metadata_file] + fpaths = [(wav_dir.joinpath(m[0]), embed_dir.joinpath(m[2])) for m in metadata] + + # TODO: improve on the multiprocessing, it's terrible. Disk I/O is the bottleneck here. + # Embed the utterances in separate threads + func = partial(embed_utterance, encoder_model_fpath=encoder_model_fpath) + job = Pool(n_processes).imap(func, fpaths) + list(tqdm(job, "Embedding", len(fpaths), unit="utterances")) diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000000000000000000000000000000000000..5ed721bf8f29f5c8d947c2d333cc371021135fb0 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,24 @@ +MIT License + +Modified & original work Copyright (c) 2019 Corentin Jemine (https://github.com/CorentinJ) +Original work Copyright (c) 2018 Rayhane Mama (https://github.com/Rayhane-mamah) +Original work Copyright (c) 2019 fatchord (https://github.com/fatchord) +Original work Copyright (c) 2015 braindead (https://github.com/braindead) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index eb47b051775426b4679ceceed4f2b1ffffe05db0..bdeecff95f1ec281b79b38de610199ccee7b8975 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,20 @@ ---- -title: README -emoji: 🏢 -colorFrom: yellow -colorTo: indigo -sdk: static -pinned: false ---- - -Edit this `README.md` markdown file to author your organization card 🔥 +** Temporarily suspended + + + +# Real-Time Korean Voice Cloning +This repository is Korean version of sv2tts. The original model (which was developed by CorentinJ(https://github.com/CorentinJ/Real-Time-Voice-Cloning)) is based on English. +To implement Korean speech on the model, I refer to tail95(https://github.com/tail95/Voice-Cloning). +I changed some codes to improve convenience in preprocessing(audio and text) and training. Also I converted tensorflow model to pytorch model and fixed some errors. + +## References +- https://github.com/CorentinJ/Real-Time-Voice-Cloning +- https://github.com/tail95/Voice-Cloning +- https://medium.com/analytics-vidhya/the-intuition-behind-voice-cloning-with-5-seconds-of-audio-5989e9b2e042 +- Transfer Learning from Speaker Verification to Multispeaker Text-To-Speech Synthesis (https://arxiv.org/abs/1806.04558) + + +## Used Dataset +- KSponspeech (https://aihub.or.kr/aidata/105) + +Make sure that your datasets has text-audio pairs. diff --git a/VCTK.txt b/VCTK.txt new file mode 100644 index 0000000000000000000000000000000000000000..b51455ac374d1e8074327b7d2a8e9381e06d5c64 --- /dev/null +++ b/VCTK.txt @@ -0,0 +1,94 @@ +--------------------------------------------------------------------- + CSTR VCTK Corpus + English Multi-speaker Corpus for CSTR Voice Cloning Toolkit + + (Version 0.92) + RELEASE September 2019 + The Centre for Speech Technology Research + University of Edinburgh + Copyright (c) 2019 + + Junichi Yamagishi + jyamagis@inf.ed.ac.uk +--------------------------------------------------------------------- + +Overview + +This CSTR VCTK Corpus includes speech data uttered by 110 English +speakers with various accents. Each speaker reads out about 400 +sentences, which were selected from a newspaper, the rainbow passage +and an elicitation paragraph used for the speech accent archive. + +The newspaper texts were taken from Herald Glasgow, with permission +from Herald & Times Group. Each speaker has a different set of the +newspaper texts selected based a greedy algorithm that increases the +contextual and phonetic coverage. The details of the text selection +algorithms are described in the following paper: + +C. Veaux, J. Yamagishi and S. King, +"The voice bank corpus: Design, collection and data analysis of +a large regional accent speech database," +https://doi.org/10.1109/ICSDA.2013.6709856 + +The rainbow passage and elicitation paragraph are the same for all +speakers. The rainbow passage can be found at International Dialects +of English Archive: +(http://web.ku.edu/~idea/readings/rainbow.htm). The elicitation +paragraph is identical to the one used for the speech accent archive +(http://accent.gmu.edu). The details of the the speech accent archive +can be found at +http://www.ualberta.ca/~aacl2009/PDFs/WeinbergerKunath2009AACL.pdf + +All speech data was recorded using an identical recording setup: an +omni-directional microphone (DPA 4035) and a small diaphragm condenser +microphone with very wide bandwidth (Sennheiser MKH 800), 96kHz +sampling frequency at 24 bits and in a hemi-anechoic chamber of +the University of Edinburgh. (However, two speakers, p280 and p315 +had technical issues of the audio recordings using MKH 800). +All recordings were converted into 16 bits, were downsampled to +48 kHz, and were manually end-pointed. + +This corpus was originally aimed for HMM-based text-to-speech synthesis +systems, especially for speaker-adaptive HMM-based speech synthesis +that uses average voice models trained on multiple speakers and speaker +adaptation technologies. This corpus is also suitable for DNN-based +multi-speaker text-to-speech synthesis systems and waveform modeling. + +COPYING + +This corpus is licensed under the Creative Commons License: Attribution 4.0 International +http://creativecommons.org/licenses/by/4.0/legalcode + +VCTK VARIANTS +There are several variants of the VCTK corpus: +Speech enhancement +- Noisy speech database for training speech enhancement algorithms and TTS models where we added various types of noises to VCTK artificially: http://dx.doi.org/10.7488/ds/2117 +- Reverberant speech database for training speech dereverberation algorithms and TTS models where we added various types of reverberantion to VCTK artificially http://dx.doi.org/10.7488/ds/1425 +- Noisy reverberant speech database for training speech enhancement algorithms and TTS models http://dx.doi.org/10.7488/ds/2139 +- Device Recorded VCTK where speech signals of the VCTK corpus were played back and re-recorded in office environments using relatively inexpensive consumer devices http://dx.doi.org/10.7488/ds/2316 +- The Microsoft Scalable Noisy Speech Dataset (MS-SNSD) https://github.com/microsoft/MS-SNSD + +ASV and anti-spoofing +- Spoofing and Anti-Spoofing (SAS) corpus, which is a collection of synthetic speech signals produced by nine techniques, two of which are speech synthesis, and seven are voice conversion. All of them were built using the VCTK corpus. http://dx.doi.org/10.7488/ds/252 +- Automatic Speaker Verification Spoofing and Countermeasures Challenge (ASVspoof 2015) Database. This database consists of synthetic speech signals produced by ten techniques and this has been used in the first Automatic Speaker Verification Spoofing and Countermeasures Challenge (ASVspoof 2015) http://dx.doi.org/10.7488/ds/298 +- ASVspoof 2019: The 3rd Automatic Speaker Verification Spoofing and Countermeasures Challenge database. This database has been used in the 3rd Automatic Speaker Verification Spoofing and Countermeasures Challenge (ASVspoof 2019) https://doi.org/10.7488/ds/2555 + + +ACKNOWLEDGEMENTS + +The CSTR VCTK Corpus was constructed by: + + Christophe Veaux (University of Edinburgh) + Junichi Yamagishi (University of Edinburgh) + Kirsten MacDonald + +The research leading to these results was partly funded from EPSRC +grants EP/I031022/1 (NST) and EP/J002526/1 (CAF), from the RSE-NSFC +grant (61111130120), and from the JST CREST (uDialogue). + +Please cite this corpus as follows: +Christophe Veaux, Junichi Yamagishi, Kirsten MacDonald, +"CSTR VCTK Corpus: English Multi-speaker Corpus for CSTR Voice Cloning Toolkit", +The Centre for Speech Technology Research (CSTR), +University of Edinburgh + diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/_cmudict.py b/_cmudict.py new file mode 100644 index 0000000000000000000000000000000000000000..2cef1f896d4fb78478884fe8e810956998d5e3b3 --- /dev/null +++ b/_cmudict.py @@ -0,0 +1,62 @@ +import re + +valid_symbols = [ + "AA", "AA0", "AA1", "AA2", "AE", "AE0", "AE1", "AE2", "AH", "AH0", "AH1", "AH2", + "AO", "AO0", "AO1", "AO2", "AW", "AW0", "AW1", "AW2", "AY", "AY0", "AY1", "AY2", + "B", "CH", "D", "DH", "EH", "EH0", "EH1", "EH2", "ER", "ER0", "ER1", "ER2", "EY", + "EY0", "EY1", "EY2", "F", "G", "HH", "IH", "IH0", "IH1", "IH2", "IY", "IY0", "IY1", + "IY2", "JH", "K", "L", "M", "N", "NG", "OW", "OW0", "OW1", "OW2", "OY", "OY0", + "OY1", "OY2", "P", "R", "S", "SH", "T", "TH", "UH", "UH0", "UH1", "UH2", "UW", + "UW0", "UW1", "UW2", "V", "W", "Y", "Z", "ZH" +] + +_valid_symbol_set = set(valid_symbols) + + +class CMUDict: + """Thin wrapper around CMUDict data. http://www.speech.cs.cmu.edu/cgi-bin/cmudict""" + def __init__(self, file_or_path, keep_ambiguous=True): + if isinstance(file_or_path, str): + with open(file_or_path, encoding="latin-1") as f: + entries = _parse_cmudict(f) + else: + entries = _parse_cmudict(file_or_path) + if not keep_ambiguous: + entries = {word: pron for word, pron in entries.items() if len(pron) == 1} + self._entries = entries + + + def __len__(self): + return len(self._entries) + + + def lookup(self, word): + """Returns list of ARPAbet pronunciations of the given word.""" + return self._entries.get(word.upper()) + + + +_alt_re = re.compile(r"\([0-9]+\)") + + +def _parse_cmudict(file): + cmudict = {} + for line in file: + if len(line) and (line[0] >= "A" and line[0] <= "Z" or line[0] == "'"): + parts = line.split(" ") + word = re.sub(_alt_re, "", parts[0]) + pronunciation = _get_pronunciation(parts[1]) + if pronunciation: + if word in cmudict: + cmudict[word].append(pronunciation) + else: + cmudict[word] = [pronunciation] + return cmudict + + +def _get_pronunciation(s): + parts = s.strip().split(" ") + for part in parts: + if part not in _valid_symbol_set: + return None + return " ".join(parts) diff --git a/argutils.py b/argutils.py new file mode 100644 index 0000000000000000000000000000000000000000..db41683027173517c910e3b259f8da48207dcb38 --- /dev/null +++ b/argutils.py @@ -0,0 +1,40 @@ +from pathlib import Path +import numpy as np +import argparse + +_type_priorities = [ # In decreasing order + Path, + str, + int, + float, + bool, +] + +def _priority(o): + p = next((i for i, t in enumerate(_type_priorities) if type(o) is t), None) + if p is not None: + return p + p = next((i for i, t in enumerate(_type_priorities) if isinstance(o, t)), None) + if p is not None: + return p + return len(_type_priorities) + +def print_args(args: argparse.Namespace, parser=None): + args = vars(args) + if parser is None: + priorities = list(map(_priority, args.values())) + else: + all_params = [a.dest for g in parser._action_groups for a in g._group_actions ] + priority = lambda p: all_params.index(p) if p in all_params else len(all_params) + priorities = list(map(priority, args.keys())) + + pad = max(map(len, args.keys())) + 3 + indices = np.lexsort((list(args.keys()), priorities)) + items = list(args.items()) + + print("Arguments:") + for i in indices: + param, value = items[i] + print(" {0}:{1}{2}".format(param, ' ' * (pad - len(param)), value)) + print("") + \ No newline at end of file diff --git a/audio.py b/audio.py new file mode 100644 index 0000000000000000000000000000000000000000..116396261e184b9968971bd06fabc6f525e0c2fe --- /dev/null +++ b/audio.py @@ -0,0 +1,108 @@ +import math +import numpy as np +import librosa +import vocoder.hparams as hp +from scipy.signal import lfilter +import soundfile as sf + + +def label_2_float(x, bits) : + return 2 * x / (2**bits - 1.) - 1. + + +def float_2_label(x, bits) : + assert abs(x).max() <= 1.0 + x = (x + 1.) * (2**bits - 1) / 2 + return x.clip(0, 2**bits - 1) + + +def load_wav(path) : + return librosa.load(str(path), sr=hp.sample_rate)[0] + + +def save_wav(x, path) : + sf.write(path, x.astype(np.float32), hp.sample_rate) + + +def split_signal(x) : + unsigned = x + 2**15 + coarse = unsigned // 256 + fine = unsigned % 256 + return coarse, fine + + +def combine_signal(coarse, fine) : + return coarse * 256 + fine - 2**15 + + +def encode_16bits(x) : + return np.clip(x * 2**15, -2**15, 2**15 - 1).astype(np.int16) + + +mel_basis = None + + +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(): + return librosa.filters.mel(hp.sample_rate, hp.n_fft, n_mels=hp.num_mels, fmin=hp.fmin) + + +def normalize(S): + return np.clip((S - hp.min_level_db) / -hp.min_level_db, 0, 1) + + +def denormalize(S): + return (np.clip(S, 0, 1) * -hp.min_level_db) + hp.min_level_db + + +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 spectrogram(y): + D = stft(y) + S = amp_to_db(np.abs(D)) - hp.ref_level_db + return normalize(S) + + +def melspectrogram(y): + D = stft(y) + S = amp_to_db(linear_to_mel(np.abs(D))) + return normalize(S) + + +def stft(y): + return librosa.stft(y=y, n_fft=hp.n_fft, hop_length=hp.hop_length, win_length=hp.win_length) + + +def pre_emphasis(x): + return lfilter([1, -hp.preemphasis], [1], x) + + +def de_emphasis(x): + return lfilter([1], [1, -hp.preemphasis], x) + + +def encode_mu_law(x, mu) : + mu = mu - 1 + fx = np.sign(x) * np.log(1 + mu * np.abs(x)) / np.log(1 + mu) + return np.floor((fx + 1) / 2 * mu + 0.5) + + +def decode_mu_law(y, mu, from_labels=True) : + if from_labels: + y = label_2_float(y, math.log2(mu)) + mu = mu - 1 + x = np.sign(y) / mu * ((1 + mu) ** np.abs(y) - 1) + return x + diff --git a/cleaners.py b/cleaners.py new file mode 100644 index 0000000000000000000000000000000000000000..a85884cbc401a3216ebdb4f3e5d333e4f3239638 --- /dev/null +++ b/cleaners.py @@ -0,0 +1,96 @@ +""" +Cleaners are transformations that run over the input text at both training and eval time. + +Cleaners can be selected by passing a comma-delimited list of cleaner names as the "cleaners" +hyperparameter. Some cleaners are English-specific. You"ll typically want to use: + 1. "english_cleaners" for English text + 2. "transliteration_cleaners" for non-English text that can be transliterated to ASCII using + the Unidecode library (https://pypi.python.org/pypi/Unidecode) + 3. "basic_cleaners" if you do not want to transliterate (in this case, you should also update + the symbols in symbols.py to match your data). +""" + +import re +from unidecode import unidecode +from .numbers import normalize_numbers +from .korean import tokenize as ko_tokenize + +# Regular expression matching whitespace: +_whitespace_re = re.compile(r"\s+") + + +def korean_cleaners(text): + '''Pipeline for Korean text, including number and abbreviation expansion.''' + text = ko_tokenize(text) # '존경하는' --> ['ᄌ', 'ᅩ', 'ᆫ', 'ᄀ', 'ᅧ', 'ᆼ', 'ᄒ', 'ᅡ', 'ᄂ', 'ᅳ', 'ᆫ', '~'] + return text + + +# List of (regular expression, replacement) pairs for abbreviations: +_abbreviations = [(re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1]) for x in [ + ("mrs", "misess"), + ("mr", "mister"), + ("dr", "doctor"), + ("st", "saint"), + ("co", "company"), + ("jr", "junior"), + ("maj", "major"), + ("gen", "general"), + ("drs", "doctors"), + ("rev", "reverend"), + ("lt", "lieutenant"), + ("hon", "honorable"), + ("sgt", "sergeant"), + ("capt", "captain"), + ("esq", "esquire"), + ("ltd", "limited"), + ("col", "colonel"), + ("ft", "fort"), +]] + + +def expand_abbreviations(text): + for regex, replacement in _abbreviations: + text = re.sub(regex, replacement, text) + return text + + +def expand_numbers(text): + return normalize_numbers(text) + + +def lowercase(text): + """lowercase input tokens.""" + return text.lower() + + +def collapse_whitespace(text): + return re.sub(_whitespace_re, " ", text) + + +def convert_to_ascii(text): + return unidecode(text) + + +def basic_cleaners(text): + """Basic pipeline that lowercases and collapses whitespace without transliteration.""" + text = lowercase(text) + text = collapse_whitespace(text) + return text + + +def transliteration_cleaners(text): + """Pipeline for non-English text that transliterates to ASCII.""" + text = convert_to_ascii(text) + text = lowercase(text) + text = collapse_whitespace(text) + return text + + +def english_cleaners(text): + """Pipeline for English text, including number and abbreviation expansion.""" + text = convert_to_ascii(text) + text = lowercase(text) + text = expand_numbers(text) + text = expand_abbreviations(text) + text = collapse_whitespace(text) + return text diff --git a/config.py b/config.py new file mode 100644 index 0000000000000000000000000000000000000000..1c21312f3de971bfa008254c6035cebc09f05e4c --- /dev/null +++ b/config.py @@ -0,0 +1,45 @@ +librispeech_datasets = { + "train": { + "clean": ["LibriSpeech/train-clean-100", "LibriSpeech/train-clean-360"], + "other": ["LibriSpeech/train-other-500"] + }, + "test": { + "clean": ["LibriSpeech/test-clean"], + "other": ["LibriSpeech/test-other"] + }, + "dev": { + "clean": ["LibriSpeech/dev-clean"], + "other": ["LibriSpeech/dev-other"] + }, +} +libritts_datasets = { + "train": { + "clean": ["LibriTTS/train-clean-100", "LibriTTS/train-clean-360"], + "other": ["LibriTTS/train-other-500"] + }, + "test": { + "clean": ["LibriTTS/test-clean"], + "other": ["LibriTTS/test-other"] + }, + "dev": { + "clean": ["LibriTTS/dev-clean"], + "other": ["LibriTTS/dev-other"] + }, +} +voxceleb_datasets = { + "voxceleb1" : { + "train": ["VoxCeleb1/wav"], + "test": ["VoxCeleb1/test_wav"] + }, + "voxceleb2" : { + "train": ["VoxCeleb2/dev/aac"], + "test": ["VoxCeleb2/test_wav"] + } +} + +other_datasets = [ + "LJSpeech-1.1", + "VCTK-Corpus/wav48", +] + +anglophone_nationalites = ["australia", "canada", "ireland", "uk", "usa"] diff --git a/deepmind_version.py b/deepmind_version.py new file mode 100644 index 0000000000000000000000000000000000000000..1d973d9b8b9ab547571abc5a3f5ea86226a25924 --- /dev/null +++ b/deepmind_version.py @@ -0,0 +1,170 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +from utils.display import * +from utils.dsp import * + + +class WaveRNN(nn.Module) : + def __init__(self, hidden_size=896, quantisation=256) : + super(WaveRNN, self).__init__() + + self.hidden_size = hidden_size + self.split_size = hidden_size // 2 + + # The main matmul + self.R = nn.Linear(self.hidden_size, 3 * self.hidden_size, bias=False) + + # Output fc layers + self.O1 = nn.Linear(self.split_size, self.split_size) + self.O2 = nn.Linear(self.split_size, quantisation) + self.O3 = nn.Linear(self.split_size, self.split_size) + self.O4 = nn.Linear(self.split_size, quantisation) + + # Input fc layers + self.I_coarse = nn.Linear(2, 3 * self.split_size, bias=False) + self.I_fine = nn.Linear(3, 3 * self.split_size, bias=False) + + # biases for the gates + self.bias_u = nn.Parameter(torch.zeros(self.hidden_size)) + self.bias_r = nn.Parameter(torch.zeros(self.hidden_size)) + self.bias_e = nn.Parameter(torch.zeros(self.hidden_size)) + + # display num params + self.num_params() + + + def forward(self, prev_y, prev_hidden, current_coarse) : + + # Main matmul - the projection is split 3 ways + R_hidden = self.R(prev_hidden) + R_u, R_r, R_e, = torch.split(R_hidden, self.hidden_size, dim=1) + + # Project the prev input + coarse_input_proj = self.I_coarse(prev_y) + I_coarse_u, I_coarse_r, I_coarse_e = \ + torch.split(coarse_input_proj, self.split_size, dim=1) + + # Project the prev input and current coarse sample + fine_input = torch.cat([prev_y, current_coarse], dim=1) + fine_input_proj = self.I_fine(fine_input) + I_fine_u, I_fine_r, I_fine_e = \ + torch.split(fine_input_proj, self.split_size, dim=1) + + # concatenate for the gates + I_u = torch.cat([I_coarse_u, I_fine_u], dim=1) + I_r = torch.cat([I_coarse_r, I_fine_r], dim=1) + I_e = torch.cat([I_coarse_e, I_fine_e], dim=1) + + # Compute all gates for coarse and fine + u = F.sigmoid(R_u + I_u + self.bias_u) + r = F.sigmoid(R_r + I_r + self.bias_r) + e = F.tanh(r * R_e + I_e + self.bias_e) + hidden = u * prev_hidden + (1. - u) * e + + # Split the hidden state + hidden_coarse, hidden_fine = torch.split(hidden, self.split_size, dim=1) + + # Compute outputs + out_coarse = self.O2(F.relu(self.O1(hidden_coarse))) + out_fine = self.O4(F.relu(self.O3(hidden_fine))) + + return out_coarse, out_fine, hidden + + + def generate(self, seq_len): + with torch.no_grad(): + # First split up the biases for the gates + b_coarse_u, b_fine_u = torch.split(self.bias_u, self.split_size) + b_coarse_r, b_fine_r = torch.split(self.bias_r, self.split_size) + b_coarse_e, b_fine_e = torch.split(self.bias_e, self.split_size) + + # Lists for the two output seqs + c_outputs, f_outputs = [], [] + + # Some initial inputs + out_coarse = torch.LongTensor([0]).cuda() + out_fine = torch.LongTensor([0]).cuda() + + # We'll meed a hidden state + hidden = self.init_hidden() + + # Need a clock for display + start = time.time() + + # Loop for generation + for i in range(seq_len) : + + # Split into two hidden states + hidden_coarse, hidden_fine = \ + torch.split(hidden, self.split_size, dim=1) + + # Scale and concat previous predictions + out_coarse = out_coarse.unsqueeze(0).float() / 127.5 - 1. + out_fine = out_fine.unsqueeze(0).float() / 127.5 - 1. + prev_outputs = torch.cat([out_coarse, out_fine], dim=1) + + # Project input + coarse_input_proj = self.I_coarse(prev_outputs) + I_coarse_u, I_coarse_r, I_coarse_e = \ + torch.split(coarse_input_proj, self.split_size, dim=1) + + # Project hidden state and split 6 ways + R_hidden = self.R(hidden) + R_coarse_u , R_fine_u, \ + R_coarse_r, R_fine_r, \ + R_coarse_e, R_fine_e = torch.split(R_hidden, self.split_size, dim=1) + + # Compute the coarse gates + u = F.sigmoid(R_coarse_u + I_coarse_u + b_coarse_u) + r = F.sigmoid(R_coarse_r + I_coarse_r + b_coarse_r) + e = F.tanh(r * R_coarse_e + I_coarse_e + b_coarse_e) + hidden_coarse = u * hidden_coarse + (1. - u) * e + + # Compute the coarse output + out_coarse = self.O2(F.relu(self.O1(hidden_coarse))) + posterior = F.softmax(out_coarse, dim=1) + distrib = torch.distributions.Categorical(posterior) + out_coarse = distrib.sample() + c_outputs.append(out_coarse) + + # Project the [prev outputs and predicted coarse sample] + coarse_pred = out_coarse.float() / 127.5 - 1. + fine_input = torch.cat([prev_outputs, coarse_pred.unsqueeze(0)], dim=1) + fine_input_proj = self.I_fine(fine_input) + I_fine_u, I_fine_r, I_fine_e = \ + torch.split(fine_input_proj, self.split_size, dim=1) + + # Compute the fine gates + u = F.sigmoid(R_fine_u + I_fine_u + b_fine_u) + r = F.sigmoid(R_fine_r + I_fine_r + b_fine_r) + e = F.tanh(r * R_fine_e + I_fine_e + b_fine_e) + hidden_fine = u * hidden_fine + (1. - u) * e + + # Compute the fine output + out_fine = self.O4(F.relu(self.O3(hidden_fine))) + posterior = F.softmax(out_fine, dim=1) + distrib = torch.distributions.Categorical(posterior) + out_fine = distrib.sample() + f_outputs.append(out_fine) + + # Put the hidden state back together + hidden = torch.cat([hidden_coarse, hidden_fine], dim=1) + + # Display progress + speed = (i + 1) / (time.time() - start) + stream('Gen: %i/%i -- Speed: %i', (i + 1, seq_len, speed)) + + coarse = torch.stack(c_outputs).squeeze(1).cpu().data.numpy() + fine = torch.stack(f_outputs).squeeze(1).cpu().data.numpy() + output = combine_signal(coarse, fine) + + return output, coarse, fine + + def init_hidden(self, batch_size=1) : + return torch.zeros(batch_size, self.hidden_size).cuda() + + def num_params(self) : + parameters = filter(lambda p: p.requires_grad, self.parameters()) + parameters = sum([np.prod(p.size()) for p in parameters]) / 1_000_000 + print('Trainable Parameters: %.3f million' % parameters) \ No newline at end of file diff --git a/demo_cli.py b/demo_cli.py new file mode 100644 index 0000000000000000000000000000000000000000..c7309e8e247b83fd5d20084db2191a0fa0f47cc8 --- /dev/null +++ b/demo_cli.py @@ -0,0 +1,225 @@ +from encoder.params_model import model_embedding_size as speaker_embedding_size +from utils.argutils import print_args +from utils.modelutils import check_model_paths +from synthesizer.inference import Synthesizer +from encoder import inference as encoder +from vocoder import inference as vocoder +from pathlib import Path +import numpy as np +import soundfile as sf +import librosa +import argparse +import torch +import sys +import os +from audioread.exceptions import NoBackendError + +if __name__ == '__main__': + ## Info & args + parser = argparse.ArgumentParser( + formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + parser.add_argument("-e", "--enc_model_fpath", type=Path, + default="encoder/saved_models/pretrained.pt", + help="Path to a saved encoder") + parser.add_argument("-s", "--syn_model_fpath", type=Path, + default="synthesizer/saved_models/pretrained/pretrained.pt", + help="Path to a saved synthesizer") + parser.add_argument("-v", "--voc_model_fpath", type=Path, + default="vocoder/saved_models/pretrained/pretrained.pt", + help="Path to a saved vocoder") + parser.add_argument("--cpu", action="store_true", help=\ + "If True, processing is done on CPU, even when a GPU is available.") + parser.add_argument("--no_sound", action="store_true", help=\ + "If True, audio won't be played.") + parser.add_argument("--seed", type=int, default=None, help=\ + "Optional random number seed value to make toolbox deterministic.") + parser.add_argument("--no_mp3_support", action="store_true", help=\ + "If True, disallows loading mp3 files to prevent audioread errors when ffmpeg is not installed.") + args = parser.parse_args() + print_args(args, parser) + if not args.no_sound: + import sounddevice as sd + + if args.cpu: + # Hide GPUs from Pytorch to force CPU processing + os.environ["CUDA_VISIBLE_DEVICES"] = "" + + if not args.no_mp3_support: + try: + librosa.load("samples/1320_00000.mp3") + except NoBackendError: + print("Librosa will be unable to open mp3 files if additional software is not installed.\n" + "Please install ffmpeg or add the '--no_mp3_support' option to proceed without support for mp3 files.") + exit(-1) + + print("Running a test of your configuration...\n") + + if torch.cuda.is_available(): + device_id = torch.cuda.current_device() + gpu_properties = torch.cuda.get_device_properties(device_id) + ## Print some environment information (for debugging purposes) + print("Found %d GPUs available. Using GPU %d (%s) of compute capability %d.%d with " + "%.1fGb total memory.\n" % + (torch.cuda.device_count(), + device_id, + gpu_properties.name, + gpu_properties.major, + gpu_properties.minor, + gpu_properties.total_memory / 1e9)) + else: + print("Using CPU for inference.\n") + + ## Remind the user to download pretrained models if needed + check_model_paths(encoder_path=args.enc_model_fpath, + synthesizer_path=args.syn_model_fpath, + vocoder_path=args.voc_model_fpath) + + ## Load the models one by one. + print("Preparing the encoder, the synthesizer and the vocoder...") + encoder.load_model(args.enc_model_fpath) + synthesizer = Synthesizer(args.syn_model_fpath) + vocoder.load_model(args.voc_model_fpath) + + + ## Run a test + print("Testing your configuration with small inputs.") + # Forward an audio waveform of zeroes that lasts 1 second. Notice how we can get the encoder's + # sampling rate, which may differ. + # If you're unfamiliar with digital audio, know that it is encoded as an array of floats + # (or sometimes integers, but mostly floats in this projects) ranging from -1 to 1. + # The sampling rate is the number of values (samples) recorded per second, it is set to + # 16000 for the encoder. Creating an array of length will always correspond + # to an audio of 1 second. + print("\tTesting the encoder...") + encoder.embed_utterance(np.zeros(encoder.sampling_rate)) + + # Create a dummy embedding. You would normally use the embedding that encoder.embed_utterance + # returns, but here we're going to make one ourselves just for the sake of showing that it's + # possible. + embed = np.random.rand(speaker_embedding_size) + # Embeddings are L2-normalized (this isn't important here, but if you want to make your own + # embeddings it will be). + embed /= np.linalg.norm(embed) + # The synthesizer can handle multiple inputs with batching. Let's create another embedding to + # illustrate that + embeds = [embed, np.zeros(speaker_embedding_size)] + texts = ["test 1", "test 2"] + print("\tTesting the synthesizer... (loading the model will output a lot of text)") + mels = synthesizer.synthesize_spectrograms(texts, embeds) + + # The vocoder synthesizes one waveform at a time, but it's more efficient for long ones. We + # can concatenate the mel spectrograms to a single one. + mel = np.concatenate(mels, axis=1) + # The vocoder can take a callback function to display the generation. More on that later. For + # now we'll simply hide it like this: + no_action = lambda *args: None + print("\tTesting the vocoder...") + # For the sake of making this test short, we'll pass a short target length. The target length + # is the length of the wav segments that are processed in parallel. E.g. for audio sampled + # at 16000 Hertz, a target length of 8000 means that the target audio will be cut in chunks of + # 0.5 seconds which will all be generated together. The parameters here are absurdly short, and + # that has a detrimental effect on the quality of the audio. The default parameters are + # recommended in general. + vocoder.infer_waveform(mel, target=200, overlap=50, progress_callback=no_action) + + print("All test passed! You can now synthesize speech.\n\n") + + + ## Interactive speech generation + print("This is a GUI-less example of interface to SV2TTS. The purpose of this script is to " + "show how you can interface this project easily with your own. See the source code for " + "an explanation of what is happening.\n") + + print("Interactive generation loop") + num_generated = 0 + while True: + try: + # Get the reference audio filepath + message = "Reference voice: enter an audio filepath of a voice to be cloned (mp3, " \ + "wav, m4a, flac, ...):\n" + in_fpath = Path(input(message).replace("\"", "").replace("\'", "")) + + if in_fpath.suffix.lower() == ".mp3" and args.no_mp3_support: + print("Can't Use mp3 files please try again:") + continue + ## Computing the embedding + # First, we load the wav using the function that the speaker encoder provides. This is + # important: there is preprocessing that must be applied. + + # The following two methods are equivalent: + # - Directly load from the filepath: + preprocessed_wav = encoder.preprocess_wav(in_fpath) + # - If the wav is already loaded: + original_wav, sampling_rate = librosa.load(str(in_fpath)) + preprocessed_wav = encoder.preprocess_wav(original_wav, sampling_rate) + print("Loaded file succesfully") + + # Then we derive the embedding. There are many functions and parameters that the + # speaker encoder interfaces. These are mostly for in-depth research. You will typically + # only use this function (with its default parameters): + embed = encoder.embed_utterance(preprocessed_wav) + print("Created the embedding") + + + ## Generating the spectrogram + text = input("Write a sentence (+-20 words) to be synthesized:\n") + + # If seed is specified, reset torch seed and force synthesizer reload + if args.seed is not None: + torch.manual_seed(args.seed) + synthesizer = Synthesizer(args.syn_model_fpath) + + # The synthesizer works in batch, so you need to put your data in a list or numpy array + texts = [text] + embeds = [embed] + # If you know what the attention layer alignments are, you can retrieve them here by + # passing return_alignments=True + specs = synthesizer.synthesize_spectrograms(texts, embeds) + spec = specs[0] + print("Created the mel spectrogram") + + + ## Generating the waveform + print("Synthesizing the waveform:") + + # If seed is specified, reset torch seed and reload vocoder + if args.seed is not None: + torch.manual_seed(args.seed) + vocoder.load_model(args.voc_model_fpath) + + # Synthesizing the waveform is fairly straightforward. Remember that the longer the + # spectrogram, the more time-efficient the vocoder. + generated_wav = vocoder.infer_waveform(spec) + + + ## Post-generation + # There's a bug with sounddevice that makes the audio cut one second earlier, so we + # pad it. + generated_wav = np.pad(generated_wav, (0, synthesizer.sample_rate), mode="constant") + + # Trim excess silences to compensate for gaps in spectrograms (issue #53) + generated_wav = encoder.preprocess_wav(generated_wav) + + # Play the audio (non-blocking) + if not args.no_sound: + try: + sd.stop() + sd.play(generated_wav, synthesizer.sample_rate) + except sd.PortAudioError as e: + print("\nCaught exception: %s" % repr(e)) + print("Continuing without audio playback. Suppress this message with the \"--no_sound\" flag.\n") + except: + raise + + # Save it on the disk + filename = "demo_output_%02d.wav" % num_generated + print(generated_wav.dtype) + sf.write(filename, generated_wav.astype(np.float32), synthesizer.sample_rate) + num_generated += 1 + print("\nSaved output as %s\n\n" % filename) + + + except Exception as e: + print("Caught exception: %s" % repr(e)) + print("Restarting\n") diff --git a/demo_toolbox.py b/demo_toolbox.py new file mode 100644 index 0000000000000000000000000000000000000000..d93803141cbcdf70133d5111942182471fff2ed7 --- /dev/null +++ b/demo_toolbox.py @@ -0,0 +1,43 @@ +from pathlib import Path +from toolbox import Toolbox +from utils.argutils import print_args +from utils.modelutils import check_model_paths +import argparse +import os + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description="Runs the toolbox", + formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + + parser.add_argument("-d", "--datasets_root", type=Path, help= \ + "Path to the directory containing your datasets. See toolbox/__init__.py for a list of " + "supported datasets.", default=None) + parser.add_argument("-e", "--enc_models_dir", type=Path, default="encoder/saved_models", + help="Directory containing saved encoder models") + parser.add_argument("-s", "--syn_models_dir", type=Path, default="synthesizer/saved_models", + help="Directory containing saved synthesizer models") + parser.add_argument("-v", "--voc_models_dir", type=Path, default="vocoder/saved_models", + help="Directory containing saved vocoder models") + parser.add_argument("--cpu", action="store_true", help=\ + "If True, processing is done on CPU, even when a GPU is available.") + parser.add_argument("--seed", type=int, default=None, help=\ + "Optional random number seed value to make toolbox deterministic.") + parser.add_argument("--no_mp3_support", action="store_true", help=\ + "If True, no mp3 files are allowed.") + args = parser.parse_args() + print_args(args, parser) + + if args.cpu: + # Hide GPUs from Pytorch to force CPU processing + os.environ["CUDA_VISIBLE_DEVICES"] = "" + del args.cpu + + ## Remind the user to download pretrained models if needed + check_model_paths(encoder_path=args.enc_models_dir, synthesizer_path=args.syn_models_dir, + vocoder_path=args.voc_models_dir) + + # Launch the toolbox + Toolbox(**vars(args)) diff --git a/display.py b/display.py new file mode 100644 index 0000000000000000000000000000000000000000..956880722a3f05613ebd06f5686b3d8a59642e92 --- /dev/null +++ b/display.py @@ -0,0 +1,120 @@ +import matplotlib.pyplot as plt +import time +import numpy as np +import sys + + +def progbar(i, n, size=16): + done = (i * size) // n + bar = '' + for i in range(size): + bar += '█' if i <= done else '░' + return bar + + +def stream(message) : + try: + sys.stdout.write("\r{%s}" % message) + except: + #Remove non-ASCII characters from message + message = ''.join(i for i in message if ord(i)<128) + sys.stdout.write("\r{%s}" % message) + + +def simple_table(item_tuples) : + + border_pattern = '+---------------------------------------' + whitespace = ' ' + + headings, cells, = [], [] + + for item in item_tuples : + + heading, cell = str(item[0]), str(item[1]) + + pad_head = True if len(heading) < len(cell) else False + + pad = abs(len(heading) - len(cell)) + pad = whitespace[:pad] + + pad_left = pad[:len(pad)//2] + pad_right = pad[len(pad)//2:] + + if pad_head : + heading = pad_left + heading + pad_right + else : + cell = pad_left + cell + pad_right + + headings += [heading] + cells += [cell] + + border, head, body = '', '', '' + + for i in range(len(item_tuples)) : + + temp_head = f'| {headings[i]} ' + temp_body = f'| {cells[i]} ' + + border += border_pattern[:len(temp_head)] + head += temp_head + body += temp_body + + if i == len(item_tuples) - 1 : + head += '|' + body += '|' + border += '+' + + print(border) + print(head) + print(border) + print(body) + print(border) + print(' ') + + +def time_since(started) : + elapsed = time.time() - started + m = int(elapsed // 60) + s = int(elapsed % 60) + if m >= 60 : + h = int(m // 60) + m = m % 60 + return f'{h}h {m}m {s}s' + else : + return f'{m}m {s}s' + + +def save_attention(attn, path) : + fig = plt.figure(figsize=(12, 6)) + plt.imshow(attn.T, interpolation='nearest', aspect='auto') + fig.savefig(f'{path}.png', bbox_inches='tight') + plt.close(fig) + + +def save_spectrogram(M, path, length=None) : + M = np.flip(M, axis=0) + if length : M = M[:, :length] + fig = plt.figure(figsize=(12, 6)) + plt.imshow(M, interpolation='nearest', aspect='auto') + fig.savefig(f'{path}.png', bbox_inches='tight') + plt.close(fig) + + +def plot(array) : + fig = plt.figure(figsize=(30, 5)) + ax = fig.add_subplot(111) + ax.xaxis.label.set_color('grey') + ax.yaxis.label.set_color('grey') + ax.xaxis.label.set_fontsize(23) + ax.yaxis.label.set_fontsize(23) + ax.tick_params(axis='x', colors='grey', labelsize=23) + ax.tick_params(axis='y', colors='grey', labelsize=23) + plt.plot(array) + + +def plot_spec(M) : + M = np.flip(M, axis=0) + plt.figure(figsize=(18,4)) + plt.imshow(M, interpolation='nearest', aspect='auto') + plt.show() + diff --git a/distribution.py b/distribution.py new file mode 100644 index 0000000000000000000000000000000000000000..d3119a5ba1e77bc25a92d2664f83d366f12399c0 --- /dev/null +++ b/distribution.py @@ -0,0 +1,132 @@ +import numpy as np +import torch +import torch.nn.functional as F + + +def log_sum_exp(x): + """ numerically stable log_sum_exp implementation that prevents overflow """ + # TF ordering + axis = len(x.size()) - 1 + m, _ = torch.max(x, dim=axis) + m2, _ = torch.max(x, dim=axis, keepdim=True) + return m + torch.log(torch.sum(torch.exp(x - m2), dim=axis)) + + +# It is adapted from https://github.com/r9y9/wavenet_vocoder/blob/master/wavenet_vocoder/mixture.py +def discretized_mix_logistic_loss(y_hat, y, num_classes=65536, + log_scale_min=None, reduce=True): + if log_scale_min is None: + log_scale_min = float(np.log(1e-14)) + y_hat = y_hat.permute(0,2,1) + assert y_hat.dim() == 3 + assert y_hat.size(1) % 3 == 0 + nr_mix = y_hat.size(1) // 3 + + # (B x T x C) + y_hat = y_hat.transpose(1, 2) + + # unpack parameters. (B, T, num_mixtures) x 3 + logit_probs = y_hat[:, :, :nr_mix] + means = y_hat[:, :, nr_mix:2 * nr_mix] + log_scales = torch.clamp(y_hat[:, :, 2 * nr_mix:3 * nr_mix], min=log_scale_min) + + # B x T x 1 -> B x T x num_mixtures + y = y.expand_as(means) + + centered_y = y - means + inv_stdv = torch.exp(-log_scales) + plus_in = inv_stdv * (centered_y + 1. / (num_classes - 1)) + cdf_plus = torch.sigmoid(plus_in) + min_in = inv_stdv * (centered_y - 1. / (num_classes - 1)) + cdf_min = torch.sigmoid(min_in) + + # log probability for edge case of 0 (before scaling) + # equivalent: torch.log(F.sigmoid(plus_in)) + log_cdf_plus = plus_in - F.softplus(plus_in) + + # log probability for edge case of 255 (before scaling) + # equivalent: (1 - F.sigmoid(min_in)).log() + log_one_minus_cdf_min = -F.softplus(min_in) + + # probability for all other cases + cdf_delta = cdf_plus - cdf_min + + mid_in = inv_stdv * centered_y + # log probability in the center of the bin, to be used in extreme cases + # (not actually used in our code) + log_pdf_mid = mid_in - log_scales - 2. * F.softplus(mid_in) + + # tf equivalent + """ + log_probs = tf.where(x < -0.999, log_cdf_plus, + tf.where(x > 0.999, log_one_minus_cdf_min, + tf.where(cdf_delta > 1e-5, + tf.log(tf.maximum(cdf_delta, 1e-12)), + log_pdf_mid - np.log(127.5)))) + """ + # TODO: cdf_delta <= 1e-5 actually can happen. How can we choose the value + # for num_classes=65536 case? 1e-7? not sure.. + inner_inner_cond = (cdf_delta > 1e-5).float() + + inner_inner_out = inner_inner_cond * \ + torch.log(torch.clamp(cdf_delta, min=1e-12)) + \ + (1. - inner_inner_cond) * (log_pdf_mid - np.log((num_classes - 1) / 2)) + inner_cond = (y > 0.999).float() + inner_out = inner_cond * log_one_minus_cdf_min + (1. - inner_cond) * inner_inner_out + cond = (y < -0.999).float() + log_probs = cond * log_cdf_plus + (1. - cond) * inner_out + + log_probs = log_probs + F.log_softmax(logit_probs, -1) + + if reduce: + return -torch.mean(log_sum_exp(log_probs)) + else: + return -log_sum_exp(log_probs).unsqueeze(-1) + + +def sample_from_discretized_mix_logistic(y, log_scale_min=None): + """ + Sample from discretized mixture of logistic distributions + Args: + y (Tensor): B x C x T + log_scale_min (float): Log scale minimum value + Returns: + Tensor: sample in range of [-1, 1]. + """ + if log_scale_min is None: + log_scale_min = float(np.log(1e-14)) + assert y.size(1) % 3 == 0 + nr_mix = y.size(1) // 3 + + # B x T x C + y = y.transpose(1, 2) + logit_probs = y[:, :, :nr_mix] + + # sample mixture indicator from softmax + temp = logit_probs.data.new(logit_probs.size()).uniform_(1e-5, 1.0 - 1e-5) + temp = logit_probs.data - torch.log(- torch.log(temp)) + _, argmax = temp.max(dim=-1) + + # (B, T) -> (B, T, nr_mix) + one_hot = to_one_hot(argmax, nr_mix) + # select logistic parameters + means = torch.sum(y[:, :, nr_mix:2 * nr_mix] * one_hot, dim=-1) + log_scales = torch.clamp(torch.sum( + y[:, :, 2 * nr_mix:3 * nr_mix] * one_hot, dim=-1), min=log_scale_min) + # sample from logistic & clip to interval + # we don't actually round to the nearest 8bit value when sampling + u = means.data.new(means.size()).uniform_(1e-5, 1.0 - 1e-5) + x = means + torch.exp(log_scales) * (torch.log(u) - torch.log(1. - u)) + + x = torch.clamp(torch.clamp(x, min=-1.), max=1.) + + return x + + +def to_one_hot(tensor, n, fill_with=1.): + # we perform one hot encore with respect to the last axis + one_hot = torch.FloatTensor(tensor.size() + (n,)).zero_() + if tensor.is_cuda: + one_hot = one_hot.cuda() + one_hot.scatter_(len(tensor.size()), tensor.unsqueeze(-1), fill_with) + return one_hot diff --git a/encoder_preprocess.py b/encoder_preprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..11502013c8d75d4652fb0ffdcdc49d55e8fb8bc9 --- /dev/null +++ b/encoder_preprocess.py @@ -0,0 +1,70 @@ +from encoder.preprocess import preprocess_librispeech, preprocess_voxceleb1, preprocess_voxceleb2 +from utils.argutils import print_args +from pathlib import Path +import argparse + +if __name__ == "__main__": + class MyFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): + pass + + parser = argparse.ArgumentParser( + description="Preprocesses audio files from datasets, encodes them as mel spectrograms and " + "writes them to the disk. This will allow you to train the encoder. The " + "datasets required are at least one of VoxCeleb1, VoxCeleb2 and LibriSpeech. " + "Ideally, you should have all three. You should extract them as they are " + "after having downloaded them and put them in a same directory, e.g.:\n" + "-[datasets_root]\n" + " -LibriSpeech\n" + " -train-other-500\n" + " -VoxCeleb1\n" + " -wav\n" + " -vox1_meta.csv\n" + " -VoxCeleb2\n" + " -dev", + formatter_class=MyFormatter + ) + parser.add_argument("datasets_root", type=Path, help=\ + "Path to the directory containing your LibriSpeech/TTS and VoxCeleb datasets.") + parser.add_argument("-o", "--out_dir", type=Path, default=argparse.SUPPRESS, help=\ + "Path to the output directory that will contain the mel spectrograms. If left out, " + "defaults to /SV2TTS/encoder/") + parser.add_argument("-d", "--datasets", type=str, + default="librispeech_other,voxceleb1,voxceleb2", help=\ + "Comma-separated list of the name of the datasets you want to preprocess. Only the train " + "set of these datasets will be used. Possible names: librispeech_other, voxceleb1, " + "voxceleb2.") + parser.add_argument("-s", "--skip_existing", action="store_true", help=\ + "Whether to skip existing output files with the same name. Useful if this script was " + "interrupted.") + parser.add_argument("--no_trim", action="store_true", help=\ + "Preprocess audio without trimming silences (not recommended).") + args = parser.parse_args() + + # Verify webrtcvad is available + if not args.no_trim: + try: + import webrtcvad + except: + raise ModuleNotFoundError("Package 'webrtcvad' not found. This package enables " + "noise removal and is recommended. Please install and try again. If installation fails, " + "use --no_trim to disable this error message.") + del args.no_trim + + # Process the arguments + args.datasets = args.datasets.split(",") + if not hasattr(args, "out_dir"): + args.out_dir = args.datasets_root.joinpath("SV2TTS", "encoder") + assert args.datasets_root.exists() + args.out_dir.mkdir(exist_ok=True, parents=True) + + # Preprocess the datasets + print_args(args, parser) + preprocess_func = { + "librispeech_other": preprocess_librispeech, + "voxceleb1": preprocess_voxceleb1, + "voxceleb2": preprocess_voxceleb2, + } + args = vars(args) + for dataset in args.pop("datasets"): + print("Preprocessing %s" % dataset) + preprocess_func[dataset](**args) diff --git a/encoder_train.py b/encoder_train.py new file mode 100644 index 0000000000000000000000000000000000000000..b8740a894d615aadfe529cb36068fc8e3496125f --- /dev/null +++ b/encoder_train.py @@ -0,0 +1,47 @@ +from utils.argutils import print_args +from encoder.train import train +from pathlib import Path +import argparse + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Trains the speaker encoder. You must have run encoder_preprocess.py first.", + formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + + parser.add_argument("run_id", type=str, help= \ + "Name for this model instance. If a model state from the same run ID was previously " + "saved, the training will restart from there. Pass -f to overwrite saved states and " + "restart from scratch.") + parser.add_argument("clean_data_root", type=Path, help= \ + "Path to the output directory of encoder_preprocess.py. If you left the default " + "output directory when preprocessing, it should be /SV2TTS/encoder/.") + parser.add_argument("-m", "--models_dir", type=Path, default="encoder/saved_models/", help=\ + "Path to the output directory that will contain the saved model weights, as well as " + "backups of those weights and plots generated during training.") + parser.add_argument("-v", "--vis_every", type=int, default=10, help= \ + "Number of steps between updates of the loss and the plots.") + parser.add_argument("-u", "--umap_every", type=int, default=100, help= \ + "Number of steps between updates of the umap projection. Set to 0 to never update the " + "projections.") + parser.add_argument("-s", "--save_every", type=int, default=500, help= \ + "Number of steps between updates of the model on the disk. Set to 0 to never save the " + "model.") + parser.add_argument("-b", "--backup_every", type=int, default=7500, help= \ + "Number of steps between backups of the model. Set to 0 to never make backups of the " + "model.") + parser.add_argument("-f", "--force_restart", action="store_true", help= \ + "Do not load any saved model.") + parser.add_argument("--visdom_server", type=str, default="http://localhost") + parser.add_argument("--no_visdom", action="store_true", help= \ + "Disable visdom.") + args = parser.parse_args() + + # Process the arguments + args.models_dir.mkdir(exist_ok=True) + + # Run the training + print_args(args, parser) + train(**vars(args)) + \ No newline at end of file diff --git a/fatchord_version.py b/fatchord_version.py new file mode 100644 index 0000000000000000000000000000000000000000..70ef1e3f6b99f32cc4fa95f64acfa58268d71ad7 --- /dev/null +++ b/fatchord_version.py @@ -0,0 +1,434 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +from vocoder.distribution import sample_from_discretized_mix_logistic +from vocoder.display import * +from vocoder.audio import * + + +class ResBlock(nn.Module): + def __init__(self, dims): + super().__init__() + self.conv1 = nn.Conv1d(dims, dims, kernel_size=1, bias=False) + self.conv2 = nn.Conv1d(dims, dims, kernel_size=1, bias=False) + self.batch_norm1 = nn.BatchNorm1d(dims) + self.batch_norm2 = nn.BatchNorm1d(dims) + + def forward(self, x): + residual = x + x = self.conv1(x) + x = self.batch_norm1(x) + x = F.relu(x) + x = self.conv2(x) + x = self.batch_norm2(x) + return x + residual + + +class MelResNet(nn.Module): + def __init__(self, res_blocks, in_dims, compute_dims, res_out_dims, pad): + super().__init__() + k_size = pad * 2 + 1 + self.conv_in = nn.Conv1d(in_dims, compute_dims, kernel_size=k_size, bias=False) + self.batch_norm = nn.BatchNorm1d(compute_dims) + self.layers = nn.ModuleList() + for i in range(res_blocks): + self.layers.append(ResBlock(compute_dims)) + self.conv_out = nn.Conv1d(compute_dims, res_out_dims, kernel_size=1) + + def forward(self, x): + x = self.conv_in(x) + x = self.batch_norm(x) + x = F.relu(x) + for f in self.layers: x = f(x) + x = self.conv_out(x) + return x + + +class Stretch2d(nn.Module): + def __init__(self, x_scale, y_scale): + super().__init__() + self.x_scale = x_scale + self.y_scale = y_scale + + def forward(self, x): + b, c, h, w = x.size() + x = x.unsqueeze(-1).unsqueeze(3) + x = x.repeat(1, 1, 1, self.y_scale, 1, self.x_scale) + return x.view(b, c, h * self.y_scale, w * self.x_scale) + + +class UpsampleNetwork(nn.Module): + def __init__(self, feat_dims, upsample_scales, compute_dims, + res_blocks, res_out_dims, pad): + super().__init__() + total_scale = np.cumproduct(upsample_scales)[-1] + self.indent = pad * total_scale + self.resnet = MelResNet(res_blocks, feat_dims, compute_dims, res_out_dims, pad) + self.resnet_stretch = Stretch2d(total_scale, 1) + self.up_layers = nn.ModuleList() + for scale in upsample_scales: + k_size = (1, scale * 2 + 1) + padding = (0, scale) + stretch = Stretch2d(scale, 1) + conv = nn.Conv2d(1, 1, kernel_size=k_size, padding=padding, bias=False) + conv.weight.data.fill_(1. / k_size[1]) + self.up_layers.append(stretch) + self.up_layers.append(conv) + + def forward(self, m): + aux = self.resnet(m).unsqueeze(1) + aux = self.resnet_stretch(aux) + aux = aux.squeeze(1) + m = m.unsqueeze(1) + for f in self.up_layers: m = f(m) + m = m.squeeze(1)[:, :, self.indent:-self.indent] + return m.transpose(1, 2), aux.transpose(1, 2) + + +class WaveRNN(nn.Module): + def __init__(self, rnn_dims, fc_dims, bits, pad, upsample_factors, + feat_dims, compute_dims, res_out_dims, res_blocks, + hop_length, sample_rate, mode='RAW'): + super().__init__() + self.mode = mode + self.pad = pad + if self.mode == 'RAW' : + self.n_classes = 2 ** bits + elif self.mode == 'MOL' : + self.n_classes = 30 + else : + RuntimeError("Unknown model mode value - ", self.mode) + + self.rnn_dims = rnn_dims + self.aux_dims = res_out_dims // 4 + self.hop_length = hop_length + self.sample_rate = sample_rate + + self.upsample = UpsampleNetwork(feat_dims, upsample_factors, compute_dims, res_blocks, res_out_dims, pad) + self.I = nn.Linear(feat_dims + self.aux_dims + 1, rnn_dims) + self.rnn1 = nn.GRU(rnn_dims, rnn_dims, batch_first=True) + self.rnn2 = nn.GRU(rnn_dims + self.aux_dims, rnn_dims, batch_first=True) + self.fc1 = nn.Linear(rnn_dims + self.aux_dims, fc_dims) + self.fc2 = nn.Linear(fc_dims + self.aux_dims, fc_dims) + self.fc3 = nn.Linear(fc_dims, self.n_classes) + + self.step = nn.Parameter(torch.zeros(1).long(), requires_grad=False) + self.num_params() + + def forward(self, x, mels): + self.step += 1 + bsize = x.size(0) + if torch.cuda.is_available(): + h1 = torch.zeros(1, bsize, self.rnn_dims).cuda() + h2 = torch.zeros(1, bsize, self.rnn_dims).cuda() + else: + h1 = torch.zeros(1, bsize, self.rnn_dims).cpu() + h2 = torch.zeros(1, bsize, self.rnn_dims).cpu() + mels, aux = self.upsample(mels) + + aux_idx = [self.aux_dims * i for i in range(5)] + a1 = aux[:, :, aux_idx[0]:aux_idx[1]] + a2 = aux[:, :, aux_idx[1]:aux_idx[2]] + a3 = aux[:, :, aux_idx[2]:aux_idx[3]] + a4 = aux[:, :, aux_idx[3]:aux_idx[4]] + + x = torch.cat([x.unsqueeze(-1), mels, a1], dim=2) + x = self.I(x) + res = x + x, _ = self.rnn1(x, h1) + + x = x + res + res = x + x = torch.cat([x, a2], dim=2) + x, _ = self.rnn2(x, h2) + + x = x + res + x = torch.cat([x, a3], dim=2) + x = F.relu(self.fc1(x)) + + x = torch.cat([x, a4], dim=2) + x = F.relu(self.fc2(x)) + return self.fc3(x) + + def generate(self, mels, batched, target, overlap, mu_law, progress_callback=None): + mu_law = mu_law if self.mode == 'RAW' else False + progress_callback = progress_callback or self.gen_display + + self.eval() + output = [] + start = time.time() + rnn1 = self.get_gru_cell(self.rnn1) + rnn2 = self.get_gru_cell(self.rnn2) + + with torch.no_grad(): + if torch.cuda.is_available(): + mels = mels.cuda() + else: + mels = mels.cpu() + wave_len = (mels.size(-1) - 1) * self.hop_length + mels = self.pad_tensor(mels.transpose(1, 2), pad=self.pad, side='both') + mels, aux = self.upsample(mels.transpose(1, 2)) + + if batched: + mels = self.fold_with_overlap(mels, target, overlap) + aux = self.fold_with_overlap(aux, target, overlap) + + b_size, seq_len, _ = mels.size() + + if torch.cuda.is_available(): + h1 = torch.zeros(b_size, self.rnn_dims).cuda() + h2 = torch.zeros(b_size, self.rnn_dims).cuda() + x = torch.zeros(b_size, 1).cuda() + else: + h1 = torch.zeros(b_size, self.rnn_dims).cpu() + h2 = torch.zeros(b_size, self.rnn_dims).cpu() + x = torch.zeros(b_size, 1).cpu() + + d = self.aux_dims + aux_split = [aux[:, :, d * i:d * (i + 1)] for i in range(4)] + + for i in range(seq_len): + + m_t = mels[:, i, :] + + a1_t, a2_t, a3_t, a4_t = (a[:, i, :] for a in aux_split) + + x = torch.cat([x, m_t, a1_t], dim=1) + x = self.I(x) + h1 = rnn1(x, h1) + + x = x + h1 + inp = torch.cat([x, a2_t], dim=1) + h2 = rnn2(inp, h2) + + x = x + h2 + x = torch.cat([x, a3_t], dim=1) + x = F.relu(self.fc1(x)) + + x = torch.cat([x, a4_t], dim=1) + x = F.relu(self.fc2(x)) + + logits = self.fc3(x) + + if self.mode == 'MOL': + sample = sample_from_discretized_mix_logistic(logits.unsqueeze(0).transpose(1, 2)) + output.append(sample.view(-1)) + if torch.cuda.is_available(): + # x = torch.FloatTensor([[sample]]).cuda() + x = sample.transpose(0, 1).cuda() + else: + x = sample.transpose(0, 1) + + elif self.mode == 'RAW' : + posterior = F.softmax(logits, dim=1) + distrib = torch.distributions.Categorical(posterior) + + sample = 2 * distrib.sample().float() / (self.n_classes - 1.) - 1. + output.append(sample) + x = sample.unsqueeze(-1) + else: + raise RuntimeError("Unknown model mode value - ", self.mode) + + if i % 100 == 0: + gen_rate = (i + 1) / (time.time() - start) * b_size / 1000 + progress_callback(i, seq_len, b_size, gen_rate) + + output = torch.stack(output).transpose(0, 1) + output = output.cpu().numpy() + output = output.astype(np.float64) + + if batched: + output = self.xfade_and_unfold(output, target, overlap) + else: + output = output[0] + + if mu_law: + output = decode_mu_law(output, self.n_classes, False) + if hp.apply_preemphasis: + output = de_emphasis(output) + + # Fade-out at the end to avoid signal cutting out suddenly + fade_out = np.linspace(1, 0, 20 * self.hop_length) + output = output[:wave_len] + output[-20 * self.hop_length:] *= fade_out + + self.train() + + return output + + + def gen_display(self, i, seq_len, b_size, gen_rate): + pbar = progbar(i, seq_len) + msg = f'| {pbar} {i*b_size}/{seq_len*b_size} | Batch Size: {b_size} | Gen Rate: {gen_rate:.1f}kHz | ' + stream(msg) + + def get_gru_cell(self, gru): + gru_cell = nn.GRUCell(gru.input_size, gru.hidden_size) + gru_cell.weight_hh.data = gru.weight_hh_l0.data + gru_cell.weight_ih.data = gru.weight_ih_l0.data + gru_cell.bias_hh.data = gru.bias_hh_l0.data + gru_cell.bias_ih.data = gru.bias_ih_l0.data + return gru_cell + + def pad_tensor(self, x, pad, side='both'): + # NB - this is just a quick method i need right now + # i.e., it won't generalise to other shapes/dims + b, t, c = x.size() + total = t + 2 * pad if side == 'both' else t + pad + if torch.cuda.is_available(): + padded = torch.zeros(b, total, c).cuda() + else: + padded = torch.zeros(b, total, c).cpu() + if side == 'before' or side == 'both': + padded[:, pad:pad + t, :] = x + elif side == 'after': + padded[:, :t, :] = x + return padded + + def fold_with_overlap(self, x, target, overlap): + + ''' Fold the tensor with overlap for quick batched inference. + Overlap will be used for crossfading in xfade_and_unfold() + + Args: + x (tensor) : Upsampled conditioning features. + shape=(1, timesteps, features) + target (int) : Target timesteps for each index of batch + overlap (int) : Timesteps for both xfade and rnn warmup + + Return: + (tensor) : shape=(num_folds, target + 2 * overlap, features) + + Details: + x = [[h1, h2, ... hn]] + + Where each h is a vector of conditioning features + + Eg: target=2, overlap=1 with x.size(1)=10 + + folded = [[h1, h2, h3, h4], + [h4, h5, h6, h7], + [h7, h8, h9, h10]] + ''' + + _, total_len, features = x.size() + + # Calculate variables needed + num_folds = (total_len - overlap) // (target + overlap) + extended_len = num_folds * (overlap + target) + overlap + remaining = total_len - extended_len + + # Pad if some time steps poking out + if remaining != 0: + num_folds += 1 + padding = target + 2 * overlap - remaining + x = self.pad_tensor(x, padding, side='after') + + if torch.cuda.is_available(): + folded = torch.zeros(num_folds, target + 2 * overlap, features).cuda() + else: + folded = torch.zeros(num_folds, target + 2 * overlap, features).cpu() + + # Get the values for the folded tensor + for i in range(num_folds): + start = i * (target + overlap) + end = start + target + 2 * overlap + folded[i] = x[:, start:end, :] + + return folded + + def xfade_and_unfold(self, y, target, overlap): + + ''' Applies a crossfade and unfolds into a 1d array. + + Args: + y (ndarry) : Batched sequences of audio samples + shape=(num_folds, target + 2 * overlap) + dtype=np.float64 + overlap (int) : Timesteps for both xfade and rnn warmup + + Return: + (ndarry) : audio samples in a 1d array + shape=(total_len) + dtype=np.float64 + + Details: + y = [[seq1], + [seq2], + [seq3]] + + Apply a gain envelope at both ends of the sequences + + y = [[seq1_in, seq1_target, seq1_out], + [seq2_in, seq2_target, seq2_out], + [seq3_in, seq3_target, seq3_out]] + + Stagger and add up the groups of samples: + + [seq1_in, seq1_target, (seq1_out + seq2_in), seq2_target, ...] + + ''' + + num_folds, length = y.shape + target = length - 2 * overlap + total_len = num_folds * (target + overlap) + overlap + + # Need some silence for the rnn warmup + silence_len = overlap // 2 + fade_len = overlap - silence_len + silence = np.zeros((silence_len), dtype=np.float64) + + # Equal power crossfade + t = np.linspace(-1, 1, fade_len, dtype=np.float64) + fade_in = np.sqrt(0.5 * (1 + t)) + fade_out = np.sqrt(0.5 * (1 - t)) + + # Concat the silence to the fades + fade_in = np.concatenate([silence, fade_in]) + fade_out = np.concatenate([fade_out, silence]) + + # Apply the gain to the overlap samples + y[:, :overlap] *= fade_in + y[:, -overlap:] *= fade_out + + unfolded = np.zeros((total_len), dtype=np.float64) + + # Loop to add up all the samples + for i in range(num_folds): + start = i * (target + overlap) + end = start + target + 2 * overlap + unfolded[start:end] += y[i] + + return unfolded + + def get_step(self) : + return self.step.data.item() + + def checkpoint(self, model_dir, optimizer) : + k_steps = self.get_step() // 1000 + self.save(model_dir.joinpath("checkpoint_%dk_steps.pt" % k_steps), optimizer) + + def log(self, path, msg) : + with open(path, 'a') as f: + print(msg, file=f) + + def load(self, path, optimizer) : + checkpoint = torch.load(path) + if "optimizer_state" in checkpoint: + self.load_state_dict(checkpoint["model_state"]) + optimizer.load_state_dict(checkpoint["optimizer_state"]) + else: + # Backwards compatibility + self.load_state_dict(checkpoint) + + def save(self, path, optimizer) : + torch.save({ + "model_state": self.state_dict(), + "optimizer_state": optimizer.state_dict(), + }, path) + + def num_params(self, print_out=True): + parameters = filter(lambda p: p.requires_grad, self.parameters()) + parameters = sum([np.prod(p.size()) for p in parameters]) / 1_000_000 + if print_out : + print('Trainable Parameters: %.3fM' % parameters) diff --git a/gen_wavernn.py b/gen_wavernn.py new file mode 100644 index 0000000000000000000000000000000000000000..2036737f805f6055893812e48f99d524624aab07 --- /dev/null +++ b/gen_wavernn.py @@ -0,0 +1,31 @@ +from vocoder.models.fatchord_version import WaveRNN +from vocoder.audio import * + + +def gen_testset(model: WaveRNN, test_set, samples, batched, target, overlap, save_path): + k = model.get_step() // 1000 + + for i, (m, x) in enumerate(test_set, 1): + if i > samples: + break + + print('\n| Generating: %i/%i' % (i, samples)) + + x = x[0].numpy() + + bits = 16 if hp.voc_mode == 'MOL' else hp.bits + + if hp.mu_law and hp.voc_mode != 'MOL' : + x = decode_mu_law(x, 2**bits, from_labels=True) + else : + x = label_2_float(x, bits) + + save_wav(x, save_path.joinpath("%dk_steps_%d_target.wav" % (k, i))) + + batch_str = "gen_batched_target%d_overlap%d" % (target, overlap) if batched else \ + "gen_not_batched" + save_str = save_path.joinpath("%dk_steps_%d_%s.wav" % (k, i, batch_str)) + + wav = model.generate(m, batched, target, overlap, hp.mu_law) + save_wav(wav, save_str) + diff --git a/hparams.py b/hparams.py new file mode 100644 index 0000000000000000000000000000000000000000..c1de9f7dcc2926735b80a28ed1226ff1b5824753 --- /dev/null +++ b/hparams.py @@ -0,0 +1,44 @@ +from synthesizer.hparams import hparams as _syn_hp + + +# Audio settings------------------------------------------------------------------------ +# Match the values of the synthesizer +sample_rate = _syn_hp.sample_rate +n_fft = _syn_hp.n_fft +num_mels = _syn_hp.num_mels +hop_length = _syn_hp.hop_size +win_length = _syn_hp.win_size +fmin = _syn_hp.fmin +min_level_db = _syn_hp.min_level_db +ref_level_db = _syn_hp.ref_level_db +mel_max_abs_value = _syn_hp.max_abs_value +preemphasis = _syn_hp.preemphasis +apply_preemphasis = _syn_hp.preemphasize + +bits = 9 # bit depth of signal +mu_law = True # Recommended to suppress noise if using raw bits in hp.voc_mode + # below + + +# WAVERNN / VOCODER -------------------------------------------------------------------------------- +voc_mode = 'RAW' # either 'RAW' (softmax on raw bits) or 'MOL' (sample from +# mixture of logistics) +voc_upsample_factors = (5, 5, 8) # NB - this needs to correctly factorise hop_length +voc_rnn_dims = 512 +voc_fc_dims = 512 +voc_compute_dims = 128 +voc_res_out_dims = 128 +voc_res_blocks = 10 + +# Training +voc_batch_size = 100 +voc_lr = 1e-4 +voc_gen_at_checkpoint = 5 # number of samples to generate at each checkpoint +voc_pad = 2 # this will pad the input so that the resnet can 'see' wider + # than input length +voc_seq_len = hop_length * 5 # must be a multiple of hop_length + +# Generating / Synthesizing +voc_gen_batched = True # very fast (realtime+) single utterance batched generation +voc_target = 8000 # target number of samples to be generated in each batch entry +voc_overlap = 400 # number of samples for crossfading between batches diff --git a/inference.py b/inference.py new file mode 100644 index 0000000000000000000000000000000000000000..7e546845da0b8cdb18b34fbd332b9aaa39cea55c --- /dev/null +++ b/inference.py @@ -0,0 +1,64 @@ +from vocoder.models.fatchord_version import WaveRNN +from vocoder import hparams as hp +import torch + + +_model = None # type: WaveRNN + +def load_model(weights_fpath, verbose=True): + global _model, _device + + if verbose: + print("Building Wave-RNN") + _model = WaveRNN( + rnn_dims=hp.voc_rnn_dims, + fc_dims=hp.voc_fc_dims, + bits=hp.bits, + pad=hp.voc_pad, + upsample_factors=hp.voc_upsample_factors, + feat_dims=hp.num_mels, + compute_dims=hp.voc_compute_dims, + res_out_dims=hp.voc_res_out_dims, + res_blocks=hp.voc_res_blocks, + hop_length=hp.hop_length, + sample_rate=hp.sample_rate, + mode=hp.voc_mode + ) + + if torch.cuda.is_available(): + _model = _model.cuda() + _device = torch.device('cuda') + else: + _device = torch.device('cpu') + + if verbose: + print("Loading model weights at %s" % weights_fpath) + checkpoint = torch.load(weights_fpath, _device) + _model.load_state_dict(checkpoint['model_state']) + _model.eval() + + +def is_loaded(): + return _model is not None + + +def infer_waveform(mel, normalize=True, batched=True, target=8000, overlap=800, + progress_callback=None): + """ + Infers the waveform of a mel spectrogram output by the synthesizer (the format must match + that of the synthesizer!) + + :param normalize: + :param batched: + :param target: + :param overlap: + :return: + """ + if _model is None: + raise Exception("Please load Wave-RNN in memory before using it") + + if normalize: + mel = mel / hp.mel_max_abs_value + mel = torch.from_numpy(mel[None, ...]) + wav = _model.generate(mel, batched, target, overlap, hp.mu_law, progress_callback) + return wav diff --git a/ko_dictionary.py b/ko_dictionary.py new file mode 100644 index 0000000000000000000000000000000000000000..508ab10c192e942ae892de7818ecf09886d51ed1 --- /dev/null +++ b/ko_dictionary.py @@ -0,0 +1,174 @@ +# coding: utf-8 + +etc_dictionary = { + '2 30대': '이삼십대', + '20~30대': '이삼십대', + '20, 30대': '이십대 삼십대', + '1+1': '원플러스원', + '3에서 6개월인': '3개월에서 육개월인', +} + +english_dictionary = { + 'Devsisters': '데브시스터즈', + 'track': '트랙', + + # krbook + 'LA': '엘에이', + 'LG': '엘지', + 'KOREA': '코리아', + 'JSA': '제이에스에이', + 'PGA': '피지에이', + 'GA': '지에이', + 'idol': '아이돌', + 'KTX': '케이티엑스', + 'AC': '에이씨', + 'DVD': '디비디', + 'US': '유에스', + 'CNN': '씨엔엔', + 'LPGA': '엘피지에이', + 'P': '피', + 'L': '엘', + 'T': '티', + 'B': '비', + 'C': '씨', + 'BIFF': '비아이에프에프', + 'GV': '지비', + + # JTBC + 'IT': '아이티', + 'IQ': '아이큐', + 'JTBC': '제이티비씨', + 'trickle down effect': '트리클 다운 이펙트', + 'trickle up effect': '트리클 업 이펙트', + 'down': '다운', + 'up': '업', + 'FCK': '에프씨케이', + 'AP': '에이피', + 'WHERETHEWILDTHINGSARE': '', + 'Rashomon Effect': '', + 'O': '오', + 'OO': '오오', + 'B': '비', + 'GDP': '지디피', + 'CIPA': '씨아이피에이', + 'YS': '와이에스', + 'Y': '와이', + 'S': '에스', + 'JTBC': '제이티비씨', + 'PC': '피씨', + 'bill': '빌', + 'Halmuny': '하모니', ##### + 'X': '엑스', + 'SNS': '에스엔에스', + 'ability': '어빌리티', + 'shy': '', + 'CCTV': '씨씨티비', + 'IT': '아이티', + 'the tenth man': '더 텐쓰 맨', #### + 'L': '엘', + 'PC': '피씨', + 'YSDJJPMB': '', ######## + 'Content Attitude Timing': '컨텐트 애티튜드 타이밍', + 'CAT': '캣', + 'IS': '아이에스', + 'SNS': '에스엔에스', + 'K': '케이', + 'Y': '와이', + 'KDI': '케이디아이', + 'DOC': '디오씨', + 'CIA': '씨아이에이', + 'PBS': '피비에스', + 'D': '디', + 'PPropertyPositionPowerPrisonP' + 'S': '에스', + 'francisco': '프란시스코', + 'I': '아이', + 'III': '아이아이', ###### + 'No joke': '노 조크', + 'BBK': '비비케이', + 'LA': '엘에이', + 'Don': '', + 't worry be happy': ' 워리 비 해피', + 'NO': '엔오', ##### + 'it was our sky': '잇 워즈 아워 스카이', + 'it is our sky': '잇 이즈 아워 스카이', #### + 'NEIS': '엔이아이에스', ##### + 'IMF': '아이엠에프', + 'apology': '어폴로지', + 'humble': '험블', + 'M': '엠', + 'Nowhere Man': '노웨어 맨', + 'The Tenth Man': '더 텐쓰 맨', + 'PBS': '피비에스', + 'BBC': '비비씨', + 'MRJ': '엠알제이', + 'CCTV': '씨씨티비', + 'Pick me up': '픽 미 업', + 'DNA': '디엔에이', + 'UN': '유엔', + 'STOP': '스탑', ##### + 'PRESS': '프레스', ##### + 'not to be': '낫 투비', + 'Denial': '디나이얼', + 'G': '지', + 'IMF': '아이엠에프', + 'GDP': '지디피', + 'JTBC': '제이티비씨', + 'Time flies like an arrow': '타임 플라이즈 라이크 언 애로우', + 'DDT': '디디티', + 'AI': '에이아이', + 'Z': '제트', + 'OECD': '오이씨디', + 'N': '앤', + 'A': '에이', + 'MB': '엠비', + 'EH': '이에이치', + 'IS': '아이에스', + 'TV': '티비', + 'MIT': '엠아이티', + 'KBO': '케이비오', + 'I love America': '아이 러브 아메리카', + 'SF': '에스에프', + 'Q': '큐', + 'KFX': '케이에프엑스', + 'PM': '피엠', + 'Prime Minister': '프라임 미니스터', + 'Swordline': '스워드라인', + 'TBS': '티비에스', + 'DDT': '디디티', + 'CS': '씨에스', + 'Reflecting Absence': '리플렉팅 앱센스', + 'PBS': '피비에스', + 'Drum being beaten by everyone': '드럼 빙 비튼 바이 에브리원', + 'negative pressure': '네거티브 프레셔', + 'F': '에프', + 'KIA': '기아', + 'FTA': '에프티에이', + 'Que sais-je': '', + 'UFC': '유에프씨', + 'P': '피', + 'DJ': '디제이', + 'Chaebol': '채벌', + 'BBC': '비비씨', + 'OECD': '오이씨디', + 'BC': '삐씨', + 'C': '씨', + 'B': '씨', + 'KY': '케이와이', + 'K': '케이', + 'CEO': '씨이오', + 'YH': '와이에치', + 'IS': '아이에스', + 'who are you': '후 얼 유', + 'Y': '와이', + 'The Devils Advocate': '더 데빌즈 어드보카트', + 'YS': '와이에스', + 'so sorry': '쏘 쏘리', + 'Santa': '산타', + 'Big Endian': '빅 엔디안', + 'Small Endian': '스몰 엔디안', + 'Oh Captain My Captain': '오 캡틴 마이 캡틴', + 'AIB': '에이아이비', + 'K': '케이', + 'PBS': '피비에스', +} diff --git a/korean.py b/korean.py new file mode 100644 index 0000000000000000000000000000000000000000..1424f8d0b73672cb788569115fab1a76253c02dd --- /dev/null +++ b/korean.py @@ -0,0 +1,349 @@ +# coding: utf-8 +# Code based on + +import re +import os +import ast +import json +from jamo import hangul_to_jamo, h2j, j2h + +from .ko_dictionary import english_dictionary, etc_dictionary + +PAD = '_' +EOS = '~' +PUNC = '!\'(),-.:;?' +SPACE = ' ' + +JAMO_LEADS = "".join([chr(_) for _ in range(0x1100, 0x1113)]) +JAMO_VOWELS = "".join([chr(_) for _ in range(0x1161, 0x1176)]) +JAMO_TAILS = "".join([chr(_) for _ in range(0x11A8, 0x11C3)]) + +VALID_CHARS = JAMO_LEADS + JAMO_VOWELS + JAMO_TAILS + PUNC + SPACE +ALL_SYMBOLS = PAD + EOS + VALID_CHARS + +char_to_id = {c: i for i, c in enumerate(ALL_SYMBOLS)} +id_to_char = {i: c for i, c in enumerate(ALL_SYMBOLS)} + +quote_checker = """([`"'"“‘])(.+?)([`"'"”’])""" + + +def is_lead(char): + return char in JAMO_LEADS + + +def is_vowel(char): + return char in JAMO_VOWELS + + +def is_tail(char): + return char in JAMO_TAILS + + +def get_mode(char): + if is_lead(char): + return 0 + elif is_vowel(char): + return 1 + elif is_tail(char): + return 2 + else: + return -1 + + +def _get_text_from_candidates(candidates): + if len(candidates) == 0: + return "" + elif len(candidates) == 1: + return _jamo_char_to_hcj(candidates[0]) + else: + return j2h(**dict(zip(["lead", "vowel", "tail"], candidates))) + + +def jamo_to_korean(text): + text = h2j(text) + + idx = 0 + new_text = "" + candidates = [] + + while True: + if idx >= len(text): + new_text += _get_text_from_candidates(candidates) + break + + char = text[idx] + mode = get_mode(char) + + if mode == 0: + new_text += _get_text_from_candidates(candidates) + candidates = [char] + elif mode == -1: + new_text += _get_text_from_candidates(candidates) + new_text += char + candidates = [] + else: + candidates.append(char) + + idx += 1 + return new_text + + +num_to_kor = { + '0': '영', + '1': '일', + '2': '이', + '3': '삼', + '4': '사', + '5': '오', + '6': '육', + '7': '칠', + '8': '팔', + '9': '구', +} + +unit_to_kor1 = { + '%': '퍼센트', + 'cm': '센치미터', + 'mm': '밀리미터', + 'km': '킬로미터', + 'kg': '킬로그람', +} +unit_to_kor2 = { + 'm': '미터', +} + +upper_to_kor = { + 'A': '에이', + 'B': '비', + 'C': '씨', + 'D': '디', + 'E': '이', + 'F': '에프', + 'G': '지', + 'H': '에이치', + 'I': '아이', + 'J': '제이', + 'K': '케이', + 'L': '엘', + 'M': '엠', + 'N': '엔', + 'O': '오', + 'P': '피', + 'Q': '큐', + 'R': '알', + 'S': '에스', + 'T': '티', + 'U': '유', + 'V': '브이', + 'W': '더블유', + 'X': '엑스', + 'Y': '와이', + 'Z': '지', +} + + +def compare_sentence_with_jamo(text1, text2): + return h2j(text1) != h2j(text2) + + +def tokenize(text, as_id=False): + # jamo package에 있는 hangul_to_jamo를 이용하여 한글 string을 초성/중성/종성으로 나눈다. + text = normalize(text) + tokens = list(hangul_to_jamo(text)) # '존경하는' --> ['ᄌ', 'ᅩ', 'ᆫ', 'ᄀ', 'ᅧ', 'ᆼ', 'ᄒ', 'ᅡ', 'ᄂ', 'ᅳ', 'ᆫ', '~'] + + if as_id: + return [char_to_id[token] for token in tokens] + [char_to_id[EOS]] + else: + return [token for token in tokens] + [EOS] + + +def tokenizer_fn(iterator): + return (token for x in iterator for token in tokenize(x, as_id=False)) + + +def normalize(text): + text = text.strip() + + text = re.sub('\(\d+일\)', '', text) + text = re.sub('\([⺀-⺙⺛-⻳⼀-⿕々〇〡-〩〸-〺〻㐀-䶵一-鿃豈-鶴侮-頻並-龎]+\)', '', text) + + text = normalize_with_dictionary(text, etc_dictionary) + text = normalize_english(text) + text = re.sub('[a-zA-Z]+', normalize_upper, text) + + text = normalize_quote(text) + text = normalize_number(text) + + return text + + +def normalize_with_dictionary(text, dic): + if any(key in text for key in dic.keys()): + pattern = re.compile('|'.join(re.escape(key) for key in dic.keys())) + return pattern.sub(lambda x: dic[x.group()], text) + else: + return text + + +def normalize_english(text): + def fn(m): + word = m.group() + if word in english_dictionary: + return english_dictionary.get(word) + else: + return word + + text = re.sub("([A-Za-z]+)", fn, text) + return text + + +def normalize_upper(text): + text = text.group(0) + + if all([char.isupper() for char in text]): + return "".join(upper_to_kor[char] for char in text) + else: + return text + + +def normalize_quote(text): + def fn(found_text): + from nltk import sent_tokenize # NLTK doesn't along with multiprocessing + + found_text = found_text.group() + unquoted_text = found_text[1:-1] + + sentences = sent_tokenize(unquoted_text) + return " ".join(["'{}'".format(sent) for sent in sentences]) + + return re.sub(quote_checker, fn, text) + + +number_checker = "([+-]?\d[\d,]*)[\.]?\d*" +count_checker = "(시|명|가지|살|마리|포기|송이|수|톨|통|점|개|벌|척|채|다발|그루|자루|줄|켤레|그릇|잔|마디|상자|사람|곡|병|판)" + + +def normalize_number(text): + text = normalize_with_dictionary(text, unit_to_kor1) + text = normalize_with_dictionary(text, unit_to_kor2) + text = re.sub(number_checker + count_checker, + lambda x: number_to_korean(x, True), text) + text = re.sub(number_checker, + lambda x: number_to_korean(x, False), text) + return text + + +num_to_kor1 = [""] + list("일이삼사오육칠팔구") +num_to_kor2 = [""] + list("만억조경해") +num_to_kor3 = [""] + list("십백천") + +# count_to_kor1 = [""] + ["하나","둘","셋","넷","다섯","여섯","일곱","여덟","아홉"] +count_to_kor1 = [""] + ["한", "두", "세", "네", "다섯", "여섯", "일곱", "여덟", "아홉"] + +count_tenth_dict = { + "십": "열", + "두십": "스물", + "세십": "서른", + "네십": "마흔", + "다섯십": "쉰", + "여섯십": "예순", + "일곱십": "일흔", + "여덟십": "여든", + "아홉십": "아흔", +} + + +def number_to_korean(num_str, is_count=False): + if is_count: + num_str, unit_str = num_str.group(1), num_str.group(2) + else: + num_str, unit_str = num_str.group(), "" + + num_str = num_str.replace(',', '') + # print("before ast : ", num_str, "dtype : ",type(num_str)) + + try: + num = ast.literal_eval(num_str) + # print("After ast :", num,"dtype : ",type(num)) + except Exception: + num_str = re.sub('^0+', '', num_str) + num = ast.literal_eval(num_str) + + if num == 0: + return "영" + + check_float = num_str.split('.') + if len(check_float) == 2: + digit_str, float_str = check_float + elif len(check_float) >= 3: + raise Exception(" [!] Wrong number format") + else: + digit_str, float_str = check_float[0], None + + if is_count and float_str is not None: + raise Exception(" [!] `is_count` and float number does not fit each other") + + digit = int(digit_str) + + if digit_str.startswith("-"): + digit, digit_str = abs(digit), str(abs(digit)) + + kor = "" + size = len(str(digit)) + tmp = [] + + for i, v in enumerate(digit_str, start=1): + v = int(v) + + if v != 0: + if is_count: + tmp += count_to_kor1[v] + else: + tmp += num_to_kor1[v] + + tmp += num_to_kor3[(size - i) % 4] + + if (size - i) % 4 == 0 and len(tmp) != 0: + kor += "".join(tmp) + tmp = [] + kor += num_to_kor2[int((size - i) / 4)] + + if is_count: + if kor.startswith("한") and len(kor) > 1: + kor = kor[1:] + + if any(word in kor for word in count_tenth_dict): + kor = re.sub( + '|'.join(count_tenth_dict.keys()), + lambda x: count_tenth_dict[x.group()], kor) + + if not is_count and kor.startswith("일") and len(kor) > 1: + kor = kor[1:] + + if float_str is not None: + kor += "쩜 " + kor += re.sub('\d', lambda x: num_to_kor[x.group()], float_str) + + if num_str.startswith("+"): + kor = "플러스 " + kor + elif num_str.startswith("-"): + kor = "마이너스 " + kor + + return kor + unit_str + + +if __name__ == "__main__": + def test_normalize(text): + print(text) + print(normalize(text)) + print("=" * 30) + + + test_normalize("JTBC는 JTBCs를 DY는 A가 Absolute") + test_normalize("오늘(13일) 3,600마리 강아지가") + test_normalize("60.3%") + test_normalize('"저돌"(猪突) 입니다.') + test_normalize('비대위원장이 지난 1월 이런 말을 했습니다. “난 그냥 산돼지처럼 돌파하는 스타일이다”') + test_normalize("지금은 -12.35%였고 종류는 5가지와 19가지, 그리고 55가지였다") + test_normalize("JTBC는 TH와 K 양이 2017년 9월 12일 오후 12시에 24살이 된다") + print(list(hangul_to_jamo(list(hangul_to_jamo('비대위원장이 지난 1월 이런 말을 했습니다? “난 그냥 산돼지처럼 돌파하는 스타일이다”'))))) \ No newline at end of file diff --git a/logmmse.py b/logmmse.py new file mode 100644 index 0000000000000000000000000000000000000000..58cc4502fa5ba0670678c3edaf5ba1587b8b58ea --- /dev/null +++ b/logmmse.py @@ -0,0 +1,247 @@ +# The MIT License (MIT) +# +# Copyright (c) 2015 braindead +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +# +# This code was extracted from the logmmse package (https://pypi.org/project/logmmse/) and I +# simply modified the interface to meet my needs. + + +import numpy as np +import math +from scipy.special import expn +from collections import namedtuple + +NoiseProfile = namedtuple("NoiseProfile", "sampling_rate window_size len1 len2 win n_fft noise_mu2") + + +def profile_noise(noise, sampling_rate, window_size=0): + """ + Creates a profile of the noise in a given waveform. + + :param noise: a waveform containing noise ONLY, as a numpy array of floats or ints. + :param sampling_rate: the sampling rate of the audio + :param window_size: the size of the window the logmmse algorithm operates on. A default value + will be picked if left as 0. + :return: a NoiseProfile object + """ + noise, dtype = to_float(noise) + noise += np.finfo(np.float64).eps + + if window_size == 0: + window_size = int(math.floor(0.02 * sampling_rate)) + + if window_size % 2 == 1: + window_size = window_size + 1 + + perc = 50 + len1 = int(math.floor(window_size * perc / 100)) + len2 = int(window_size - len1) + + win = np.hanning(window_size) + win = win * len2 / np.sum(win) + n_fft = 2 * window_size + + noise_mean = np.zeros(n_fft) + n_frames = len(noise) // window_size + for j in range(0, window_size * n_frames, window_size): + noise_mean += np.absolute(np.fft.fft(win * noise[j:j + window_size], n_fft, axis=0)) + noise_mu2 = (noise_mean / n_frames) ** 2 + + return NoiseProfile(sampling_rate, window_size, len1, len2, win, n_fft, noise_mu2) + + +def denoise(wav, noise_profile: NoiseProfile, eta=0.15): + """ + Cleans the noise from a speech waveform given a noise profile. The waveform must have the + same sampling rate as the one used to create the noise profile. + + :param wav: a speech waveform as a numpy array of floats or ints. + :param noise_profile: a NoiseProfile object that was created from a similar (or a segment of + the same) waveform. + :param eta: voice threshold for noise update. While the voice activation detection value is + below this threshold, the noise profile will be continuously updated throughout the audio. + Set to 0 to disable updating the noise profile. + :return: the clean wav as a numpy array of floats or ints of the same length. + """ + wav, dtype = to_float(wav) + wav += np.finfo(np.float64).eps + p = noise_profile + + nframes = int(math.floor(len(wav) / p.len2) - math.floor(p.window_size / p.len2)) + x_final = np.zeros(nframes * p.len2) + + aa = 0.98 + mu = 0.98 + ksi_min = 10 ** (-25 / 10) + + x_old = np.zeros(p.len1) + xk_prev = np.zeros(p.len1) + noise_mu2 = p.noise_mu2 + for k in range(0, nframes * p.len2, p.len2): + insign = p.win * wav[k:k + p.window_size] + + spec = np.fft.fft(insign, p.n_fft, axis=0) + sig = np.absolute(spec) + sig2 = sig ** 2 + + gammak = np.minimum(sig2 / noise_mu2, 40) + + if xk_prev.all() == 0: + ksi = aa + (1 - aa) * np.maximum(gammak - 1, 0) + else: + ksi = aa * xk_prev / noise_mu2 + (1 - aa) * np.maximum(gammak - 1, 0) + ksi = np.maximum(ksi_min, ksi) + + log_sigma_k = gammak * ksi/(1 + ksi) - np.log(1 + ksi) + vad_decision = np.sum(log_sigma_k) / p.window_size + if vad_decision < eta: + noise_mu2 = mu * noise_mu2 + (1 - mu) * sig2 + + a = ksi / (1 + ksi) + vk = a * gammak + ei_vk = 0.5 * expn(1, np.maximum(vk, 1e-8)) + hw = a * np.exp(ei_vk) + sig = sig * hw + xk_prev = sig ** 2 + xi_w = np.fft.ifft(hw * spec, p.n_fft, axis=0) + xi_w = np.real(xi_w) + + x_final[k:k + p.len2] = x_old + xi_w[0:p.len1] + x_old = xi_w[p.len1:p.window_size] + + output = from_float(x_final, dtype) + output = np.pad(output, (0, len(wav) - len(output)), mode="constant") + return output + + +## Alternative VAD algorithm to webrctvad. It has the advantage of not requiring to install that +## darn package and it also works for any sampling rate. Maybe I'll eventually use it instead of +## webrctvad +# def vad(wav, sampling_rate, eta=0.15, window_size=0): +# """ +# TODO: fix doc +# Creates a profile of the noise in a given waveform. +# +# :param wav: a waveform containing noise ONLY, as a numpy array of floats or ints. +# :param sampling_rate: the sampling rate of the audio +# :param window_size: the size of the window the logmmse algorithm operates on. A default value +# will be picked if left as 0. +# :param eta: voice threshold for noise update. While the voice activation detection value is +# below this threshold, the noise profile will be continuously updated throughout the audio. +# Set to 0 to disable updating the noise profile. +# """ +# wav, dtype = to_float(wav) +# wav += np.finfo(np.float64).eps +# +# if window_size == 0: +# window_size = int(math.floor(0.02 * sampling_rate)) +# +# if window_size % 2 == 1: +# window_size = window_size + 1 +# +# perc = 50 +# len1 = int(math.floor(window_size * perc / 100)) +# len2 = int(window_size - len1) +# +# win = np.hanning(window_size) +# win = win * len2 / np.sum(win) +# n_fft = 2 * window_size +# +# wav_mean = np.zeros(n_fft) +# n_frames = len(wav) // window_size +# for j in range(0, window_size * n_frames, window_size): +# wav_mean += np.absolute(np.fft.fft(win * wav[j:j + window_size], n_fft, axis=0)) +# noise_mu2 = (wav_mean / n_frames) ** 2 +# +# wav, dtype = to_float(wav) +# wav += np.finfo(np.float64).eps +# +# nframes = int(math.floor(len(wav) / len2) - math.floor(window_size / len2)) +# vad = np.zeros(nframes * len2, dtype=np.bool) +# +# aa = 0.98 +# mu = 0.98 +# ksi_min = 10 ** (-25 / 10) +# +# xk_prev = np.zeros(len1) +# noise_mu2 = noise_mu2 +# for k in range(0, nframes * len2, len2): +# insign = win * wav[k:k + window_size] +# +# spec = np.fft.fft(insign, n_fft, axis=0) +# sig = np.absolute(spec) +# sig2 = sig ** 2 +# +# gammak = np.minimum(sig2 / noise_mu2, 40) +# +# if xk_prev.all() == 0: +# ksi = aa + (1 - aa) * np.maximum(gammak - 1, 0) +# else: +# ksi = aa * xk_prev / noise_mu2 + (1 - aa) * np.maximum(gammak - 1, 0) +# ksi = np.maximum(ksi_min, ksi) +# +# log_sigma_k = gammak * ksi / (1 + ksi) - np.log(1 + ksi) +# vad_decision = np.sum(log_sigma_k) / window_size +# if vad_decision < eta: +# noise_mu2 = mu * noise_mu2 + (1 - mu) * sig2 +# print(vad_decision) +# +# a = ksi / (1 + ksi) +# vk = a * gammak +# ei_vk = 0.5 * expn(1, np.maximum(vk, 1e-8)) +# hw = a * np.exp(ei_vk) +# sig = sig * hw +# xk_prev = sig ** 2 +# +# vad[k:k + len2] = vad_decision >= eta +# +# vad = np.pad(vad, (0, len(wav) - len(vad)), mode="constant") +# return vad + + +def to_float(_input): + if _input.dtype == np.float64: + return _input, _input.dtype + elif _input.dtype == np.float32: + return _input.astype(np.float64), _input.dtype + elif _input.dtype == np.uint8: + return (_input - 128) / 128., _input.dtype + elif _input.dtype == np.int16: + return _input / 32768., _input.dtype + elif _input.dtype == np.int32: + return _input / 2147483648., _input.dtype + raise ValueError('Unsupported wave file format') + + +def from_float(_input, dtype): + if dtype == np.float64: + return _input, np.float64 + elif dtype == np.float32: + return _input.astype(np.float32) + elif dtype == np.uint8: + return ((_input * 128) + 128).astype(np.uint8) + elif dtype == np.int16: + return (_input * 32768).astype(np.int16) + elif dtype == np.int32: + print(_input) + return (_input * 2147483648).astype(np.int32) + raise ValueError('Unsupported wave file format') diff --git a/model.py b/model.py new file mode 100644 index 0000000000000000000000000000000000000000..e050d3204d8f1becdf0f8b3133470708e5420cea --- /dev/null +++ b/model.py @@ -0,0 +1,135 @@ +from encoder.params_model import * +from encoder.params_data import * +from scipy.interpolate import interp1d +from sklearn.metrics import roc_curve +from torch.nn.utils import clip_grad_norm_ +from scipy.optimize import brentq +from torch import nn +import numpy as np +import torch + + +class SpeakerEncoder(nn.Module): + def __init__(self, device, loss_device): + super().__init__() + self.loss_device = loss_device + + # Network defition + self.lstm = nn.LSTM(input_size=mel_n_channels, + hidden_size=model_hidden_size, + num_layers=model_num_layers, + batch_first=True).to(device) + self.linear = nn.Linear(in_features=model_hidden_size, + out_features=model_embedding_size).to(device) + self.relu = torch.nn.ReLU().to(device) + + # Cosine similarity scaling (with fixed initial parameter values) + self.similarity_weight = nn.Parameter(torch.tensor([10.])).to(loss_device) + self.similarity_bias = nn.Parameter(torch.tensor([-5.])).to(loss_device) + + # Loss + self.loss_fn = nn.CrossEntropyLoss().to(loss_device) + + def do_gradient_ops(self): + # Gradient scale + self.similarity_weight.grad *= 0.01 + self.similarity_bias.grad *= 0.01 + + # Gradient clipping + clip_grad_norm_(self.parameters(), 3, norm_type=2) + + def forward(self, utterances, hidden_init=None): + """ + Computes the embeddings of a batch of utterance spectrograms. + + :param utterances: batch of mel-scale filterbanks of same duration as a tensor of shape + (batch_size, n_frames, n_channels) + :param hidden_init: initial hidden state of the LSTM as a tensor of shape (num_layers, + batch_size, hidden_size). Will default to a tensor of zeros if None. + :return: the embeddings as a tensor of shape (batch_size, embedding_size) + """ + # Pass the input through the LSTM layers and retrieve all outputs, the final hidden state + # and the final cell state. + out, (hidden, cell) = self.lstm(utterances, hidden_init) + + # We take only the hidden state of the last layer + embeds_raw = self.relu(self.linear(hidden[-1])) + + # L2-normalize it + embeds = embeds_raw / (torch.norm(embeds_raw, dim=1, keepdim=True) + 1e-5) + + return embeds + + def similarity_matrix(self, embeds): + """ + Computes the similarity matrix according the section 2.1 of GE2E. + + :param embeds: the embeddings as a tensor of shape (speakers_per_batch, + utterances_per_speaker, embedding_size) + :return: the similarity matrix as a tensor of shape (speakers_per_batch, + utterances_per_speaker, speakers_per_batch) + """ + speakers_per_batch, utterances_per_speaker = embeds.shape[:2] + + # Inclusive centroids (1 per speaker). Cloning is needed for reverse differentiation + centroids_incl = torch.mean(embeds, dim=1, keepdim=True) + centroids_incl = centroids_incl.clone() / (torch.norm(centroids_incl, dim=2, keepdim=True) + 1e-5) + + # Exclusive centroids (1 per utterance) + centroids_excl = (torch.sum(embeds, dim=1, keepdim=True) - embeds) + centroids_excl /= (utterances_per_speaker - 1) + centroids_excl = centroids_excl.clone() / (torch.norm(centroids_excl, dim=2, keepdim=True) + 1e-5) + + # Similarity matrix. The cosine similarity of already 2-normed vectors is simply the dot + # product of these vectors (which is just an element-wise multiplication reduced by a sum). + # We vectorize the computation for efficiency. + sim_matrix = torch.zeros(speakers_per_batch, utterances_per_speaker, + speakers_per_batch).to(self.loss_device) + mask_matrix = 1 - np.eye(speakers_per_batch, dtype=np.int) + for j in range(speakers_per_batch): + mask = np.where(mask_matrix[j])[0] + sim_matrix[mask, :, j] = (embeds[mask] * centroids_incl[j]).sum(dim=2) + sim_matrix[j, :, j] = (embeds[j] * centroids_excl[j]).sum(dim=1) + + ## Even more vectorized version (slower maybe because of transpose) + # sim_matrix2 = torch.zeros(speakers_per_batch, speakers_per_batch, utterances_per_speaker + # ).to(self.loss_device) + # eye = np.eye(speakers_per_batch, dtype=np.int) + # mask = np.where(1 - eye) + # sim_matrix2[mask] = (embeds[mask[0]] * centroids_incl[mask[1]]).sum(dim=2) + # mask = np.where(eye) + # sim_matrix2[mask] = (embeds * centroids_excl).sum(dim=2) + # sim_matrix2 = sim_matrix2.transpose(1, 2) + + sim_matrix = sim_matrix * self.similarity_weight + self.similarity_bias + return sim_matrix + + def loss(self, embeds): + """ + Computes the softmax loss according the section 2.1 of GE2E. + + :param embeds: the embeddings as a tensor of shape (speakers_per_batch, + utterances_per_speaker, embedding_size) + :return: the loss and the EER for this batch of embeddings. + """ + speakers_per_batch, utterances_per_speaker = embeds.shape[:2] + + # Loss + sim_matrix = self.similarity_matrix(embeds) + sim_matrix = sim_matrix.reshape((speakers_per_batch * utterances_per_speaker, + speakers_per_batch)) + ground_truth = np.repeat(np.arange(speakers_per_batch), utterances_per_speaker) + target = torch.from_numpy(ground_truth).long().to(self.loss_device) + loss = self.loss_fn(sim_matrix, target) + + # EER (not backpropagated) + with torch.no_grad(): + inv_argmax = lambda i: np.eye(1, speakers_per_batch, i, dtype=np.int)[0] + labels = np.array([inv_argmax(i) for i in ground_truth]) + preds = sim_matrix.detach().cpu().numpy() + + # Snippet from https://yangcha.github.io/EER-ROC/ + fpr, tpr, thresholds = roc_curve(labels.flatten(), preds.flatten()) + eer = brentq(lambda x: 1. - x - interp1d(fpr, tpr)(x), 0., 1.) + + return loss, eer diff --git a/modelutils.py b/modelutils.py new file mode 100644 index 0000000000000000000000000000000000000000..6acaa984e0c7876f9149fc1ff99001b7761dc80b --- /dev/null +++ b/modelutils.py @@ -0,0 +1,17 @@ +from pathlib import Path + +def check_model_paths(encoder_path: Path, synthesizer_path: Path, vocoder_path: Path): + # This function tests the model paths and makes sure at least one is valid. + if encoder_path.is_file() or encoder_path.is_dir(): + return + if synthesizer_path.is_file() or synthesizer_path.is_dir(): + return + if vocoder_path.is_file() or vocoder_path.is_dir(): + return + + # If none of the paths exist, remind the user to download models if needed + print("********************************************************************************") + print("Error: Model files not found. Follow these instructions to get and install the models:") + print("https://github.com/CorentinJ/Real-Time-Voice-Cloning/wiki/Pretrained-models") + print("********************************************************************************\n") + quit(-1) diff --git a/numbers.py b/numbers.py new file mode 100644 index 0000000000000000000000000000000000000000..75020a0bd732830f603d7c7d250c9e087033cc24 --- /dev/null +++ b/numbers.py @@ -0,0 +1,68 @@ +import re +import inflect + +_inflect = inflect.engine() +_comma_number_re = re.compile(r"([0-9][0-9\,]+[0-9])") +_decimal_number_re = re.compile(r"([0-9]+\.[0-9]+)") +_pounds_re = re.compile(r"£([0-9\,]*[0-9]+)") +_dollars_re = re.compile(r"\$([0-9\.\,]*[0-9]+)") +_ordinal_re = re.compile(r"[0-9]+(st|nd|rd|th)") +_number_re = re.compile(r"[0-9]+") + + +def _remove_commas(m): + return m.group(1).replace(",", "") + + +def _expand_decimal_point(m): + return m.group(1).replace(".", " point ") + + +def _expand_dollars(m): + match = m.group(1) + parts = match.split(".") + if len(parts) > 2: + return match + " dollars" # Unexpected format + dollars = int(parts[0]) if parts[0] else 0 + cents = int(parts[1]) if len(parts) > 1 and parts[1] else 0 + if dollars and cents: + dollar_unit = "dollar" if dollars == 1 else "dollars" + cent_unit = "cent" if cents == 1 else "cents" + return "%s %s, %s %s" % (dollars, dollar_unit, cents, cent_unit) + elif dollars: + dollar_unit = "dollar" if dollars == 1 else "dollars" + return "%s %s" % (dollars, dollar_unit) + elif cents: + cent_unit = "cent" if cents == 1 else "cents" + return "%s %s" % (cents, cent_unit) + else: + return "zero dollars" + + +def _expand_ordinal(m): + return _inflect.number_to_words(m.group(0)) + + +def _expand_number(m): + num = int(m.group(0)) + if num > 1000 and num < 3000: + if num == 2000: + return "two thousand" + elif num > 2000 and num < 2010: + return "two thousand " + _inflect.number_to_words(num % 100) + elif num % 100 == 0: + return _inflect.number_to_words(num // 100) + " hundred" + else: + return _inflect.number_to_words(num, andword="", zero="oh", group=2).replace(", ", " ") + else: + return _inflect.number_to_words(num, andword="") + + +def normalize_numbers(text): + text = re.sub(_comma_number_re, _remove_commas, text) + text = re.sub(_pounds_re, r"\1 pounds", text) + text = re.sub(_dollars_re, _expand_dollars, text) + text = re.sub(_decimal_number_re, _expand_decimal_point, text) + text = re.sub(_ordinal_re, _expand_ordinal, text) + text = re.sub(_number_re, _expand_number, text) + return text diff --git a/p240_00000.mp3 b/p240_00000.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..4787405c20679691dd8ae77c5f5d50520e55eebd Binary files /dev/null and b/p240_00000.mp3 differ diff --git a/p260_00000.mp3 b/p260_00000.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..ff5f5032e9c2aabb6a825a7c0a9eb1c654cceb27 Binary files /dev/null and b/p260_00000.mp3 differ diff --git a/params_data.py b/params_data.py new file mode 100644 index 0000000000000000000000000000000000000000..bdb1716ed45617f2b127a7fb8885afe6cc74fb71 --- /dev/null +++ b/params_data.py @@ -0,0 +1,29 @@ + +## Mel-filterbank +mel_window_length = 25 # In milliseconds +mel_window_step = 10 # In milliseconds +mel_n_channels = 40 + + +## Audio +sampling_rate = 16000 +# Number of spectrogram frames in a partial utterance +partials_n_frames = 160 # 1600 ms +# Number of spectrogram frames at inference +inference_n_frames = 80 # 800 ms + + +## Voice Activation Detection +# Window size of the VAD. Must be either 10, 20 or 30 milliseconds. +# This sets the granularity of the VAD. Should not need to be changed. +vad_window_length = 30 # In milliseconds +# Number of frames to average together when performing the moving average smoothing. +# The larger this value, the larger the VAD variations must be to not get smoothed out. +vad_moving_average_width = 8 +# Maximum number of consecutive silent frames a segment can have. +vad_max_silence_length = 6 + + +## Audio volume normalization +audio_norm_target_dBFS = -30 + diff --git a/params_model.py b/params_model.py new file mode 100644 index 0000000000000000000000000000000000000000..3e356472fb5a27f370cb3920976a11d12a76c1b7 --- /dev/null +++ b/params_model.py @@ -0,0 +1,11 @@ + +## Model parameters +model_hidden_size = 256 +model_embedding_size = 256 +model_num_layers = 3 + + +## Training parameters +learning_rate_init = 1e-4 +speakers_per_batch = 64 +utterances_per_speaker = 10 diff --git a/plot.py b/plot.py new file mode 100644 index 0000000000000000000000000000000000000000..f47d2713d4daa6cf387b37970fd879548abc8d88 --- /dev/null +++ b/plot.py @@ -0,0 +1,76 @@ +import matplotlib +matplotlib.use("Agg") +import matplotlib.pyplot as plt +import numpy as np + + +def split_title_line(title_text, max_words=5): + """ + A function that splits any string based on specific character + (returning it with the string), with maximum number of words on it + """ + seq = title_text.split() + return "\n".join([" ".join(seq[i:i + max_words]) for i in range(0, len(seq), max_words)]) + +def plot_alignment(alignment, path, title=None, split_title=False, max_len=None): + if max_len is not None: + alignment = alignment[:, :max_len] + + fig = plt.figure(figsize=(8, 6)) + ax = fig.add_subplot(111) + + im = ax.imshow( + alignment, + aspect="auto", + origin="lower", + interpolation="none") + fig.colorbar(im, ax=ax) + xlabel = "Decoder timestep" + + if split_title: + title = split_title_line(title) + + plt.xlabel(xlabel) + plt.title(title) + plt.ylabel("Encoder timestep") + plt.tight_layout() + plt.savefig(path, format="png") + plt.close() + + +def plot_spectrogram(pred_spectrogram, path, title=None, split_title=False, target_spectrogram=None, max_len=None, auto_aspect=False): + if max_len is not None: + target_spectrogram = target_spectrogram[:max_len] + pred_spectrogram = pred_spectrogram[:max_len] + + if split_title: + title = split_title_line(title) + + fig = plt.figure(figsize=(10, 8)) + # Set common labels + fig.text(0.5, 0.18, title, horizontalalignment="center", fontsize=16) + + #target spectrogram subplot + if target_spectrogram is not None: + ax1 = fig.add_subplot(311) + ax2 = fig.add_subplot(312) + + if auto_aspect: + im = ax1.imshow(np.rot90(target_spectrogram), aspect="auto", interpolation="none") + else: + im = ax1.imshow(np.rot90(target_spectrogram), interpolation="none") + ax1.set_title("Target Mel-Spectrogram") + fig.colorbar(mappable=im, shrink=0.65, orientation="horizontal", ax=ax1) + ax2.set_title("Predicted Mel-Spectrogram") + else: + ax2 = fig.add_subplot(211) + + if auto_aspect: + im = ax2.imshow(np.rot90(pred_spectrogram), aspect="auto", interpolation="none") + else: + im = ax2.imshow(np.rot90(pred_spectrogram), interpolation="none") + fig.colorbar(mappable=im, shrink=0.65, orientation="horizontal", ax=ax2) + + plt.tight_layout() + plt.savefig(path, format="png") + plt.close() diff --git a/preprocess.py b/preprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..e60a7717b2ae7b8864bcfd448ea181a09d328d8a --- /dev/null +++ b/preprocess.py @@ -0,0 +1,259 @@ +from multiprocessing.pool import Pool +from synthesizer import audio +from functools import partial +from itertools import chain +from encoder import inference as encoder +from pathlib import Path +from utils import logmmse +from tqdm import tqdm +import numpy as np +import librosa + + +def preprocess_dataset(datasets_root: Path, out_dir: Path, n_processes: int, + skip_existing: bool, hparams, no_alignments: bool, + datasets_name: str, subfolders: str): + # Gather the input directories + dataset_root = datasets_root.joinpath(datasets_name) + input_dirs = [dataset_root.joinpath(subfolder.strip()) for subfolder in subfolders.split(",")] + print("\n ".join(map(str, ["Using data from:"] + input_dirs))) + assert all(input_dir.exists() for input_dir in input_dirs) + + # Create the output directories for each output file type + out_dir.joinpath("mels").mkdir(exist_ok=True) + out_dir.joinpath("audio").mkdir(exist_ok=True) + + # Create a metadata file + metadata_fpath = out_dir.joinpath("train.txt") + metadata_file = metadata_fpath.open("a" if skip_existing else "w", encoding="cp949") + + # Preprocess the dataset + speaker_dirs = list(chain.from_iterable(input_dir.glob("*") for input_dir in input_dirs)) + func = partial(preprocess_speaker, out_dir=out_dir, skip_existing=skip_existing, + hparams=hparams, no_alignments=no_alignments) + job = Pool(n_processes).imap(func, speaker_dirs) + for speaker_metadata in tqdm(job, datasets_name, len(speaker_dirs), unit="speakers"): + for metadatum in speaker_metadata: + metadata_file.write("|".join(str(x) for x in metadatum) + "\n") + metadata_file.close() + + # Verify the contents of the metadata file + with metadata_fpath.open("r", encoding="cp949") as metadata_file: + metadata = [line.split("|") for line in metadata_file] + mel_frames = sum([int(m[4]) for m in metadata]) + timesteps = sum([int(m[3]) for m in metadata]) + sample_rate = hparams.sample_rate + hours = (timesteps / sample_rate) / 3600 + print("The dataset consists of %d utterances, %d mel frames, %d audio timesteps (%.2f hours)." % + (len(metadata), mel_frames, timesteps, hours)) + print("Max input length (text chars): %d" % max(len(m[5]) for m in metadata)) + print("Max mel frames length: %d" % max(int(m[4]) for m in metadata)) + print("Max audio timesteps length: %d" % max(int(m[3]) for m in metadata)) + + +def preprocess_speaker(speaker_dir, out_dir: Path, skip_existing: bool, hparams, no_alignments: bool): + metadata = [] + for book_dir in speaker_dir.glob("*"): + if no_alignments: + # Gather the utterance audios and texts + # LibriTTS uses .wav but we will include extensions for compatibility with other datasets + extensions = ["*.wav", "*.flac", "*.mp3"] + for extension in extensions: + wav_fpaths = book_dir.glob(extension) + + for wav_fpath in wav_fpaths: + # Load the audio waveform + wav, _ = librosa.load(str(wav_fpath), hparams.sample_rate) + if hparams.rescale: + wav = wav / np.abs(wav).max() * hparams.rescaling_max + + # Get the corresponding text + # Check for .txt (for compatibility with other datasets) + text_fpath = wav_fpath.with_suffix(".txt") + if not text_fpath.exists(): + # Check for .normalized.txt (LibriTTS) + text_fpath = wav_fpath.with_suffix(".normalized.txt") + assert text_fpath.exists() + with text_fpath.open("r") as text_file: + text = "".join([line for line in text_file]) + text = text.replace("\"", "") + text = text.strip() + + # Process the utterance + metadata.append(process_utterance(wav, text, out_dir, str(wav_fpath.with_suffix("").name), + skip_existing, hparams)) + else: + # Process alignment file (LibriSpeech support) + # Gather the utterance audios and texts + try: + alignments_fpath = next(book_dir.glob("*.alignment.txt")) + with alignments_fpath.open("r") as alignments_file: + alignments = [line.rstrip().split(" ") for line in alignments_file] + except StopIteration: + # A few alignment files will be missing + continue + + # Iterate over each entry in the alignments file + for wav_fname, words, end_times in alignments: + wav_fpath = book_dir.joinpath(wav_fname + ".flac") + assert wav_fpath.exists() + words = words.replace("\"", "").split(",") + end_times = list(map(float, end_times.replace("\"", "").split(","))) + + # Process each sub-utterance + wavs, texts = split_on_silences(wav_fpath, words, end_times, hparams) + for i, (wav, text) in enumerate(zip(wavs, texts)): + sub_basename = "%s_%02d" % (wav_fname, i) + metadata.append(process_utterance(wav, text, out_dir, sub_basename, + skip_existing, hparams)) + + return [m for m in metadata if m is not None] + + +def split_on_silences(wav_fpath, words, end_times, hparams): + # Load the audio waveform + wav, _ = librosa.load(str(wav_fpath), hparams.sample_rate) + if hparams.rescale: + wav = wav / np.abs(wav).max() * hparams.rescaling_max + + words = np.array(words) + start_times = np.array([0.0] + end_times[:-1]) + end_times = np.array(end_times) + assert len(words) == len(end_times) == len(start_times) + assert words[0] == "" and words[-1] == "" + + # Find pauses that are too long + mask = (words == "") & (end_times - start_times >= hparams.silence_min_duration_split) + mask[0] = mask[-1] = True + breaks = np.where(mask)[0] + + # Profile the noise from the silences and perform noise reduction on the waveform + silence_times = [[start_times[i], end_times[i]] for i in breaks] + silence_times = (np.array(silence_times) * hparams.sample_rate).astype(np.int) + noisy_wav = np.concatenate([wav[stime[0]:stime[1]] for stime in silence_times]) + if len(noisy_wav) > hparams.sample_rate * 0.02: + profile = logmmse.profile_noise(noisy_wav, hparams.sample_rate) + wav = logmmse.denoise(wav, profile, eta=0) + + # Re-attach segments that are too short + segments = list(zip(breaks[:-1], breaks[1:])) + segment_durations = [start_times[end] - end_times[start] for start, end in segments] + i = 0 + while i < len(segments) and len(segments) > 1: + if segment_durations[i] < hparams.utterance_min_duration: + # See if the segment can be re-attached with the right or the left segment + left_duration = float("inf") if i == 0 else segment_durations[i - 1] + right_duration = float("inf") if i == len(segments) - 1 else segment_durations[i + 1] + joined_duration = segment_durations[i] + min(left_duration, right_duration) + + # Do not re-attach if it causes the joined utterance to be too long + if joined_duration > hparams.hop_size * hparams.max_mel_frames / hparams.sample_rate: + i += 1 + continue + + # Re-attach the segment with the neighbour of shortest duration + j = i - 1 if left_duration <= right_duration else i + segments[j] = (segments[j][0], segments[j + 1][1]) + segment_durations[j] = joined_duration + del segments[j + 1], segment_durations[j + 1] + else: + i += 1 + + # Split the utterance + segment_times = [[end_times[start], start_times[end]] for start, end in segments] + segment_times = (np.array(segment_times) * hparams.sample_rate).astype(np.int) + wavs = [wav[segment_time[0]:segment_time[1]] for segment_time in segment_times] + texts = [" ".join(words[start + 1:end]).replace(" ", " ") for start, end in segments] + + # # DEBUG: play the audio segments (run with -n=1) + # import sounddevice as sd + # if len(wavs) > 1: + # print("This sentence was split in %d segments:" % len(wavs)) + # else: + # print("There are no silences long enough for this sentence to be split:") + # for wav, text in zip(wavs, texts): + # # Pad the waveform with 1 second of silence because sounddevice tends to cut them early + # # when playing them. You shouldn't need to do that in your parsers. + # wav = np.concatenate((wav, [0] * 16000)) + # print("\t%s" % text) + # sd.play(wav, 16000, blocking=True) + # print("") + + return wavs, texts + + +def process_utterance(wav: np.ndarray, text: str, out_dir: Path, basename: str, + skip_existing: bool, hparams): + ## FOR REFERENCE: + # For you not to lose your head if you ever wish to change things here or implement your own + # synthesizer. + # - Both the audios and the mel spectrograms are saved as numpy arrays + # - There is no processing done to the audios that will be saved to disk beyond volume + # normalization (in split_on_silences) + # - However, pre-emphasis is applied to the audios before computing the mel spectrogram. This + # is why we re-apply it on the audio on the side of the vocoder. + # - Librosa pads the waveform before computing the mel spectrogram. Here, the waveform is saved + # without extra padding. This means that you won't have an exact relation between the length + # of the wav and of the mel spectrogram. See the vocoder data loader. + + + # Skip existing utterances if needed + mel_fpath = out_dir.joinpath("mels", "mel-%s.npy" % basename) + wav_fpath = out_dir.joinpath("audio", "audio-%s.npy" % basename) + if skip_existing and mel_fpath.exists() and wav_fpath.exists(): + return None + + # Trim silence + if hparams.trim_silence: + wav = encoder.preprocess_wav(wav, normalize=False, trim_silence=True) + + # Skip utterances that are too short + if len(wav) < hparams.utterance_min_duration * hparams.sample_rate: + return None + + # Compute the mel spectrogram + mel_spectrogram = audio.melspectrogram(wav, hparams).astype(np.float32) + mel_frames = mel_spectrogram.shape[1] + + # Skip utterances that are too long + if mel_frames > hparams.max_mel_frames and hparams.clip_mels_length: + return None + + # Write the spectrogram, embed and audio to disk + np.save(mel_fpath, mel_spectrogram.T, allow_pickle=False) + np.save(wav_fpath, wav, allow_pickle=False) + + # Return a tuple describing this training example + return wav_fpath.name, mel_fpath.name, "embed-%s.npy" % basename, len(wav), mel_frames, text + + +def embed_utterance(fpaths, encoder_model_fpath): + if not encoder.is_loaded(): + encoder.load_model(encoder_model_fpath) + + # Compute the speaker embedding of the utterance + wav_fpath, embed_fpath = fpaths + wav = np.load(wav_fpath) + wav = encoder.preprocess_wav(wav) + embed = encoder.embed_utterance(wav) + np.save(embed_fpath, embed, allow_pickle=False) + + +def create_embeddings(synthesizer_root: Path, encoder_model_fpath: Path, n_processes: int): + wav_dir = synthesizer_root.joinpath("audio") + metadata_fpath = synthesizer_root.joinpath("train.txt") + assert wav_dir.exists() and metadata_fpath.exists() + embed_dir = synthesizer_root.joinpath("embeds") + embed_dir.mkdir(exist_ok=True) + + # Gather the input wave filepath and the target output embed filepath + with metadata_fpath.open("r",encoding='cp949') as metadata_file: + metadata = [line.split("|") for line in metadata_file] + fpaths = [(wav_dir.joinpath(m[0]), embed_dir.joinpath(m[2])) for m in metadata] + + # TODO: improve on the multiprocessing, it's terrible. Disk I/O is the bottleneck here. + # Embed the utterances in separate threads + func = partial(embed_utterance, encoder_model_fpath=encoder_model_fpath) + job = Pool(n_processes).imap(func, fpaths) + list(tqdm(job, "Embedding", len(fpaths), unit="utterances")) + diff --git a/preprocess_kspon.py b/preprocess_kspon.py new file mode 100644 index 0000000000000000000000000000000000000000..51e5e44ebe9d38c1970746f3f97f2f3cb28204b3 --- /dev/null +++ b/preprocess_kspon.py @@ -0,0 +1,65 @@ +from utils.argutils import print_args +from pathlib import Path +import argparse +import sys +import wave +import os +from itertools import chain +from tqdm import tqdm +import re + +def preprocess_kspon(input_dirs): + folders = list(chain.from_iterable(input_dir.glob("*") for input_dir in input_dirs)) + + for folder in tqdm(folders, "folders", len(folders), unit="folders"): + texts = list() + symbol = ["o/", "b/", "l/", "n/", "u/", "+", "*", "(", "/"] + punctuation = [" ", ".", "?", "!"] + white = [" ", " ", ",,", ",,,"] + + existing_fnames = list() + for file in folder.glob("*"): + existing_fnames.append(file) + + + if str(file).endswith(".txt") and not str(file).endswith("alignment.txt"): + s = os.path.splitext(file) # 확장자와 확장자 아닌부분 + s = os.path.split(s[0]) # 확장자아닌 부분에서 분리 + + with open(file, "r", encoding='cp949') as f: + texts.append(s[1] + "$\"" + "|" + " ".join(f.read().splitlines()) + "|" + "\"\n") + + for i, text in enumerate(texts): + text = re.sub('\)\/\([가-힣\s\w]*\)', "", text) + for sym in symbol: + text = text.replace(sym, "") + for pun in punctuation: + text = text.replace(pun, " ") + for wh in white: + text = text.replace(wh, ",") + text = text.replace("$", " ") + text = text.replace("|", ",") + text = text.replace(",,", ",") + texts[i] = text + with open(os.path.join(folder, os.path.basename(folder) + "_alignment.txt"), "w", encoding='cp949') as a: + for text in texts: + a.write(text) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="pcm, raw 확장자 파일을 wav확장자로 변환", + formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + + parser.add_argument("path", type=str, help="처리할 폴더 경로") + args = parser.parse_args() + + dataset_root = Path(args.path) + input_dirs = [dataset_root.joinpath("KsponSpeech_01"), + dataset_root.joinpath("KsponSpeech_02"), + dataset_root.joinpath("KsponSpeech_03"), + dataset_root.joinpath("KsponSpeech_04"), + dataset_root.joinpath("KsponSpeech_05")] + + preprocess_kspon(input_dirs) diff --git a/profiler.py b/profiler.py new file mode 100644 index 0000000000000000000000000000000000000000..17175b9e1b0eb17fdc015199e5194a5c1afb8a28 --- /dev/null +++ b/profiler.py @@ -0,0 +1,45 @@ +from time import perf_counter as timer +from collections import OrderedDict +import numpy as np + + +class Profiler: + def __init__(self, summarize_every=5, disabled=False): + self.last_tick = timer() + self.logs = OrderedDict() + self.summarize_every = summarize_every + self.disabled = disabled + + def tick(self, name): + if self.disabled: + return + + # Log the time needed to execute that function + if not name in self.logs: + self.logs[name] = [] + if len(self.logs[name]) >= self.summarize_every: + self.summarize() + self.purge_logs() + self.logs[name].append(timer() - self.last_tick) + + self.reset_timer() + + def purge_logs(self): + for name in self.logs: + self.logs[name].clear() + + def reset_timer(self): + self.last_tick = timer() + + def summarize(self): + n = max(map(len, self.logs.values())) + assert n == self.summarize_every + print("\nAverage execution time over %d steps:" % n) + + name_msgs = ["%s (%d/%d):" % (name, len(deltas), n) for name, deltas in self.logs.items()] + pad = max(map(len, name_msgs)) + for name_msg, deltas in zip(name_msgs, self.logs.values()): + print(" %s mean: %4.0fms std: %4.0fms" % + (name_msg.ljust(pad), np.mean(deltas) * 1000, np.std(deltas) * 1000)) + print("", flush=True) + \ No newline at end of file diff --git a/random_cycler.py b/random_cycler.py new file mode 100644 index 0000000000000000000000000000000000000000..c405db6b27f46d874d8feb37e3f9c1e12c251109 --- /dev/null +++ b/random_cycler.py @@ -0,0 +1,37 @@ +import random + +class RandomCycler: + """ + Creates an internal copy of a sequence and allows access to its items in a constrained random + order. For a source sequence of n items and one or several consecutive queries of a total + of m items, the following guarantees hold (one implies the other): + - Each item will be returned between m // n and ((m - 1) // n) + 1 times. + - Between two appearances of the same item, there may be at most 2 * (n - 1) other items. + """ + + def __init__(self, source): + if len(source) == 0: + raise Exception("Can't create RandomCycler from an empty collection") + self.all_items = list(source) + self.next_items = [] + + def sample(self, count: int): + shuffle = lambda l: random.sample(l, len(l)) + + out = [] + while count > 0: + if count >= len(self.all_items): + out.extend(shuffle(list(self.all_items))) + count -= len(self.all_items) + continue + n = min(count, len(self.next_items)) + out.extend(self.next_items[:n]) + count -= n + self.next_items = self.next_items[n:] + if len(self.next_items) == 0: + self.next_items = shuffle(list(self.all_items)) + return out + + def __next__(self): + return self.sample(1)[0] + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..c134253ed3df2c91a910af2a9524a72e75eecb42 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +torch +nltk +umap-learn +visdom +librosa>=0.8.0 +matplotlib>=3.3.0 +numpy==1.19.3; platform_system == "Windows" +numpy==1.19.4; platform_system != "Windows" +scipy>=1.0.0 +tqdm +sounddevice +SoundFile +Unidecode +inflect +PyQt5 +multiprocess +numba +webrtcvad; platform_system != "Windows" +jamo diff --git a/speaker.py b/speaker.py new file mode 100644 index 0000000000000000000000000000000000000000..494e882fe34fc38dcc793ab8c74a6cc2376bb7b5 --- /dev/null +++ b/speaker.py @@ -0,0 +1,40 @@ +from encoder.data_objects.random_cycler import RandomCycler +from encoder.data_objects.utterance import Utterance +from pathlib import Path + +# Contains the set of utterances of a single speaker +class Speaker: + def __init__(self, root: Path): + self.root = root + self.name = root.name + self.utterances = None + self.utterance_cycler = None + + def _load_utterances(self): + with self.root.joinpath("_sources.txt").open("r") as sources_file: + sources = [l.split(",") for l in sources_file] + sources = {frames_fname: wave_fpath for frames_fname, wave_fpath in sources} + self.utterances = [Utterance(self.root.joinpath(f), w) for f, w in sources.items()] + self.utterance_cycler = RandomCycler(self.utterances) + + def random_partial(self, count, n_frames): + """ + Samples a batch of unique partial utterances from the disk in a way that all + utterances come up at least once every two cycles and in a random order every time. + + :param count: The number of partial utterances to sample from the set of utterances from + that speaker. Utterances are guaranteed not to be repeated if is not larger than + the number of utterances available. + :param n_frames: The number of frames in the partial utterance. + :return: A list of tuples (utterance, frames, range) where utterance is an Utterance, + frames are the frames of the partial utterances and range is the range of the partial + utterance with regard to the complete utterance. + """ + if self.utterances is None: + self._load_utterances() + + utterances = self.utterance_cycler.sample(count) + + a = [(u,) + u.random_partial(n_frames) for u in utterances] + + return a diff --git a/speaker_batch.py b/speaker_batch.py new file mode 100644 index 0000000000000000000000000000000000000000..56651dba5804a0c59c334e49ac18f8f5a4bfa444 --- /dev/null +++ b/speaker_batch.py @@ -0,0 +1,12 @@ +import numpy as np +from typing import List +from encoder.data_objects.speaker import Speaker + +class SpeakerBatch: + def __init__(self, speakers: List[Speaker], utterances_per_speaker: int, n_frames: int): + self.speakers = speakers + self.partials = {s: s.random_partial(utterances_per_speaker, n_frames) for s in speakers} + + # Array of shape (n_speakers * n_utterances, n_frames, mel_n), e.g. for 3 speakers with + # 4 utterances each of 160 frames of 40 mel coefficients: (12, 160, 40) + self.data = np.array([frames for s in speakers for _, frames, _ in self.partials[s]]) diff --git a/speaker_verification_dataset.py b/speaker_verification_dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..77a6e05eae6a939ae7575ae70b7173644141fffe --- /dev/null +++ b/speaker_verification_dataset.py @@ -0,0 +1,56 @@ +from encoder.data_objects.random_cycler import RandomCycler +from encoder.data_objects.speaker_batch import SpeakerBatch +from encoder.data_objects.speaker import Speaker +from encoder.params_data import partials_n_frames +from torch.utils.data import Dataset, DataLoader +from pathlib import Path + +# TODO: improve with a pool of speakers for data efficiency + +class SpeakerVerificationDataset(Dataset): + def __init__(self, datasets_root: Path): + self.root = datasets_root + speaker_dirs = [f for f in self.root.glob("*") if f.is_dir()] + if len(speaker_dirs) == 0: + raise Exception("No speakers found. Make sure you are pointing to the directory " + "containing all preprocessed speaker directories.") + self.speakers = [Speaker(speaker_dir) for speaker_dir in speaker_dirs] + self.speaker_cycler = RandomCycler(self.speakers) + + def __len__(self): + return int(1e10) + + def __getitem__(self, index): + return next(self.speaker_cycler) + + def get_logs(self): + log_string = "" + for log_fpath in self.root.glob("*.txt"): + with log_fpath.open("r") as log_file: + log_string += "".join(log_file.readlines()) + return log_string + + +class SpeakerVerificationDataLoader(DataLoader): + def __init__(self, dataset, speakers_per_batch, utterances_per_speaker, sampler=None, + batch_sampler=None, num_workers=0, pin_memory=False, timeout=0, + worker_init_fn=None): + self.utterances_per_speaker = utterances_per_speaker + + super().__init__( + dataset=dataset, + batch_size=speakers_per_batch, + shuffle=False, + sampler=sampler, + batch_sampler=batch_sampler, + num_workers=num_workers, + collate_fn=self.collate, + pin_memory=pin_memory, + drop_last=False, + timeout=timeout, + worker_init_fn=worker_init_fn + ) + + def collate(self, speakers): + return SpeakerBatch(speakers, self.utterances_per_speaker, partials_n_frames) + \ No newline at end of file diff --git a/symbols.py b/symbols.py new file mode 100644 index 0000000000000000000000000000000000000000..f7d3bf0115c72a5028dedf5dabba0255c4b27993 --- /dev/null +++ b/symbols.py @@ -0,0 +1,20 @@ +""" +Defines the set of symbols used in text input to the model. + +The default is a set of ASCII characters that works well for English or text that has been run +through Unidecode. For other data, you can modify _characters. See TRAINING_DATA.md for details. +""" +# from . import cmudict +from .korean import ALL_SYMBOLS, PAD, EOS + + +# For english +en_symbols = PAD+EOS+'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\'(),-.:;? ' #<-For deployment(Because korean ALL_SYMBOLS follow this convention) + +symbols = ALL_SYMBOLS # for korean +# +# # Prepend "@" to ARPAbet symbols to ensure uniqueness (some are the same as uppercase letters): +# #_arpabet = ["@' + s for s in cmudict.valid_symbols] +# +# # Export all symbols: +# symbols = [PAD, EOS] + list(_characters) #+ _arpabet diff --git a/synthesize.py b/synthesize.py new file mode 100644 index 0000000000000000000000000000000000000000..0ce6b0e25aae9e2285843366a88af2f0fdde393b --- /dev/null +++ b/synthesize.py @@ -0,0 +1,97 @@ +import torch +from torch.utils.data import DataLoader +from synthesizer.hparams import hparams_debug_string +from synthesizer.synthesizer_dataset import SynthesizerDataset, collate_synthesizer +from synthesizer.models.tacotron import Tacotron +from synthesizer.utils.text import text_to_sequence +from synthesizer.utils.symbols import symbols +import numpy as np +from pathlib import Path +from tqdm import tqdm + + +def run_synthesis(in_dir, out_dir, model_dir, hparams): + # This generates ground truth-aligned mels for vocoder training + synth_dir = Path(out_dir).joinpath("mels_gta") + synth_dir.mkdir(exist_ok=True) + print(hparams_debug_string(hparams)) + + # Check for GPU + if torch.cuda.is_available(): + device = torch.device("cuda") + if hparams.synthesis_batch_size % torch.cuda.device_count() != 0: + raise ValueError("`hparams.synthesis_batch_size` must be evenly divisible by n_gpus!") + else: + device = torch.device("cpu") + print("Synthesizer using device:", device) + + # Instantiate Tacotron model + model = Tacotron(embed_dims=hparams.tts_embed_dims, + num_chars=len(symbols), + encoder_dims=hparams.tts_encoder_dims, + decoder_dims=hparams.tts_decoder_dims, + n_mels=hparams.num_mels, + fft_bins=hparams.num_mels, + postnet_dims=hparams.tts_postnet_dims, + encoder_K=hparams.tts_encoder_K, + lstm_dims=hparams.tts_lstm_dims, + postnet_K=hparams.tts_postnet_K, + num_highways=hparams.tts_num_highways, + dropout=0., # Use zero dropout for gta mels + stop_threshold=hparams.tts_stop_threshold, + speaker_embedding_size=hparams.speaker_embedding_size).to(device) + + # Load the weights + model_dir = Path(model_dir) + model_fpath = model_dir.joinpath(model_dir.stem).with_suffix(".pt") + print("\nLoading weights at %s" % model_fpath) + model.load(model_fpath) + print("Tacotron weights loaded from step %d" % model.step) + + # Synthesize using same reduction factor as the model is currently trained + r = np.int32(model.r) + + # Set model to eval mode (disable gradient and zoneout) + model.eval() + + # Initialize the dataset + in_dir = Path(in_dir) + metadata_fpath = in_dir.joinpath("train.txt") + mel_dir = in_dir.joinpath("mels") + embed_dir = in_dir.joinpath("embeds") + + dataset = SynthesizerDataset(metadata_fpath, mel_dir, embed_dir, hparams) + data_loader = DataLoader(dataset, + collate_fn=lambda batch: collate_synthesizer(batch, r,hparams), + batch_size=hparams.synthesis_batch_size, + num_workers=0, #Having an error(Can't pickle local object 'run_synthesis..') when training in Windows unless you set num_workers=0 + shuffle=False, + pin_memory=True) + + # Generate GTA mels + meta_out_fpath = Path(out_dir).joinpath("synthesized.txt") + with open(meta_out_fpath, "w") as file: + for i, (texts, mels, embeds, idx) in tqdm(enumerate(data_loader), total=len(data_loader)): + texts = texts.to(device) + mels = mels.to(device) + embeds = embeds.to(device) + + # Parallelize model onto GPUS using workaround due to python bug + if device.type == "cuda" and torch.cuda.device_count() > 1: + _, mels_out,_,_ = data_parallel_workaround(model, texts, mels, embeds) + else: + _,mels_out, _,_ = model(texts, mels, embeds) + + for j, k in enumerate(idx): + # Note: outputs mel-spectrogram files and target ones have same names, just different folders + mel_filename = Path(synth_dir).joinpath(dataset.metadata[k][1]) + mel_out = mels_out[j].detach().cpu().numpy().T + + # Use the length of the ground truth mel to remove padding from the generated mels + mel_out = mel_out[:int(dataset.metadata[k][4])] + + # Write the spectrogram to disk + np.save(mel_filename, mel_out, allow_pickle=False) + + # Write metadata into the synthesized file + file.write("|".join(dataset.metadata[k])) diff --git a/synthesizer_dataset.py b/synthesizer_dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..bc9fec5774f4892e889f58f0060f883962d7e1d2 --- /dev/null +++ b/synthesizer_dataset.py @@ -0,0 +1,96 @@ +import torch +from torch.utils.data import Dataset +import numpy as np +from pathlib import Path +from synthesizer.utils.text import text_to_sequence +import nltk +nltk.download('punkt') +## + +class SynthesizerDataset(Dataset): + def __init__(self, metadata_fpath: Path, mel_dir: Path, embed_dir: Path, hparams): + print("Using inputs from:\n\t%s\n\t%s\n\t%s" % (metadata_fpath, mel_dir, embed_dir)) + + with metadata_fpath.open("r",encoding="cp949") as metadata_file: + metadata = [line.split("|") for line in metadata_file] + + mel_fnames = [x[1] for x in metadata if int(x[4])] + mel_fpaths = [mel_dir.joinpath(fname) for fname in mel_fnames] + embed_fnames = [x[2] for x in metadata if int(x[4])] + embed_fpaths = [embed_dir.joinpath(fname) for fname in embed_fnames] + self.samples_fpaths = list(zip(mel_fpaths, embed_fpaths)) + self.samples_texts = [x[5].strip() for x in metadata if int(x[4])] + self.metadata = metadata + self.hparams = hparams + + print("Found %d samples" % len(self.samples_fpaths)) + + def __getitem__(self, index): + # Sometimes index may be a list of 2 (not sure why this happens) + # If that is the case, return a single item corresponding to first element in index + if index is list: + index = index[0] + + mel_path, embed_path = self.samples_fpaths[index] + mel = np.load(mel_path).T.astype(np.float32) + + # Load the embed + embed = np.load(embed_path) + + print(self.samples_texts[index]) + + # Get the text and clean it + text = text_to_sequence(self.samples_texts[index], self.hparams.tts_cleaner_names) + + # Convert the list returned by text_to_sequence to a numpy array + text = np.asarray(text).astype(np.int32) + + return text, mel.astype(np.float32), embed.astype(np.float32), index + + def __len__(self): + return len(self.samples_fpaths) + + +def collate_synthesizer(batch, r, hparams): + # Text + x_lens = [len(x[0]) for x in batch] + max_x_len = max(x_lens) + + chars = [pad1d(x[0], max_x_len) for x in batch] + chars = np.stack(chars) + + # Mel spectrogram + spec_lens = [x[1].shape[-1] for x in batch] + max_spec_len = max(spec_lens) + 1 + if max_spec_len % r != 0: + max_spec_len += r - max_spec_len % r + + # WaveRNN mel spectrograms are normalized to [0, 1] so zero padding adds silence + # By default, SV2TTS uses symmetric mels, where -1*max_abs_value is silence. + if hparams.symmetric_mels: + mel_pad_value = -1 * hparams.max_abs_value + else: + mel_pad_value = 0 + + mel = [pad2d(x[1], max_spec_len, pad_value=mel_pad_value) for x in batch] + mel = np.stack(mel) + + # Speaker embedding (SV2TTS) + embeds = [x[2] for x in batch] + + # Index (for vocoder preprocessing) + indices = [x[3] for x in batch] + + + # Convert all to tensor + chars = torch.tensor(chars).long() + mel = torch.tensor(mel) + embeds = torch.tensor(embeds) + + return chars, mel, embeds, indices + +def pad1d(x, max_len, pad_value=0): + return np.pad(x, (0, max_len - len(x)), mode="constant", constant_values=pad_value) + +def pad2d(x, max_len, pad_value=0): + return np.pad(x, ((0, 0), (0, max_len - x.shape[-1])), mode="constant", constant_values=pad_value) diff --git a/synthesizer_preprocess_audio.py b/synthesizer_preprocess_audio.py new file mode 100644 index 0000000000000000000000000000000000000000..016c4ab65111fd67c358b79c6966ff224d759278 --- /dev/null +++ b/synthesizer_preprocess_audio.py @@ -0,0 +1,42 @@ +from synthesizer.Kor_preprocess import preprocess_KSponSpeech +from synthesizer.hparams import hparams +from utils.argutils import print_args +from pathlib import Path +import argparse + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Preprocesses audio files from datasets, encodes them as mel spectrograms " + "and writes them to the disk. Audio files are also saved, to be used by the " + "vocoder for training.", + formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + parser.add_argument("datasets_root", type=Path, help=\ + "Path to the directory containing your LibriSpeech/TTS datasets.") + parser.add_argument("-o", "--out_dir", type=Path, default=argparse.SUPPRESS, help=\ + "Path to the output directory that will contain the mel spectrograms, the audios and the " + "embeds. Defaults to /SV2TTS/synthesizer/") + parser.add_argument("-n", "--n_processes", type=int, default=None, help=\ + "Number of processes in parallel.") + parser.add_argument("-s", "--skip_existing", action="store_true", help=\ + "Whether to overwrite existing files with the same name. Useful if the preprocessing was " + "interrupted.") + parser.add_argument("--hparams", type=str, default="", help=\ + "Hyperparameter overrides as a comma-separated list of name-value pairs") + args = parser.parse_args() + + # Process the arguments + if not hasattr(args, "out_dir"): + args.out_dir = args.datasets_root.joinpath("SV2TTS", "synthesizer") + + # Create directories + assert args.datasets_root.exists() + args.out_dir.mkdir(exist_ok=True, parents=True) + + # Preprocess the dataset + print_args(args, parser) + args.hparams = hparams.parse(args.hparams) + # preprocess_librispeech(**vars(args)) + preprocess_KSponSpeech(**vars(args)) + diff --git a/synthesizer_preprocess_embeds.py b/synthesizer_preprocess_embeds.py new file mode 100644 index 0000000000000000000000000000000000000000..94f864d5d3c36c6177b211f5818e7c920a41cd8c --- /dev/null +++ b/synthesizer_preprocess_embeds.py @@ -0,0 +1,25 @@ +from synthesizer.preprocess import create_embeddings +from utils.argutils import print_args +from pathlib import Path +import argparse + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Creates embeddings for the synthesizer from the LibriSpeech utterances.", + formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + parser.add_argument("synthesizer_root", type=Path, help=\ + "Path to the synthesizer training data that contains the audios and the train.txt file. " + "If you let everything as default, it should be /SV2TTS/synthesizer/.") + parser.add_argument("-e", "--encoder_model_fpath", type=Path, + default="encoder/saved_models/pretrained.pt", help=\ + "Path your trained encoder model.") + parser.add_argument("-n", "--n_processes", type=int, default=4, help= \ + "Number of parallel processes. An encoder is created for each, so you may need to lower " + "this value on GPUs with low memory. Set it to 1 if CUDA is unhappy.") + args = parser.parse_args() + + # Preprocess the dataset + print_args(args, parser) + create_embeddings(**vars(args)) diff --git a/synthesizer_train.py b/synthesizer_train.py new file mode 100644 index 0000000000000000000000000000000000000000..2743d590d882f209734b68921b84a9d23492942c --- /dev/null +++ b/synthesizer_train.py @@ -0,0 +1,35 @@ +from synthesizer.hparams import hparams +from synthesizer.train import train +from utils.argutils import print_args +import argparse + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("run_id", type=str, help= \ + "Name for this model instance. If a model state from the same run ID was previously " + "saved, the training will restart from there. Pass -f to overwrite saved states and " + "restart from scratch.") + parser.add_argument("syn_dir", type=str, default=argparse.SUPPRESS, help= \ + "Path to the synthesizer directory that contains the ground truth mel spectrograms, " + "the wavs and the embeds.") + parser.add_argument("-m", "--models_dir", type=str, default="synthesizer/saved_models/", help=\ + "Path to the output directory that will contain the saved model weights and the logs.") + parser.add_argument("-s", "--save_every", type=int, default=1000, help= \ + "Number of steps between updates of the model on the disk. Set to 0 to never save the " + "model.") + parser.add_argument("-b", "--backup_every", type=int, default=25000, help= \ + "Number of steps between backups of the model. Set to 0 to never make backups of the " + "model.") + parser.add_argument("-f", "--force_restart", action="store_true", help= \ + "Do not load any saved model and restart from scratch.") + parser.add_argument("--hparams", default="", + help="Hyperparameter overrides as a comma-separated list of name=value " + "pairs") + args = parser.parse_args() + print_args(args, parser) + + args.hparams = hparams.parse(args.hparams) + + # Run the training + train(**vars(args)) diff --git a/tacotron.py b/tacotron.py new file mode 100644 index 0000000000000000000000000000000000000000..769f7f98b79100ff587af3609010dd55e3b2a146 --- /dev/null +++ b/tacotron.py @@ -0,0 +1,519 @@ +import os +import numpy as np +import torch +import torch.nn as nn +import torch.nn.functional as F +from pathlib import Path +from typing import Union + + +class HighwayNetwork(nn.Module): + def __init__(self, size): + super().__init__() + self.W1 = nn.Linear(size, size) + self.W2 = nn.Linear(size, size) + self.W1.bias.data.fill_(0.) + + def forward(self, x): + x1 = self.W1(x) + x2 = self.W2(x) + g = torch.sigmoid(x2) + y = g * F.relu(x1) + (1. - g) * x + return y + + +class Encoder(nn.Module): + def __init__(self, embed_dims, num_chars, encoder_dims, K, num_highways, dropout): + super().__init__() + prenet_dims = (encoder_dims, encoder_dims) + cbhg_channels = encoder_dims + self.embedding = nn.Embedding(num_chars, embed_dims) + self.pre_net = PreNet(embed_dims, fc1_dims=prenet_dims[0], fc2_dims=prenet_dims[1], + dropout=dropout) + self.cbhg = CBHG(K=K, in_channels=cbhg_channels, channels=cbhg_channels, + proj_channels=[cbhg_channels, cbhg_channels], + num_highways=num_highways) + + def forward(self, x, speaker_embedding=None): + x = self.embedding(x) + x = self.pre_net(x) + x.transpose_(1, 2) + x = self.cbhg(x) + if speaker_embedding is not None: + x = self.add_speaker_embedding(x, speaker_embedding) + return x + + def add_speaker_embedding(self, x, speaker_embedding): + # SV2TTS + # The input x is the encoder output and is a 3D tensor with size (batch_size, num_chars, tts_embed_dims) + # When training, speaker_embedding is also a 2D tensor with size (batch_size, speaker_embedding_size) + # (for inference, speaker_embedding is a 1D tensor with size (speaker_embedding_size)) + # This concats the speaker embedding for each char in the encoder output + + # Save the dimensions as human-readable names + batch_size = x.size()[0] + num_chars = x.size()[1] + + if speaker_embedding.dim() == 1: + idx = 0 + else: + idx = 1 + + # Start by making a copy of each speaker embedding to match the input text length + # The output of this has size (batch_size, num_chars * tts_embed_dims) + speaker_embedding_size = speaker_embedding.size()[idx] + e = speaker_embedding.repeat_interleave(num_chars, dim=idx) + + # Reshape it and transpose + e = e.reshape(batch_size, speaker_embedding_size, num_chars) + e = e.transpose(1, 2) + + # Concatenate the tiled speaker embedding with the encoder output + x = torch.cat((x, e), 2) + return x + + +class BatchNormConv(nn.Module): + def __init__(self, in_channels, out_channels, kernel, relu=True): + super().__init__() + self.conv = nn.Conv1d(in_channels, out_channels, kernel, stride=1, padding=kernel // 2, bias=False) + self.bnorm = nn.BatchNorm1d(out_channels) + self.relu = relu + + def forward(self, x): + x = self.conv(x) + x = F.relu(x) if self.relu is True else x + return self.bnorm(x) + + +class CBHG(nn.Module): + def __init__(self, K, in_channels, channels, proj_channels, num_highways): + super().__init__() + + # List of all rnns to call `flatten_parameters()` on + self._to_flatten = [] + + self.bank_kernels = [i for i in range(1, K + 1)] + self.conv1d_bank = nn.ModuleList() + for k in self.bank_kernels: + conv = BatchNormConv(in_channels, channels, k) + self.conv1d_bank.append(conv) + + self.maxpool = nn.MaxPool1d(kernel_size=2, stride=1, padding=1) + + self.conv_project1 = BatchNormConv(len(self.bank_kernels) * channels, proj_channels[0], 3) + self.conv_project2 = BatchNormConv(proj_channels[0], proj_channels[1], 3, relu=False) + + # Fix the highway input if necessary + if proj_channels[-1] != channels: + self.highway_mismatch = True + self.pre_highway = nn.Linear(proj_channels[-1], channels, bias=False) + else: + self.highway_mismatch = False + + self.highways = nn.ModuleList() + for i in range(num_highways): + hn = HighwayNetwork(channels) + self.highways.append(hn) + + self.rnn = nn.GRU(channels, channels // 2, batch_first=True, bidirectional=True) + self._to_flatten.append(self.rnn) + + # Avoid fragmentation of RNN parameters and associated warning + self._flatten_parameters() + + def forward(self, x): + # Although we `_flatten_parameters()` on init, when using DataParallel + # the model gets replicated, making it no longer guaranteed that the + # weights are contiguous in GPU memory. Hence, we must call it again + self._flatten_parameters() + + # Save these for later + residual = x + seq_len = x.size(-1) + conv_bank = [] + + # Convolution Bank + for conv in self.conv1d_bank: + c = conv(x) # Convolution + conv_bank.append(c[:, :, :seq_len]) + + # Stack along the channel axis + conv_bank = torch.cat(conv_bank, dim=1) + + # dump the last padding to fit residual + x = self.maxpool(conv_bank)[:, :, :seq_len] + + # Conv1d projections + x = self.conv_project1(x) + x = self.conv_project2(x) + + # Residual Connect + x = x + residual + + # Through the highways + x = x.transpose(1, 2) + if self.highway_mismatch is True: + x = self.pre_highway(x) + for h in self.highways: x = h(x) + + # And then the RNN + x, _ = self.rnn(x) + return x + + def _flatten_parameters(self): + """Calls `flatten_parameters` on all the rnns used by the WaveRNN. Used + to improve efficiency and avoid PyTorch yelling at us.""" + [m.flatten_parameters() for m in self._to_flatten] + +class PreNet(nn.Module): + def __init__(self, in_dims, fc1_dims=256, fc2_dims=128, dropout=0.5): + super().__init__() + self.fc1 = nn.Linear(in_dims, fc1_dims) + self.fc2 = nn.Linear(fc1_dims, fc2_dims) + self.p = dropout + + def forward(self, x): + x = self.fc1(x) + x = F.relu(x) + x = F.dropout(x, self.p, training=True) + x = self.fc2(x) + x = F.relu(x) + x = F.dropout(x, self.p, training=True) + return x + + +class Attention(nn.Module): + def __init__(self, attn_dims): + super().__init__() + self.W = nn.Linear(attn_dims, attn_dims, bias=False) + self.v = nn.Linear(attn_dims, 1, bias=False) + + def forward(self, encoder_seq_proj, query, t): + + # print(encoder_seq_proj.shape) + # Transform the query vector + query_proj = self.W(query).unsqueeze(1) + + # Compute the scores + u = self.v(torch.tanh(encoder_seq_proj + query_proj)) + scores = F.softmax(u, dim=1) + + return scores.transpose(1, 2) + + +class LSA(nn.Module): + def __init__(self, attn_dim, kernel_size=31, filters=32): + super().__init__() + self.conv = nn.Conv1d(1, filters, padding=(kernel_size - 1) // 2, kernel_size=kernel_size, bias=True) + self.L = nn.Linear(filters, attn_dim, bias=False) + self.W = nn.Linear(attn_dim, attn_dim, bias=True) # Include the attention bias in this term + self.v = nn.Linear(attn_dim, 1, bias=False) + self.cumulative = None + self.attention = None + + def init_attention(self, encoder_seq_proj): + device = next(self.parameters()).device # use same device as parameters + b, t, c = encoder_seq_proj.size() + self.cumulative = torch.zeros(b, t, device=device) + self.attention = torch.zeros(b, t, device=device) + + def forward(self, encoder_seq_proj, query, t, chars): + + if t == 0: self.init_attention(encoder_seq_proj) + + processed_query = self.W(query).unsqueeze(1) + + location = self.cumulative.unsqueeze(1) + processed_loc = self.L(self.conv(location).transpose(1, 2)) + + u = self.v(torch.tanh(processed_query + encoder_seq_proj + processed_loc)) + u = u.squeeze(-1) + + # Mask zero padding chars + u = u * (chars != 0).float() + + # Smooth Attention + # scores = torch.sigmoid(u) / torch.sigmoid(u).sum(dim=1, keepdim=True) + scores = F.softmax(u, dim=1) + self.attention = scores + self.cumulative = self.cumulative + self.attention + + return scores.unsqueeze(-1).transpose(1, 2) + + +class Decoder(nn.Module): + # Class variable because its value doesn't change between classes + # yet ought to be scoped by class because its a property of a Decoder + max_r = 20 + def __init__(self, n_mels, encoder_dims, decoder_dims, lstm_dims, + dropout, speaker_embedding_size): + super().__init__() + self.register_buffer("r", torch.tensor(1, dtype=torch.int)) + self.n_mels = n_mels + prenet_dims = (decoder_dims * 2, decoder_dims * 2) + self.prenet = PreNet(n_mels, fc1_dims=prenet_dims[0], fc2_dims=prenet_dims[1], + dropout=dropout) + self.attn_net = LSA(decoder_dims) + self.attn_rnn = nn.GRUCell(encoder_dims + prenet_dims[1] + speaker_embedding_size, decoder_dims) + self.rnn_input = nn.Linear(encoder_dims + decoder_dims + speaker_embedding_size, lstm_dims) + self.res_rnn1 = nn.LSTMCell(lstm_dims, lstm_dims) + self.res_rnn2 = nn.LSTMCell(lstm_dims, lstm_dims) + self.mel_proj = nn.Linear(lstm_dims, n_mels * self.max_r, bias=False) + self.stop_proj = nn.Linear(encoder_dims + speaker_embedding_size + lstm_dims, 1) + + def zoneout(self, prev, current, p=0.1): + device = next(self.parameters()).device # Use same device as parameters + mask = torch.zeros(prev.size(), device=device).bernoulli_(p) + return prev * mask + current * (1 - mask) + + def forward(self, encoder_seq, encoder_seq_proj, prenet_in, + hidden_states, cell_states, context_vec, t, chars): + + # Need this for reshaping mels + batch_size = encoder_seq.size(0) + + # Unpack the hidden and cell states + attn_hidden, rnn1_hidden, rnn2_hidden = hidden_states + rnn1_cell, rnn2_cell = cell_states + + # PreNet for the Attention RNN + prenet_out = self.prenet(prenet_in) + + # Compute the Attention RNN hidden state + attn_rnn_in = torch.cat([context_vec, prenet_out], dim=-1) + attn_hidden = self.attn_rnn(attn_rnn_in.squeeze(1), attn_hidden) + + # Compute the attention scores + scores = self.attn_net(encoder_seq_proj, attn_hidden, t, chars) + + # Dot product to create the context vector + context_vec = scores @ encoder_seq + context_vec = context_vec.squeeze(1) + + # Concat Attention RNN output w. Context Vector & project + x = torch.cat([context_vec, attn_hidden], dim=1) + x = self.rnn_input(x) + + # Compute first Residual RNN + rnn1_hidden_next, rnn1_cell = self.res_rnn1(x, (rnn1_hidden, rnn1_cell)) + if self.training: + rnn1_hidden = self.zoneout(rnn1_hidden, rnn1_hidden_next) + else: + rnn1_hidden = rnn1_hidden_next + x = x + rnn1_hidden + + # Compute second Residual RNN + rnn2_hidden_next, rnn2_cell = self.res_rnn2(x, (rnn2_hidden, rnn2_cell)) + if self.training: + rnn2_hidden = self.zoneout(rnn2_hidden, rnn2_hidden_next) + else: + rnn2_hidden = rnn2_hidden_next + x = x + rnn2_hidden + + # Project Mels + mels = self.mel_proj(x) + mels = mels.view(batch_size, self.n_mels, self.max_r)[:, :, :self.r] + hidden_states = (attn_hidden, rnn1_hidden, rnn2_hidden) + cell_states = (rnn1_cell, rnn2_cell) + + # Stop token prediction + s = torch.cat((x, context_vec), dim=1) + s = self.stop_proj(s) + stop_tokens = torch.sigmoid(s) + + return mels, scores, hidden_states, cell_states, context_vec, stop_tokens + + +class Tacotron(nn.Module): + def __init__(self, embed_dims, num_chars, encoder_dims, decoder_dims, n_mels, + fft_bins, postnet_dims, encoder_K, lstm_dims, postnet_K, num_highways, + dropout, stop_threshold, speaker_embedding_size): + super().__init__() + self.n_mels = n_mels + self.lstm_dims = lstm_dims + self.encoder_dims = encoder_dims + self.decoder_dims = decoder_dims + self.speaker_embedding_size = speaker_embedding_size + self.encoder = Encoder(embed_dims, num_chars, encoder_dims, + encoder_K, num_highways, dropout) + self.encoder_proj = nn.Linear(encoder_dims + speaker_embedding_size, decoder_dims, bias=False) + self.decoder = Decoder(n_mels, encoder_dims, decoder_dims, lstm_dims, + dropout, speaker_embedding_size) + self.postnet = CBHG(postnet_K, n_mels, postnet_dims, + [postnet_dims, fft_bins], num_highways) + self.post_proj = nn.Linear(postnet_dims, fft_bins, bias=False) + + self.init_model() + self.num_params() + + self.register_buffer("step", torch.zeros(1, dtype=torch.long)) + self.register_buffer("stop_threshold", torch.tensor(stop_threshold, dtype=torch.float32)) + + @property + def r(self): + return self.decoder.r.item() + + @r.setter + def r(self, value): + self.decoder.r = self.decoder.r.new_tensor(value, requires_grad=False) + + def forward(self, x, m, speaker_embedding): + device = next(self.parameters()).device # use same device as parameters + + self.step += 1 + batch_size, _, steps = m.size() + + # Initialise all hidden states and pack into tuple + attn_hidden = torch.zeros(batch_size, self.decoder_dims, device=device) + rnn1_hidden = torch.zeros(batch_size, self.lstm_dims, device=device) + rnn2_hidden = torch.zeros(batch_size, self.lstm_dims, device=device) + hidden_states = (attn_hidden, rnn1_hidden, rnn2_hidden) + + # Initialise all lstm cell states and pack into tuple + rnn1_cell = torch.zeros(batch_size, self.lstm_dims, device=device) + rnn2_cell = torch.zeros(batch_size, self.lstm_dims, device=device) + cell_states = (rnn1_cell, rnn2_cell) + + # Frame for start of decoder loop + go_frame = torch.zeros(batch_size, self.n_mels, device=device) + + # Need an initial context vector + context_vec = torch.zeros(batch_size, self.encoder_dims + self.speaker_embedding_size, device=device) + + # SV2TTS: Run the encoder with the speaker embedding + # The projection avoids unnecessary matmuls in the decoder loop + encoder_seq = self.encoder(x, speaker_embedding) + encoder_seq_proj = self.encoder_proj(encoder_seq) + + # Need a couple of lists for outputs + mel_outputs, attn_scores, stop_outputs = [], [], [] + + # Run the decoder loop + for t in range(0, steps, self.r): + prenet_in = m[:, :, t - 1] if t > 0 else go_frame + mel_frames, scores, hidden_states, cell_states, context_vec, stop_tokens = \ + self.decoder(encoder_seq, encoder_seq_proj, prenet_in, + hidden_states, cell_states, context_vec, t, x) + mel_outputs.append(mel_frames) + attn_scores.append(scores) + stop_outputs.extend([stop_tokens] * self.r) + + # Concat the mel outputs into sequence + mel_outputs = torch.cat(mel_outputs, dim=2) + + # Post-Process for Linear Spectrograms + postnet_out = self.postnet(mel_outputs) + linear = self.post_proj(postnet_out) + linear = linear.transpose(1, 2) + + # For easy visualisation + attn_scores = torch.cat(attn_scores, 1) + # attn_scores = attn_scores.cpu().data.numpy() + stop_outputs = torch.cat(stop_outputs, 1) + + return mel_outputs, linear, attn_scores, stop_outputs + + def generate(self, x, speaker_embedding=None, steps=2000): + self.eval() + device = next(self.parameters()).device # use same device as parameters + + batch_size, _ = x.size() + + # Need to initialise all hidden states and pack into tuple for tidyness + attn_hidden = torch.zeros(batch_size, self.decoder_dims, device=device) + rnn1_hidden = torch.zeros(batch_size, self.lstm_dims, device=device) + rnn2_hidden = torch.zeros(batch_size, self.lstm_dims, device=device) + hidden_states = (attn_hidden, rnn1_hidden, rnn2_hidden) + + # Need to initialise all lstm cell states and pack into tuple for tidyness + rnn1_cell = torch.zeros(batch_size, self.lstm_dims, device=device) + rnn2_cell = torch.zeros(batch_size, self.lstm_dims, device=device) + cell_states = (rnn1_cell, rnn2_cell) + + # Need a Frame for start of decoder loop + go_frame = torch.zeros(batch_size, self.n_mels, device=device) + + # Need an initial context vector + context_vec = torch.zeros(batch_size, self.encoder_dims + self.speaker_embedding_size, device=device) + + # SV2TTS: Run the encoder with the speaker embedding + # The projection avoids unnecessary matmuls in the decoder loop + encoder_seq = self.encoder(x, speaker_embedding) + encoder_seq_proj = self.encoder_proj(encoder_seq) + + # Need a couple of lists for outputs + mel_outputs, attn_scores, stop_outputs = [], [], [] + + # Run the decoder loop + for t in range(0, steps, self.r): + prenet_in = mel_outputs[-1][:, :, -1] if t > 0 else go_frame + mel_frames, scores, hidden_states, cell_states, context_vec, stop_tokens = \ + self.decoder(encoder_seq, encoder_seq_proj, prenet_in, + hidden_states, cell_states, context_vec, t, x) + mel_outputs.append(mel_frames) + attn_scores.append(scores) + stop_outputs.extend([stop_tokens] * self.r) + # Stop the loop when all stop tokens in batch exceed threshold + if (stop_tokens > 0.5).all() and t > 10: break + + # Concat the mel outputs into sequence + mel_outputs = torch.cat(mel_outputs, dim=2) + + # Post-Process for Linear Spectrograms + postnet_out = self.postnet(mel_outputs) + linear = self.post_proj(postnet_out) + + + linear = linear.transpose(1, 2) + + # For easy visualisation + attn_scores = torch.cat(attn_scores, 1) + stop_outputs = torch.cat(stop_outputs, 1) + + self.train() + + return mel_outputs, linear, attn_scores + + def init_model(self): + for p in self.parameters(): + if p.dim() > 1: nn.init.xavier_uniform_(p) + + def get_step(self): + return self.step.data.item() + + def reset_step(self): + # assignment to parameters or buffers is overloaded, updates internal dict entry + self.step = self.step.data.new_tensor(1) + + def log(self, path, msg): + with open(path, "a") as f: + print(msg, file=f) + + def load(self, path, optimizer=None): + # Use device of model params as location for loaded state + device = next(self.parameters()).device + checkpoint = torch.load(str(path), map_location=device) + self.load_state_dict(checkpoint["model_state"]) + + if "optimizer_state" in checkpoint and optimizer is not None: + optimizer.load_state_dict(checkpoint["optimizer_state"]) + + def save(self, path, optimizer=None): + if optimizer is not None: + torch.save({ + "model_state": self.state_dict(), + "optimizer_state": optimizer.state_dict(), + }, str(path)) + else: + torch.save({ + "model_state": self.state_dict(), + }, str(path)) + + + def num_params(self, print_out=True): + parameters = filter(lambda p: p.requires_grad, self.parameters()) + parameters = sum([np.prod(p.size()) for p in parameters]) / 1_000_000 + if print_out: + print("Trainable Parameters: %.3fM" % parameters) + return parameters diff --git a/text.py b/text.py new file mode 100644 index 0000000000000000000000000000000000000000..5dcc4889a8efb79b36d17bb307471b66b1fa99f2 --- /dev/null +++ b/text.py @@ -0,0 +1,117 @@ +from .symbols import symbols +from . import cleaners +import re +from .korean import jamo_to_korean, EOS, PAD +from .symbols import en_symbols +import string + +# Mappings from symbol to numeric ID and vice versa: +_symbol_to_id = {s: i for i, s in enumerate(symbols)} +_id_to_symbol = {i: s for i, s in enumerate(symbols)} +isEn = False + +# Regular expression matching text enclosed in curly braces: +_curly_re = re.compile(r"(.*?)\{(.+?)\}(.*)") + +puncuation_table = str.maketrans({key: None for key in string.punctuation}) + + +def convert_to_en_symbols(): + '''Converts built-in korean symbols to english, to be used for english training + +''' + global _symbol_to_id, _id_to_symbol, isEn + if not isEn: + print(" [!] Converting to english mode") + _symbol_to_id = {s: i for i, s in enumerate(en_symbols)} + _id_to_symbol = {i: s for i, s in enumerate(en_symbols)} + isEn = True + + +def text_to_sequence(text, cleaner_names): + """Converts a string of text to a sequence of IDs corresponding to the symbols in the text. + + The text can optionally have ARPAbet sequences enclosed in curly braces embedded + in it. For example, "Turn left on {HH AW1 S S T AH0 N} Street." + + Args: + text: string to convert to a sequence + cleaner_names: names of the cleaner functions to run the text through + + Returns: + List of integers corresponding to the symbols in the text + """ + sequence = [] + + # Check for curly braces and treat their contents as ARPAbet: + while len(text): + m = _curly_re.match(text) + if not m: + sequence += _symbols_to_sequence(_clean_text(text, cleaner_names)) + break + sequence += _symbols_to_sequence(_clean_text(m.group(1), cleaner_names)) + sequence += _arpabet_to_sequence(m.group(2)) + text = m.group(3) + + # Append EOS token + sequence.append(_symbol_to_id["~"]) + return sequence + +# +# def sequence_to_text(sequence): +# """Converts a sequence of IDs back to a string""" +# result = "" +# for symbol_id in sequence: +# if symbol_id in _id_to_symbol: +# s = _id_to_symbol[symbol_id] +# # Enclose ARPAbet back in curly braces: +# if len(s) > 1 and s[0] == "@": +# s = "{%s}" % s[1:] +# result += s +# return result.replace("}{", " ") + + +def sequence_to_text(sequence, hparams, skip_eos_and_pad=False, combine_jamo=False): + # '''Converts a sequence of IDs back to a string''' + # cleaner_names = [x.strip() for x in hparams.cleaners.split(',')] + # if 'english_cleaners' in cleaner_names and isEn == False: + # convert_to_en_symbols() + + result = '' + for symbol_id in sequence: + if symbol_id in _id_to_symbol: + s = _id_to_symbol[symbol_id] + # Enclose ARPAbet back in curly braces: + if len(s) > 1 and s[0] == '@': + s = '{%s}' % s[1:] + + if not skip_eos_and_pad or s not in [EOS, PAD]: + result += s + + result = result.replace('}{', ' ') + + if combine_jamo: + return jamo_to_korean(result) + else: + return result + + +def _clean_text(text, cleaner_names): + for name in cleaner_names: + cleaner = getattr(cleaners, name) + if not cleaner: + raise Exception("Unknown cleaner: %s" % name) + text = cleaner(text) + return text + + +def _symbols_to_sequence(symbols): + return [_symbol_to_id[s] for s in symbols if _should_keep_symbol(s)] + + +def _arpabet_to_sequence(text): + return _symbols_to_sequence(["@" + s for s in text.split()]) + + +def _should_keep_symbol(s): + return s in _symbol_to_id and s not in ("_", "~") diff --git a/train.py b/train.py new file mode 100644 index 0000000000000000000000000000000000000000..491246937479b89c5a80e3474895ef2373569262 --- /dev/null +++ b/train.py @@ -0,0 +1,127 @@ +from vocoder.models.fatchord_version import WaveRNN +from vocoder.vocoder_dataset import VocoderDataset, collate_vocoder +from vocoder.distribution import discretized_mix_logistic_loss +from vocoder.display import stream, simple_table +from vocoder.gen_wavernn import gen_testset +from torch.utils.data import DataLoader +from pathlib import Path +from torch import optim +import torch.nn.functional as F +import vocoder.hparams as hp +import numpy as np +import time +import torch + + +def train(run_id: str, syn_dir: Path, voc_dir: Path, models_dir: Path, ground_truth: bool, + save_every: int, backup_every: int, force_restart: bool): + # Check to make sure the hop length is correctly factorised + assert np.cumprod(hp.voc_upsample_factors)[-1] == hp.hop_length + + # Instantiate the model + print("Initializing the model...") + model = WaveRNN( + rnn_dims=hp.voc_rnn_dims, + fc_dims=hp.voc_fc_dims, + bits=hp.bits, + pad=hp.voc_pad, + upsample_factors=hp.voc_upsample_factors, + feat_dims=hp.num_mels, + compute_dims=hp.voc_compute_dims, + res_out_dims=hp.voc_res_out_dims, + res_blocks=hp.voc_res_blocks, + hop_length=hp.hop_length, + sample_rate=hp.sample_rate, + mode=hp.voc_mode + ) + + if torch.cuda.is_available(): + model = model.cuda() + device = torch.device('cuda') + else: + device = torch.device('cpu') + + # Initialize the optimizer + optimizer = optim.Adam(model.parameters()) + for p in optimizer.param_groups: + p["lr"] = hp.voc_lr + loss_func = F.cross_entropy if model.mode == "RAW" else discretized_mix_logistic_loss + + # Load the weights + model_dir = models_dir.joinpath(run_id) + model_dir.mkdir(exist_ok=True) + weights_fpath = model_dir.joinpath(run_id + ".pt") + if force_restart or not weights_fpath.exists(): + print("\nStarting the training of WaveRNN from scratch\n") + model.save(weights_fpath, optimizer) + else: + print("\nLoading weights at %s" % weights_fpath) + model.load(weights_fpath, optimizer) + print("WaveRNN weights loaded from step %d" % model.step) + + # Initialize the dataset + metadata_fpath = syn_dir.joinpath("train.txt") if ground_truth else \ + voc_dir.joinpath("synthesized.txt") + mel_dir = syn_dir.joinpath("mels") if ground_truth else voc_dir.joinpath("mels_gta") + wav_dir = syn_dir.joinpath("audio") + dataset = VocoderDataset(metadata_fpath, mel_dir, wav_dir) + test_loader = DataLoader(dataset, + batch_size=1, + shuffle=True, + pin_memory=True) + + # Begin the training + simple_table([('Batch size', hp.voc_batch_size), + ('LR', hp.voc_lr), + ('Sequence Len', hp.voc_seq_len)]) + + for epoch in range(1, 350): + data_loader = DataLoader(dataset, + collate_fn=collate_vocoder, + batch_size=hp.voc_batch_size, + num_workers=2, + shuffle=True, + pin_memory=True) + start = time.time() + running_loss = 0. + + for i, (x, y, m) in enumerate(data_loader, 1): + if torch.cuda.is_available(): + x, m, y = x.cuda(), m.cuda(), y.cuda() + + # Forward pass + y_hat = model(x, m) + if model.mode == 'RAW': + y_hat = y_hat.transpose(1, 2).unsqueeze(-1) + elif model.mode == 'MOL': + y = y.float() + y = y.unsqueeze(-1) + + # Backward pass + loss = loss_func(y_hat, y) + optimizer.zero_grad() + loss.backward() + optimizer.step() + + running_loss += loss.item() + speed = i / (time.time() - start) + avg_loss = running_loss / i + + step = model.get_step() + k = step // 1000 + + if backup_every != 0 and step % backup_every == 0 : + model.checkpoint(model_dir, optimizer) + + if save_every != 0 and step % save_every == 0 : + model.save(weights_fpath, optimizer) + + msg = f"| Epoch: {epoch} ({i}/{len(data_loader)}) | " \ + f"Loss: {avg_loss:.4f} | {speed:.1f} " \ + f"steps/s | Step: {k}k | " + stream(msg) + + + gen_testset(model, test_loader, hp.voc_gen_at_checkpoint, hp.voc_gen_batched, + hp.voc_target, hp.voc_overlap, model_dir) + print("") diff --git a/ui.py b/ui.py new file mode 100644 index 0000000000000000000000000000000000000000..d56b5740e276751f954aae1ca17e5ed485b48937 --- /dev/null +++ b/ui.py @@ -0,0 +1,611 @@ +import matplotlib.pyplot as plt +from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas +from matplotlib.figure import Figure +from PyQt5.QtCore import Qt, QStringListModel +from PyQt5.QtWidgets import * +from encoder.inference import plot_embedding_as_heatmap +from toolbox.utterance import Utterance +from pathlib import Path +from typing import List, Set +import sounddevice as sd +import soundfile as sf +import numpy as np +# from sklearn.manifold import TSNE # You can try with TSNE if you like, I prefer UMAP +from time import sleep +import umap +import sys +from warnings import filterwarnings, warn +filterwarnings("ignore") + + +colormap = np.array([ + [0, 127, 70], + [255, 0, 0], + [255, 217, 38], + [0, 135, 255], + [165, 0, 165], + [255, 167, 255], + [97, 142, 151], + [0, 255, 255], + [255, 96, 38], + [142, 76, 0], + [33, 0, 127], + [0, 0, 0], + [183, 183, 183], + [76, 255, 0], +], dtype=np.float) / 255 + +default_text = \ + "Welcome to the toolbox! To begin, load an utterance from your datasets or record one " \ + "yourself.\nOnce its embedding has been created, you can synthesize any text written here.\n" \ + "The synthesizer expects to generate " \ + "outputs that are somewhere between 5 and 12 seconds.\nTo mark breaks, write a new line. " \ + "Each line will be treated separately.\nThen, they are joined together to make the final " \ + "spectrogram. Use the vocoder to generate audio.\nThe vocoder generates almost in constant " \ + "time, so it will be more time efficient for longer inputs like this one.\nOn the left you " \ + "have the embedding projections. Load or record more utterances to see them.\nIf you have " \ + "at least 2 or 3 utterances from a same speaker, a cluster should form.\nSynthesized " \ + "utterances are of the same color as the speaker whose voice was used, but they're " \ + "represented with a cross." + + +class UI(QDialog): + min_umap_points = 4 + max_log_lines = 5 + max_saved_utterances = 20 + + def draw_utterance(self, utterance: Utterance, which): + self.draw_spec(utterance.spec, which) + self.draw_embed(utterance.embed, utterance.name, which) + + def draw_embed(self, embed, name, which): + embed_ax, _ = self.current_ax if which == "current" else self.gen_ax + embed_ax.figure.suptitle("" if embed is None else name) + + ## Embedding + # Clear the plot + if len(embed_ax.images) > 0: + embed_ax.images[0].colorbar.remove() + embed_ax.clear() + + # Draw the embed + if embed is not None: + plot_embedding_as_heatmap(embed, embed_ax) + embed_ax.set_title("embedding") + embed_ax.set_aspect("equal", "datalim") + embed_ax.set_xticks([]) + embed_ax.set_yticks([]) + embed_ax.figure.canvas.draw() + + def draw_spec(self, spec, which): + _, spec_ax = self.current_ax if which == "current" else self.gen_ax + + ## Spectrogram + # Draw the spectrogram + spec_ax.clear() + if spec is not None: + im = spec_ax.imshow(spec, aspect="auto", interpolation="none") + # spec_ax.figure.colorbar(mappable=im, shrink=0.65, orientation="horizontal", + # spec_ax=spec_ax) + spec_ax.set_title("mel spectrogram") + + spec_ax.set_xticks([]) + spec_ax.set_yticks([]) + spec_ax.figure.canvas.draw() + if which != "current": + self.vocode_button.setDisabled(spec is None) + + def draw_umap_projections(self, utterances: Set[Utterance]): + self.umap_ax.clear() + + speakers = np.unique([u.speaker_name for u in utterances]) + colors = {speaker_name: colormap[i] for i, speaker_name in enumerate(speakers)} + embeds = [u.embed for u in utterances] + + # Display a message if there aren't enough points + if len(utterances) < self.min_umap_points: + self.umap_ax.text(.5, .5, "Add %d more points to\ngenerate the projections" % + (self.min_umap_points - len(utterances)), + horizontalalignment='center', fontsize=15) + self.umap_ax.set_title("") + + # Compute the projections + else: + if not self.umap_hot: + self.log( + "Drawing UMAP projections for the first time, this will take a few seconds.") + self.umap_hot = True + + reducer = umap.UMAP(int(np.ceil(np.sqrt(len(embeds)))), metric="cosine") + # reducer = TSNE() + projections = reducer.fit_transform(embeds) + + speakers_done = set() + for projection, utterance in zip(projections, utterances): + color = colors[utterance.speaker_name] + mark = "x" if "_gen_" in utterance.name else "o" + label = None if utterance.speaker_name in speakers_done else utterance.speaker_name + speakers_done.add(utterance.speaker_name) + self.umap_ax.scatter(projection[0], projection[1], c=[color], marker=mark, + label=label) + # self.umap_ax.set_title("UMAP projections") + self.umap_ax.legend(prop={'size': 10}) + + # Draw the plot + self.umap_ax.set_aspect("equal", "datalim") + self.umap_ax.set_xticks([]) + self.umap_ax.set_yticks([]) + self.umap_ax.figure.canvas.draw() + + def save_audio_file(self, wav, sample_rate): + dialog = QFileDialog() + dialog.setDefaultSuffix(".wav") + fpath, _ = dialog.getSaveFileName( + parent=self, + caption="Select a path to save the audio file", + filter="Audio Files (*.flac *.wav)" + ) + if fpath: + #Default format is wav + if Path(fpath).suffix == "": + fpath += ".wav" + sf.write(fpath, wav, sample_rate) + + def setup_audio_devices(self, sample_rate): + input_devices = [] + output_devices = [] + for device in sd.query_devices(): + # Check if valid input + try: + sd.check_input_settings(device=device["name"], samplerate=sample_rate) + input_devices.append(device["name"]) + except: + pass + + # Check if valid output + try: + sd.check_output_settings(device=device["name"], samplerate=sample_rate) + output_devices.append(device["name"]) + except Exception as e: + # Log a warning only if the device is not an input + if not device["name"] in input_devices: + warn("Unsupported output device %s for the sample rate: %d \nError: %s" % (device["name"], sample_rate, str(e))) + + if len(input_devices) == 0: + self.log("No audio input device detected. Recording may not work.") + self.audio_in_device = None + else: + self.audio_in_device = input_devices[0] + + if len(output_devices) == 0: + self.log("No supported output audio devices were found! Audio output may not work.") + self.audio_out_devices_cb.addItems(["None"]) + self.audio_out_devices_cb.setDisabled(True) + else: + self.audio_out_devices_cb.clear() + self.audio_out_devices_cb.addItems(output_devices) + self.audio_out_devices_cb.currentTextChanged.connect(self.set_audio_device) + + self.set_audio_device() + + def set_audio_device(self): + + output_device = self.audio_out_devices_cb.currentText() + if output_device == "None": + output_device = None + + # If None, sounddevice queries portaudio + sd.default.device = (self.audio_in_device, output_device) + + def play(self, wav, sample_rate): + try: + sd.stop() + sd.play(wav, sample_rate) + except Exception as e: + print(e) + self.log("Error in audio playback. Try selecting a different audio output device.") + self.log("Your device must be connected before you start the toolbox.") + + def stop(self): + sd.stop() + + def record_one(self, sample_rate, duration): + self.record_button.setText("Recording...") + self.record_button.setDisabled(True) + + self.log("Recording %d seconds of audio" % duration) + sd.stop() + try: + wav = sd.rec(duration * sample_rate, sample_rate, 1) + except Exception as e: + print(e) + self.log("Could not record anything. Is your recording device enabled?") + self.log("Your device must be connected before you start the toolbox.") + return None + + for i in np.arange(0, duration, 0.1): + self.set_loading(i, duration) + sleep(0.1) + self.set_loading(duration, duration) + sd.wait() + + self.log("Done recording.") + self.record_button.setText("Record") + self.record_button.setDisabled(False) + + return wav.squeeze() + + @property + def current_dataset_name(self): + return self.dataset_box.currentText() + + @property + def current_speaker_name(self): + return self.speaker_box.currentText() + + @property + def current_utterance_name(self): + return self.utterance_box.currentText() + + def browse_file(self): + fpath = QFileDialog().getOpenFileName( + parent=self, + caption="Select an audio file", + filter="Audio Files (*.mp3 *.flac *.wav *.m4a)" + ) + return Path(fpath[0]) if fpath[0] != "" else "" + + @staticmethod + def repopulate_box(box, items, random=False): + """ + Resets a box and adds a list of items. Pass a list of (item, data) pairs instead to join + data to the items + """ + box.blockSignals(True) + box.clear() + for item in items: + item = list(item) if isinstance(item, tuple) else [item] + box.addItem(str(item[0]), *item[1:]) + if len(items) > 0: + box.setCurrentIndex(np.random.randint(len(items)) if random else 0) + box.setDisabled(len(items) == 0) + box.blockSignals(False) + + def populate_browser(self, datasets_root: Path, recognized_datasets: List, level: int, + random=True): + # Select a random dataset + if level <= 0: + if datasets_root is not None: + datasets = [datasets_root.joinpath(d) for d in recognized_datasets] + datasets = [d.relative_to(datasets_root) for d in datasets if d.exists()] + self.browser_load_button.setDisabled(len(datasets) == 0) + if datasets_root is None or len(datasets) == 0: + msg = "Warning: you d" + ("id not pass a root directory for datasets as argument" \ + if datasets_root is None else "o not have any of the recognized datasets" \ + " in %s" % datasets_root) + self.log(msg) + msg += ".\nThe recognized datasets are:\n\t%s\nFeel free to add your own. You " \ + "can still use the toolbox by recording samples yourself." % \ + ("\n\t".join(recognized_datasets)) + print(msg, file=sys.stderr) + + self.random_utterance_button.setDisabled(True) + self.random_speaker_button.setDisabled(True) + self.random_dataset_button.setDisabled(True) + self.utterance_box.setDisabled(True) + self.speaker_box.setDisabled(True) + self.dataset_box.setDisabled(True) + self.browser_load_button.setDisabled(True) + self.auto_next_checkbox.setDisabled(True) + return + self.repopulate_box(self.dataset_box, datasets, random) + + # Select a random speaker + if level <= 1: + speakers_root = datasets_root.joinpath(self.current_dataset_name) + speaker_names = [d.stem for d in speakers_root.glob("*") if d.is_dir()] + self.repopulate_box(self.speaker_box, speaker_names, random) + + # Select a random utterance + if level <= 2: + utterances_root = datasets_root.joinpath( + self.current_dataset_name, + self.current_speaker_name + ) + utterances = [] + for extension in ['mp3', 'flac', 'wav', 'm4a']: + utterances.extend(Path(utterances_root).glob("**/*.%s" % extension)) + utterances = [fpath.relative_to(utterances_root) for fpath in utterances] + self.repopulate_box(self.utterance_box, utterances, random) + + def browser_select_next(self): + index = (self.utterance_box.currentIndex() + 1) % len(self.utterance_box) + self.utterance_box.setCurrentIndex(index) + + @property + def current_encoder_fpath(self): + return self.encoder_box.itemData(self.encoder_box.currentIndex()) + + @property + def current_synthesizer_fpath(self): + return self.synthesizer_box.itemData(self.synthesizer_box.currentIndex()) + + @property + def current_vocoder_fpath(self): + return self.vocoder_box.itemData(self.vocoder_box.currentIndex()) + + def populate_models(self, encoder_models_dir: Path, synthesizer_models_dir: Path, + vocoder_models_dir: Path): + # Encoder + encoder_fpaths = list(encoder_models_dir.glob("*.pt")) + if len(encoder_fpaths) == 0: + raise Exception("No encoder models found in %s" % encoder_models_dir) + self.repopulate_box(self.encoder_box, [(f.stem, f) for f in encoder_fpaths]) + + # Synthesizer + synthesizer_fpaths = list(synthesizer_models_dir.glob("**/*.pt")) + if len(synthesizer_fpaths) == 0: + raise Exception("No synthesizer models found in %s" % synthesizer_models_dir) + self.repopulate_box(self.synthesizer_box, [(f.stem, f) for f in synthesizer_fpaths]) + + # Vocoder + vocoder_fpaths = list(vocoder_models_dir.glob("**/*.pt")) + vocoder_items = [(f.stem, f) for f in vocoder_fpaths] + [("Griffin-Lim", None)] + self.repopulate_box(self.vocoder_box, vocoder_items) + + @property + def selected_utterance(self): + return self.utterance_history.itemData(self.utterance_history.currentIndex()) + + def register_utterance(self, utterance: Utterance): + self.utterance_history.blockSignals(True) + self.utterance_history.insertItem(0, utterance.name, utterance) + self.utterance_history.setCurrentIndex(0) + self.utterance_history.blockSignals(False) + + if len(self.utterance_history) > self.max_saved_utterances: + self.utterance_history.removeItem(self.max_saved_utterances) + + self.play_button.setDisabled(False) + self.generate_button.setDisabled(False) + self.synthesize_button.setDisabled(False) + + def log(self, line, mode="newline"): + if mode == "newline": + self.logs.append(line) + if len(self.logs) > self.max_log_lines: + del self.logs[0] + elif mode == "append": + self.logs[-1] += line + elif mode == "overwrite": + self.logs[-1] = line + log_text = '\n'.join(self.logs) + + self.log_window.setText(log_text) + self.app.processEvents() + + def set_loading(self, value, maximum=1): + self.loading_bar.setValue(value * 100) + self.loading_bar.setMaximum(maximum * 100) + self.loading_bar.setTextVisible(value != 0) + self.app.processEvents() + + def populate_gen_options(self, seed, trim_silences): + if seed is not None: + self.random_seed_checkbox.setChecked(True) + self.seed_textbox.setText(str(seed)) + self.seed_textbox.setEnabled(True) + else: + self.random_seed_checkbox.setChecked(False) + self.seed_textbox.setText(str(0)) + self.seed_textbox.setEnabled(False) + + if not trim_silences: + self.trim_silences_checkbox.setChecked(False) + self.trim_silences_checkbox.setDisabled(True) + + def update_seed_textbox(self): + if self.random_seed_checkbox.isChecked(): + self.seed_textbox.setEnabled(True) + else: + self.seed_textbox.setEnabled(False) + + def reset_interface(self): + self.draw_embed(None, None, "current") + self.draw_embed(None, None, "generated") + self.draw_spec(None, "current") + self.draw_spec(None, "generated") + self.draw_umap_projections(set()) + self.set_loading(0) + self.play_button.setDisabled(True) + self.generate_button.setDisabled(True) + self.synthesize_button.setDisabled(True) + self.vocode_button.setDisabled(True) + self.replay_wav_button.setDisabled(True) + self.export_wav_button.setDisabled(True) + [self.log("") for _ in range(self.max_log_lines)] + + def __init__(self): + ## Initialize the application + self.app = QApplication(sys.argv) + super().__init__(None) + self.setWindowTitle("SV2TTS toolbox") + + + ## Main layouts + # Root + root_layout = QGridLayout() + self.setLayout(root_layout) + + # Browser + browser_layout = QGridLayout() + root_layout.addLayout(browser_layout, 0, 0, 1, 2) + + # Generation + gen_layout = QVBoxLayout() + root_layout.addLayout(gen_layout, 0, 2, 1, 2) + + # Projections + self.projections_layout = QVBoxLayout() + root_layout.addLayout(self.projections_layout, 1, 0, 1, 1) + + # Visualizations + vis_layout = QVBoxLayout() + root_layout.addLayout(vis_layout, 1, 1, 1, 3) + + + ## Projections + # UMap + fig, self.umap_ax = plt.subplots(figsize=(3, 3), facecolor="#F0F0F0") + fig.subplots_adjust(left=0.02, bottom=0.02, right=0.98, top=0.98) + self.projections_layout.addWidget(FigureCanvas(fig)) + self.umap_hot = False + self.clear_button = QPushButton("Clear") + self.projections_layout.addWidget(self.clear_button) + + + ## Browser + # Dataset, speaker and utterance selection + i = 0 + self.dataset_box = QComboBox() + browser_layout.addWidget(QLabel("Dataset"), i, 0) + browser_layout.addWidget(self.dataset_box, i + 1, 0) + self.speaker_box = QComboBox() + browser_layout.addWidget(QLabel("Speaker"), i, 1) + browser_layout.addWidget(self.speaker_box, i + 1, 1) + self.utterance_box = QComboBox() + browser_layout.addWidget(QLabel("Utterance"), i, 2) + browser_layout.addWidget(self.utterance_box, i + 1, 2) + self.browser_load_button = QPushButton("Load") + browser_layout.addWidget(self.browser_load_button, i + 1, 3) + i += 2 + + # Random buttons + self.random_dataset_button = QPushButton("Random") + browser_layout.addWidget(self.random_dataset_button, i, 0) + self.random_speaker_button = QPushButton("Random") + browser_layout.addWidget(self.random_speaker_button, i, 1) + self.random_utterance_button = QPushButton("Random") + browser_layout.addWidget(self.random_utterance_button, i, 2) + self.auto_next_checkbox = QCheckBox("Auto select next") + self.auto_next_checkbox.setChecked(True) + browser_layout.addWidget(self.auto_next_checkbox, i, 3) + i += 1 + + # Utterance box + browser_layout.addWidget(QLabel("Use embedding from:"), i, 0) + self.utterance_history = QComboBox() + browser_layout.addWidget(self.utterance_history, i, 1, 1, 3) + i += 1 + + # Random & next utterance buttons + self.browser_browse_button = QPushButton("Browse") + browser_layout.addWidget(self.browser_browse_button, i, 0) + self.record_button = QPushButton("Record") + browser_layout.addWidget(self.record_button, i, 1) + self.play_button = QPushButton("Play") + browser_layout.addWidget(self.play_button, i, 2) + self.stop_button = QPushButton("Stop") + browser_layout.addWidget(self.stop_button, i, 3) + i += 1 + + + # Model and audio output selection + self.encoder_box = QComboBox() + browser_layout.addWidget(QLabel("Encoder"), i, 0) + browser_layout.addWidget(self.encoder_box, i + 1, 0) + self.synthesizer_box = QComboBox() + browser_layout.addWidget(QLabel("Synthesizer"), i, 1) + browser_layout.addWidget(self.synthesizer_box, i + 1, 1) + self.vocoder_box = QComboBox() + browser_layout.addWidget(QLabel("Vocoder"), i, 2) + browser_layout.addWidget(self.vocoder_box, i + 1, 2) + + self.audio_out_devices_cb=QComboBox() + browser_layout.addWidget(QLabel("Audio Output"), i, 3) + browser_layout.addWidget(self.audio_out_devices_cb, i + 1, 3) + i += 2 + + #Replay & Save Audio + browser_layout.addWidget(QLabel("Toolbox Output:"), i, 0) + self.waves_cb = QComboBox() + self.waves_cb_model = QStringListModel() + self.waves_cb.setModel(self.waves_cb_model) + self.waves_cb.setToolTip("Select one of the last generated waves in this section for replaying or exporting") + browser_layout.addWidget(self.waves_cb, i, 1) + self.replay_wav_button = QPushButton("Replay") + self.replay_wav_button.setToolTip("Replay last generated vocoder") + browser_layout.addWidget(self.replay_wav_button, i, 2) + self.export_wav_button = QPushButton("Export") + self.export_wav_button.setToolTip("Save last generated vocoder audio in filesystem as a wav file") + browser_layout.addWidget(self.export_wav_button, i, 3) + i += 1 + + + ## Embed & spectrograms + vis_layout.addStretch() + + gridspec_kw = {"width_ratios": [1, 4]} + fig, self.current_ax = plt.subplots(1, 2, figsize=(10, 2.25), facecolor="#F0F0F0", + gridspec_kw=gridspec_kw) + fig.subplots_adjust(left=0, bottom=0.1, right=1, top=0.8) + vis_layout.addWidget(FigureCanvas(fig)) + + fig, self.gen_ax = plt.subplots(1, 2, figsize=(10, 2.25), facecolor="#F0F0F0", + gridspec_kw=gridspec_kw) + fig.subplots_adjust(left=0, bottom=0.1, right=1, top=0.8) + vis_layout.addWidget(FigureCanvas(fig)) + + for ax in self.current_ax.tolist() + self.gen_ax.tolist(): + ax.set_facecolor("#F0F0F0") + for side in ["top", "right", "bottom", "left"]: + ax.spines[side].set_visible(False) + + + ## Generation + self.text_prompt = QPlainTextEdit(default_text) + gen_layout.addWidget(self.text_prompt, stretch=1) + + self.generate_button = QPushButton("Synthesize and vocode") + gen_layout.addWidget(self.generate_button) + + layout = QHBoxLayout() + self.synthesize_button = QPushButton("Synthesize only") + layout.addWidget(self.synthesize_button) + self.vocode_button = QPushButton("Vocode only") + layout.addWidget(self.vocode_button) + gen_layout.addLayout(layout) + + layout_seed = QGridLayout() + self.random_seed_checkbox = QCheckBox("Random seed:") + self.random_seed_checkbox.setToolTip("When checked, makes the synthesizer and vocoder deterministic.") + layout_seed.addWidget(self.random_seed_checkbox, 0, 0) + self.seed_textbox = QLineEdit() + self.seed_textbox.setMaximumWidth(80) + layout_seed.addWidget(self.seed_textbox, 0, 1) + self.trim_silences_checkbox = QCheckBox("Enhance vocoder output") + self.trim_silences_checkbox.setToolTip("When checked, trims excess silence in vocoder output." + " This feature requires `webrtcvad` to be installed.") + layout_seed.addWidget(self.trim_silences_checkbox, 0, 2, 1, 2) + gen_layout.addLayout(layout_seed) + + self.loading_bar = QProgressBar() + gen_layout.addWidget(self.loading_bar) + + self.log_window = QLabel() + self.log_window.setAlignment(Qt.AlignBottom | Qt.AlignLeft) + gen_layout.addWidget(self.log_window) + self.logs = [] + gen_layout.addStretch() + + + ## Set the size of the window and of the elements + max_size = QDesktopWidget().availableGeometry(self).size() * 0.8 + self.resize(max_size) + + ## Finalize the display + self.reset_interface() + self.show() + + def start(self): + self.app.exec_() diff --git a/utterance.py b/utterance.py new file mode 100644 index 0000000000000000000000000000000000000000..844c8a2adb0c8eba2992eaf5ea357d7add3c1896 --- /dev/null +++ b/utterance.py @@ -0,0 +1,5 @@ +from collections import namedtuple + +Utterance = namedtuple("Utterance", "name speaker_name wav spec embed partial_embeds synth") +Utterance.__eq__ = lambda x, y: x.name == y.name +Utterance.__hash__ = lambda x: hash(x.name) diff --git a/visualizations.py b/visualizations.py new file mode 100644 index 0000000000000000000000000000000000000000..980c74f95f1f7df41ebccc983600b2713c0b0502 --- /dev/null +++ b/visualizations.py @@ -0,0 +1,178 @@ +from encoder.data_objects.speaker_verification_dataset import SpeakerVerificationDataset +from datetime import datetime +from time import perf_counter as timer +import matplotlib.pyplot as plt +import numpy as np +# import webbrowser +import visdom +import umap + +colormap = np.array([ + [76, 255, 0], + [0, 127, 70], + [255, 0, 0], + [255, 217, 38], + [0, 135, 255], + [165, 0, 165], + [255, 167, 255], + [0, 255, 255], + [255, 96, 38], + [142, 76, 0], + [33, 0, 127], + [0, 0, 0], + [183, 183, 183], +], dtype=np.float) / 255 + + +class Visualizations: + def __init__(self, env_name=None, update_every=10, server="http://localhost", disabled=False): + # Tracking data + self.last_update_timestamp = timer() + self.update_every = update_every + self.step_times = [] + self.losses = [] + self.eers = [] + print("Updating the visualizations every %d steps." % update_every) + + # If visdom is disabled TODO: use a better paradigm for that + self.disabled = disabled + if self.disabled: + return + + # Set the environment name + now = str(datetime.now().strftime("%d-%m %Hh%M")) + if env_name is None: + self.env_name = now + else: + self.env_name = "%s (%s)" % (env_name, now) + + # Connect to visdom and open the corresponding window in the browser + try: + self.vis = visdom.Visdom(server, env=self.env_name, raise_exceptions=True) + except ConnectionError: + raise Exception("No visdom server detected. Run the command \"visdom\" in your CLI to " + "start it.") + # webbrowser.open("http://localhost:8097/env/" + self.env_name) + + # Create the windows + self.loss_win = None + self.eer_win = None + # self.lr_win = None + self.implementation_win = None + self.projection_win = None + self.implementation_string = "" + + def log_params(self): + if self.disabled: + return + from encoder import params_data + from encoder import params_model + param_string = "Model parameters:
" + for param_name in (p for p in dir(params_model) if not p.startswith("__")): + value = getattr(params_model, param_name) + param_string += "\t%s: %s
" % (param_name, value) + param_string += "Data parameters:
" + for param_name in (p for p in dir(params_data) if not p.startswith("__")): + value = getattr(params_data, param_name) + param_string += "\t%s: %s
" % (param_name, value) + self.vis.text(param_string, opts={"title": "Parameters"}) + + def log_dataset(self, dataset: SpeakerVerificationDataset): + if self.disabled: + return + dataset_string = "" + dataset_string += "Speakers: %s\n" % len(dataset.speakers) + dataset_string += "\n" + dataset.get_logs() + dataset_string = dataset_string.replace("\n", "
") + self.vis.text(dataset_string, opts={"title": "Dataset"}) + + def log_implementation(self, params): + if self.disabled: + return + implementation_string = "" + for param, value in params.items(): + implementation_string += "%s: %s\n" % (param, value) + implementation_string = implementation_string.replace("\n", "
") + self.implementation_string = implementation_string + self.implementation_win = self.vis.text( + implementation_string, + opts={"title": "Training implementation"} + ) + + def update(self, loss, eer, step): + # Update the tracking data + now = timer() + self.step_times.append(1000 * (now - self.last_update_timestamp)) + self.last_update_timestamp = now + self.losses.append(loss) + self.eers.append(eer) + print(".", end="") + + # Update the plots every steps + if step % self.update_every != 0: + return + time_string = "Step time: mean: %5dms std: %5dms" % \ + (int(np.mean(self.step_times)), int(np.std(self.step_times))) + print("\nStep %6d Loss: %.4f EER: %.4f %s" % + (step, np.mean(self.losses), np.mean(self.eers), time_string)) + if not self.disabled: + self.loss_win = self.vis.line( + [np.mean(self.losses)], + [step], + win=self.loss_win, + update="append" if self.loss_win else None, + opts=dict( + legend=["Avg. loss"], + xlabel="Step", + ylabel="Loss", + title="Loss", + ) + ) + self.eer_win = self.vis.line( + [np.mean(self.eers)], + [step], + win=self.eer_win, + update="append" if self.eer_win else None, + opts=dict( + legend=["Avg. EER"], + xlabel="Step", + ylabel="EER", + title="Equal error rate" + ) + ) + if self.implementation_win is not None: + self.vis.text( + self.implementation_string + ("%s" % time_string), + win=self.implementation_win, + opts={"title": "Training implementation"}, + ) + + # Reset the tracking + self.losses.clear() + self.eers.clear() + self.step_times.clear() + + def draw_projections(self, embeds, utterances_per_speaker, step, out_fpath=None, + max_speakers=10): + max_speakers = min(max_speakers, len(colormap)) + embeds = embeds[:max_speakers * utterances_per_speaker] + + n_speakers = len(embeds) // utterances_per_speaker + ground_truth = np.repeat(np.arange(n_speakers), utterances_per_speaker) + colors = [colormap[i] for i in ground_truth] + + reducer = umap.UMAP() + projected = reducer.fit_transform(embeds) + plt.scatter(projected[:, 0], projected[:, 1], c=colors) + plt.gca().set_aspect("equal", "datalim") + plt.title("UMAP projection (step %d)" % step) + if not self.disabled: + self.projection_win = self.vis.matplot(plt, win=self.projection_win) + if out_fpath is not None: + plt.savefig(out_fpath) + plt.clf() + + def save(self): + if not self.disabled: + self.vis.save([self.env_name]) + \ No newline at end of file diff --git a/vocoder_dataset.py b/vocoder_dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..9eae1b5f20117feef0a06e264a99b3c0c6143bac --- /dev/null +++ b/vocoder_dataset.py @@ -0,0 +1,84 @@ +from torch.utils.data import Dataset +from pathlib import Path +from vocoder import audio +import vocoder.hparams as hp +import numpy as np +import torch + + +class VocoderDataset(Dataset): + def __init__(self, metadata_fpath: Path, mel_dir: Path, wav_dir: Path): + print("Using inputs from:\n\t%s\n\t%s\n\t%s" % (metadata_fpath, mel_dir, wav_dir)) + + with metadata_fpath.open("r") as metadata_file: + metadata = [line.split("|") for line in metadata_file] + + gta_fnames = [x[1] for x in metadata if int(x[4])] + gta_fpaths = [mel_dir.joinpath(fname) for fname in gta_fnames] + wav_fnames = [x[0] for x in metadata if int(x[4])] + wav_fpaths = [wav_dir.joinpath(fname) for fname in wav_fnames] + self.samples_fpaths = list(zip(gta_fpaths, wav_fpaths)) + + print("Found %d samples" % len(self.samples_fpaths)) + + def __getitem__(self, index): + mel_path, wav_path = self.samples_fpaths[index] + + # Load the mel spectrogram and adjust its range to [-1, 1] + mel = np.load(mel_path).T.astype(np.float32) / hp.mel_max_abs_value + + # Load the wav + wav = np.load(wav_path) + if hp.apply_preemphasis: + wav = audio.pre_emphasis(wav) + wav = np.clip(wav, -1, 1) + + # Fix for missing padding # TODO: settle on whether this is any useful + r_pad = (len(wav) // hp.hop_length + 1) * hp.hop_length - len(wav) + wav = np.pad(wav, (0, r_pad), mode='constant') + assert len(wav) >= mel.shape[1] * hp.hop_length + wav = wav[:mel.shape[1] * hp.hop_length] + assert len(wav) % hp.hop_length == 0 + + # Quantize the wav + if hp.voc_mode == 'RAW': + if hp.mu_law: + quant = audio.encode_mu_law(wav, mu=2 ** hp.bits) + else: + quant = audio.float_2_label(wav, bits=hp.bits) + elif hp.voc_mode == 'MOL': + quant = audio.float_2_label(wav, bits=16) + + return mel.astype(np.float32), quant.astype(np.int64) + + def __len__(self): + return len(self.samples_fpaths) + + +def collate_vocoder(batch): + mel_win = hp.voc_seq_len // hp.hop_length + 2 * hp.voc_pad + max_offsets = [x[0].shape[-1] -2 - (mel_win + 2 * hp.voc_pad) for x in batch] + mel_offsets = [np.random.randint(0, offset) for offset in max_offsets] + sig_offsets = [(offset + hp.voc_pad) * hp.hop_length for offset in mel_offsets] + + mels = [x[0][:, mel_offsets[i]:mel_offsets[i] + mel_win] for i, x in enumerate(batch)] + + labels = [x[1][sig_offsets[i]:sig_offsets[i] + hp.voc_seq_len + 1] for i, x in enumerate(batch)] + + mels = np.stack(mels).astype(np.float32) + labels = np.stack(labels).astype(np.int64) + + mels = torch.tensor(mels) + labels = torch.tensor(labels).long() + + x = labels[:, :hp.voc_seq_len] + y = labels[:, 1:] + + bits = 16 if hp.voc_mode == 'MOL' else hp.bits + + x = audio.label_2_float(x.float(), bits) + + if hp.voc_mode == 'MOL' : + y = audio.label_2_float(y.float(), bits) + + return x, y, mels \ No newline at end of file diff --git a/vocoder_preprocess.py b/vocoder_preprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..0828d72e4191fa457c23eea223c74b6af8f34ba7 --- /dev/null +++ b/vocoder_preprocess.py @@ -0,0 +1,59 @@ +from synthesizer.synthesize import run_synthesis +from synthesizer.hparams import hparams +from utils.argutils import print_args +import argparse +import os + + +if __name__ == "__main__": + class MyFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): + pass + + parser = argparse.ArgumentParser( + description="Creates ground-truth aligned (GTA) spectrograms from the vocoder.", + formatter_class=MyFormatter + ) + parser.add_argument("datasets_root", type=str, help=\ + "Path to the directory containing your SV2TTS directory. If you specify both --in_dir and " + "--out_dir, this argument won't be used.") + parser.add_argument("--model_dir", type=str, + default="synthesizer/saved_models/pretrained/", help=\ + "Path to the pretrained model directory.") + parser.add_argument("-i", "--in_dir", type=str, default=argparse.SUPPRESS, help= \ + "Path to the synthesizer directory that contains the mel spectrograms, the wavs and the " + "embeds. Defaults to /SV2TTS/synthesizer/.") + parser.add_argument("-o", "--out_dir", type=str, default=argparse.SUPPRESS, help= \ + "Path to the output vocoder directory that will contain the ground truth aligned mel " + "spectrograms. Defaults to /SV2TTS/vocoder/.") + parser.add_argument("--hparams", default="", + help="Hyperparameter overrides as a comma-separated list of name=value " + "pairs") + parser.add_argument("--no_trim", action="store_true", help=\ + "Preprocess audio without trimming silences (not recommended).") + parser.add_argument("--cpu", action="store_true", help=\ + "If True, processing is done on CPU, even when a GPU is available.") + args = parser.parse_args() + print_args(args, parser) + modified_hp = hparams.parse(args.hparams) + + if not hasattr(args, "in_dir"): + args.in_dir = os.path.join(args.datasets_root, "SV2TTS", "synthesizer") + if not hasattr(args, "out_dir"): + args.out_dir = os.path.join(args.datasets_root, "SV2TTS", "vocoder") + + if args.cpu: + # Hide GPUs from Pytorch to force CPU processing + os.environ["CUDA_VISIBLE_DEVICES"] = "" + + # Verify webrtcvad is available + if not args.no_trim: + try: + import webrtcvad + except: + raise ModuleNotFoundError("Package 'webrtcvad' not found. This package enables " + "noise removal and is recommended. Please install and try again. If installation fails, " + "use --no_trim to disable this error message.") + del args.no_trim + + run_synthesis(args.in_dir, args.out_dir, args.model_dir, modified_hp) + diff --git a/vocoder_train.py b/vocoder_train.py new file mode 100644 index 0000000000000000000000000000000000000000..d712ffa3e6c92a091aa18dc90f0027f46940e400 --- /dev/null +++ b/vocoder_train.py @@ -0,0 +1,56 @@ +from utils.argutils import print_args +from vocoder.train import train +from pathlib import Path +import argparse + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Trains the vocoder from the synthesizer audios and the GTA synthesized mels, " + "or ground truth mels.", + formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + + parser.add_argument("run_id", type=str, help= \ + "Name for this model instance. If a model state from the same run ID was previously " + "saved, the training will restart from there. Pass -f to overwrite saved states and " + "restart from scratch.") + parser.add_argument("datasets_root", type=str, help= \ + "Path to the directory containing your SV2TTS directory. Specifying --syn_dir or --voc_dir " + "will take priority over this argument.") + parser.add_argument("--syn_dir", type=str, default=argparse.SUPPRESS, help= \ + "Path to the synthesizer directory that contains the ground truth mel spectrograms, " + "the wavs and the embeds. Defaults to /SV2TTS/synthesizer/.") + parser.add_argument("--voc_dir", type=str, default=argparse.SUPPRESS, help= \ + "Path to the vocoder directory that contains the GTA synthesized mel spectrograms. " + "Defaults to /SV2TTS/vocoder/. Unused if --ground_truth is passed.") + parser.add_argument("-m", "--models_dir", type=str, default="vocoder/saved_models/", help=\ + "Path to the directory that will contain the saved model weights, as well as backups " + "of those weights and wavs generated during training.") + parser.add_argument("-g", "--ground_truth", action="store_true", help= \ + "Train on ground truth spectrograms (/SV2TTS/synthesizer/mels).") + parser.add_argument("-s", "--save_every", type=int, default=1000, help= \ + "Number of steps between updates of the model on the disk. Set to 0 to never save the " + "model.") + parser.add_argument("-b", "--backup_every", type=int, default=25000, help= \ + "Number of steps between backups of the model. Set to 0 to never make backups of the " + "model.") + parser.add_argument("-f", "--force_restart", action="store_true", help= \ + "Do not load any saved model and restart from scratch.") + args = parser.parse_args() + + # Process the arguments + if not hasattr(args, "syn_dir"): + args.syn_dir = Path(args.datasets_root, "SV2TTS", "synthesizer") + args.syn_dir = Path(args.syn_dir) + if not hasattr(args, "voc_dir"): + args.voc_dir = Path(args.datasets_root, "SV2TTS", "vocoder") + args.voc_dir = Path(args.voc_dir) + del args.datasets_root + args.models_dir = Path(args.models_dir) + args.models_dir.mkdir(exist_ok=True) + + # Run the training + print_args(args, parser) + train(**vars(args)) + \ No newline at end of file