-
Notifications
You must be signed in to change notification settings - Fork 3
/
SepProcessor.py
396 lines (300 loc) · 13.3 KB
/
SepProcessor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
from bytesep.models.lightning_modules import get_model_class
from bytesep.separator import Separator
from bytesep.utils import load_audio, read_yaml
import os
import sys
import ctypes
import pathlib
import shutil
import time
from typing import NoReturn
import numpy as np
import soundfile
import torch
LOCAL_CHECKPOINTS_DIR = os.path.join(os.getcwd(), "models")
def init_abn() -> NoReturn:
# Need to use torch.distributed if models contain inplace_abn.abn.InPlaceABNSync.
import torch.distributed as dist
dist.init_process_group(
'gloo', init_method='file:///tmp/somefile', rank=0, world_size=1
)
def build_separator(config_yaml: str, checkpoint_path: str, device: str) -> Separator:
r"""Build separator.
Args:
config_yaml: str
checkpoint_path: str
device: "cuda" | "cpu"
Returns:
separator: Separator
"""
# Read config file.
configs = read_yaml(config_yaml)
sample_rate = configs['train']['sample_rate']
input_channels = configs['train']['input_channels']
output_channels = configs['train']['output_channels']
target_source_types = configs['train']['target_source_types']
target_sources_num = len(target_source_types)
model_type = configs['train']['model_type']
segment_seconds = 30
segment_samples = int(segment_seconds * sample_rate)
batch_size = 1
print("Using {} for separating ..".format(device))
models_contains_inplaceabn = False
if models_contains_inplaceabn:
init_abn(models_contains_inplaceabn)
# Get model class.
Model = get_model_class(model_type)
# Create model.
model = Model(
input_channels=input_channels,
output_channels=output_channels,
target_sources_num=target_sources_num,
)
# Load checkpoint.
checkpoint = torch.load(checkpoint_path, map_location='cpu')
model.load_state_dict(checkpoint["model"])
# Move model to device.
model.to(device)
# Create separator.
separator = Separator(
model=model,
segment_samples=segment_samples,
batch_size=batch_size,
device=device,
)
return separator
def match_audio_channels(audio: np.array, input_channels: int) -> np.array:
r"""Match input audio to correct channels.
Args:
audio: (audio_channels, audio_segments)
input_channels: int
Returns:
(input_channels, audio_segments)
"""
audio_channels = audio.shape[0]
if audio_channels == input_channels:
return audio
elif audio_channels == 2 and input_channels == 1:
return np.mean(audio, axis=0)[None, :]
elif audio_channels == 1 and input_channels == 2:
return np.tile(audio, (2, 1))
else:
raise NotImplementedError
def separate_file( config_yamls, checkpoint_paths, audio_path, output_path,
scale_volume, cpu, extension, source_type, progress) -> NoReturn:
r"""Separate a single file.
"""
deal_with_mats()
task_num = 1 if len(config_yamls) == 1 else 2
if cpu or not torch.cuda.is_available():
device = torch.device('cpu')
else:
device = torch.device('cuda')
print("GPU: ",torch.cuda.get_device_name(0))
if os.path.dirname(output_path) != "":
os.makedirs(os.path.dirname(output_path), exist_ok=True)
for i, (config_yaml, checkpoint_path) in enumerate(zip(config_yamls, checkpoint_paths)):
# Read yaml files.
configs = read_yaml(config_yaml)
sample_rate = configs['train']['sample_rate']
input_channels = configs['train']['input_channels']
# deal with output suffix
type_dict = {0: 'vocal', 1: 'accompaniment'}
type_str = source_type if source_type != "both" else type_dict[i]
if extension:
suffix = os.path.basename(audio_path).split('.')[-2] + \
f"_{type_str}." + extension
else:
suffix = os.path.basename(audio_path).split('.')[-2] + \
f"_{type_str}." + os.path.basename(audio_path).split('.')[-1]
# Build Separator.
separator = build_separator(config_yaml, checkpoint_path, device)
# Load audio.
audio = load_audio(audio_path=audio_path, mono=False, sample_rate=sample_rate)
audio = match_audio_channels(audio, input_channels)
# Separate
input_dict = {'waveform': audio}
separate_time = time.time()
sep_audio = separator.separate(input_dict) # (input_channels, audio_samples)
print('Separate time: {:.3f} s'.format(time.time() - separate_time))
# Write out separated audio.
if scale_volume:
sep_audio /= np.max(np.abs(sep_audio))
# Write out separated audio.
tmp_wav_path = os.path.join("tmp.wav")
target_path = os.path.join(output_path, suffix)
soundfile.write(file=tmp_wav_path, data=sep_audio.T, samplerate=sample_rate)
cmd = os.path.join(os.getcwd(), "tools/ffmpeg.exe") + \
f' -y -loglevel panic -i "{tmp_wav_path}" "{target_path}"'
os.system(cmd)
os.remove(tmp_wav_path)
print(f'Write out to {target_path}')
print( (i+1) / task_num * 100)
progress.do_simple_progress( (i+1) / task_num * 100)
def separate_dir(config_yamls, checkpoint_paths, audios_dir, outputs_dir,
scale_volume, cpu, extension, source_type, progress) -> NoReturn:
r"""Separate all audios in a directory.
"""
deal_with_mats()
if cpu or not torch.cuda.is_available():
device = torch.device('cpu')
else:
device = torch.device('cuda')
print("GPU: ",torch.cuda.get_device_name(0))
os.makedirs(outputs_dir, exist_ok=True)
audio_names = sorted(os.listdir(audios_dir))
audios_num = len(audio_names)
task_num = audios_num if len(config_yamls) == 1 else audios_num * 2
for i, (config_yaml, checkpoint_path) in enumerate(zip(config_yamls, checkpoint_paths)):
configs = read_yaml(config_yaml)
sample_rate = configs['train']['sample_rate']
separator = build_separator(config_yaml, checkpoint_path, device)
for n, audio_name in enumerate(audio_names):
# deal with path and suffix
audio_path = os.path.join(audios_dir, audio_name)
type_dict = {0: 'vocal', 1: 'accompaniment'}
type_str = source_type if source_type != "both" else type_dict[i]
if extension:
suffix = os.path.basename(audio_path).split('.')[-2] + \
f"_{type_str}." + extension
else:
suffix = os.path.basename(audio_path).split('.')[-2] + \
f"_{type_str}." + os.path.basename(audio_path).split('.')[-1]
# Load audio.
audio = load_audio(audio_path=audio_path, mono=False, sample_rate=sample_rate)
# Separate
input_dict = {'waveform': audio}
separate_time = time.time()
sep_audio = separator.separate(input_dict) # (input_channels, audio_samples)
print('Separate time: {:.3f} s'.format(time.time() - separate_time))
# Write out separated audio.
if scale_volume:
sep_audio /= np.max(np.abs(sep_audio))
# postprocess
tmp_wav_path = os.path.join("tmp.wav")
target_path = os.path.join(outputs_dir, suffix)
soundfile.write(file=tmp_wav_path, data=sep_audio.T, samplerate=sample_rate)
cmd = os.path.join(os.getcwd(), "tools/ffmpeg.exe") + \
f' -y -loglevel panic -i "{tmp_wav_path}" "{target_path}"'
os.system(cmd)
os.remove(tmp_wav_path)
# inform observer
progress.do_multiple_progress( (n + 1 + i*audios_num) / task_num * 100)
print(f'{n+1} / {audios_num}, Write out to {outputs_dir}')
def get_paths(source_type: str, model_type: str) -> [str, str]:
r"""Get config_yaml and checkpoint paths.
Args:
source_type: str, "vocals" | "accompaniment"
model_type: str, "MobileNet_Subbandtime" | "ResUNet143_Subbandtime"
Returns:
config_yaml: str
checkpoint_path: str
"""
local_checkpoints_dir = LOCAL_CHECKPOINTS_DIR
error_message = "Checkpoint is incomplete, please download again!"
try:
if model_type == "MobileNet_Subbandtime":
if source_type == "vocals":
config_yaml = os.path.join(
local_checkpoints_dir,
"train_scripts/musdb18/vocals-accompaniment,mobilenet_subbandtime.yaml",
)
checkpoint_path = os.path.join(
local_checkpoints_dir,
"mobilenet_subbtandtime_vocals_7.2dB_500k_steps_v2.pth",
)
assert os.path.getsize(checkpoint_path) == 4621773, error_message
elif source_type == "accompaniment":
config_yaml = os.path.join(
local_checkpoints_dir,
"train_scripts/musdb18/accompaniment-vocals,mobilenet_subbandtime.yaml",
)
checkpoint_path = os.path.join(
local_checkpoints_dir,
"mobilenet_subbtandtime_accompaniment_14.6dB_500k_steps_v2.pth",
)
assert os.path.getsize(checkpoint_path) == 4621773, error_message
else:
raise NotImplementedError
elif model_type == "ResUNet143_Subbandtime":
if source_type == "vocals":
config_yaml = os.path.join(
local_checkpoints_dir,
"train_scripts/musdb18/vocals-accompaniment,resunet_subbandtime.yaml",
)
checkpoint_path = os.path.join(
local_checkpoints_dir,
"resunet143_subbtandtime_vocals_8.7dB_500k_steps_v2.pth",
)
assert os.path.getsize(checkpoint_path) == 414046363, error_message
elif source_type == "accompaniment":
config_yaml = os.path.join(
local_checkpoints_dir,
"train_scripts/musdb18/accompaniment-vocals,resunet_subbandtime.yaml",
)
checkpoint_path = os.path.join(
local_checkpoints_dir,
"resunet143_subbtandtime_accompaniment_16.4dB_500k_steps_v2.pth",
)
assert os.path.getsize(checkpoint_path) == 414036369, error_message
else:
raise NotImplementedError
else:
raise NotImplementedError
except:
download_checkpoints()
return config_yaml, checkpoint_path
def download_checkpoints() -> NoReturn:
r"""Download checkpoints and config yaml files from Zenodo."""
zenodo_dir = "https://zenodo.org/record/5804160/files"
local_checkpoints_dir = LOCAL_CHECKPOINTS_DIR
# Download checkpoints.
checkpoint_names = [
"mobilenet_subbtandtime_vocals_7.2dB_500k_steps_v2.pth?download=1",
"mobilenet_subbtandtime_accompaniment_14.6dB_500k_steps_v2.pth?download=1",
"resunet143_subbtandtime_vocals_8.7dB_500k_steps_v2.pth?download=1",
"resunet143_subbtandtime_accompaniment_16.4dB_500k_steps_v2.pth?download=1",
]
os.makedirs(local_checkpoints_dir, exist_ok=True)
for checkpoint_name in checkpoint_names:
remote_checkpoint_link = os.path.join(zenodo_dir, checkpoint_name).replace('\\', '/')
local_checkpoint_link = os.path.join(
local_checkpoints_dir, checkpoint_name.split("?")[0]
)
command_str = os.path.join(os.getcwd(), "tools/wget.exe") + \
f" -O {local_checkpoint_link} {remote_checkpoint_link}"
os.system(command_str)
# Download and unzip config yaml files.
remote_zip_scripts_link = os.path.join(
zenodo_dir,
"train_scripts.zip?download=1").replace('\\', '/'
)
local_zip_scripts_path = os.path.join(local_checkpoints_dir, "train_scripts.zip")
cmd1 = os.path.join(os.getcwd(), "tools/wget.exe") + f" -O {local_zip_scripts_path} {remote_zip_scripts_link}"
cmd2 = os.path.join(os.getcwd(), "tools/unzip.exe") + f" {local_zip_scripts_path} -d {local_checkpoints_dir}"
os.system(cmd1)
os.system(cmd2)
def deal_with_mats():
'''
Mat should at {filters_dir}. We first try to copy mat to that dir.
If it failed, we try to download from zenodo.
Anyway, we want to get a admin power to ensure success.
'''
filters_dir = f'{str( pathlib.Path.home() )}/bytesep_data/filters'
source_dir = './models/filters'
for _name in ['f_4_64.mat', 'h_4_64.mat']:
_source = os.path.join(source_dir, _name)
_path = os.path.join(filters_dir, _name)
if not os.path.isfile(_path):
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
os.makedirs(os.path.dirname(_path), exist_ok=True)
try:
shutil.copyfile(_source, _path)
except Exception as error:
print(error)
print("Downloading mat...")
remote_path = (
f"https://zenodo.org/record/5513378/files/{_name}?download=1"
)
command_str = os.path.join(os.getcwd(), "tools/wget.exe") + f'-O "{_path}" "{remote_path}"'
os.system(command_str)