From 4b68fb0e1361b5ce861f1dbb34ec58c0ff826a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 27 Nov 2024 22:16:06 +0900 Subject: [PATCH] feat: update to latest torch & gradio version --- .github/workflows/unitest.yml | 1 - gui.py | 2 +- infer/lib/train/data_utils.py | 2 +- infer/lib/train/utils.py | 4 +--- infer/modules/train/train.py | 19 ++++++++++------ infer/modules/vc/utils.py | 4 ++-- rvc/f0/models.py | 3 ++- web.py | 42 +++++++++++++++++------------------ 8 files changed, 40 insertions(+), 37 deletions(-) diff --git a/.github/workflows/unitest.yml b/.github/workflows/unitest.yml index eeaea69..b9cfc91 100644 --- a/.github/workflows/unitest.yml +++ b/.github/workflows/unitest.yml @@ -21,7 +21,6 @@ jobs: wget https://github.com/fumiama/RVC-Models-Downloader/releases/download/v0.2.5/rvcmd_linux_amd64.deb sudo apt -y install ./rvcmd_linux_amd64.deb pip install --force pip==24.0 # fix fairseq installing issue https://github.com/facebookresearch/fairseq/issues/5552 - python -m pip install --upgrade pip python -m pip install --upgrade setuptools python -m pip install --upgrade wheel pip install torch torchvision torchaudio diff --git a/gui.py b/gui.py index 0de16b0..f2fc95d 100644 --- a/gui.py +++ b/gui.py @@ -251,7 +251,7 @@ if __name__ == "__main__": sg.FileBrowse( i18n("Select the .pth file"), initial_folder=os.path.join( - os.getcwd(), "assets/weights" + os.getcwd(), "assets", "weights" ), file_types=[("Model File", "*.pth")], ), diff --git a/infer/lib/train/data_utils.py b/infer/lib/train/data_utils.py index 1e1d1db..a5e4a93 100644 --- a/infer/lib/train/data_utils.py +++ b/infer/lib/train/data_utils.py @@ -111,7 +111,7 @@ class TextAudioLoaderMultiNSFsid(torch.utils.data.Dataset): spec_filename = filename.replace(".wav", ".spec.pt") if os.path.exists(spec_filename): try: - spec = torch.load(spec_filename) + spec = torch.load(spec_filename, weights_only=True) except: logger.warning("%s %s", spec_filename, traceback.format_exc()) spec = spectrogram_torch( diff --git a/infer/lib/train/utils.py b/infer/lib/train/utils.py index bad2c78..5d8b555 100644 --- a/infer/lib/train/utils.py +++ b/infer/lib/train/utils.py @@ -71,9 +71,7 @@ def load_checkpoint_d(checkpoint_path, combd, sbd, optimizer=None, load_opt=1): def load_checkpoint(checkpoint_path, model, optimizer=None, load_opt=1): assert os.path.isfile(checkpoint_path) - checkpoint_dict = torch.load(checkpoint_path, map_location="cpu") - - saved_state_dict = checkpoint_dict["model"] + saved_state_dict = torch.load(checkpoint_path, map_location="cpu", weights_only=True)["model"] if hasattr(model, "module"): state_dict = model.module.state_dict() else: diff --git a/infer/modules/train/train.py b/infer/modules/train/train.py index 789d1bc..bb6a4c0 100644 --- a/infer/modules/train/train.py +++ b/infer/modules/train/train.py @@ -131,9 +131,14 @@ def run(rank, n_gpus, hps: utils.HParams, logger: logging.Logger): writer = SummaryWriter(log_dir=hps.model_dir) writer_eval = SummaryWriter(log_dir=os.path.join(hps.model_dir, "eval")) - dist.init_process_group( - backend="gloo", init_method="env://", world_size=n_gpus, rank=rank - ) + try: + dist.init_process_group( + backend="gloo" if os.name == "nt" or not torch.cuda.is_available() else "nccl", init_method="env://", world_size=n_gpus, rank=rank + ) + except: + dist.init_process_group( + backend="gloo" if os.name == "nt" or not torch.cuda.is_available() else "nccl", init_method="env://?use_libuv=False", world_size=n_gpus, rank=rank + ) torch.manual_seed(hps.train.seed) if torch.cuda.is_available(): torch.cuda.set_device(rank) @@ -238,13 +243,13 @@ def run(rank, n_gpus, hps: utils.HParams, logger: logging.Logger): if hasattr(net_g, "module"): logger.info( net_g.module.load_state_dict( - torch.load(hps.pretrainG, map_location="cpu")["model"] + torch.load(hps.pretrainG, map_location="cpu", weights_only=True)["model"] ) ) ##测试不加载优化器 else: logger.info( net_g.load_state_dict( - torch.load(hps.pretrainG, map_location="cpu")["model"] + torch.load(hps.pretrainG, map_location="cpu", weights_only=True)["model"] ) ) ##测试不加载优化器 if hps.pretrainD != "": @@ -253,13 +258,13 @@ def run(rank, n_gpus, hps: utils.HParams, logger: logging.Logger): if hasattr(net_d, "module"): logger.info( net_d.module.load_state_dict( - torch.load(hps.pretrainD, map_location="cpu")["model"] + torch.load(hps.pretrainD, map_location="cpu", weights_only=True)["model"] ) ) else: logger.info( net_d.load_state_dict( - torch.load(hps.pretrainD, map_location="cpu")["model"] + torch.load(hps.pretrainD, map_location="cpu", weights_only=True)["model"] ) ) diff --git a/infer/modules/vc/utils.py b/infer/modules/vc/utils.py index 0df05f6..dfe4b72 100644 --- a/infer/modules/vc/utils.py +++ b/infer/modules/vc/utils.py @@ -1,4 +1,4 @@ -import os +import os, pathlib from fairseq import checkpoint_utils @@ -8,7 +8,7 @@ def get_index_path_from_model(sid): ( f for f in [ - os.path.join(root, name) + str(pathlib.Path(root, name)) for path in [os.getenv("outside_index_root"), os.getenv("index_root")] for root, _, files in os.walk(path, topdown=False) for name in files diff --git a/rvc/f0/models.py b/rvc/f0/models.py index 7c2853e..cb48905 100644 --- a/rvc/f0/models.py +++ b/rvc/f0/models.py @@ -7,8 +7,9 @@ def get_rmvpe( from rvc.f0.e2e import E2E model = E2E(4, 1, (2, 2)) - ckpt = torch.load(model_path, map_location=device) + ckpt = torch.load(model_path, map_location=device, weights_only=True) model.load_state_dict(ckpt) + del ckpt model.eval() if is_half: model = model.half() diff --git a/web.py b/web.py index 83414df..31e506c 100644 --- a/web.py +++ b/web.py @@ -46,7 +46,7 @@ tmp = os.path.join(now_dir, "TEMP") shutil.rmtree(tmp, ignore_errors=True) os.makedirs(tmp, exist_ok=True) os.makedirs(os.path.join(now_dir, "logs"), exist_ok=True) -os.makedirs(os.path.join(now_dir, "assets/weights"), exist_ok=True) +os.makedirs(os.path.join(now_dir, "assets", "weights"), exist_ok=True) os.environ["TEMP"] = tmp warnings.filterwarnings("ignore") torch.manual_seed(114514) @@ -142,20 +142,22 @@ index_root = os.getenv("index_root") outside_index_root = os.getenv("outside_index_root") names = [] -for name in os.listdir(weight_root): - if name.endswith(".pth"): - names.append(name) index_paths = [] +def lookup_names(weight_root): + global names + for name in os.listdir(weight_root): + if name.endswith(".pth"): + names.append(name) def lookup_indices(index_root): global index_paths - for root, dirs, files in os.walk(index_root, topdown=False): + for root, _, files in os.walk(index_root, topdown=False): for name in files: if name.endswith(".index") and "trained" not in name: - index_paths.append("%s/%s" % (root, name)) - + index_paths.append(str(pathlib.Path(root, name))) +lookup_names(weight_root) lookup_indices(index_root) lookup_indices(outside_index_root) uvr5_names = [] @@ -165,15 +167,12 @@ for name in os.listdir(weight_uvr5_root): def change_choices(): + global index_paths, names names = [] - for name in os.listdir(weight_root): - if name.endswith(".pth"): - names.append(name) + lookup_names(weight_root) index_paths = [] - for root, dirs, files in os.walk(index_root, topdown=False): - for name in files: - if name.endswith(".index") and "trained" not in name: - index_paths.append("%s/%s" % (root, name)) + lookup_indices(index_root) + lookup_indices(outside_index_root) return {"choices": sorted(names), "__type__": "update"}, { "choices": sorted(index_paths), "__type__": "update", @@ -223,16 +222,17 @@ def if_done_multi(done, ps): def preprocess_dataset(trainset_dir, exp_dir, sr, n_p): sr = sr_dict[sr] - os.makedirs("%s/logs/%s" % (now_dir, exp_dir), exist_ok=True) - f = open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir), "w") + exp_path = pathlib.Path(now_dir, "logs", exp_dir) + os.makedirs(exp_path, exist_ok=True) + log_file_path = exp_path / "preprocess.log" + f = open(log_file_path, "w") f.close() - cmd = '"%s" infer/modules/train/preprocess.py "%s" %s %s "%s/logs/%s" %s %.1f' % ( + cmd = '"%s" infer/modules/train/preprocess.py "%s" %s %s "%s" %s %.1f' % ( config.python_cmd, trainset_dir, sr, n_p, - now_dir, - exp_dir, + str(exp_path), config.noparallel, config.preprocess_per, ) @@ -249,12 +249,12 @@ def preprocess_dataset(trainset_dir, exp_dir, sr, n_p): ), ).start() while 1: - with open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir), "r") as f: + with open(log_file_path, "r") as f: yield (f.read()) sleep(1) if done[0]: break - with open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir), "r") as f: + with open(log_file_path, "r") as f: log = f.read() logger.info(log) yield log