-
Notifications
You must be signed in to change notification settings - Fork 79
/
prediction.py
48 lines (36 loc) · 1.21 KB
/
prediction.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
from optparse import OptionParser
from pydub import AudioSegment
from keras.models import load_model
from utility import globalvars
from utility.audio import extract
import librosa
import numpy as np
import sys
try:
import cPickle as pickle
except ImportError:
import pickle
'''
Predict for one sample data
'''
if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-p', '--predicted_wav_path', dest='wav_path', default='')
parser.add_option('-m', '--model_path', dest='model_path', default='')
parser.add_option('-c', '--nb_classes', dest='nb_classes', type='int', default=7)
(options, args) = parser.parse_args(sys.argv)
wav_path = options.wav_path
model_path = options.model_path
nb_classes = options.nb_classes
globalvars.nb_classes = nb_classes
y, sr = librosa.load(wav_path, sr=16000)
wav = AudioSegment.from_file(wav_path)
f = extract(y, sr)
u = np.full((f.shape[0], globalvars.nb_attention_param), globalvars.attention_init_value,
dtype=np.float32)
# load model
model = load_model(model_path)
# prediction
results = model.predict([u, f], batch_size=128, verbose=1)
for result in results:
print(result)