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
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
@Desc: 全局配置文件读取
|
3 |
+
"""
|
4 |
+
import argparse
|
5 |
+
import yaml
|
6 |
+
from typing import Dict, List
|
7 |
+
import os
|
8 |
+
import shutil
|
9 |
+
import sys
|
10 |
+
|
11 |
+
|
12 |
+
class Resample_config:
|
13 |
+
"""重采样配置"""
|
14 |
+
|
15 |
+
def __init__(self, in_dir: str, out_dir: str, sampling_rate: int = 44100):
|
16 |
+
self.sampling_rate: int = sampling_rate # 目标采样率
|
17 |
+
self.in_dir: str = in_dir # 待处理音频目录路径
|
18 |
+
self.out_dir: str = out_dir # 重采样输出路径
|
19 |
+
|
20 |
+
@classmethod
|
21 |
+
def from_dict(cls, dataset_path: str, data: Dict[str, any]):
|
22 |
+
"""从字典中生成实例"""
|
23 |
+
|
24 |
+
# 不检查路径是否有效,此逻辑在resample.py中处理
|
25 |
+
data["in_dir"] = os.path.join(dataset_path, data["in_dir"])
|
26 |
+
data["out_dir"] = os.path.join(dataset_path, data["out_dir"])
|
27 |
+
|
28 |
+
return cls(**data)
|
29 |
+
|
30 |
+
|
31 |
+
class Preprocess_text_config:
|
32 |
+
"""数据预处理配置"""
|
33 |
+
|
34 |
+
def __init__(
|
35 |
+
self,
|
36 |
+
transcription_path: str,
|
37 |
+
cleaned_path: str,
|
38 |
+
train_path: str,
|
39 |
+
val_path: str,
|
40 |
+
config_path: str,
|
41 |
+
val_per_spk: int = 5,
|
42 |
+
max_val_total: int = 10000,
|
43 |
+
clean: bool = True,
|
44 |
+
):
|
45 |
+
self.transcription_path: str = transcription_path # 原始文本文件路径,文本格式应为{wav_path}|{speaker_name}|{language}|{text}。
|
46 |
+
self.cleaned_path: str = cleaned_path # 数据清洗后文本路径,可以不填。不填则将在原始文本目录生成
|
47 |
+
self.train_path: str = train_path # 训练集路径,可以不填。不填则将在原始文本目录生成
|
48 |
+
self.val_path: str = val_path # 验证集路径,可以不填。不填则将在原始文本目录生成
|
49 |
+
self.config_path: str = config_path # 配置文件路径
|
50 |
+
self.val_per_spk: int = val_per_spk # 每个speaker的验证集条数
|
51 |
+
self.max_val_total: int = max_val_total # 验证集最大条数,多于的会被截断并放到训练集中
|
52 |
+
self.clean: bool = clean # 是否进行数据清洗
|
53 |
+
|
54 |
+
@classmethod
|
55 |
+
def from_dict(cls, dataset_path: str, data: Dict[str, any]):
|
56 |
+
"""从字典中生成实例"""
|
57 |
+
|
58 |
+
data["transcription_path"] = os.path.join(
|
59 |
+
dataset_path, data["transcription_path"]
|
60 |
+
)
|
61 |
+
if data["cleaned_path"] == "" or data["cleaned_path"] is None:
|
62 |
+
data["cleaned_path"] = None
|
63 |
+
else:
|
64 |
+
data["cleaned_path"] = os.path.join(dataset_path, data["cleaned_path"])
|
65 |
+
data["train_path"] = os.path.join(dataset_path, data["train_path"])
|
66 |
+
data["val_path"] = os.path.join(dataset_path, data["val_path"])
|
67 |
+
data["config_path"] = os.path.join(dataset_path, data["config_path"])
|
68 |
+
|
69 |
+
return cls(**data)
|
70 |
+
|
71 |
+
|
72 |
+
class Bert_gen_config:
|
73 |
+
"""bert_gen 配置"""
|
74 |
+
|
75 |
+
def __init__(
|
76 |
+
self,
|
77 |
+
config_path: str,
|
78 |
+
num_processes: int = 2,
|
79 |
+
device: str = "cuda",
|
80 |
+
use_multi_device: bool = False,
|
81 |
+
):
|
82 |
+
self.config_path = config_path
|
83 |
+
self.num_processes = num_processes
|
84 |
+
self.device = device
|
85 |
+
self.use_multi_device = use_multi_device
|
86 |
+
|
87 |
+
@classmethod
|
88 |
+
def from_dict(cls, dataset_path: str, data: Dict[str, any]):
|
89 |
+
data["config_path"] = os.path.join(dataset_path, data["config_path"])
|
90 |
+
|
91 |
+
return cls(**data)
|
92 |
+
|
93 |
+
|
94 |
+
class Emo_gen_config:
|
95 |
+
"""emo_gen 配置"""
|
96 |
+
|
97 |
+
def __init__(
|
98 |
+
self,
|
99 |
+
config_path: str,
|
100 |
+
num_processes: int = 2,
|
101 |
+
device: str = "cuda",
|
102 |
+
):
|
103 |
+
self.config_path = config_path
|
104 |
+
self.num_processes = num_processes
|
105 |
+
self.device = device
|
106 |
+
|
107 |
+
@classmethod
|
108 |
+
def from_dict(cls, dataset_path: str, data: Dict[str, any]):
|
109 |
+
data["config_path"] = os.path.join(dataset_path, data["config_path"])
|
110 |
+
|
111 |
+
return cls(**data)
|
112 |
+
|
113 |
+
|
114 |
+
class Train_ms_config:
|
115 |
+
"""训练配置"""
|
116 |
+
|
117 |
+
def __init__(
|
118 |
+
self,
|
119 |
+
config_path: str,
|
120 |
+
env: Dict[str, any],
|
121 |
+
base: Dict[str, any],
|
122 |
+
model: str,
|
123 |
+
num_workers: int,
|
124 |
+
spec_cache: bool,
|
125 |
+
keep_ckpts: int,
|
126 |
+
):
|
127 |
+
self.env = env # 需要加载的环境变量
|
128 |
+
self.base = base # 底模配置
|
129 |
+
self.model = model # 训练模型存储目录,该路径为相对于dataset_path的路径,而非项目根目录
|
130 |
+
self.config_path = config_path # 配置文件路径
|
131 |
+
self.num_workers = num_workers # worker数量
|
132 |
+
self.spec_cache = spec_cache # 是否启用spec缓存
|
133 |
+
self.keep_ckpts = keep_ckpts # ckpt数量
|
134 |
+
|
135 |
+
@classmethod
|
136 |
+
def from_dict(cls, dataset_path: str, data: Dict[str, any]):
|
137 |
+
# data["model"] = os.path.join(dataset_path, data["model"])
|
138 |
+
data["config_path"] = os.path.join(dataset_path, data["config_path"])
|
139 |
+
|
140 |
+
return cls(**data)
|
141 |
+
|
142 |
+
|
143 |
+
class Webui_config:
|
144 |
+
"""webui 配置"""
|
145 |
+
|
146 |
+
def __init__(
|
147 |
+
self,
|
148 |
+
device: str,
|
149 |
+
model: str,
|
150 |
+
config_path: str,
|
151 |
+
language_identification_library: str,
|
152 |
+
port: int = 7860,
|
153 |
+
share: bool = False,
|
154 |
+
debug: bool = False,
|
155 |
+
):
|
156 |
+
self.device: str = device
|
157 |
+
self.model: str = model # 端口号
|
158 |
+
self.config_path: str = config_path # 是否公开部署,对外网开放
|
159 |
+
self.port: int = port # 是否开启debug模式
|
160 |
+
self.share: bool = share # 模型路径
|
161 |
+
self.debug: bool = debug # 配置文件路径
|
162 |
+
self.language_identification_library: str = (
|
163 |
+
language_identification_library # 语种识别库
|
164 |
+
)
|
165 |
+
|
166 |
+
@classmethod
|
167 |
+
def from_dict(cls, dataset_path: str, data: Dict[str, any]):
|
168 |
+
data["config_path"] = os.path.join(dataset_path, data["config_path"])
|
169 |
+
data["model"] = os.path.join(dataset_path, data["model"])
|
170 |
+
return cls(**data)
|
171 |
+
|
172 |
+
|
173 |
+
class Server_config:
|
174 |
+
def __init__(
|
175 |
+
self, models: List[Dict[str, any]], port: int = 5000, device: str = "cuda"
|
176 |
+
):
|
177 |
+
self.models: List[Dict[str, any]] = models # 需要加载的所有模型的配置
|
178 |
+
self.port: int = port # 端口号
|
179 |
+
self.device: str = device # 模型默认使用设备
|
180 |
+
|
181 |
+
@classmethod
|
182 |
+
def from_dict(cls, data: Dict[str, any]):
|
183 |
+
return cls(**data)
|
184 |
+
|
185 |
+
|
186 |
+
class Translate_config:
|
187 |
+
"""翻译api配置"""
|
188 |
+
|
189 |
+
def __init__(self, app_key: str, secret_key: str):
|
190 |
+
self.app_key = app_key
|
191 |
+
self.secret_key = secret_key
|
192 |
+
|
193 |
+
@classmethod
|
194 |
+
def from_dict(cls, data: Dict[str, any]):
|
195 |
+
return cls(**data)
|
196 |
+
|
197 |
+
|
198 |
+
class Config:
|
199 |
+
def __init__(self, config_path: str):
|
200 |
+
if not os.path.isfile(config_path) and os.path.isfile("default_config.yml"):
|
201 |
+
shutil.copy(src="default_config.yml", dst=config_path)
|
202 |
+
print(
|
203 |
+
f"已根据默认配置文件default_config.yml生成配置文件{config_path}。请按该配置文件的说明进行配置后重新运行。"
|
204 |
+
)
|
205 |
+
print("如无特殊需求,请勿修改default_config.yml或备份该文件。")
|
206 |
+
sys.exit(0)
|
207 |
+
with open(file=config_path, mode="r", encoding="utf-8") as file:
|
208 |
+
yaml_config: Dict[str, any] = yaml.safe_load(file.read())
|
209 |
+
dataset_path: str = yaml_config["dataset_path"]
|
210 |
+
openi_token: str = yaml_config["openi_token"]
|
211 |
+
self.dataset_path: str = dataset_path
|
212 |
+
self.mirror: str = yaml_config["mirror"]
|
213 |
+
self.openi_token: str = openi_token
|
214 |
+
self.resample_config: Resample_config = Resample_config.from_dict(
|
215 |
+
dataset_path, yaml_config["resample"]
|
216 |
+
)
|
217 |
+
self.preprocess_text_config: Preprocess_text_config = (
|
218 |
+
Preprocess_text_config.from_dict(
|
219 |
+
dataset_path, yaml_config["preprocess_text"]
|
220 |
+
)
|
221 |
+
)
|
222 |
+
self.bert_gen_config: Bert_gen_config = Bert_gen_config.from_dict(
|
223 |
+
dataset_path, yaml_config["bert_gen"]
|
224 |
+
)
|
225 |
+
self.train_ms_config: Train_ms_config = Train_ms_config.from_dict(
|
226 |
+
dataset_path, yaml_config["train_ms"]
|
227 |
+
)
|
228 |
+
self.webui_config: Webui_config = Webui_config.from_dict(
|
229 |
+
dataset_path, yaml_config["webui"]
|
230 |
+
)
|
231 |
+
self.server_config: Server_config = Server_config.from_dict(
|
232 |
+
yaml_config["server"]
|
233 |
+
)
|
234 |
+
self.translate_config: Translate_config = Translate_config.from_dict(
|
235 |
+
yaml_config["translate"]
|
236 |
+
)
|
237 |
+
|
238 |
+
|
239 |
+
parser = argparse.ArgumentParser()
|
240 |
+
# 为避免与以前的config.json起冲突,将其更名如下
|
241 |
+
parser.add_argument("-y", "--yml_config", type=str, default="config.yml")
|
242 |
+
args, _ = parser.parse_known_args()
|
243 |
+
config = Config(args.yml_config)
|