From 051a1fbccd81c70d0a8f025bfdf7ebce2612eea6 Mon Sep 17 00:00:00 2001 From: BenAAndrew Date: Fri, 26 Nov 2021 17:15:58 +0000 Subject: [PATCH] Fix English synthesis --- alphabets/English.txt | 33 --------------------------------- application/views.py | 26 +++++++++++++------------- training/__init__.py | 2 +- training/train.py | 8 +++----- 4 files changed, 17 insertions(+), 52 deletions(-) delete mode 100644 alphabets/English.txt diff --git a/alphabets/English.txt b/alphabets/English.txt deleted file mode 100644 index 46aa35e..0000000 --- a/alphabets/English.txt +++ /dev/null @@ -1,33 +0,0 @@ -# Each line in this file represents the Unicode codepoint (UTF-8 encoded) -# associated with a numeric label. -# A line that starts with # is a comment. You can escape it with \# if you wish -# to use '#' as a label. - -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 -' -# The last (non-comment) line needs to end with a newline. diff --git a/application/views.py b/application/views.py index 1297cf4..f71e3a0 100644 --- a/application/views.py +++ b/application/views.py @@ -28,7 +28,7 @@ from dataset.clip_generator import CHARACTER_ENCODING, add_suffix from dataset.extend_existing_dataset import extend_existing_dataset from dataset.analysis import get_total_audio_duration, validate_dataset from dataset.transcribe import Silero, DeepSpeech, SILERO_LANGUAGES -from training.train import train, TRAINING_PATH +from training.train import train, TRAINING_PATH, DEFAULT_ALPHABET from training.utils import get_available_memory, get_batch_size, load_symbols, generate_timelapse_gif from synthesis.synthesize import load_model, synthesize from synthesis.vocoders import Hifigan @@ -45,6 +45,7 @@ GRAPH_FILE = "graph.png" RESULTS_FILE = "out.wav" TEMP_DATASET_UPLOAD = "temp.zip" TRANSCRIPTION_MODEL = "model.pbmm" +ENGLISH_LANGUAGE = "English" ALPHABET_FILE = "alphabet.txt" model = None @@ -73,6 +74,14 @@ def get_checkpoints(): if os.listdir(os.path.join(paths["models"], model)) } +def get_symbols(language): + if language == ENGLISH_LANGUAGE: + return DEFAULT_ALPHABET + elif language in SILERO_LANGUAGES: + return load_symbols(os.path.join(ALPHABET_FOLDER, f"{language}.txt")) + else: + return load_symbols(os.path.join(paths["languages"], language, ALPHABET_FILE)) + @app.errorhandler(Exception) def handle_bad_request(e): @@ -202,11 +211,7 @@ def get_train(): @app.route("/train", methods=["POST"]) def train_post(): language = request.form["language"] - alphabet_path = ( - os.path.join(ALPHABET_FOLDER, f"{language}.txt") - if language in SILERO_LANGUAGES - else os.path.join(paths["languages"], language, ALPHABET_FILE) - ) + symbols = get_symbols(language) dataset_name = request.form["dataset"] epochs = request.form["epochs"] batch_size = request.form["batch_size"] @@ -237,7 +242,7 @@ def train_post(): metadata_path=metadata_path, dataset_directory=audio_folder, output_directory=checkpoint_folder, - alphabet_path=alphabet_path, + symbols=symbols, checkpoint_path=checkpoint_path, transfer_learning_path=transfer_learning_path, epochs=int(epochs), @@ -285,12 +290,7 @@ def synthesis_setup_post(): vocoder = Hifigan(model_path, model_config_path) dataset_name = request.form["model"] language = request.form["language"] - alphabet_path = ( - os.path.join(ALPHABET_FOLDER, f"{language}.txt") - if language in SILERO_LANGUAGES - else os.path.join(paths["languages"], language, ALPHABET_FILE) - ) - symbols = load_symbols(alphabet_path) + symbols = get_symbols(language) checkpoint_folder = os.path.join(paths["models"], dataset_name) checkpoint = os.path.join(checkpoint_folder, request.form["checkpoint"]) model = load_model(checkpoint) diff --git a/training/__init__.py b/training/__init__.py index eec4634..44bd98e 100644 --- a/training/__init__.py +++ b/training/__init__.py @@ -1,3 +1,3 @@ PUNCTUATION = list("_-!'(),.:;?") BASE_SYMBOLS = PUNCTUATION + [" "] -DEFAULT_ALPHABET = "_-!'(),.:;? ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +DEFAULT_ALPHABET = list("_-!'(),.:;? ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") diff --git a/training/train.py b/training/train.py index 0283cbd..2bee541 100644 --- a/training/train.py +++ b/training/train.py @@ -25,7 +25,6 @@ from training.utils import ( get_batch_size, get_learning_rate, load_metadata, - load_symbols, check_early_stopping, calc_avgmax_attention, train_test_split, @@ -46,7 +45,7 @@ def train( metadata_path, dataset_directory, output_directory, - alphabet_path=None, + symbols=DEFAULT_ALPHABET, checkpoint_path=None, transfer_learning_path=None, epochs=8000, @@ -70,8 +69,8 @@ def train( Path to dataset clips output_directory : str Path to save checkpoints to - alphabet_path : str - Path to alphabet file (default is English) + symbols : list (optional) + Valid symbols (default is English) checkpoint_path : str (optional) Path to a checkpoint to load (default is None) transfer_learning_path : str (optional) @@ -137,7 +136,6 @@ def train( # Load data logging.info("Loading data...") filepaths_and_text = load_metadata(metadata_path) - symbols = load_symbols(alphabet_path) if alphabet_path else DEFAULT_ALPHABET validate_dataset(filepaths_and_text, dataset_directory, symbols) train_files, test_files = train_test_split(filepaths_and_text, train_size) trainset = VoiceDataset(train_files, dataset_directory, symbols)