Update hevrvc.py
Browse files
hevrvc.py
CHANGED
@@ -7,6 +7,119 @@ import faiss
|
|
7 |
from sklearn.cluster import MiniBatchKMeans
|
8 |
import traceback
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def calculate_audio_duration(file_path):
|
11 |
duration_seconds = len(AudioSegment.from_file(file_path)) / 1000.0
|
12 |
return duration_seconds
|
@@ -107,22 +220,52 @@ def train_index(exp_dir1, version19):
|
|
107 |
return "\n".join(infos)
|
108 |
|
109 |
with gr.Blocks() as demo:
|
110 |
-
with gr.Tab("
|
111 |
-
with gr.
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
demo.launch()
|
128 |
|
|
|
7 |
from sklearn.cluster import MiniBatchKMeans
|
8 |
import traceback
|
9 |
|
10 |
+
import gradio as gr
|
11 |
+
import os
|
12 |
+
from random import shuffle
|
13 |
+
import json
|
14 |
+
import pathlib
|
15 |
+
from subprocess import Popen, PIPE, STDOUT
|
16 |
+
|
17 |
+
# Define the function for training
|
18 |
+
def click_train(
|
19 |
+
exp_dir1,
|
20 |
+
sr2,
|
21 |
+
if_f0_3,
|
22 |
+
spk_id5,
|
23 |
+
save_epoch10,
|
24 |
+
total_epoch11,
|
25 |
+
batch_size12,
|
26 |
+
if_save_latest13,
|
27 |
+
pretrained_G14,
|
28 |
+
pretrained_D15,
|
29 |
+
gpus16,
|
30 |
+
if_cache_gpu17,
|
31 |
+
if_save_every_weights18,
|
32 |
+
version19,
|
33 |
+
):
|
34 |
+
now_dir = os.getcwd()
|
35 |
+
exp_dir = f"{now_dir}/logs/{exp_dir1}"
|
36 |
+
os.makedirs(exp_dir, exist_ok=True)
|
37 |
+
gt_wavs_dir = f"{exp_dir}/0_gt_wavs"
|
38 |
+
feature_dir = (
|
39 |
+
f"{exp_dir}/3_feature256" if version19 == "v1" else f"{exp_dir}/3_feature768"
|
40 |
+
)
|
41 |
+
|
42 |
+
if if_f0_3:
|
43 |
+
f0_dir = f"{exp_dir}/2a_f0"
|
44 |
+
f0nsf_dir = f"{exp_dir}/2b-f0nsf"
|
45 |
+
names = (
|
46 |
+
set([name.split(".")[0] for name in os.listdir(gt_wavs_dir)])
|
47 |
+
& set([name.split(".")[0] for name in os.listdir(feature_dir)])
|
48 |
+
& set([name.split(".")[0] for name in os.listdir(f0_dir)])
|
49 |
+
& set([name.split(".")[0] for name in os.listdir(f0nsf_dir)])
|
50 |
+
)
|
51 |
+
else:
|
52 |
+
names = set([name.split(".")[0] for name in os.listdir(gt_wavs_dir)]) & set(
|
53 |
+
[name.split(".")[0] for name in os.listdir(feature_dir)]
|
54 |
+
)
|
55 |
+
|
56 |
+
opt = []
|
57 |
+
for name in names:
|
58 |
+
if if_f0_3:
|
59 |
+
opt.append(
|
60 |
+
f"{gt_wavs_dir.replace('\\', '\\\\')}/{name}.wav|{feature_dir.replace('\\', '\\\\')}/{name}.npy|{f0_dir.replace('\\', '\\\\')}/{name}.wav.npy|{f0nsf_dir.replace('\\', '\\\\')}/{name}.wav.npy|{spk_id5}"
|
61 |
+
)
|
62 |
+
else:
|
63 |
+
opt.append(
|
64 |
+
f"{gt_wavs_dir.replace('\\', '\\\\')}/{name}.wav|{feature_dir.replace('\\', '\\\\')}/{name}.npy|{spk_id5}"
|
65 |
+
)
|
66 |
+
|
67 |
+
fea_dim = 256 if version19 == "v1" else 768
|
68 |
+
if if_f0_3:
|
69 |
+
for _ in range(2):
|
70 |
+
opt.append(
|
71 |
+
f"{now_dir}/logs/mute/0_gt_wavs/mute{sr2}.wav|{now_dir}/logs/mute/3_feature{fea_dim}/mute.npy|{now_dir}/logs/mute/2a_f0/mute.wav.npy|{now_dir}/logs/mute/2b-f0nsf/mute.wav.npy|{spk_id5}"
|
72 |
+
)
|
73 |
+
else:
|
74 |
+
for _ in range(2):
|
75 |
+
opt.append(
|
76 |
+
f"{now_dir}/logs/mute/0_gt_wavs/mute{sr2}.wav|{now_dir}/logs/mute/3_feature{fea_dim}/mute.npy|{spk_id5}"
|
77 |
+
)
|
78 |
+
|
79 |
+
shuffle(opt)
|
80 |
+
with open(f"{exp_dir}/filelist.txt", "w") as f:
|
81 |
+
f.write("\n".join(opt))
|
82 |
+
|
83 |
+
print("Write filelist done")
|
84 |
+
print("Use gpus:", str(gpus16))
|
85 |
+
if pretrained_G14 == "":
|
86 |
+
print("No pretrained Generator")
|
87 |
+
if pretrained_D15 == "":
|
88 |
+
print("No pretrained Discriminator")
|
89 |
+
|
90 |
+
if version19 == "v1" or sr2 == "40k":
|
91 |
+
config_path = f"configs/v1/{sr2}.json"
|
92 |
+
else:
|
93 |
+
config_path = f"configs/v2/{sr2}.json"
|
94 |
+
|
95 |
+
config_save_path = os.path.join(exp_dir, "config.json")
|
96 |
+
if not pathlib.Path(config_save_path).exists():
|
97 |
+
with open(config_save_path, "w", encoding="utf-8") as f:
|
98 |
+
with open(config_path, "r") as config_file:
|
99 |
+
config_data = json.load(config_file)
|
100 |
+
json.dump(
|
101 |
+
config_data,
|
102 |
+
f,
|
103 |
+
ensure_ascii=False,
|
104 |
+
indent=4,
|
105 |
+
sort_keys=True,
|
106 |
+
)
|
107 |
+
f.write("\n")
|
108 |
+
|
109 |
+
cmd = (
|
110 |
+
f'python infer/modules/train/train.py -e "{exp_dir1}" -sr {sr2} -f0 {1 if if_f0_3 else 0} -bs {batch_size12} -g {gpus16} -te {total_epoch11} -se {save_epoch10} {"-pg " + pretrained_G14 if pretrained_G14 != "" else ""} {"-pd " + pretrained_D15 if pretrained_D15 != "" else ""} -l {1 if if_save_latest13 else 0} -c {1 if if_cache_gpu17 else 0} -sw {1 if if_save_every_weights18 else 0} -v {version19}'
|
111 |
+
)
|
112 |
+
|
113 |
+
p = Popen(cmd, shell=True, cwd=now_dir, stdout=PIPE, stderr=STDOUT, bufsize=1, universal_newlines=True)
|
114 |
+
|
115 |
+
for line in p.stdout:
|
116 |
+
print(line.strip())
|
117 |
+
|
118 |
+
p.wait()
|
119 |
+
return "After the training is completed, you can view the console training log or train.log under the experiment folder"
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
def calculate_audio_duration(file_path):
|
124 |
duration_seconds = len(AudioSegment.from_file(file_path)) / 1000.0
|
125 |
return duration_seconds
|
|
|
220 |
return "\n".join(infos)
|
221 |
|
222 |
with gr.Blocks() as demo:
|
223 |
+
with gr.Tab("Training"):
|
224 |
+
with gr.Tab("CREATE TRANING FILES - This will process the data, extract the features and create your index file for you!"):
|
225 |
+
with gr.Row():
|
226 |
+
model_name = gr.Textbox(label="Model Name", value="My-Voice")
|
227 |
+
dataset_folder = gr.Textbox(label="Dataset Folder", value="/content/dataset")
|
228 |
+
youtube_link = gr.Textbox(label="YouTube Link (optional)")
|
229 |
+
with gr.Row():
|
230 |
+
start_button = gr.Button("Create Training Files")
|
231 |
+
f0method = gr.Dropdown(["pm", "harvest", "rmvpe", "rmvpe_gpu"], label="F0 Method", value="rmvpe_gpu")
|
232 |
+
extract_button = gr.Button("Extract Features")
|
233 |
+
train_button = gr.Button("Train Index")
|
234 |
+
|
235 |
+
output = gr.Textbox(label="Output")
|
236 |
+
|
237 |
+
start_button.click(create_training_files, inputs=[model_name, dataset_folder, youtube_link], outputs=output)
|
238 |
+
extract_button.click(extract_features, inputs=[model_name, f0method], outputs=output)
|
239 |
+
train_button.click(train_index, inputs=[model_name, "v2"], outputs=output)
|
240 |
+
with gr.Tab("train"):
|
241 |
+
exp_dir1 = gr.Textbox(label="Experiment Directory", value="mymodel")
|
242 |
+
sr2 = gr.Dropdown(choices=["32k", "40k", "48k"], label="Sample Rate", value="32k")
|
243 |
+
if_f0_3 = gr.Checkbox(label="Use F0", value=True)
|
244 |
+
spk_id5 = gr.Number(label="Speaker ID", value=0)
|
245 |
+
save_epoch10 = gr.Slider(label="Save Frequency", minimum=5, maximum=50, step=5, value=25)
|
246 |
+
total_epoch11 = gr.Slider(label="Total Epochs", minimum=10, maximum=2000, step=10, value=500)
|
247 |
+
batch_size12 = gr.Slider(label="Batch Size", minimum=1, maximum=20, step=1, value=8)
|
248 |
+
if_save_latest13 = gr.Checkbox(label="Save Latest", value=True)
|
249 |
+
pretrained_G14 = gr.Textbox(label="Pretrained Generator File", value="/content/pre/assets/pretrained_v2/f0Ov2Super32kG.pth")
|
250 |
+
pretrained_D15 = gr.Textbox(label="Pretrained Discriminator File", value="/content/pre/assets/pretrained_v2/f0Ov2Super32kD.pth")
|
251 |
+
gpus16 = gr.Number(label="GPUs", value=0)
|
252 |
+
if_cache_gpu17 = gr.Checkbox(label="Cache GPU", value=False)
|
253 |
+
if_save_every_weights18 = gr.Checkbox(label="Save Every Weights", value=True)
|
254 |
+
version19 = gr.Textbox(label="Version", value="v2")
|
255 |
+
training_log = gr.Textbox(label="Training Log", interactive=False)
|
256 |
+
train_button = gr.Button("Start Training")
|
257 |
+
|
258 |
+
train_button.click(
|
259 |
+
fn=click_train,
|
260 |
+
inputs=[
|
261 |
+
exp_dir1, sr2, if_f0_3, spk_id5, save_epoch10, total_epoch11, batch_size12,
|
262 |
+
if_save_latest13, pretrained_G14, pretrained_D15, gpus16, if_cache_gpu17,
|
263 |
+
if_save_every_weights18, version19
|
264 |
+
],
|
265 |
+
outputs=training_log
|
266 |
+
)
|
267 |
+
|
268 |
+
|
269 |
|
270 |
demo.launch()
|
271 |
|