-
Notifications
You must be signed in to change notification settings - Fork 4
/
service.py
190 lines (169 loc) · 6.74 KB
/
service.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
import io
import os
import shutil
import subprocess
import uuid
import threading
import wave
import librosa
import numpy as np
try:
import torch
import torch.nn.functional as F
except ImportError:
pass
try:
import onnx
import onnx_caffe2.backend
except ImportError:
pass
from utils.manage_audio import AudioSnippet, preprocess_audio
try:
import utils.model as model
except ImportError:
pass
def _softmax(x):
return np.exp(x) / np.sum(np.exp(x))
class LabelService(object):
def evaluate(self, speech_dirs, indices=[]):
dir_labels = {}
if indices:
real_labels = [self.labels[i] for i in indices]
else:
real_labels = [os.dirname(d) for d in speech_dirs]
for i, label in enumerate(real_labels):
if label not in self.labels:
real_labels[i] = "_unknown_"
dir_labels[speech_dirs[i]] = real_labels[i]
accuracy = []
for folder in speech_dirs:
for filename in os.listdir(folder):
fp = os.path.join(folder, filename)
with wave.open(fp) as f:
b_data = f.readframes(16000)
label, _ = self.label(b_data)
accuracy.append(int(label == dir_labels[folder]))
return sum(accuracy) / len(accuracy)
def label(self, wav_data):
raise NotImplementedError
class Caffe2LabelService(LabelService):
def __init__(self, onnx_filename, labels):
self.labels = labels
self.model_filename = onnx_filename
self.filters = librosa.filters.dct(40, 40)
self._graph = onnx.load(onnx_filename)
self._in_name = self._graph.graph.input[0].name
self.model = onnx_caffe2.backend.prepare(self._graph)
def label(self, wav_data):
wav_data = np.frombuffer(wav_data, dtype=np.int16) / 32768.
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)], np.max(predictions))
class TorchLabelService(LabelService):
def __init__(self, model_filename, no_cuda=False, labels=["_silence_", "_unknown_", "command", "random"]):
self.labels = labels
self.model_filename = model_filename
self.no_cuda = no_cuda
self.filters = librosa.filters.dct(40, 40)
self.reload()
def reload(self):
config = model.find_config(model.ConfigType.CNN_TRAD_POOL2)
config["n_labels"] = len(self.labels)
self.model = model.SpeechModel(config)
if not self.no_cuda:
self.model.cuda()
self.model.load(self.model_filename)
self.model.eval()
def label(self, wav_data):
"""Labels audio data as one of the specified trained labels
Args:
wav_data: The WAVE to label
Returns:
A (most likely label, probability) tuple
"""
wav_data = np.frombuffer(wav_data, dtype=np.int16) / 32768.
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)], np.max(predictions))
def stride(array, stride_size, window_size):
i = 0
while i + window_size <= len(array):
yield array[i:i + window_size]
i += stride_size
class TrainingService(object):
def __init__(self, train_script, speech_dataset_path, options):
self.train_script = train_script
self.neg_directory = os.path.join(speech_dataset_path, "random")
self.pos_directory = os.path.join(speech_dataset_path, "command")
self.options = options
self._run_lck = threading.Lock()
self.script_running = False
self._create_dirs()
def _create_dirs(self):
if not os.path.exists(self.neg_directory):
os.makedirs(self.neg_directory)
if not os.path.exists(self.pos_directory):
os.makedirs(self.pos_directory)
def generate_contrastive(self, data):
snippet = AudioSnippet(data)
phoneme_chunks = AudioSnippet(data).chunk_phonemes()
phoneme_chunks2 = AudioSnippet(data).chunk_phonemes(factor=0.8, group_threshold=500)
joined_chunks = []
for i in range(len(phoneme_chunks) - 1):
joined_chunks.append(AudioSnippet.join([phoneme_chunks[i], phoneme_chunks[i + 1]]))
if len(joined_chunks) == 1:
joined_chunks = []
if len(phoneme_chunks) == 1:
phoneme_chunks = []
if len(phoneme_chunks2) == 1:
phoneme_chunks2 = []
chunks = [c.copy() for c in phoneme_chunks2]
for chunk_list in (phoneme_chunks, joined_chunks, phoneme_chunks2):
for chunk in chunk_list:
chunk.rand_pad(32000)
for chunk in chunks:
chunk.repeat_fill(32000)
chunk.rand_pad(32000)
chunks.extend(phoneme_chunks)
chunks.extend(phoneme_chunks2)
chunks.extend(joined_chunks)
return chunks
def clear_examples(self, positive=True, tag=""):
directory = self.pos_directory if positive else self.neg_directory
if not tag:
shutil.rmtree(directory)
self._create_dirs()
else:
for name in os.listdir(directory):
if name.startswith("{}-".format(tag)):
os.unlink(os.path.join(directory, name))
def write_example(self, wav_data, positive=True, filename=None, tag=""):
if tag:
tag = "{}-".format(tag)
if not filename:
filename = "{}{}.wav".format(tag, str(uuid.uuid4()))
directory = self.pos_directory if positive else self.neg_directory
filename = os.path.join(directory, filename)
AudioSnippet(wav_data).save(filename)
def _run_script(self, script, options):
cmd_strs = ["python", script]
for option, value in options.items():
cmd_strs.append("--{}={}".format(option, value))
subprocess.run(cmd_strs)
def _run_training_script(self, callback):
with self._run_lck:
self.script_running = True
self._run_script(self.train_script, self.options)
if callback:
callback()
self.script_running = False
def run_train_script(self, callback=None):
if self.script_running:
return False
threading.Thread(target=self._run_training_script, args=(callback,)).start()
return True