Eempostor commited on
Commit
5a88a9e
·
verified ·
1 Parent(s): 98c7a3f

Delete lib/infer.py

Browse files
Files changed (1) hide show
  1. lib/infer.py +0 -219
lib/infer.py DELETED
@@ -1,219 +0,0 @@
1
- import os
2
- import shutil
3
- import gc
4
- import torch
5
- from multiprocessing import cpu_count
6
- from lib.modules import VC
7
- from lib.split_audio import split_silence_nonsilent, adjust_audio_lengths, combine_silence_nonsilent
8
-
9
- class Configs:
10
- def __init__(self, device, is_half):
11
- self.device = device
12
- self.is_half = is_half
13
- self.n_cpu = 0
14
- self.gpu_name = None
15
- self.gpu_mem = None
16
- self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
17
-
18
- def device_config(self) -> tuple:
19
- if torch.cuda.is_available():
20
- i_device = int(self.device.split(":")[-1])
21
- self.gpu_name = torch.cuda.get_device_name(i_device)
22
- #if (
23
- # ("16" in self.gpu_name and "V100" not in self.gpu_name.upper())
24
- # or "P40" in self.gpu_name.upper()
25
- # or "1060" in self.gpu_name
26
- # or "1070" in self.gpu_name
27
- # or "1080" in self.gpu_name
28
- # ):
29
- # print("16 series/10 series P40 forced single precision")
30
- # self.is_half = False
31
- # for config_file in ["32k.json", "40k.json", "48k.json"]:
32
- # with open(BASE_DIR / "src" / "configs" / config_file, "r") as f:
33
- # strr = f.read().replace("true", "false")
34
- # with open(BASE_DIR / "src" / "configs" / config_file, "w") as f:
35
- # f.write(strr)
36
- # with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "r") as f:
37
- # strr = f.read().replace("3.7", "3.0")
38
- # with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "w") as f:
39
- # f.write(strr)
40
- # else:
41
- # self.gpu_name = None
42
- # self.gpu_mem = int(
43
- # torch.cuda.get_device_properties(i_device).total_memory
44
- # / 1024
45
- # / 1024
46
- # / 1024
47
- # + 0.4
48
- # )
49
- # if self.gpu_mem <= 4:
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
- elif torch.backends.mps.is_available():
55
- print("No supported N-card found, use MPS for inference")
56
- self.device = "mps"
57
- else:
58
- print("No supported N-card found, use CPU for inference")
59
- self.device = "cpu"
60
- self.is_half = True
61
-
62
- if self.n_cpu == 0:
63
- self.n_cpu = cpu_count()
64
-
65
- if self.is_half:
66
- # 6G memory config
67
- x_pad = 3
68
- x_query = 10
69
- x_center = 60
70
- x_max = 65
71
- else:
72
- # 5G memory config
73
- x_pad = 1
74
- x_query = 6
75
- x_center = 38
76
- x_max = 41
77
-
78
- if self.gpu_mem != None and self.gpu_mem <= 4:
79
- x_pad = 1
80
- x_query = 5
81
- x_center = 30
82
- x_max = 32
83
-
84
- return x_pad, x_query, x_center, x_max
85
-
86
- def get_model(voice_model):
87
- model_dir = os.path.join(os.getcwd(), "models", voice_model)
88
- model_filename, index_filename = None, None
89
- for file in os.listdir(model_dir):
90
- ext = os.path.splitext(file)[1]
91
- if ext == '.pth':
92
- model_filename = file
93
- if ext == '.index':
94
- index_filename = file
95
-
96
- if model_filename is None:
97
- print(f'No model file exists in {models_dir}.')
98
- return None, None
99
-
100
- return os.path.join(model_dir, model_filename), os.path.join(model_dir, index_filename) if index_filename else ''
101
-
102
- def infer_audio(
103
- model_name,
104
- audio_path,
105
- f0_change=0,
106
- f0_method="rmvpe+",
107
- min_pitch="50",
108
- max_pitch="1100",
109
- crepe_hop_length=128,
110
- index_rate=0.75,
111
- filter_radius=3,
112
- rms_mix_rate=0.25,
113
- protect=0.33,
114
- split_infer=False,
115
- min_silence=500,
116
- silence_threshold=-50,
117
- seek_step=1,
118
- keep_silence=100,
119
- do_formant=False,
120
- quefrency=0,
121
- timbre=1,
122
- f0_autotune=False,
123
- audio_format="wav",
124
- resample_sr=0,
125
- hubert_model_path="assets/hubert/hubert_base.pt",
126
- rmvpe_model_path="assets/rmvpe/rmvpe.pt",
127
- fcpe_model_path="assets/fcpe/fcpe.pt"
128
- ):
129
- os.environ["rmvpe_model_path"] = rmvpe_model_path
130
- os.environ["fcpe_model_path"] = fcpe_model_path
131
- configs = Configs('cuda:0', True)
132
- vc = VC(configs)
133
- pth_path, index_path = get_model(model_name)
134
- vc_data = vc.get_vc(pth_path, protect, 0.5)
135
-
136
- if split_infer:
137
- inferred_files = []
138
- temp_dir = os.path.join(os.getcwd(), "seperate", "temp")
139
- os.makedirs(temp_dir, exist_ok=True)
140
- print("Splitting audio to silence and nonsilent segments.")
141
- silence_files, nonsilent_files = split_silence_nonsilent(audio_path, min_silence, silence_threshold, seek_step, keep_silence)
142
- print(f"Total silence segments: {len(silence_files)}.\nTotal nonsilent segments: {len(nonsilent_files)}.")
143
- for i, nonsilent_file in enumerate(nonsilent_files):
144
- print(f"Inferring nonsilent audio {i+1}")
145
- inference_info, audio_data, output_path = vc.vc_single(
146
- 0,
147
- audio_path,
148
- f0_change,
149
- f0_method,
150
- index_path,
151
- index_path,
152
- index_rate,
153
- filter_radius,
154
- resample_sr,
155
- rms_mix_rate,
156
- protect,
157
- audio_format,
158
- crepe_hop_length,
159
- do_formant,
160
- quefrency,
161
- timbre,
162
- min_pitch,
163
- max_pitch,
164
- f0_autotune,
165
- hubert_model_path
166
- )
167
- if inference_info[0] == "Success.":
168
- print("Inference ran successfully.")
169
- print(inference_info[1])
170
- print("Times:\nnpy: %.2fs f0: %.2fs infer: %.2fs\nTotal time: %.2fs" % (*inference_info[2],))
171
- else:
172
- print(f"An error occurred while processing.\n{inference_info[0]}")
173
- return None
174
- shutil.move(output_path, temp_dir)
175
- inferred_files.append(os.path.join(temp_dir, os.path.basename(output_path)))
176
- print("Adjusting inferred audio lengths.")
177
- adjusted_inferred_files = adjust_audio_lengths(nonsilent_files, inferred_files)
178
- print("Combining silence and inferred audios.")
179
- output_count = 1
180
- while True:
181
- output_path = os.path.join(os.getcwd(), "output", f"{os.path.splitext(os.path.basename(audio_path))[0]}{model_name}{f0_method.capitalize()}_{output_count}.{audio_format}")
182
- if not os.path.exists(output_path):
183
- break
184
- output_count += 1
185
- inferred_audio = combine_silence_nonsilent(silence_files, adjusted_inferred_files, keep_silence, output_path)
186
- shutil.rmtree(os.path.join(main_dir, "seperate", "temp"))
187
- else:
188
- inference_info, audio_data, output_path = vc.vc_single(
189
- 0,
190
- audio_path,
191
- f0_change,
192
- f0_method,
193
- index_path,
194
- index_path,
195
- index_rate,
196
- filter_radius,
197
- resample_sr,
198
- rms_mix_rate,
199
- protect,
200
- audio_format,
201
- crepe_hop_length,
202
- do_formant,
203
- quefrency,
204
- timbre,
205
- min_pitch,
206
- max_pitch,
207
- f0_autotune
208
- )
209
- if inference_info[0] == "Success.":
210
- print("Inference ran successfully.")
211
- print(inference_info[1])
212
- print("Times:\nnpy: %.2fs f0: %.2fs infer: %.2fs\nTotal time: %.2fs" % (*inference_info[2],))
213
- else:
214
- print(f"An error occurred while processing.\n{inference_info[0]}")
215
- return None
216
-
217
- del configs, vc
218
- gc.collect()
219
- return output_path