Skip to content

Commit

Permalink
Add RPi-specific requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
daemon committed Oct 31, 2017
1 parent cfda14c commit a7e6ede
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 8 additions & 0 deletions requirements_rpi.txt
@@ -0,0 +1,8 @@
chainmap
cherrypy>=11.0.0,<=11.99
PyOpenGL>=3.1.0,<=3.1.99
PyOpenGL_accelerate
requests>=2.18,<=2.99
audioread
joblib
future
14 changes: 9 additions & 5 deletions service.py
Expand Up @@ -20,7 +20,10 @@
pass

from utils.manage_audio import AudioSnippet, preprocess_audio
import utils.model as model
try:
import utils.model as model
except ImportError:
pass

def _softmax(x):
return np.exp(x) / np.sum(np.exp(x))
Expand Down Expand Up @@ -60,10 +63,11 @@ def __init__(self, onnx_filename, labels):

def label(self, wav_data):
wav_data = np.frombuffer(wav_data, dtype=np.int16) / 32768.
model_in = preprocess_audio(wav_data, 40, self.filters).unsqueeze(0)
model_in = np.expand_dims(preprocess_audio(wav_data, 40, self.filters), 0)
model_in = np.expand_dims(model_in, 0)
model_in = model_in.astype(np.float32)
predictions = _softmax(self.model.run({self._in_name: model_in})[0])
return (self.labels[np.argmax(predictions)], max(predictions))
return (self.labels[np.argmax(predictions)], np.max(predictions))

class TorchLabelService(LabelService):
def __init__(self, model_filename, no_cuda=False, labels=["_silence_", "_unknown_", "command", "random"]):
Expand Down Expand Up @@ -92,12 +96,12 @@ def label(self, wav_data):
A (most likely label, probability) tuple
"""
wav_data = np.frombuffer(wav_data, dtype=np.int16) / 32768.
model_in = preprocess_audio(wav_data, 40, self.filters).unsqueeze(0)
model_in = torch.from_numpy(preprocess_audio(wav_data, 40, self.filters)).unsqueeze(0)
model_in = torch.autograd.Variable(model_in, requires_grad=False)
if not self.no_cuda:
model_in = model_in.cuda()
predictions = F.softmax(self.model(model_in).squeeze(0).cpu()).data.numpy()
return (self.labels[np.argmax(predictions)], max(predictions))
return (self.labels[np.argmax(predictions)], np.max(predictions))

def stride(array, stride_size, window_size):
i = 0
Expand Down

0 comments on commit a7e6ede

Please sign in to comment.