-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_e2e.py
executable file
·240 lines (205 loc) · 7.92 KB
/
test_e2e.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
#!/usr/bin/env python
import subprocess
import os
import h5py
import sys
import librosa
import tensorflow as tf
import argparse
import shutil
from mixin.params import (
data_dir,
model_dir,
checkpoint_dir,
sample_rate,
dim_2,
dim_1,
)
from mixin.mixin_model import create_model
# print colored outputs
class bcolors:
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
def errexit(message):
print(f"{bcolors.FAIL}[MiXiN e2e test] {message}{bcolors.ENDC}", file=sys.stdout)
sys.exit(1)
def warn(message):
print(f"{bcolors.WARNING}[MiXiN e2e test] {message}{bcolors.ENDC}", file=sys.stdout)
def nextstep(message):
print(f"{bcolors.OKBLUE}[MiXiN e2e test] {message}{bcolors.ENDC}", file=sys.stdout)
def success(message):
print(f"{bcolors.OKGREEN}[MiXiN e2e test] {message}{bcolors.ENDC}", file=sys.stdout)
if __name__ == "__main__":
nextstep("Early sanity check - Keras model layers and input/output dimensions...")
tmpmodel = create_model()
if len(tmpmodel.layers) != 13:
errexit(
"model created with wrong number of layers: {0}".format(
len(tmpmodel.layers)
)
)
x = tf.ones((5, dim_1, dim_2, 1))
y = tmpmodel(x)
if x.shape != y.shape:
errexit(
"input shape {0} into network, got output shape {1}".format(
x.shape, y.shape
)
)
success("good!")
parser = argparse.ArgumentParser()
parser.add_argument(
"--delete", action="store_true", help="delete data,model,logdir before testing"
)
args = parser.parse_args()
if args.delete:
nextstep("Deleting dirs...")
for todelete_dir in [data_dir, model_dir, checkpoint_dir]:
try:
shutil.rmtree(todelete_dir)
except Exception as e:
warn(
"encountered {0} when attempting to delete dir {1}".format(
str(e), todelete_dir
)
)
pass
nextstep("Checking if dirs exist... dont want to overwrite user training")
if (
os.path.isdir(data_dir)
or os.path.isdir(model_dir)
or os.path.isdir(checkpoint_dir)
):
errexit("delete data, model, and logdir before running tests")
nextstep("Checking if STEM_DIR is defined")
stem_dir = os.environ.get("STEM_DIR", None)
if not stem_dir:
errexit("specify STEM_DIR, e.g. STEM_DIR=/path/to/musdb18hq/test")
nextstep("Preparing stems with train util")
try:
stem_out = subprocess.check_output(
"./train_util.py --prepare-stems --stem-dirs {0} --segment-offset 1 --segment-limit 3 --track-limit 2 --segment-duration 10".format(
stem_dir
),
shell=True,
)
except subprocess.CalledProcessError as e:
errexit("error when running train util: {0}".format(str(e)))
nextstep("Verifying prepared stems")
segment_dirs = os.listdir(data_dir)
track_1_count = len([s for s in segment_dirs if s.startswith("001")])
track_2_count = len([s for s in segment_dirs if s.startswith("000")])
novocal_count = len([s for s in segment_dirs if s.endswith("nov")])
total = len(segment_dirs)
if (
track_1_count != track_2_count
or track_1_count != 4
or novocal_count != 4
or total != 8
):
errexit("--prepare-stems did not do what was expected...")
nextstep("Verifying prepared segments for audio properties")
try:
n_tested = 0
for vocaldir in ["0000001v", "0000002v", "0010001v", "0010002v"]:
for track in ["mix.wav", "vocal.wav", "harmonic.wav", "percussive.wav"]:
audio_segment = os.path.join(data_dir, vocaldir, track)
x, fs = librosa.load(audio_segment, sr=None)
n_tested += 1
if len(x) != 10 * fs or fs != 44100:
errexit(
"tested track: {0} - wrong duration {1} or sample rate {2}".format(
audio_segment, float(len(x)) / fs, fs
)
)
for novocaldir in ["0000001nov", "0000002nov", "0010001nov", "0010002nov"]:
for track in ["mix.wav", "harmonic.wav", "percussive.wav"]:
audio_segment = os.path.join(data_dir, novocaldir, track)
x, fs = librosa.load(audio_segment, sr=None)
n_tested += 1
if len(x) != 10 * fs or fs != 44100:
errexit(
"tested track: {0} - wrong duration {1} or sample rate {2}".format(
audio_segment, float(len(x)) / fs, fs
)
)
if n_tested != 28:
errexit("expected 28 segments, got {0}".format(n_tested))
except Exception as e:
errexit("error when veriying prepared stem segments: {0}".format(str(e)))
success("good")
nextstep("Creating hdf5 with train util")
try:
subprocess.check_output("./train_util.py --create-hdf5", shell=True)
except subprocess.CalledProcessError as e:
errexit("error when running train util: {0}".format(str(e)))
hdf5_files = ["data_harmonic.hdf5", "data_percussive.hdf5", "data_vocal.hdf5"]
data_files = [f for f in os.listdir(data_dir) if f.endswith(".hdf5")]
if len(data_files) != 3 or not all([h in data_files for h in hdf5_files]):
errexit("--create-hdf5 did not do what was expected...")
success("good")
nextstep("Verifying dimensionality of hdf5 files")
for hdf5_file in hdf5_files:
hdf5_file_p = os.path.join(data_dir, hdf5_file)
try:
with h5py.File(hdf5_file_p, "r") as hf:
xtr = hf["data-x-train"]
ytr = hf["data-y-train"]
xte = hf["data-x-test"]
yte = hf["data-y-test"]
xv = hf["data-x-validation"]
yv = hf["data-y-validation"]
if hdf5_file != "data_vocal.hdf5":
tr_len = 65
val_len = 9
else:
tr_len = 33
val_len = 5
if (
xtr.shape != (tr_len, dim_1, dim_2, 1)
or ytr.shape != (tr_len, dim_1, dim_2, 1)
or xte.shape != (val_len, dim_1, dim_2, 1)
or yte.shape != (val_len, dim_1, dim_2, 1)
or xv.shape != (val_len, dim_1, dim_2, 1)
or yv.shape != (val_len, dim_1, dim_2, 1)
):
errexit(
"hdf5 file has incorrect datasets or dimensions. expected data-{x,y}-{train,test,validation}"
)
except Exception as e:
errexit("error when loading datasets from hdf5: {0}".format(str(e)))
success("good")
nextstep("Training the 3 networks")
try:
subprocess.check_output("./train_util.py --train", shell=True)
except subprocess.CalledProcessError as e:
errexit("error when running train util: {0}".format(str(e)))
nextstep("Verifying training outputs")
checkpoints = os.listdir(checkpoint_dir)
models = os.listdir(model_dir)
if not all(
[
c in checkpoints
for c in [
"model_harmonic.ckpt",
"model_percussive.ckpt",
"model_vocal.ckpt",
]
]
):
errexit("did not find expected checkpoints: {0}".format(checkpoints))
if not all(
[
m in models
for m in ["model_harmonic.h5", "model_percussive.h5", "model_vocal.h5"]
]
):
errexit("did not find expected models: {0}".format(models))
success("finished!")