From 74af2c831c71b481ead7ce2d191c5f09abffd53c Mon Sep 17 00:00:00 2001 From: tsukumi Date: Tue, 12 Mar 2024 17:45:10 +0000 Subject: [PATCH 1/6] Add: cache_dir and revision optional arguments to load_model() / load_tokenizer() --- style_bert_vits2/nlp/bert_models.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/style_bert_vits2/nlp/bert_models.py b/style_bert_vits2/nlp/bert_models.py index 1e346a4..5f630c3 100644 --- a/style_bert_vits2/nlp/bert_models.py +++ b/style_bert_vits2/nlp/bert_models.py @@ -38,12 +38,15 @@ __loaded_tokenizers: dict[ def load_model( language: Languages, pretrained_model_name_or_path: Optional[str] = None, + cache_dir: Optional[str] = None, + revision: str = 'main', ) -> Union[PreTrainedModel, DebertaV2Model]: """ 指定された言語の BERT モデルをロードし、ロード済みの BERT モデルを返す。 一度ロードされていれば、ロード済みの BERT モデルを即座に返す。 - ライブラリ利用時は常に pretrain_model_name_or_path (Hugging Face のリポジトリ名 or ローカルのファイルパス) を指定する必要がある。 + ライブラリ利用時は常に必ず pretrain_model_name_or_path (Hugging Face のリポジトリ名 or ローカルのファイルパス) を指定する必要がある。 ロードにはそれなりに時間がかかるため、ライブラリ利用前に明示的に pretrained_model_name_or_path を指定してロードしておくべき。 + cache_dir と revision は pretrain_model_name_or_path がリポジトリ名の場合のみ有効。 Style-Bert-VITS2 では、BERT モデルに下記の 3 つが利用されている。 これ以外の BERT モデルを指定した場合は正常に動作しない可能性が高い。 @@ -54,6 +57,8 @@ def load_model( Args: language (Languages): ロードする学習済みモデルの対象言語 pretrained_model_name_or_path (Optional[str]): ロードする学習済みモデルの名前またはパス。指定しない場合はデフォルトのパスが利用される (デフォルト: None) + cache_dir (Optional[str]): モデルのキャッシュディレクトリ。指定しない場合はデフォルトのキャッシュディレクトリが利用される (デフォルト: None) + revision (str): モデルの Hugging Face 上の Git リビジョン。指定しない場合は最新の main ブランチの内容が利用される (デフォルト: None) Returns: Union[PreTrainedModel, DebertaV2Model]: ロード済みの BERT モデル @@ -75,10 +80,10 @@ def load_model( if language == Languages.EN: model = cast( DebertaV2Model, - DebertaV2Model.from_pretrained(pretrained_model_name_or_path), + DebertaV2Model.from_pretrained(pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision), ) else: - model = AutoModelForMaskedLM.from_pretrained(pretrained_model_name_or_path) + model = AutoModelForMaskedLM.from_pretrained(pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision) __loaded_models[language] = model logger.info( f"Loaded the {language} BERT model from {pretrained_model_name_or_path}" @@ -90,12 +95,15 @@ def load_model( def load_tokenizer( language: Languages, pretrained_model_name_or_path: Optional[str] = None, + cache_dir: Optional[str] = None, + revision: str = 'main', ) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast, DebertaV2Tokenizer]: """ 指定された言語の BERT モデルをロードし、ロード済みの BERT トークナイザーを返す。 一度ロードされていれば、ロード済みの BERT トークナイザーを即座に返す。 - ライブラリ利用時は常に pretrain_model_name_or_path (Hugging Face のリポジトリ名 or ローカルのファイルパス) を指定する必要がある。 + ライブラリ利用時は常に必ず pretrain_model_name_or_path (Hugging Face のリポジトリ名 or ローカルのファイルパス) を指定する必要がある。 ロードにはそれなりに時間がかかるため、ライブラリ利用前に明示的に pretrained_model_name_or_path を指定してロードしておくべき。 + cache_dir と revision は pretrain_model_name_or_path がリポジトリ名の場合のみ有効。 Style-Bert-VITS2 では、BERT モデルに下記の 3 つが利用されている。 これ以外の BERT モデルを指定した場合は正常に動作しない可能性が高い。 @@ -106,6 +114,8 @@ def load_tokenizer( Args: language (Languages): ロードする学習済みモデルの対象言語 pretrained_model_name_or_path (Optional[str]): ロードする学習済みモデルの名前またはパス。指定しない場合はデフォルトのパスが利用される (デフォルト: None) + cache_dir (Optional[str]): モデルのキャッシュディレクトリ。指定しない場合はデフォルトのキャッシュディレクトリが利用される (デフォルト: None) + revision (str): モデルの Hugging Face 上の Git リビジョン。指定しない場合は最新の main ブランチの内容が利用される (デフォルト: None) Returns: Union[PreTrainedTokenizer, PreTrainedTokenizerFast, DebertaV2Tokenizer]: ロード済みの BERT トークナイザー @@ -125,9 +135,9 @@ def load_tokenizer( # BERT トークナイザーをロードし、辞書に格納して返す ## 英語のみ DebertaV2Tokenizer でロードする必要がある if language == Languages.EN: - tokenizer = DebertaV2Tokenizer.from_pretrained(pretrained_model_name_or_path) + tokenizer = DebertaV2Tokenizer.from_pretrained(pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision) else: - tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path) + tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision) __loaded_tokenizers[language] = tokenizer logger.info( f"Loaded the {language} BERT tokenizer from {pretrained_model_name_or_path}" From 483bc68d579fb028ae3c6c6febbebc9c42afefca Mon Sep 17 00:00:00 2001 From: tsukumi Date: Tue, 12 Mar 2024 18:00:51 +0000 Subject: [PATCH 2/6] Refactor: unify invoke format of open() function --- bert_gen.py | 4 ++-- config.py | 2 +- style_bert_vits2/models/utils/__init__.py | 8 +++++--- style_bert_vits2/models/utils/checkpoints.py | 2 +- style_bert_vits2/nlp/bert_models.py | 4 ++-- style_bert_vits2/nlp/chinese/g2p.py | 9 +++++---- style_bert_vits2/nlp/english/cmudict.py | 2 +- style_gen.py | 4 ++-- webui/merge.py | 10 +++++----- 9 files changed, 24 insertions(+), 21 deletions(-) diff --git a/bert_gen.py b/bert_gen.py index 2ded369..068afd6 100644 --- a/bert_gen.py +++ b/bert_gen.py @@ -73,10 +73,10 @@ if __name__ == "__main__": config_path = args.config hps = HyperParameters.load_from_json(config_path) lines = [] - with open(hps.data.training_files, encoding="utf-8") as f: + with open(hps.data.training_files, "r", encoding="utf-8") as f: lines.extend(f.readlines()) - with open(hps.data.validation_files, encoding="utf-8") as f: + with open(hps.data.validation_files, "r", encoding="utf-8") as f: lines.extend(f.readlines()) add_blank = [hps.data.add_blank] * len(lines) diff --git a/config.py b/config.py index 77c384d..40f3f53 100644 --- a/config.py +++ b/config.py @@ -238,7 +238,7 @@ class Config: "If you have no special needs, please do not modify default_config.yml." ) # sys.exit(0) - with open(file=config_path, mode="r", encoding="utf-8") as file: + with open(config_path, "r", encoding="utf-8") as file: yaml_config: Dict[str, any] = yaml.safe_load(file.read()) model_name: str = yaml_config["model_name"] self.model_name: str = model_name diff --git a/style_bert_vits2/models/utils/__init__.py b/style_bert_vits2/models/utils/__init__.py index 33e1324..edd51cc 100644 --- a/style_bert_vits2/models/utils/__init__.py +++ b/style_bert_vits2/models/utils/__init__.py @@ -180,7 +180,7 @@ def load_filepaths_and_text( list[list[str]]: ファイルパスとテキストのリスト """ - with open(filename, encoding="utf-8") as f: + with open(filename, "r", encoding="utf-8") as f: filepaths_and_text = [line.strip().split(split) for line in f] return filepaths_and_text @@ -249,7 +249,8 @@ def check_git_hash(model_dir_path: Union[str, Path]) -> None: path = os.path.join(model_dir_path, "githash") if os.path.exists(path): - saved_hash = open(path).read() + with open(path, "r", encoding="utf-8") as f: + saved_hash = f.read() if saved_hash != cur_hash: logger.warning( "git hash values are different. {}(saved) != {}(current)".format( @@ -257,4 +258,5 @@ def check_git_hash(model_dir_path: Union[str, Path]) -> None: ) ) else: - open(path, "w").write(cur_hash) + with open(path, "w", encoding="utf-8") as f: + f.write(cur_hash) diff --git a/style_bert_vits2/models/utils/checkpoints.py b/style_bert_vits2/models/utils/checkpoints.py index 63a5fa3..c601dda 100644 --- a/style_bert_vits2/models/utils/checkpoints.py +++ b/style_bert_vits2/models/utils/checkpoints.py @@ -85,7 +85,7 @@ def load_checkpoint( else: model.load_state_dict(new_state_dict, strict=False) - logger.info("Loaded '{}' (iteration {})".format(checkpoint_path, iteration)) + logger.info(f"Loaded '{checkpoint_path}' (iteration {iteration})") return model, optimizer, learning_rate, iteration diff --git a/style_bert_vits2/nlp/bert_models.py b/style_bert_vits2/nlp/bert_models.py index 5f630c3..eb84eb7 100644 --- a/style_bert_vits2/nlp/bert_models.py +++ b/style_bert_vits2/nlp/bert_models.py @@ -39,7 +39,7 @@ def load_model( language: Languages, pretrained_model_name_or_path: Optional[str] = None, cache_dir: Optional[str] = None, - revision: str = 'main', + revision: str = "main", ) -> Union[PreTrainedModel, DebertaV2Model]: """ 指定された言語の BERT モデルをロードし、ロード済みの BERT モデルを返す。 @@ -96,7 +96,7 @@ def load_tokenizer( language: Languages, pretrained_model_name_or_path: Optional[str] = None, cache_dir: Optional[str] = None, - revision: str = 'main', + revision: str = "main", ) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast, DebertaV2Tokenizer]: """ 指定された言語の BERT モデルをロードし、ロード済みの BERT トークナイザーを返す。 diff --git a/style_bert_vits2/nlp/chinese/g2p.py b/style_bert_vits2/nlp/chinese/g2p.py index 0046167..4ce2b26 100644 --- a/style_bert_vits2/nlp/chinese/g2p.py +++ b/style_bert_vits2/nlp/chinese/g2p.py @@ -8,10 +8,11 @@ from style_bert_vits2.nlp.chinese.tone_sandhi import ToneSandhi from style_bert_vits2.nlp.symbols import PUNCTUATIONS -__PINYIN_TO_SYMBOL_MAP = { - line.split("\t")[0]: line.strip().split("\t")[1] - for line in open(Path(__file__).parent / "opencpop-strict.txt").readlines() -} +with open(Path(__file__).parent / "opencpop-strict.txt", "r", encoding="utf-8") as f: + __PINYIN_TO_SYMBOL_MAP = { + line.split("\t")[0]: line.strip().split("\t")[1] + for line in f.readlines() + } def g2p(text: str) -> tuple[list[str], list[int], list[int]]: diff --git a/style_bert_vits2/nlp/english/cmudict.py b/style_bert_vits2/nlp/english/cmudict.py index e6afb89..7772e77 100644 --- a/style_bert_vits2/nlp/english/cmudict.py +++ b/style_bert_vits2/nlp/english/cmudict.py @@ -20,7 +20,7 @@ def get_dict() -> dict[str, list[list[str]]]: def read_dict() -> dict[str, list[list[str]]]: g2p_dict = {} start_line = 49 - with open(CMU_DICT_PATH) as f: + with open(CMU_DICT_PATH, "r", encoding="utf-8") as f: line = f.readline() line_index = 1 while line: diff --git a/style_gen.py b/style_gen.py index 384319a..02af067 100644 --- a/style_gen.py +++ b/style_gen.py @@ -73,7 +73,7 @@ if __name__ == "__main__": device = config.style_gen_config.device training_lines: list[str] = [] - with open(hps.data.training_files, encoding="utf-8") as f: + with open(hps.data.training_files, "r", encoding="utf-8") as f: training_lines.extend(f.readlines()) with ThreadPoolExecutor(max_workers=num_processes) as executor: training_results = list( @@ -94,7 +94,7 @@ if __name__ == "__main__": ) val_lines: list[str] = [] - with open(hps.data.validation_files, encoding="utf-8") as f: + with open(hps.data.validation_files, "r", encoding="utf-8") as f: val_lines.extend(f.readlines()) with ThreadPoolExecutor(max_workers=num_processes) as executor: diff --git a/webui/merge.py b/webui/merge.py index f692b67..18454c9 100644 --- a/webui/merge.py +++ b/webui/merge.py @@ -47,11 +47,11 @@ def merge_style(model_name_a, model_name_b, weight, output_name, style_triple_li os.path.join(assets_root, model_name_b, "style_vectors.npy") ) # (style_num_b, 256) with open( - os.path.join(assets_root, model_name_a, "config.json"), encoding="utf-8" + os.path.join(assets_root, model_name_a, "config.json"), "r", encoding="utf-8" ) as f: config_a = json.load(f) with open( - os.path.join(assets_root, model_name_b, "config.json"), encoding="utf-8" + os.path.join(assets_root, model_name_b, "config.json"), "r", encoding="utf-8" ) as f: config_b = json.load(f) style2id_a = config_a["data"]["style2id"] @@ -88,7 +88,7 @@ def merge_style(model_name_a, model_name_b, weight, output_name, style_triple_li # recipe.jsonを読み込んで、style_triple_listを追記 info_path = os.path.join(assets_root, output_name, "recipe.json") if os.path.exists(info_path): - with open(info_path, encoding="utf-8") as f: + with open(info_path, "r", encoding="utf-8") as f: info = json.load(f) else: info = {} @@ -261,12 +261,12 @@ def update_two_model_names_dropdown(model_holder: TTSModelHolder): def load_styles_gr(model_name_a, model_name_b): config_path_a = os.path.join(assets_root, model_name_a, "config.json") - with open(config_path_a, encoding="utf-8") as f: + with open(config_path_a, "r", encoding="utf-8") as f: config_a = json.load(f) styles_a = list(config_a["data"]["style2id"].keys()) config_path_b = os.path.join(assets_root, model_name_b, "config.json") - with open(config_path_b, encoding="utf-8") as f: + with open(config_path_b, "r", encoding="utf-8") as f: config_b = json.load(f) styles_b = list(config_b["data"]["style2id"].keys()) From e8a76e547bc32aeb6108fdae929e20402e91245d Mon Sep 17 00:00:00 2001 From: tsukumi Date: Tue, 12 Mar 2024 18:22:34 +0000 Subject: [PATCH 3/6] Refactor: No preloading of BERT models to avoid unnecessary GPU VRAM consumption during training in the Web UI Since the BERT features of the dataset are pre-extracted by bert_gen.py, there is no need to load the BERT model at training time. --- style_bert_vits2/tts_model.py | 8 ++++++++ webui/inference.py | 13 ++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/style_bert_vits2/tts_model.py b/style_bert_vits2/tts_model.py index f7d4f21..da30f48 100644 --- a/style_bert_vits2/tts_model.py +++ b/style_bert_vits2/tts_model.py @@ -29,6 +29,7 @@ from style_bert_vits2.models.models import SynthesizerTrn from style_bert_vits2.models.models_jp_extra import ( SynthesizerTrn as SynthesizerTrnJPExtra, ) +from style_bert_vits2.nlp import bert_models from style_bert_vits2.voice import adjust_voice @@ -379,6 +380,13 @@ class TTSModelHolder: def get_model_for_gradio( self, model_name: str, model_path_str: str ) -> tuple[gr.Dropdown, gr.Button, gr.Dropdown]: + bert_models.load_model(Languages.JP) + bert_models.load_tokenizer(Languages.JP) + bert_models.load_model(Languages.EN) + bert_models.load_tokenizer(Languages.EN) + bert_models.load_model(Languages.ZH) + bert_models.load_tokenizer(Languages.ZH) + model_path = Path(model_path_str) if model_name not in self.model_files_dict: raise ValueError(f"Model `{model_name}` is not found") diff --git a/webui/inference.py b/webui/inference.py index db59829..d711846 100644 --- a/webui/inference.py +++ b/webui/inference.py @@ -19,7 +19,6 @@ from style_bert_vits2.constants import ( ) from style_bert_vits2.logging import logger from style_bert_vits2.models.infer import InvalidToneError -from style_bert_vits2.nlp import bert_models from style_bert_vits2.nlp.japanese import pyopenjtalk_worker as pyopenjtalk from style_bert_vits2.nlp.japanese.g2p_utils import g2kata_tone, kata_tone2phone_tone from style_bert_vits2.nlp.japanese.normalizer import normalize_text @@ -30,14 +29,10 @@ from style_bert_vits2.tts_model import TTSModelHolder ## pyopenjtalk_worker は TCP ソケットサーバーのため、ここで起動する pyopenjtalk.initialize_worker() -# 事前に BERT モデル/トークナイザーをロードしておく -## ここでロードしなくても必要になった際に自動ロードされるが、時間がかかるため事前にロードしておいた方が体験が良い -bert_models.load_model(Languages.JP) -bert_models.load_tokenizer(Languages.JP) -bert_models.load_model(Languages.EN) -bert_models.load_tokenizer(Languages.EN) -bert_models.load_model(Languages.ZH) -bert_models.load_tokenizer(Languages.ZH) +# Web UI での学習時の無駄な GPU VRAM 消費を避けるため、あえてここでは BERT モデルの事前ロードを行わない +# データセットの BERT 特徴量は事前に bert_gen.py により抽出されているため、学習時に BERT モデルをロードしておく必要はない +# BERT モデルの事前ロードは「ロード」ボタン押下時に実行される TTSModelHolder.get_model_for_gradio() 内で行われる +# Web UI での学習時、音声合成タブの「ロード」ボタンを押さなければ、BERT モデルが VRAM にロードされていない状態で学習を開始できる languages = [lang.value for lang in Languages] From 07d246b98b2f476eb7f6a0aa4856313d010cbb62 Mon Sep 17 00:00:00 2001 From: tsukumi Date: Tue, 12 Mar 2024 18:27:08 +0000 Subject: [PATCH 4/6] Refactor: run "hatch run style:fmt" --- pyproject.toml | 14 +++++++------- style_bert_vits2/nlp/bert_models.py | 20 ++++++++++++++++---- style_bert_vits2/nlp/chinese/g2p.py | 3 +-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e8c218d..a5f53ec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -75,18 +75,18 @@ cov = [ [tool.hatch.envs.style] detached = true dependencies = [ - "black", - "isort", + "black", + "isort", ] [tool.hatch.envs.style.scripts] check = [ - "black --check --diff .", - "isort --check-only --diff --profile black --gitignore --lai 2 .", + "black --check --diff .", + "isort --check-only --diff --profile black --gitignore --lai 2 .", ] fmt = [ - "black .", - "isort --profile black --gitignore --lai 2 .", - "check", + "black .", + "isort --profile black --gitignore --lai 2 .", + "check", ] [[tool.hatch.envs.test.matrix]] diff --git a/style_bert_vits2/nlp/bert_models.py b/style_bert_vits2/nlp/bert_models.py index eb84eb7..1166846 100644 --- a/style_bert_vits2/nlp/bert_models.py +++ b/style_bert_vits2/nlp/bert_models.py @@ -80,10 +80,14 @@ def load_model( if language == Languages.EN: model = cast( DebertaV2Model, - DebertaV2Model.from_pretrained(pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision), + DebertaV2Model.from_pretrained( + pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision + ), ) else: - model = AutoModelForMaskedLM.from_pretrained(pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision) + model = AutoModelForMaskedLM.from_pretrained( + pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision + ) __loaded_models[language] = model logger.info( f"Loaded the {language} BERT model from {pretrained_model_name_or_path}" @@ -135,9 +139,17 @@ def load_tokenizer( # BERT トークナイザーをロードし、辞書に格納して返す ## 英語のみ DebertaV2Tokenizer でロードする必要がある if language == Languages.EN: - tokenizer = DebertaV2Tokenizer.from_pretrained(pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision) + tokenizer = DebertaV2Tokenizer.from_pretrained( + pretrained_model_name_or_path, + cache_dir=cache_dir, + revision=revision, + ) else: - tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path, cache_dir=cache_dir, revision=revision) + tokenizer = AutoTokenizer.from_pretrained( + pretrained_model_name_or_path, + cache_dir=cache_dir, + revision=revision, + ) __loaded_tokenizers[language] = tokenizer logger.info( f"Loaded the {language} BERT tokenizer from {pretrained_model_name_or_path}" diff --git a/style_bert_vits2/nlp/chinese/g2p.py b/style_bert_vits2/nlp/chinese/g2p.py index 4ce2b26..f38e09f 100644 --- a/style_bert_vits2/nlp/chinese/g2p.py +++ b/style_bert_vits2/nlp/chinese/g2p.py @@ -10,8 +10,7 @@ from style_bert_vits2.nlp.symbols import PUNCTUATIONS with open(Path(__file__).parent / "opencpop-strict.txt", "r", encoding="utf-8") as f: __PINYIN_TO_SYMBOL_MAP = { - line.split("\t")[0]: line.strip().split("\t")[1] - for line in f.readlines() + line.split("\t")[0]: line.strip().split("\t")[1] for line in f.readlines() } From c4d6a8cdb7011f3478348541bbf2df3464b68aef Mon Sep 17 00:00:00 2001 From: tsukumi Date: Tue, 12 Mar 2024 18:45:54 +0000 Subject: [PATCH 5/6] Fix: large number of unnecessary files in built sdist Include in sdist only the minimum required files for style-bert-vits2 as a library. --- pyproject.toml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index a5f53ec..3045f03 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,6 +51,27 @@ Source = "https://github.com/litagin02/Style-Bert-VITS2" [tool.hatch.version] path = "style_bert_vits2/constants.py" +[tool.hatch.build.targets.sdist] +only-include = [ + ".vscode", + "dict_data/default.csv", + "docs", + "style_bert_vits2", + "tests", + "LGPL_LICENSE", + "LICENSE", + "pyproject.toml", + "README.md", +] +exclude = [ + ".git", + ".gitignore", + ".gitattributes", +] + +[tool.hatch.build.targets.wheel] +packages = ["style_bert_vits2"] + [tool.hatch.envs.test] dependencies = [ "coverage[toml]>=6.5", From fedd019c04cd46ffd18125bb31181deaadc3de41 Mon Sep 17 00:00:00 2001 From: tsukumi Date: Tue, 12 Mar 2024 19:01:45 +0000 Subject: [PATCH 6/6] Revert "Refactor: No preloading of BERT models to avoid unnecessary GPU VRAM consumption during training in the Web UI" This reverts commit e8a76e547bc32aeb6108fdae929e20402e91245d. --- style_bert_vits2/tts_model.py | 8 -------- webui/inference.py | 13 +++++++++---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/style_bert_vits2/tts_model.py b/style_bert_vits2/tts_model.py index da30f48..f7d4f21 100644 --- a/style_bert_vits2/tts_model.py +++ b/style_bert_vits2/tts_model.py @@ -29,7 +29,6 @@ from style_bert_vits2.models.models import SynthesizerTrn from style_bert_vits2.models.models_jp_extra import ( SynthesizerTrn as SynthesizerTrnJPExtra, ) -from style_bert_vits2.nlp import bert_models from style_bert_vits2.voice import adjust_voice @@ -380,13 +379,6 @@ class TTSModelHolder: def get_model_for_gradio( self, model_name: str, model_path_str: str ) -> tuple[gr.Dropdown, gr.Button, gr.Dropdown]: - bert_models.load_model(Languages.JP) - bert_models.load_tokenizer(Languages.JP) - bert_models.load_model(Languages.EN) - bert_models.load_tokenizer(Languages.EN) - bert_models.load_model(Languages.ZH) - bert_models.load_tokenizer(Languages.ZH) - model_path = Path(model_path_str) if model_name not in self.model_files_dict: raise ValueError(f"Model `{model_name}` is not found") diff --git a/webui/inference.py b/webui/inference.py index d711846..db59829 100644 --- a/webui/inference.py +++ b/webui/inference.py @@ -19,6 +19,7 @@ from style_bert_vits2.constants import ( ) from style_bert_vits2.logging import logger from style_bert_vits2.models.infer import InvalidToneError +from style_bert_vits2.nlp import bert_models from style_bert_vits2.nlp.japanese import pyopenjtalk_worker as pyopenjtalk from style_bert_vits2.nlp.japanese.g2p_utils import g2kata_tone, kata_tone2phone_tone from style_bert_vits2.nlp.japanese.normalizer import normalize_text @@ -29,10 +30,14 @@ from style_bert_vits2.tts_model import TTSModelHolder ## pyopenjtalk_worker は TCP ソケットサーバーのため、ここで起動する pyopenjtalk.initialize_worker() -# Web UI での学習時の無駄な GPU VRAM 消費を避けるため、あえてここでは BERT モデルの事前ロードを行わない -# データセットの BERT 特徴量は事前に bert_gen.py により抽出されているため、学習時に BERT モデルをロードしておく必要はない -# BERT モデルの事前ロードは「ロード」ボタン押下時に実行される TTSModelHolder.get_model_for_gradio() 内で行われる -# Web UI での学習時、音声合成タブの「ロード」ボタンを押さなければ、BERT モデルが VRAM にロードされていない状態で学習を開始できる +# 事前に BERT モデル/トークナイザーをロードしておく +## ここでロードしなくても必要になった際に自動ロードされるが、時間がかかるため事前にロードしておいた方が体験が良い +bert_models.load_model(Languages.JP) +bert_models.load_tokenizer(Languages.JP) +bert_models.load_model(Languages.EN) +bert_models.load_tokenizer(Languages.EN) +bert_models.load_model(Languages.ZH) +bert_models.load_tokenizer(Languages.ZH) languages = [lang.value for lang in Languages]