-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_lrmusdb_openunmix.py
executable file
·275 lines (221 loc) · 10.9 KB
/
eval_lrmusdb_openunmix.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
import torch
import numpy as np
import argparse
import soundfile as sf
from datasets.lr_musdb import musdb
import museval
import norbert
from pathlib import Path
import scipy.signal
import resampy
from asteroid.complex_nn import torch_complex_from_magphase
import os
import warnings
import sys
import pandas as pd
import torch.nn.functional as F
import torchaudio
from eval import inference_args
#input: 1x2xsamples, in a torch tensor
#output: (with zero padding if needed), and number of padded samples
def frame_cutter(audio_tensor, frame_len_s, sample_rate):
frame_size = frame_len_s * sample_rate
#padding:
remainder = frame_size - (audio_tensor.shape[2] % frame_size)
if remainder !=0:
audio_tensor = F.pad(input=audio_tensor, pad=(0, remainder, 0, 0, 0, 0), value=0)
split_tensor = torch.split(audio_tensor, frame_size, dim=2)
#split_tensor = torch.cat(split_tensor, dim=0)
return split_tensor, remainder
#input nx4x2xsamples, start padding
#output 1x2xsamples, without the previously applied padding
def frame_gluer(prediction, remainder):
#maybe no need to remove padding since it is appleid at the end, I can just cut the audio like i did in the data loaders after merging
#torch_seq = torch.split(prediction, 1, dim=0) #commented as now we pass ready tuples
return torch.cat(prediction, dim=3)
def eval_main(
root,
samplerate=44100,
niter=1,
alpha=1.0,
softmask=False,
residual_model=False,
model_path='.',
outdir=None,
start=0.0,
duration=-1.0,
no_cuda=False,
eval_data_path=None,
instrument='drums',
variant='no_concat',
):
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 eval_data_path:
print("No location given for test data, please set one in cfg/eval.yml", file=sys.stderr)
exit()
torch.cuda.empty_cache()
use_cuda = not no_cuda and torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
#separator = torch.hub.load('sigsep/open-unmix-pytorch', 'umxhq', device=device)
separator = torch.hub.load('sigsep/open-unmix-pytorch', 'umxhq', device=device)
test_dataset = musdb.DB(root=root, subsets="test", is_wav=True, instrument=instrument, data_path=eval_data_path)
results = museval.EvalStore()
results_df = pd.DataFrame(columns=['target', 'metric', 'mean_values', 'median_values', 'variant', 'track_name'])
txtout = os.path.join(outdir, "results.txt")
fp = open(txtout, "w")
for track in test_dataset:
input_file = os.path.join(os.path.dirname(track.path), "degraded_audio_mix.wav")
clean_backing_track = os.path.join(os.path.dirname(track.path), "clean_backing_track.wav")
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)
# handling an input audio path. we already assume it's 44100
if audio.shape[1] > 2:
warnings.warn("Channel count > 2! " "Only the first two channels will be processed!")
audio = audio[:, :2]
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)
#audio_torch = torch.tensor(audio.T[None, ...]).float().to(device)
audio_torch = torch.tensor(audio.T[None, ...]).float()
split_audio, padding = frame_cutter(audio_torch, 6, 44100)
pred = [] #list which will hold predictions and later casted to tuple
for chunk in split_audio:
#only attach the necessary chunk before prediction, and detach after prediction.
chunk = chunk.to(device)
pred.append(separator(chunk).cpu().detach())
chunk.cpu().detach().numpy()
#pred[-1].detach() #detach prediction and input chunks
prediction = frame_gluer(tuple(pred), 0)
#squeeze the prediction to the length of the input audio to remove the initial padding
prediction = prediction.detach()
estimates = {}
estimates['vocals'] = prediction[0][0]
estimates['drums'] = prediction[0][1]
estimates['bass'] = prediction[0][2]
estimates['other'] = prediction[0][3]
variant_number = os.path.basename(os.path.split(track.path)[0])
output_path = Path(os.path.join(outdir, instrument, track.name, variant_number))
output_path.mkdir(exist_ok=True, parents=True)
print("Processing... {}".format(track.name), file=sys.stderr)
print(track.name, file=fp)
#adapt the output of openunmix: it outputs 22050, our gt loaded from the musdb package is
#in 44100. so, we resample
if instrument == 'bass':
estimates['degraded_instrument_track'] = estimates['bass']
estimates['degraded_backing_track'] = estimates['vocals'] + estimates['other'] + estimates['drums']
elif instrument == 'drums':
estimates['degraded_instrument_track'] = estimates['drums']
estimates['degraded_backing_track'] = estimates['vocals'] + estimates['other'] + estimates['bass']
#for key, val in estimates.items():
# estimates[key] = resampy.resample(estimates[val], 22050, 44100, axis=0)
# Narrow all estimates to the length of clean backing track. We'll use estimates['bass'] as the ref since all estimates should
# be the same length.
clean_bk_track, rate = sf.read(clean_backing_track, always_2d=True, start=start, stop=stop)
shortest = np.min([estimates['bass'].shape[1], clean_bk_track.shape[0]]) #since we are assuming stereo audio, so the audio length is on the second dim.
# the estimates are already torch tensors, and convert to numpy arrays for the purposes of eval, and transpose for sf.write
estimates['degraded_instrument_track'] = np.array(torch.narrow(estimates['degraded_instrument_track'], 1, 0, shortest))
estimates['degraded_backing_track'] = np.array(torch.narrow(estimates['degraded_backing_track'], 1, 0, shortest))
for target, estimate in estimates.items():
estimates[target] = estimate.T
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, {'degraded_instrument_track': estimates['degraded_instrument_track'],
'degraded_backing_track': estimates['degraded_backing_track']}) #pass only the relevant ones
track_scores.df.to_csv(os.path.join(output_path, 'frame_result.csv'))
#generate the results stats:
summary_target = ['degraded_backing_track', 'degraded_instrument_track']
summary_metrics = ['SDR', 'SIR', 'SAR']
summary_target_col = []
summary_metric_col = []
summary_metric_median = []
summary_metric_mean = []
track_variant = []
track_name = []
for t in summary_target:
for m in summary_metrics:
summary_target_col.append(t)
summary_metric_col.append(m)
rel_cols = track_scores.df[(track_scores.df['metric'] == m) & (track_scores.df['target'] == t)]
summary_metric_median.append(track_scores.frames_agg(rel_cols['score']))
summary_metric_mean.append(np.nanmean(rel_cols['score']))
track_variant.append(os.path.basename(os.path.dirname(track.path)))
track_name.append(track_scores.track_name)
summary_df = pd.DataFrame({ 'target': summary_target_col,
'metric': summary_metric_col,
'mean_values': summary_metric_mean,
'median_values': summary_metric_median,
'variant': track_variant,
'track_name': track_name
})
summary_df.to_csv(os.path.join(output_path, 'results_summary.csv'))
results.add_track(track_scores.df)
results_df = results_df.append(summary_df)
print(track_scores, file=sys.stderr)
results_df.to_csv(os.path.join(outdir, instrument, 'all_result_summaries.csv'))
#aggregate results of all runs
print(results, file=sys.stderr)
results_df.to_csv(os.path.join(outdir, instrument, 'all_result_summaries.csv'))
results.frames_agg = "mean"
print(results, file=sys.stderr)
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)
# Somehow these are not getting called at all.
import yaml
from asteroid.utils import prepare_parser_from_dict, parse_args_as_dict
with open("cfg/eval_lrmusdb_umx.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)
#model = os.path.join("test.pth")
eval_main(
root=musdb.__path__[0],
samplerate=plain_args.samplerate,
alpha=args.alpha,
softmask=args.softmask,
niter=args.niter,
residual_model=args.residual_model,
model_path=plain_args.model_path,
outdir=plain_args.output_path,
start=args.start,
duration=args.duration,
no_cuda=args.no_cuda,
eval_data_path = plain_args.test_data_path,
instrument=plain_args.instrument,
variant=plain_args.model
)