Eempostor commited on
Commit
aa497de
·
verified ·
1 Parent(s): 6eb8c5d

Delete lib/infer.py

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