Eempostor commited on
Commit
7bd5239
·
verified ·
1 Parent(s): 58d9737

Delete lib/infer.py

Browse files
Files changed (1) hide show
  1. lib/infer.py +0 -212
lib/infer.py DELETED
@@ -1,212 +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
- ):
125
- configs = Configs('cuda:0', True)
126
- vc = VC(configs)
127
- pth_path, index_path = get_model(model_name)
128
- vc_data = vc.get_vc(pth_path, protect, 0.5)
129
-
130
- if split_infer:
131
- inferred_files = []
132
- temp_dir = os.path.join(main_dir, "seperate", "temp")
133
- os.makedirs(temp_dir, exist_ok=True)
134
- print("Splitting audio to silence and nonsilent segments.")
135
- silence_files, nonsilent_files = split_silence_nonsilent(audio_path, min_silence, silence_threshold, seek_step, keep_silence)
136
- print(f"Total silence segments: {len(silence_files)}.\nTotal nonsilent segments: {len(nonsilent_files)}.")
137
- for i, nonsilent_file in enumerate(nonsilent_files):
138
- print(f"Inferring nonsilent audio {i+1}")
139
- inference_info, audio_data, output_path = vc.vc_single(
140
- 0,
141
- audio_path,
142
- f0_change,
143
- f0_method,
144
- index_path,
145
- index_path,
146
- index_rate,
147
- filter_radius,
148
- resample_sr,
149
- rms_mix_rate,
150
- protect,
151
- audio_format,
152
- crepe_hop_length,
153
- do_formant,
154
- quefrency,
155
- timbre,
156
- min_pitch,
157
- max_pitch,
158
- f0_autotune
159
- )
160
- if inference_info[0] == "Success.":
161
- print("Inference ran successfully.")
162
- print(inference_info[1])
163
- print("Times:\nnpy: %.2fs f0: %.2fs infer: %.2fs\nTotal time: %.2fs" % (*inference_info[2],))
164
- else:
165
- print(f"An error occurred while processing.\n{inference_info[0]}")
166
- return None
167
- shutil.move(output_path, temp_dir)
168
- inferred_files.append(os.path.join(temp_dir, os.path.basename(output_path)))
169
- print("Adjusting inferred audio lengths.")
170
- adjusted_inferred_files = adjust_audio_lengths(nonsilent_files, inferred_files)
171
- print("Combining silence and inferred audios.")
172
- output_count = 1
173
- while True:
174
- 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}")
175
- if not os.path.exists(output_path):
176
- break
177
- output_count += 1
178
- inferred_audio = combine_silence_nonsilent(silence_files, adjusted_inferred_files, keep_silence, output_path)
179
- shutil.rmtree(os.path.join(main_dir, "seperate", "temp"))
180
- else:
181
- inference_info, audio_data, output_path = vc.vc_single(
182
- 0,
183
- audio_path,
184
- f0_change,
185
- f0_method,
186
- index_path,
187
- index_path,
188
- index_rate,
189
- filter_radius,
190
- resample_sr,
191
- rms_mix_rate,
192
- protect,
193
- audio_format,
194
- crepe_hop_length,
195
- do_formant,
196
- quefrency,
197
- timbre,
198
- min_pitch,
199
- max_pitch,
200
- f0_autotune
201
- )
202
- if inference_info[0] == "Success.":
203
- print("Inference ran successfully.")
204
- print(inference_info[1])
205
- print("Times:\nnpy: %.2fs f0: %.2fs infer: %.2fs\nTotal time: %.2fs" % (*inference_info[2],))
206
- else:
207
- print(f"An error occurred while processing.\n{inference_info[0]}")
208
- return None
209
-
210
- del configs, vc
211
- gc.collect()
212
- return output_path