-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
executable file
·346 lines (274 loc) · 10.8 KB
/
eval.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
#Todo: have a different eval.py for each model variant.
# not sure if train.py will have the same fate
#ALSO, I DON'T THINK I EVER RAN THIS ON THE MODIFIED DATASET
import torch
import numpy as np
import argparse
import soundfile as sf
import musdb
import museval
import norbert
from pathlib import Path
import scipy.signal
import resampy
from models import Leakage_XUMX, Leakage_Concat_XUMX, Leakage_Concat2_XUMX
from asteroid.complex_nn import torch_complex_from_magphase
import os
import warnings
import sys
def load_model(variant, model_name, device="cpu"):
print("Loading model from: {}".format(model_name), file=sys.stderr)
if variant == 'no_concat':
model = Leakage_XUMX.from_pretrained(model_name)
elif variant == 'concat_1':
model = Leakage_Concat_XUMX.from_pretrained(model_name)
elif variant == 'concat_2':
model = Leakage_Concat2_XUMX.from_pretrained(model_name)
model.eval()
model.to(device)
return model, model.outputs
def istft(X, rate=44100, n_fft=4096, n_hopsize=1024):
t, audio = scipy.signal.istft(
X / (n_fft / 2), rate, nperseg=n_fft, noverlap=n_fft - n_hopsize, boundary=True
)
return audio
def separate(
audio,
x_umx_target,
instruments,
niter=1,
softmask=False,
alpha=1.0,
residual_model=False,
device="cpu",
variant='no_concat',
):
"""
Performing the separation on audio input
Parameters
----------
audio: np.ndarray [shape=(nb_samples, nb_channels, nb_timesteps)]
mixture audio
x_umx_target: asteroid.models
X-UMX model used for separating
instruments: list
The list of instruments, e.g., ["bass", "drums", "vocals"]
niter: int
Number of EM steps for refining initial estimates in a
post-processing stage, defaults to 1.
softmask: boolean
if activated, then the initial estimates for the sources will
be obtained through a ratio mask of the mixture STFT, and not
by using the default behavior of reconstructing waveforms
by using the mixture phase, defaults to False
alpha: float
changes the exponent to use for building ratio masks, defaults to 1.0
residual_model: boolean
computes a residual target, for custom separation scenarios
when not all targets are available, defaults to False
device: str
set torch device. Defaults to `cpu`.
Returns
-------
estimates: `dict` [`str`, `np.ndarray`]
dictionary with all estimates obtained by the separation model.
"""
# convert numpy audio to torch
audio_torch = torch.tensor(audio.T[None, ...]).float().to(device)
source_names = []
V = []
masked_tf_rep, _ = x_umx_target(audio_torch)
# shape: (Sources, frames, batch, channels, fbin)
for j, target in enumerate(instruments):
Vj = masked_tf_rep[j, Ellipsis].cpu().detach().numpy()
if softmask:
# only exponentiate the model if we use softmask
Vj = Vj ** alpha
# output is nb_frames, nb_samples, nb_channels, nb_bins
V.append(Vj[:, 0, Ellipsis]) # remove sample dim
source_names += [target]
V = np.transpose(np.array(V), (1, 3, 2, 0)) #Alia: so, we have nb_frames, nb_bins, nb_channels, nb_sources
# convert to complex numpy type
tmp = x_umx_target.encoder(audio_torch)
import pdb #try shapes to verify, both in concat case or no concat case
pdb.set_trace()
if variant == 'concat_2':
#tmp = x_umx_target.adapter(tmp)
print(variant)
X = torch_complex_from_magphase(tmp[0].permute(1, 2, 3, 0), tmp[1]) #Alia: strange, why are tmp[0] and tmp[1] shaped differently
X = X.detach().cpu().numpy()
X = X[0].transpose(2, 1, 0) #Alia: making it nb_frames, nb_bins, nb_sources
if residual_model or len(instruments) == 1:
V = norbert.residual_model(V, X, alpha if softmask else 1)
source_names += ["residual"] if len(instruments) > 1 else ["accompaniment"]
if variant == 'concat_1': #for now, just drop the last to channels. concat_2 should not need this since the encoder should output the proper shape
X = X[..., :2] #since X is samples, bins, channels, we want to just take the first 2 out of the 4 channels.
Y = norbert.wiener(V, X.astype(np.complex128), niter, use_softmask=softmask) #V has 2 masks, one for each source, X is the audio spectrogram
estimates = {}
for j, name in enumerate(source_names):
audio_hat = istft(
Y[..., j].T,
rate=x_umx_target.sample_rate,
n_fft=x_umx_target.in_chan,
n_hopsize=x_umx_target.n_hop,
)
estimates[name] = audio_hat.T
return estimates
def inference_args(parser, remaining_args):
inf_parser = argparse.ArgumentParser(
description=__doc__,
parents=[parser],
add_help=True,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
inf_parser.add_argument(
"--softmask",
dest="softmask",
action="store_true",
help=(
"if enabled, will initialize separation with softmask."
"otherwise, will use mixture phase with spectrogram"
),
)
inf_parser.add_argument(
"--niter", type=int, default=1, help="number of iterations for refining results."
)
inf_parser.add_argument(
"--alpha", type=float, default=1.0, help="exponent in case of softmask separation"
)
inf_parser.add_argument("--samplerate", type=int, default=44100, help="model samplerate")
inf_parser.add_argument(
"--residual-model", action="store_true", help="create a model for the residual"
)
return inf_parser.parse_args()
def eval_main(
root,
samplerate=44100,
niter=1,
alpha=1.0,
softmask=False,
residual_model=False,
model_path=".",
model_name="leakage_xumx",
outdir=None,
start=0.0,
duration=-1.0,
no_cuda=False,
test_data_path=None
):
model_name = os.path.join(model_path, model_name)
if not (os.path.exists(model_name)):
print("model does not exist: {}. Please update path in cnf/eval.yml".format(model_name), file=sys.stderr)
quit()
if os.path.exists(outdir):
print("Results of previous run saved in your chosen outdir: {}, please choose another location".format(outdir), file=sys.stderr)
else:
outdir = os.path.abspath(outdir)
Path(outdir).mkdir(exist_ok=True, parents=True)
print("Evaluated results will be saved in:\n {}".format(outdir), file=sys.stderr)
if not test_data_path:
print("No location given for test data, please set one in cfg/eval.yml", file=sys.stderr)
exit()
use_cuda = not no_cuda and torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
model, instruments = load_model(model_name, device)
test_dataset = musdb.DB(root=root, subsets="test", is_wav=True)
results = museval.EvalStore()
#write the config file in outdir
txtout = os.path.join(outdir, "results.txt")
fp = open(txtout, "w")
for track in test_dataset:
input_file = os.path.join(root, "test", track.name, "mixture.wav")
# handling an input audio path
info = sf.info(input_file)
start = int(start * info.samplerate)
# check if dur is none
if duration > 0:
# stop in soundfile is calc in samples, not seconds
stop = start + int(duration * info.samplerate)
else:
# set to None for reading complete file
stop = None
audio, rate = sf.read(input_file, always_2d=True, start=start, stop=stop)
if audio.shape[1] > 2:
warnings.warn("Channel count > 2! " "Only the first two channels will be processed!")
audio = audio[:, :2]
if rate != samplerate:
# resample to model samplerate if needed
audio = resampy.resample(audio, rate, samplerate, axis=0)
if audio.shape[1] == 1:
# if we have mono, let's duplicate it
# as the input of OpenUnmix is always stereo
audio = np.repeat(audio, 2, axis=1)
estimates = separate(
audio,
model,
instruments,
niter=niter,
alpha=alpha,
softmask=softmask,
residual_model=residual_model,
device=device,
)
output_path = Path(os.path.join(outdir, track.name))
output_path.mkdir(exist_ok=True, parents=True)
print("Processing... {}".format(track.name), file=sys.stderr)
print(track.name, file=fp)
for target, estimate in estimates.items():
sf.write(str(output_path / Path(target).with_suffix(".wav")), estimate, samplerate)
track_scores = museval.eval_mus_track(track, estimates)
results.add_track(track_scores.df)
print(track_scores, file=sys.stderr)
print(track_scores, file=fp)
print(results, file=sys.stderr)
print(results, file=fp)
results.save(os.path.join(outdir, "results.pandas"))
results.frames_agg = "mean"
print(results, file=sys.stderr)
print(results, file=fp)
fp.close()
if __name__ == "__main__":
# Training settings
parser = argparse.ArgumentParser(description="OSU Inference", add_help=False)
parser.add_argument("--root", type=str, help="The path to the MUSDB18 dataset")
parser.add_argument(
"--outdir",
type=str,
default="./results_using_pre-trained",
help="Results path where " "best_model.pth" " is stored",
)
parser.add_argument("--start", type=float, default=0.0, help="Audio chunk start in seconds")
parser.add_argument(
"--duration",
type=float,
default=-1.0,
help="Audio chunk duration in seconds, negative values load full track",
)
parser.add_argument(
"--no-cuda", action="store_true", default=False, help="disables CUDA inference"
)
args, _ = parser.parse_known_args()
args = inference_args(parser, args)
#keep these args as is, but read the other file.
import yaml
from asteroid.utils import prepare_parser_from_dict, parse_args_as_dict
with open("cfg/eval.yml") as f:
eval_conf = yaml.safe_load(f)
eval_parser = prepare_parser_from_dict(eval_conf, parser=parser)
arg_dic, plain_args = parse_args_as_dict(eval_parser, return_plain_args=True)
model = os.path.join(plain_args.model_path, plain_args.model_name)
eval_main(
root=args.root,
samplerate=args.samplerate,
alpha=args.alpha,
softmask=args.softmask,
niter=args.niter,
residual_model=args.residual_model,
model_name=plain_args.model_name,
model_path=plain_args.model_path,
test_data_path=plain_args.test_data_path,
outdir=plain_args.output_path,
start=args.start,
duration=args.duration,
no_cuda=args.no_cuda,
)