Fix: ensure encoding=utf-8 for json

This commit is contained in:
litagin02
2024-01-01 09:26:36 +09:00
parent e9db74ac6d
commit f2e7c18aa9
5 changed files with 22 additions and 14 deletions

View File

@@ -11,7 +11,7 @@ def set_style_config(json_path, output_path):
json_dict = json.load(f) json_dict = json.load(f)
json_dict["data"]["num_styles"] = 1 json_dict["data"]["num_styles"] = 1
json_dict["data"]["style2id"] = {DEFAULT_STYLE: 0} 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) json.dump(json_dict, f, indent=2)
logger.info(f"Save style config (only {DEFAULT_STYLE}) to {output_path}") logger.info(f"Save style config (only {DEFAULT_STYLE}) to {output_path}")

View File

@@ -703,8 +703,8 @@ def train_and_evaluate(
global_step += 1 global_step += 1
# 本家ではこれをスピードアップのために消すと書かれていたので、一応消してみる # 本家ではこれをスピードアップのために消すと書かれていたので、一応消してみる
gc.collect() # gc.collect()
torch.cuda.empty_cache() # torch.cuda.empty_cache()
if rank == 0: if rank == 0:
logger.info(f"====> Epoch: {epoch}, step: {global_step}") logger.info(f"====> Epoch: {epoch}, step: {global_step}")

View File

@@ -42,9 +42,13 @@ def merge_style(model_name_a, model_name_b, weight, output_name, style_triple_li
style_vectors_b = np.load( style_vectors_b = np.load(
os.path.join(model_dir, model_name_b, "style_vectors.npy") os.path.join(model_dir, model_name_b, "style_vectors.npy")
) # (style_num_b, 256) ) # (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) 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) config_b = json.load(f)
style2id_a = config_a["data"]["style2id"] style2id_a = config_a["data"]["style2id"]
style2id_b = config_b["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"]["num_styles"] = len(new_style2id)
new_config["data"]["style2id"] = new_style2id new_config["data"]["style2id"] = new_style2id
new_config["model_name"] = output_name 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) json.dump(new_config, f, indent=2)
return output_style_path, list(new_style2id.keys()) 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): def load_styles_gr(model_name_a, model_name_b):
config_path_a = os.path.join(model_dir, model_name_a, "config.json") 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) config_a = json.load(f)
styles_a = list(config_a["data"]["style2id"].keys()) styles_a = list(config_a["data"]["style2id"].keys())
config_path_b = os.path.join(model_dir, model_name_b, "config.json") 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) config_b = json.load(f)
styles_b = list(config_b["data"]["style2id"].keys()) styles_b = list(config_b["data"]["style2id"].keys())
return gr.Textbox(value=", ".join(styles_a)), gr.Textbox(value=", ".join(styles_b)) return gr.Textbox(value=", ".join(styles_a)), gr.Textbox(value=", ".join(styles_b))

View File

@@ -131,12 +131,12 @@ def save_style_vectors(model_name, style_names: str):
return f"スタイルの数が合いません。`,`で正しく{len(centroids)}個に区切られているか確認してください: {style_names}" return f"スタイルの数が合いません。`,`で正しく{len(centroids)}個に区切られているか確認してください: {style_names}"
style_name_list = [name.strip() for name in style_name_list] 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 = json.load(f)
json_dict["data"]["num_styles"] = len(style_name_list) json_dict["data"]["num_styles"] = len(style_name_list)
style_dict = {name: i for i, name in enumerate(style_name_list)} style_dict = {name: i for i, name in enumerate(style_name_list)}
json_dict["data"]["style2id"] = style_dict 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) json.dump(json_dict, f, indent=2)
return f"成功!\n{style_vector_path}に保存し{config_path}を更新しました。" 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 style_name_list = style_name_list + style_names
assert len(style_name_list) == len(style_vectors) 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 = json.load(f)
json_dict["data"]["num_styles"] = len(style_name_list) json_dict["data"]["num_styles"] = len(style_name_list)
style_dict = {name: i for i, name in enumerate(style_name_list)} style_dict = {name: i for i, name in enumerate(style_name_list)}
json_dict["data"]["style2id"] = style_dict 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) json.dump(json_dict, f, indent=2)
return f"成功!\n{style_vector_path}に保存し{config_path}を更新しました。" return f"成功!\n{style_vector_path}に保存し{config_path}を更新しました。"

View File

@@ -39,10 +39,12 @@ def initialize(model_name, batch_size, epochs, save_every_steps, bf16_run):
logger.info("Step 1: start initialization...") logger.info("Step 1: start initialization...")
dataset_path, _, train_path, val_path, config_path = get_path(model_name) dataset_path, _, train_path, val_path, config_path = get_path(model_name)
if os.path.isfile(config_path): 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: else:
# Use default config # 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["model_name"] = model_name
config["data"]["training_files"] = train_path config["data"]["training_files"] = train_path
config["data"]["validation_files"] = val_path config["data"]["validation_files"] = val_path