Spaces:
Sleeping
Sleeping
File size: 17,865 Bytes
b5a064f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
import math
import os
import shutil
from multiprocessing import cpu_count
import gradio as gr
from lib.rvc.preprocessing import extract_f0, extract_feature, split
from lib.rvc.train import create_dataset_meta, glob_dataset, train_index, train_model
from modules import models, utils
from modules.shared import MODELS_DIR, device, half_support
from modules.ui import Tab
SR_DICT = {
"32k": 32000,
"40k": 40000,
"48k": 48000,
}
class Training(Tab):
def title(self):
return "Training"
def sort(self):
return 2
def ui(self, outlet):
def train_index_only(
model_name,
target_sr,
f0,
dataset_glob,
recursive,
multiple_speakers,
speaker_id,
gpu_id,
num_cpu_process,
norm_audio_when_preprocess,
pitch_extraction_algo,
run_train_index,
reduce_index_size,
maximum_index_size,
embedder_name,
embedding_channels,
embedding_output_layer,
ignore_cache,
):
maximum_index_size = int(maximum_index_size)
f0 = f0 == "Yes"
norm_audio_when_preprocess = norm_audio_when_preprocess == "Yes"
run_train_index = run_train_index == "Yes"
reduce_index_size = reduce_index_size == "Yes"
training_dir = os.path.join(MODELS_DIR, "training", "models", model_name)
gpu_ids = [int(x.strip()) for x in gpu_id.split(",")] if gpu_id else []
yield f"Training directory: {training_dir}"
if os.path.exists(training_dir) and ignore_cache:
shutil.rmtree(training_dir)
os.makedirs(training_dir, exist_ok=True)
datasets = glob_dataset(
dataset_glob,
speaker_id,
multiple_speakers=multiple_speakers,
recursive=recursive,
)
if len(datasets) == 0:
raise Exception("No audio files found")
yield "Preprocessing..."
split.preprocess_audio(
datasets,
SR_DICT[target_sr],
num_cpu_process,
training_dir,
norm_audio_when_preprocess,
os.path.join(
MODELS_DIR,
"training",
"mute",
"0_gt_wavs",
f"mute{target_sr}.wav",
),
)
if f0:
yield "Extracting f0..."
extract_f0.run(training_dir, num_cpu_process, pitch_extraction_algo)
yield "Extracting features..."
embedder_filepath, _, embedder_load_from = models.get_embedder(
embedder_name
)
if embedder_load_from == "local":
embedder_filepath = os.path.join(
MODELS_DIR, "embeddings", embedder_filepath
)
extract_feature.run(
training_dir,
embedder_filepath,
embedder_load_from,
int(embedding_channels),
int(embedding_output_layer),
gpu_ids,
)
out_dir = os.path.join(MODELS_DIR, "checkpoints")
yield "Training index..."
if run_train_index:
if not reduce_index_size:
maximum_index_size = None
train_index(
training_dir,
model_name,
out_dir,
int(embedding_channels),
num_cpu_process,
maximum_index_size,
)
yield "Training complete"
def train_all(
model_name,
version,
sampling_rate_str,
f0,
dataset_glob,
recursive,
multiple_speakers,
speaker_id,
gpu_id,
num_cpu_process,
norm_audio_when_preprocess,
pitch_extraction_algo,
batch_size,
augment,
augment_from_pretrain,
augment_path,
speaker_info_path,
cache_batch,
num_epochs,
save_every_epoch,
save_wav_with_checkpoint,
fp16,
save_only_last,
pre_trained_bottom_model_g,
pre_trained_bottom_model_d,
run_train_index,
reduce_index_size,
maximum_index_size,
embedder_name,
embedding_channels,
embedding_output_layer,
ignore_cache,
):
batch_size = int(batch_size)
num_epochs = int(num_epochs)
maximum_index_size = int(maximum_index_size)
f0 = f0 == "Yes"
norm_audio_when_preprocess = norm_audio_when_preprocess == "Yes"
run_train_index = run_train_index == "Yes"
reduce_index_size = reduce_index_size == "Yes"
training_dir = os.path.join(MODELS_DIR, "training", "models", model_name)
gpu_ids = [int(x.strip()) for x in gpu_id.split(",")] if gpu_id else []
if os.path.exists(training_dir) and ignore_cache:
shutil.rmtree(training_dir)
os.makedirs(training_dir, exist_ok=True)
yield f"Training directory: {training_dir}"
datasets = glob_dataset(
dataset_glob,
speaker_id,
multiple_speakers=multiple_speakers,
recursive=recursive,
training_dir=training_dir,
)
if len(datasets) == 0:
raise Exception("No audio files found")
yield "Preprocessing..."
split.preprocess_audio(
datasets,
SR_DICT[sampling_rate_str],
num_cpu_process,
training_dir,
norm_audio_when_preprocess,
os.path.join(
MODELS_DIR,
"training",
"mute",
"0_gt_wavs",
f"mute{sampling_rate_str}.wav",
),
)
if f0:
yield "Extracting f0..."
extract_f0.run(training_dir, num_cpu_process, pitch_extraction_algo)
yield "Extracting features..."
embedder_filepath, _, embedder_load_from = models.get_embedder(
embedder_name
)
if embedder_load_from == "local":
embedder_filepath = os.path.join(
MODELS_DIR, "embeddings", embedder_filepath
)
extract_feature.run(
training_dir,
embedder_filepath,
embedder_load_from,
int(embedding_channels),
int(embedding_output_layer),
gpu_ids,
None if len(gpu_ids) > 1 else device,
)
create_dataset_meta(training_dir, f0)
yield "Training model..."
print(f"train_all: emb_name: {embedder_name}")
config = utils.load_config(
version, training_dir, sampling_rate_str, embedding_channels, fp16
)
out_dir = os.path.join(MODELS_DIR, "checkpoints")
if not augment_from_pretrain:
augment_path = None
speaker_info_path = None
train_model(
gpu_ids,
config,
training_dir,
model_name,
out_dir,
sampling_rate_str,
f0,
batch_size,
augment,
augment_path,
speaker_info_path,
cache_batch,
num_epochs,
save_every_epoch,
save_wav_with_checkpoint,
pre_trained_bottom_model_g,
pre_trained_bottom_model_d,
embedder_name,
int(embedding_output_layer),
save_only_last,
None if len(gpu_ids) > 1 else device,
)
yield "Training index..."
if run_train_index:
if not reduce_index_size:
maximum_index_size = None
train_index(
training_dir,
model_name,
out_dir,
int(embedding_channels),
num_cpu_process,
maximum_index_size,
)
yield "Training completed"
with gr.Group():
with gr.Box():
with gr.Column():
with gr.Row():
with gr.Column():
model_name = gr.Textbox(label="Model Name")
ignore_cache = gr.Checkbox(label="Ignore cache")
with gr.Column():
dataset_glob = gr.Textbox(
label="Dataset glob", placeholder="data/**/*.wav"
)
recursive = gr.Checkbox(label="Recursive", value=True)
multiple_speakers = gr.Checkbox(
label="Multiple speakers", value=False
)
speaker_id = gr.Slider(
maximum=4,
minimum=0,
value=0,
step=1,
label="Speaker ID",
)
with gr.Row(equal_height=False):
version = gr.Radio(
choices=["v1", "v2"],
value="v2",
label="Model version",
)
target_sr = gr.Radio(
choices=["32k", "40k", "48k"],
value="40k",
label="Target sampling rate",
)
f0 = gr.Radio(
choices=["Yes", "No"],
value="Yes",
label="f0 Model",
)
with gr.Row(equal_height=False):
embedding_name = gr.Radio(
choices=list(models.EMBEDDINGS_LIST.keys()),
value="contentvec",
label="Using phone embedder",
)
embedding_channels = gr.Radio(
choices=["256", "768"],
value="768",
label="Embedding channels",
)
embedding_output_layer = gr.Radio(
choices=["9", "12"],
value="12",
label="Embedding output layer",
)
with gr.Row(equal_height=False):
gpu_id = gr.Textbox(
label="GPU ID",
value=", ".join([f"{x.index}" for x in utils.get_gpus()]),
)
num_cpu_process = gr.Slider(
minimum=0,
maximum=cpu_count(),
step=1,
value=math.ceil(cpu_count() / 2),
label="Number of CPU processes",
)
norm_audio_when_preprocess = gr.Radio(
choices=["Yes", "No"],
value="Yes",
label="Normalize audio volume when preprocess",
)
pitch_extraction_algo = gr.Radio(
choices=["dio", "harvest", "mangio-crepe", "crepe"],
value="crepe",
label="Pitch extraction algorithm",
)
with gr.Row(equal_height=False):
batch_size = gr.Number(value=4, label="Batch size")
num_epochs = gr.Number(
value=30,
label="Number of epochs",
)
save_every_epoch = gr.Slider(
minimum=0,
maximum=100,
value=10,
step=1,
label="Save every epoch",
)
save_wav_with_checkpoint = gr.Checkbox(
label="save_wav_with_checkpoint", value=False
)
cache_batch = gr.Checkbox(label="Cache batch", value=True)
fp16 = gr.Checkbox(
label="FP16", value=half_support, disabled=not half_support
)
save_only_last = gr.Checkbox(
label="Save only the latest G and D files", value=False
)
with gr.Row(equal_height=False):
augment = gr.Checkbox(label="Augment", value=False)
augment_from_pretrain = gr.Checkbox(
label="Augment From Pretrain", value=False
)
augment_path = gr.Textbox(
label="Pre trained generator path (pth)",
value="file is not prepared",
)
speaker_info_path = gr.Textbox(
label="speaker info path (npy)",
value="file is not prepared",
)
with gr.Row(equal_height=False):
pre_trained_generator = gr.Textbox(
label="Pre trained generator path",
value=os.path.join(
MODELS_DIR, "pretrained", "v2", "f0G40k.pth"
),
)
pre_trained_discriminator = gr.Textbox(
label="Pre trained discriminator path",
value=os.path.join(
MODELS_DIR, "pretrained", "v2", "f0D40k.pth"
),
)
with gr.Row(equal_height=False):
run_train_index = gr.Radio(
choices=["Yes", "No"],
value="Yes",
label="Train Index",
)
reduce_index_size = gr.Radio(
choices=["Yes", "No"],
value="No",
label="Reduce index size with kmeans",
)
maximum_index_size = gr.Number(
value=10000, label="maximum index size"
)
with gr.Row(equal_height=False):
status = gr.Textbox(value="", label="Status")
with gr.Row(equal_height=False):
train_index_button = gr.Button("Train Index", variant="primary")
train_all_button = gr.Button("Train", variant="primary")
train_index_button.click(
train_index_only,
inputs=[
model_name,
target_sr,
f0,
dataset_glob,
recursive,
multiple_speakers,
speaker_id,
gpu_id,
num_cpu_process,
norm_audio_when_preprocess,
pitch_extraction_algo,
run_train_index,
reduce_index_size,
maximum_index_size,
embedding_name,
embedding_channels,
embedding_output_layer,
ignore_cache,
],
outputs=[status],
)
train_all_button.click(
train_all,
inputs=[
model_name,
version,
target_sr,
f0,
dataset_glob,
recursive,
multiple_speakers,
speaker_id,
gpu_id,
num_cpu_process,
norm_audio_when_preprocess,
pitch_extraction_algo,
batch_size,
augment,
augment_from_pretrain,
augment_path,
speaker_info_path,
cache_batch,
num_epochs,
save_every_epoch,
save_wav_with_checkpoint,
fp16,
save_only_last,
pre_trained_generator,
pre_trained_discriminator,
run_train_index,
reduce_index_size,
maximum_index_size,
embedding_name,
embedding_channels,
embedding_output_layer,
ignore_cache,
],
outputs=[status],
)
|