Eempostor commited on
Commit
188d16b
·
verified ·
1 Parent(s): 8b44d44

Delete lib/infer.py

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