-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_one.py
160 lines (130 loc) · 4.93 KB
/
eval_one.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
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 asteroid.complex_nn import torch_complex_from_magphase
import os
import warnings
import sys
from eval import load_model, separate
#insert path of model to load
model_path = 'test.pth'
def eval_main(root,
samplerate=44100,
niter=1,
alpha=1.0,
softmask=False,
residual_model=False,
model_name="leakage_xumx",
outdir='dummy_test_outputs',
start=0.0,
duration=-1.0,
no_cuda=False,
):
model_name = os.path.abspath(model_name)
if not (os.path.exists(model_name)):
outdir = os.path.abspath("./checkpoint_results")
model_name = "leakage_xumx_checkpoint"
else:
outdir = os.path.join(
os.path.abspath(outdir),
"EvaluateResults_musdb18_testdata",
)
Path(outdir).mkdir(exist_ok=True, parents=True)
print("Evaluated results will be saved in:\n {}".format(outdir), file=sys.stderr)
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)
# TODO: write a class to travers the entire dataset (train/test, and file number)
# results = museval.EvalStore()
#Path(outdir).mkdir(exist_ok=True, parents=True)
#txtout = os.path.join(outdir, "results.txt")
#fp = open(txtout, "w")
test_path = '/media/data/alia/Documents/datasets/leakage_removal/test/drums/Al James - Schoolboy Facination/4'
test_dataset = [test_path]
for track in test_dataset:
#input_file = os.path.join(root, "test", track.name, "mixture.wav")
input_file = os.path.join(test_path, 'degraded_audio_mix.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, 'first_track_test'))
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)
#model = os.path.join(args.outdir, "test.pth")
model = "test.pth"
eval_main("",
model_name=model,
outdir='dummy_test',
)