Pj12 commited on
Commit
fbe9aca
·
verified ·
1 Parent(s): 4f65c89

Delete rvc.py

Browse files
Files changed (1) hide show
  1. rvc.py +0 -159
rvc.py DELETED
@@ -1,159 +0,0 @@
1
- from multiprocessing import cpu_count
2
- from pathlib import Path
3
-
4
- import os
5
- import torch
6
- from fairseq import checkpoint_utils
7
- from scipy.io import wavfile
8
-
9
- from infer_pack.models import (
10
- SynthesizerTrnMs256NSFsid,
11
- SynthesizerTrnMs256NSFsid_nono,
12
- SynthesizerTrnMs768NSFsid,
13
- SynthesizerTrnMs768NSFsid_nono,
14
- SynthesizerTrnMs1024NSFsid,
15
- SynthesizerTrnMs1024NSFsid_nono,
16
- )
17
- from my_utils import load_audio
18
- from vc_infer_pipeline import VC
19
-
20
- BASE_DIR = Path(__file__).resolve().parent.parent
21
-
22
-
23
- class Config:
24
- def __init__(self, device, is_half):
25
- self.device = device
26
- self.is_half = is_half
27
- self.n_cpu = 0
28
- self.gpu_name = None
29
- self.gpu_mem = None
30
- self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
31
-
32
- def device_config(self) -> tuple:
33
- if torch.cuda.is_available():
34
- i_device = int(self.device.split(":")[-1])
35
- self.gpu_name = torch.cuda.get_device_name(i_device)
36
- if (
37
- ("16" in self.gpu_name and "V100" not in self.gpu_name.upper())
38
- or "P40" in self.gpu_name.upper()
39
- or "1060" in self.gpu_name
40
- or "1070" in self.gpu_name
41
- or "1080" in self.gpu_name
42
- ):
43
- print("16 series/10 series P40 forced single precision")
44
- self.is_half = False
45
- for config_file in ["32k.json", "40k.json", "48k.json"]:
46
- with open(BASE_DIR / "src" / "configs" / config_file, "r") as f:
47
- strr = f.read().replace("true", "false")
48
- with open(BASE_DIR / "src" / "configs" / config_file, "w") as f:
49
- f.write(strr)
50
- with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "r") as f:
51
- strr = f.read().replace("3.7", "3.0")
52
- with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "w") as f:
53
- f.write(strr)
54
- else:
55
- self.gpu_name = None
56
- self.gpu_mem = int(
57
- torch.cuda.get_device_properties(i_device).total_memory
58
- / 1024
59
- / 1024
60
- / 1024
61
- + 0.4
62
- )
63
- if self.gpu_mem <= 4:
64
- with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "r") as f:
65
- strr = f.read().replace("3.7", "3.0")
66
- with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "w") as f:
67
- f.write(strr)
68
- elif torch.backends.mps.is_available():
69
- print("No supported N-card found, use MPS for inference")
70
- self.device = "mps"
71
- else:
72
- print("No supported N-card found, use CPU for inference")
73
- self.device = "cpu"
74
- self.is_half = True
75
-
76
- if self.n_cpu == 0:
77
- self.n_cpu = cpu_count()
78
-
79
- if self.is_half:
80
- # 6G memory config
81
- x_pad = 3
82
- x_query = 10
83
- x_center = 60
84
- x_max = 65
85
- else:
86
- # 5G memory config
87
- x_pad = 1
88
- x_query = 6
89
- x_center = 38
90
- x_max = 41
91
-
92
- if self.gpu_mem != None and self.gpu_mem <= 4:
93
- x_pad = 1
94
- x_query = 5
95
- x_center = 30
96
- x_max = 32
97
-
98
- return x_pad, x_query, x_center, x_max
99
-
100
-
101
- def load_hubert(device, is_half, model_path):
102
- models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task([model_path], suffix='', )
103
- hubert = models[0]
104
- hubert = hubert.to(device)
105
-
106
- if is_half:
107
- hubert = hubert.half()
108
- else:
109
- hubert = hubert.float()
110
-
111
- hubert.eval()
112
- return hubert
113
-
114
-
115
- def get_vc(device, is_half, config, model_path):
116
- cpt = torch.load(model_path, map_location='cpu')
117
- if "config" not in cpt or "weight" not in cpt:
118
- raise ValueError(f'Incorrect format for {model_path}. Use a voice model trained using RVC v2 instead.')
119
-
120
- tgt_sr = cpt["config"][-1]
121
- cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
122
- if_f0 = cpt.get("f0", 1)
123
- version = cpt.get("version", "v1")
124
-
125
- if version == "v1" and os.path.exists("/kaggle/working/AICoverGen-NO-UI-en/HuBert_Large.txt") == False:
126
- if if_f0 == 1:
127
- net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half)
128
- else:
129
- net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
130
- elif version == "v2" and os.path.exists("/kaggle/working/AICoverGen-NO-UI-en/HuBert_Large.txt") == False:
131
- if if_f0 == 1:
132
- net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=is_half)
133
- else:
134
- net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
135
- else:
136
- if if_f0 == 1:
137
- net_g = SynthesizerTrnMs1024NSFsid(*cpt["config"], is_half=is_half)
138
- else:
139
- net_g = SynthesizerTrnMs1024NSFsid_nono(*cpt["config"])
140
-
141
- del net_g.enc_q
142
- print(net_g.load_state_dict(cpt["weight"], strict=False))
143
- net_g.eval().to(device)
144
-
145
- if is_half:
146
- net_g = net_g.half()
147
- else:
148
- net_g = net_g.float()
149
-
150
- vc = VC(tgt_sr, config)
151
- return cpt, version, net_g, tgt_sr, vc
152
-
153
-
154
- def rvc_infer(index_path, index_rate, input_path, output_path, pitch_change, f0_method, cpt, version, net_g, filter_radius, tgt_sr, rms_mix_rate, protect, crepe_hop_length, vc, hubert_model):
155
- audio = load_audio(input_path, 16000)
156
- times = [0, 0, 0]
157
- if_f0 = cpt.get('f0', 1)
158
- audio_opt = vc.pipeline(hubert_model, net_g, 0, audio, input_path, times, pitch_change, f0_method, index_path, index_rate, if_f0, filter_radius, tgt_sr, 0, rms_mix_rate, version, protect, crepe_hop_length)
159
- wavfile.write(output_path, tgt_sr, audio_opt)