Skip to content

Commit

Permalink
Update because training works
Browse files Browse the repository at this point in the history
  • Loading branch information
aitronz committed Jan 21, 2024
1 parent a59390c commit 338ca01
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 89 deletions.
2 changes: 1 addition & 1 deletion core.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def run_preprocess_script(model_name, dataset_path, sampling_rate):
str(sampling_rate),
str(per),
]
os.makedirs(os.path.join(logs_path, str(model_name)), exist_ok=True)

os.mkdir(os.path.join(logs_path, str(model_name)))
subprocess.run(command)
return f"Model {model_name} preprocessed successfully."

Expand Down
35 changes: 25 additions & 10 deletions rvc/train/index_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,39 @@

n_ivf = min(int(16 * np.sqrt(big_npy.shape[0])), big_npy.shape[0] // 39)

index = faiss.index_factory(256 if version == "v1" else 768, f"IVF{n_ivf},Flat")
# index_trained
index_trained = faiss.index_factory(
256 if version == "v1" else 768, f"IVF{n_ivf},Flat"
)
index_ivf_trained = faiss.extract_index_ivf(index_trained)
index_ivf_trained.nprobe = 1
index_trained.train(big_npy)

index_filename_trained = (
f"trained_IVF{n_ivf}_Flat_nprobe_{index_ivf_trained.nprobe}_{version}.index"
)
index_filepath_trained = os.path.join(exp_dir, index_filename_trained)

index_ivf = faiss.extract_index_ivf(index)
index_ivf.nprobe = 1
index.train(big_npy)
faiss.write_index(index_trained, index_filepath_trained)

index_filename = (
f"trained_IVF{n_ivf}_Flat_nprobe_{index_ivf.nprobe}_{version}.index"
# index_added
index_added = faiss.index_factory(
256 if version == "v1" else 768, f"IVF{n_ivf},Flat"
)
index_filepath = os.path.join(exp_dir, index_filename)
index_ivf_added = faiss.extract_index_ivf(index_added)
index_ivf_added.nprobe = 1
index_added.train(big_npy)

faiss.write_index(index, index_filepath)
index_filename_added = (
f"added_IVF{n_ivf}_Flat_nprobe_{index_ivf_added.nprobe}_{version}.index"
)
index_filepath_added = os.path.join(exp_dir, index_filename_added)

batch_size_add = 8192
for i in range(0, big_npy.shape[0], batch_size_add):
index.add(big_npy[i : i + batch_size_add])
index_added.add(big_npy[i : i + batch_size_add])

faiss.write_index(index, index_filepath)
faiss.write_index(index_added, index_filepath_added)

except Exception as error:
print(f"Failed to train index: {error}")
Expand Down
1 change: 1 addition & 0 deletions rvc/train/losses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch


def feature_loss(fmap_r, fmap_g):
loss = 0
for dr, dg in zip(fmap_r, fmap_g):
Expand Down
1 change: 1 addition & 0 deletions rvc/train/slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import soundfile
import numpy as np


class Slicer:
def __init__(
self,
Expand Down
21 changes: 11 additions & 10 deletions rvc/train/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def record(self):
elapsed_time = round(elapsed_time, 1)
elapsed_time_str = str(datetime.timedelta(seconds=int(elapsed_time)))
current_time = datetime.datetime.now().strftime("%H:%M:%S")
return f"[{current_time}] | ({elapsed_time_str})"
return f"time={current_time} | training_speed={elapsed_time_str}"


def main():
Expand Down Expand Up @@ -201,6 +201,7 @@ def run(
net_d = DDP(net_d)

try:
print("Starting training...")
_, _, _, epoch_str = load_checkpoint(
latest_checkpoint_path(hps.model_dir, "D_*.pth"), net_d, optim_d
)
Expand Down Expand Up @@ -477,10 +478,6 @@ def train_and_evaluate(rank, epoch, hps, nets, optims, scaler, loaders, writers,
if loss_kl > 9:
loss_kl = 9

# print([global_step, lr])
print(
f"[loss_disc={loss_disc:.3f} | loss_gen={loss_gen:.3f} | loss_fm={loss_fm:.3f} | loss_mel={loss_mel:.3f} | loss_kl={loss_kl:.3f}]"
)
scalar_dict = {
"loss/g/total": loss_gen_all,
"loss/d/total": loss_disc,
Expand Down Expand Up @@ -559,15 +556,15 @@ def train_and_evaluate(rank, epoch, hps, nets, optims, scaler, loaders, writers,
else:
ckpt = net_g.state_dict()
print(
"Saving small model... %s_e%s:%s"
"saving ckpt %s_e%s:%s"
% (
hps.name,
epoch,
save_final(
ckpt,
hps.sample_rate,
hps.if_f0,
hps.name + f"_e{epoch}_s{global_step}",
hps.name + "_e%s_s%s" % (epoch, global_step),
epoch,
hps.version,
hps,
Expand All @@ -576,16 +573,20 @@ def train_and_evaluate(rank, epoch, hps, nets, optims, scaler, loaders, writers,
)

if rank == 0:
print(f"Epoch {epoch}: {epoch_recorder.record()}")
print(
f"{hps.name} | epoch={epoch} | step={global_step} | {epoch_recorder.record()} | loss_disc={loss_disc:.3f} | loss_gen={loss_gen:.3f} | loss_fm={loss_fm:.3f} | loss_mel={loss_mel:.3f} | loss_kl={loss_kl:.3f}"
)
if epoch >= hps.total_epoch and rank == 0:
print("The training is over, finalizing the program...")
print(
f"Training has been successfully completed with {epoch} epoch and {global_step} steps."
)

if hasattr(net_g, "module"):
ckpt = net_g.module.state_dict()
else:
ckpt = net_g.state_dict()
print(
"Saving final model... %s"
"Saving final checkpoint: %s"
% (
save_final(
ckpt, hps.sample_rate, hps.if_f0, hps.name, epoch, hps.version, hps
Expand Down
37 changes: 2 additions & 35 deletions rvc/train/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ def load_checkpoint(checkpoint_path, model, optimizer=None, load_opt=1):
learning_rate = checkpoint_dict["learning_rate"]
if optimizer is not None and load_opt == 1:
optimizer.load_state_dict(checkpoint_dict["optimizer"])
print(f"Loaded checkpoint '{checkpoint_path}' (epochs {iteration})")
print(f"Loaded checkpoint '{checkpoint_path}' (epoch {iteration})")
return model, optimizer, learning_rate, iteration


def save_checkpoint(model, optimizer, learning_rate, iteration, checkpoint_path):
print(f"Saving model '{checkpoint_path}' (epochs {iteration})")
print(f"Saving model '{checkpoint_path}' (epoch {iteration})")
if hasattr(model, "module"):
state_dict = model.module.state_dict()
else:
Expand Down Expand Up @@ -218,34 +218,6 @@ def get_hparams():
required=True,
help="if caching the dataset in GPU memory, 1 or 0",
)
parser.add_argument(
"-sof",
"--stop_on_fit",
type=int,
required=False,
help="if retraining mode collapses, 1 or 0",
)
parser.add_argument(
"-sm",
"--smoothness",
type=float,
required=False,
help="smoothness for --stop_on_fit",
)
parser.add_argument(
"-rc",
"--retrain_collapse",
type=int,
required=False,
help="if retraining mode collapses, 1 or 0",
)
parser.add_argument(
"-ct",
"--collapse_threshold",
type=int,
required=False,
help="if retraining mode collapses, 1 or 0",
)
args = parser.parse_args()
name = args.experiment_dir
experiment_dir = os.path.join("./logs", args.experiment_dir)
Expand All @@ -267,11 +239,6 @@ def get_hparams():
hparams.if_latest = args.if_latest
hparams.save_every_weights = args.save_every_weights
hparams.if_cache_data_in_gpu = args.if_cache_data_in_gpu
hparams.if_stop_on_fit = args.stop_on_fit
hparams.smoothness = args.smoothness
hparams.if_retrain_collapse = args.retrain_collapse
if args.collapse_threshold != None:
hparams.collapse_threshold = args.collapse_threshold * 0.01
hparams.data.training_files = f"{experiment_dir}/filelist.txt"
return hparams

Expand Down
21 changes: 4 additions & 17 deletions tabs/inference/inference.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os, sys
import gradio as gr
import re
import regex as re
import shutil
import datetime
import random
Expand Down Expand Up @@ -39,10 +39,7 @@
os.path.join(root, file)
for root, _, files in os.walk(model_root, topdown=False)
for file in files
if (
file.endswith((".pth", ".onnx"))
and not (file.startswith("G_") or file.startswith("D_"))
)
if file.endswith((".pth", ".onnx"))
]

indexes_list = [
Expand Down Expand Up @@ -76,10 +73,7 @@ def change_choices():
os.path.join(root, file)
for root, _, files in os.walk(model_root, topdown=False)
for file in files
if (
file.endswith((".pth", ".onnx"))
and not (file.startswith("G_") or file.startswith("D_"))
)
if file.endswith((".pth", ".onnx"))
]

indexes_list = [
Expand Down Expand Up @@ -268,14 +262,7 @@ def inference_tab():
value=False,
interactive=True,
)
pitch = gr.Slider(
minimum=-12,
maximum=12,
step=1,
label=i18n("Pitch"),
value=0,
interactive=True,
)
pitch = gr.Slider(-12, 12, 0, label=i18n("Pitch"))
filter_radius = gr.Slider(
minimum=0,
maximum=7,
Expand Down
19 changes: 3 additions & 16 deletions tabs/tts/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
os.path.join(root, file)
for root, _, files in os.walk(model_root, topdown=False)
for file in files
if (
file.endswith((".pth", ".onnx"))
and not (file.startswith("G_") or file.startswith("D_"))
)
if file.endswith((".pth", ".onnx"))
]

indexes_list = [
Expand All @@ -67,10 +64,7 @@ def change_choices():
os.path.join(root, file)
for root, _, files in os.walk(model_root, topdown=False)
for file in files
if (
file.endswith((".pth", ".onnx"))
and not (file.startswith("G_") or file.startswith("D_"))
)
if file.endswith((".pth", ".onnx"))
]

indexes_list = [
Expand Down Expand Up @@ -257,14 +251,7 @@ def tts_tab():
interactive=True,
)

pitch = gr.Slider(
minimum=-12,
maximum=12,
step=1,
label=i18n("Pitch"),
value=0,
interactive=True,
)
pitch = gr.Slider(-12, 12, 0, label=i18n("Pitch"))
filter_radius = gr.Slider(
minimum=0,
maximum=7,
Expand Down

0 comments on commit 338ca01

Please sign in to comment.