-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
94 lines (70 loc) · 2.56 KB
/
evaluate.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
import sys
import librosa
import librosa.output
import tensorflow
import numpy as np
from tensorflow.keras.models import model_from_json
def process_song(song, hop_length=512, n_fft=1024, context_size=25):
"""
Preprocesses one song and creates x-frames with associated y-labels in the target directory
parameters:
song: (ndarray) audio to be processed
hop_length, n_fft, context_size: preprocessing parameters pertaining to the STFT spectrograms; make sure
they are the same as the ones used in training
"""
mix_spec = np.abs(librosa.stft(song, hop_length=hop_length, n_fft=n_fft))
n_bins, n_frames = mix_spec.shape
frames = []
for i in range(n_frames):
# container for one image of size n_bins, context_size
x = np.zeros(shape=(n_bins, context_size))
for j in range(context_size):
curr_idx = i - context_size//2 + j
# if current index out of range, leave 0s as padding
if curr_idx < 0:
continue
elif curr_idx >= n_frames:
break
else:
x[:, j] = mix_spec[:, curr_idx]
frames.append(x)
return np.expand_dims(np.asarray(frames), axis=-1)
def evaluate_song(song, model):
"""
Convenience method for quick application of the drum separation network.
parameters:
song: (ndarray) audio to be processed
model: trained Keras model
"""
song_data = process_song(song)
ibm = model.predict(song_data)
ibm = ibm.T
mixspec = librosa.stft(song, hop_length=512, n_fft=1024)
reconst = librosa.istft(mixspec * ibm, hop_length=512)
return reconst
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: %s <input filename> <output filename>" % (sys.argv[0]))
exit()
infile = sys.argv[1]
outfile = sys.argv[2]
# load user specified song
song, sr = librosa.load(infile, sr=22050)
# load and compile model
model_path = "trained_model/"
model_name = "drumsep_full"
json_file = open(model_path + model_name + ".json", 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load weights into new model
model.load_weights(model_path + model_name + ".h5")
print("Loaded model %s from disk" % model_name)
precision = tensorflow.keras.metrics.Precision()
recall = tensorflow.keras.metrics.Recall()
model.compile(loss=tensorflow.keras.losses.BinaryCrossentropy(), optimizer='adam', metrics=[precision, recall])
# apply trained network to song
result = evaluate_song(song, model)
# write file to user specified path
librosa.output.write_wav(outfile, result, sr=22050, norm=False)
print("\nProcessed %s successfully. Written result to %s.\n" % (infile, outfile))