From f2e7c18aa966f99876eaf98d48c6461bd0503219 Mon Sep 17 00:00:00 2001 From: litagin02 Date: Mon, 1 Jan 2024 09:26:36 +0900 Subject: [PATCH] Fix: ensure encoding=utf-8 for json --- default_style.py | 2 +- train_ms.py | 4 ++-- webui_merge.py | 16 +++++++++++----- webui_style_vectors.py | 8 ++++---- webui_train.py | 6 ++++-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/default_style.py b/default_style.py index f869290..ced093c 100644 --- a/default_style.py +++ b/default_style.py @@ -11,7 +11,7 @@ def set_style_config(json_path, output_path): json_dict = json.load(f) json_dict["data"]["num_styles"] = 1 json_dict["data"]["style2id"] = {DEFAULT_STYLE: 0} - with open(output_path, "w") as f: + with open(output_path, "w", encoding="utf-8") as f: json.dump(json_dict, f, indent=2) logger.info(f"Save style config (only {DEFAULT_STYLE}) to {output_path}") diff --git a/train_ms.py b/train_ms.py index 46c2d11..e494bf2 100644 --- a/train_ms.py +++ b/train_ms.py @@ -703,8 +703,8 @@ def train_and_evaluate( global_step += 1 # 本家ではこれをスピードアップのために消すと書かれていたので、一応消してみる - gc.collect() - torch.cuda.empty_cache() + # gc.collect() + # torch.cuda.empty_cache() if rank == 0: logger.info(f"====> Epoch: {epoch}, step: {global_step}") diff --git a/webui_merge.py b/webui_merge.py index c23ecc2..91adc01 100644 --- a/webui_merge.py +++ b/webui_merge.py @@ -42,9 +42,13 @@ def merge_style(model_name_a, model_name_b, weight, output_name, style_triple_li style_vectors_b = np.load( os.path.join(model_dir, model_name_b, "style_vectors.npy") ) # (style_num_b, 256) - with open(os.path.join(model_dir, model_name_a, "config.json")) as f: + with open( + os.path.join(model_dir, model_name_a, "config.json"), encoding="utf-8" + ) as f: config_a = json.load(f) - with open(os.path.join(model_dir, model_name_b, "config.json")) as f: + with open( + os.path.join(model_dir, model_name_b, "config.json"), encoding="utf-8" + ) as f: config_b = json.load(f) style2id_a = config_a["data"]["style2id"] style2id_b = config_b["data"]["style2id"] @@ -72,7 +76,9 @@ def merge_style(model_name_a, model_name_b, weight, output_name, style_triple_li new_config["data"]["num_styles"] = len(new_style2id) new_config["data"]["style2id"] = new_style2id new_config["model_name"] = output_name - with open(os.path.join(model_dir, output_name, "config.json"), "w") as f: + with open( + os.path.join(model_dir, output_name, "config.json"), "w", encoding="utf-8" + ) as f: json.dump(new_config, f, indent=2) return output_style_path, list(new_style2id.keys()) @@ -189,12 +195,12 @@ def update_two_model_names_dropdown(): def load_styles_gr(model_name_a, model_name_b): config_path_a = os.path.join(model_dir, model_name_a, "config.json") - with open(config_path_a) as f: + with open(config_path_a, encoding="utf-8") as f: config_a = json.load(f) styles_a = list(config_a["data"]["style2id"].keys()) config_path_b = os.path.join(model_dir, model_name_b, "config.json") - with open(config_path_b) as f: + with open(config_path_b, encoding="utf-8") as f: config_b = json.load(f) styles_b = list(config_b["data"]["style2id"].keys()) return gr.Textbox(value=", ".join(styles_a)), gr.Textbox(value=", ".join(styles_b)) diff --git a/webui_style_vectors.py b/webui_style_vectors.py index 3ca3f15..7635a6b 100644 --- a/webui_style_vectors.py +++ b/webui_style_vectors.py @@ -131,12 +131,12 @@ def save_style_vectors(model_name, style_names: str): return f"スタイルの数が合いません。`,`で正しく{len(centroids)}個に区切られているか確認してください: {style_names}" style_name_list = [name.strip() for name in style_name_list] - with open(config_path, "r") as f: + with open(config_path, "r", encoding="utf-8") as f: json_dict = json.load(f) json_dict["data"]["num_styles"] = len(style_name_list) style_dict = {name: i for i, name in enumerate(style_name_list)} json_dict["data"]["style2id"] = style_dict - with open(config_path, "w") as f: + with open(config_path, "w", encoding="utf-8") as f: json.dump(json_dict, f, indent=2) return f"成功!\n{style_vector_path}に保存し{config_path}を更新しました。" @@ -178,13 +178,13 @@ def save_style_vectors_from_files(model_name, audio_files_text, style_names_text style_name_list = style_name_list + style_names assert len(style_name_list) == len(style_vectors) - with open(config_path, "r") as f: + with open(config_path, "r", encoding="utf-8") as f: json_dict = json.load(f) json_dict["data"]["num_styles"] = len(style_name_list) style_dict = {name: i for i, name in enumerate(style_name_list)} json_dict["data"]["style2id"] = style_dict - with open(config_path, "w") as f: + with open(config_path, "w", encoding="utf-8") as f: json.dump(json_dict, f, indent=2) return f"成功!\n{style_vector_path}に保存し{config_path}を更新しました。" diff --git a/webui_train.py b/webui_train.py index 57a3242..7ffb00c 100644 --- a/webui_train.py +++ b/webui_train.py @@ -39,10 +39,12 @@ def initialize(model_name, batch_size, epochs, save_every_steps, bf16_run): logger.info("Step 1: start initialization...") dataset_path, _, train_path, val_path, config_path = get_path(model_name) if os.path.isfile(config_path): - config = json.load(open(config_path, "r", encoding="utf-8")) + with open(config_path, "r", encoding="utf-8") as f: + config = json.load(f) else: # Use default config - config = json.load(open("configs/config.json", "r", encoding="utf-8")) + with open("configs/config.json", "r", encoding="utf-8") as f: + config = json.load(f) config["model_name"] = model_name config["data"]["training_files"] = train_path config["data"]["validation_files"] = val_path