-
Notifications
You must be signed in to change notification settings - Fork 0
/
kws_server.py
executable file
·276 lines (221 loc) · 7.72 KB
/
kws_server.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
276
#!/usr/bin/python
from collections import deque
import argparse
import os
import subprocess
import time
import threading
import OSC
import cPickle as pkl
import numpy as np
import glob2 as glob
import deepdish
import theano
from theano import tensor as T
import lasagne
import neural_networks
import kaldi_nnet_tools as knt
from params import nnet_params, standard_scaler_path, KEYWORD
import pdb
##############
# METHODS #
##############
def printing_handler(addr, tags, data, source):
print "---"
print "received new osc msg from %s" % OSC.getUrlStr(source)
print "with addr : %s" % addr
print "typetags %s" % tags
print "data %s" % data
print "---"
def _extract_features(filename):
subprocess.call("./kaldi_extract_features.sh {}".format(filename),
shell=True)
def extract_features(addr, tags, data, source):
_extract_features(data[0])
print "done extract_features"
def _decode_loglikelihoods(filename):
subprocess.call("./kaldi_decode_loglikelihoods.sh {}".format(filename),
shell=True)
def decode_loglikelihoods(addr, tags, data, source):
_decode_loglikelihoods(data[0])
print "done decode_loglikelihoods"
def _compute_loglikelihoods(filename):
global clf
left_context = abs(clf[2]['<Context>'][0])
right_context = abs(clf[2]['<Context>'][-1])
const_component_dim = clf[2]['<ConstComponentDim>']
# create generator with spliced data and iVectors
data = knt.splice(
knt.read_kaldi_features("features/{}.ark".format(filename)),
left_context,
right_context,
const_component_dim)
output = [knt.forward(i, clf[3:], verbose=False) for i in data]
output = np.clip(output, 1.0e-20, np.inf)
output = np.log(output)
knt.save_kaldi_loglikelihoods(
output,
'log_likelihoods/{}.ark'.format(filename))
print "done compute_loglikelihoods"
def compute_loglikelihoods(addr, tags, data, source):
_compute_loglikelihoods(data[0])
def decode_audio(addr, tags, data, source):
global clf
_extract_features(data[0])
_compute_loglikelihoods(data[0])
_decode_loglikelihoods(data[0])
def kws_max(addr, tags, data, source):
"""Computes class probability using a list of MFCC sent by the real-time
feature extractor
params
------
data : array <float>
List of floats sent by the feature extractor
"""
data = np.array(data).reshape((N_COLS, N_ROWS)).T
data = np.append(data, data[:, -3:]).reshape((1, 13, 101))
print(pred_fn(data))
def kws_file(addr, tags, data, source):
"""Computes class probability using a list of MFCC sent by the real-time
feature extractor
params
------
data : array <float>
List of floats sent by the feature extractor
"""
data = data[:min(len(data), 13*101)]
if len(data) > 13*101:
data = data[:13*101]
elif len(data) < 13*101:
data.extend(data[len(data)-13*101:])
data = np.array(data).T.reshape((1, 13, 101))
eval_prediction(data)
def kws_mic(addr, tags, data, source):
"""Computes class probability using a list of MFCC sent by the real-time
feature extractor
params
------
data : array <float>
List of floats sent by the feature extractor
"""
data = data[:min(len(data), 13*101)]
if len(data) == 13 * 101:
data = np.array(data).T.reshape((1, 13, 101))
else:
data = np.array(data).reshape((N_ROWS, N_COLS))
data = np.append(data, data[:, -3:]).reshape((1, 13, 101))
def eval_prediction(data):
label = np.argmax(pred_fn(data))
output = "Other"
if label == 1:
output = KEYWORD
print("Heard {}".format(output))
def play_audio(filepath, verbose=False):
if verbose:
stdout = subprocess.PIPE
else:
stdout = open(os.devnull, 'w')
subprocess.call(["play", "-q", filepath], stdout=stdout)
def send_filepath():
filepath = get_next_audiopath(audio_paths)
play_audio(filepath)
msg = OSC.OSCMessage()
msg.setAddress("/send_mfcc")
msg.append(filepath)
osc_client.send(msg)
def get_next_audiopath(filepaths):
global q
if len(q) == 0:
q = deque(range(len(filepaths)))
return filepaths[q.pop()]
def dnn(dnn_filepath, nnet_params):
"""Loads a lasagne saved model and instantiates a function for prediction
"""
ss = pkl.load(open(standard_scaler_path, 'rb'))
nnet_params['offset'] = ss.mean_
nnet_params['scale'] = ss.scale_
network = neural_networks.build_general_network(
(None, 13, 101), # last is target
nnet_params['n_layers'],
nnet_params['widths'],
nnet_params['non_linearities'],
nnet_params['offset'],
nnet_params['scale'],
drop_out=False)
# load best network model so far
parameters = deepdish.io.load(dnn_filepath)
# load and set network weights
for i in xrange(len(parameters)):
parameters[i] = parameters[i].astype('float32')
lasagne.layers.set_all_param_values(network, parameters)
# set up prediction function
input_var = T.tensor3()
prediction = lasagne.layers.get_output(
network, input_var, deterministic=True)
return theano.function([input_var], prediction)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"-m", default=False, action='store_true',
help="Use Microphone audio instead of files, default False")
parser.add_argument(
"-audio_glob", default='/Users/rafaelvalle/Desktop/paasr/*.wav',
type=str, help="Glob string to find audio files")
args = parser.parse_args()
print "arguments", args
# define network addresses
receive_address = '127.0.0.1', 31337
send_address = '127.0.0.1', 12345
# feature dimensions from listener client
N_ROWS = 13
N_COLS = 98
dnn_filepath = "models/kws_model.h5"
pred_fn = dnn(dnn_filepath, nnet_params)
kws = kws_mic
q = deque()
if not args.m:
audio_paths_glob = args.audio_glob
audio_paths = glob.glob(audio_paths_glob)
kws = kws_file
# start server and client
osc_server = OSC.OSCServer(receive_address)
osc_server.addDefaultHandlers()
osc_client = OSC.OSCClient()
osc_client.connect(send_address)
# add message handlers
osc_server.addMsgHandler("/print_handlers", printing_handler)
osc_server.addMsgHandler("/extract_features", extract_features)
osc_server.addMsgHandler("/decode_loglikelihoods", decode_loglikelihoods)
osc_server.addMsgHandler("/compute_loglikelihoods", compute_loglikelihoods)
osc_server.addMsgHandler("/decode_audio", decode_audio)
osc_server.addMsgHandler("/kws", kws)
osc_server.addMsgHandler("/kws_max", kws_max)
osc_server.addMsgHandler("/kws_file", kws_file)
"""
print "Instantiate ANN Classifier"
# path where model info nnet-am-info and copy nnet-am-copy are saved
am_copy_path = 'models/fisher_final.mdl.nnet.txt'
am_info_path = 'models/fisher_final.mdl.info.txt'
# convert kaldi model to python
clf = knt.parseNNET(am_copy_path, am_info_path)
"""
# check which handlers we have added
print "Registered Callback-functions are :"
for addr in sorted(osc_server.getOSCAddressSpace()):
print addr
# Start OSCServer
print "\nStarting OSCServer. Use ctrl-C to quit."
st = threading.Thread(target=osc_server.serve_forever)
st.start()
try:
while 1:
if not args.m:
raw_input("Press Enter to play new file...")
send_filepath()
time.sleep(1)
except KeyboardInterrupt:
print "\nClosing OSCServer."
osc_server.close()
print "Waiting for Server-thread to finish"
st.join() # !!!
print "Done!"