Fixed bug with custom verifier model loading/prediction and incremented versioning accordingly

This commit is contained in:
dscripka 2023-03-05 20:20:44 -05:00
parent a0311f21d8
commit 8322a96fa8
4 changed files with 25 additions and 8 deletions

View file

@ -39,7 +39,7 @@ class Model():
class_mapping_dicts: List[dict] = [],
enable_speex_noise_suppression: bool = False,
vad_threshold: float = 0,
custom_verifier_models: Union[bool, dict] = False,
custom_verifier_models: dict = {},
custom_verifier_threshold: float = 0.1,
**kwargs
):
@ -112,6 +112,14 @@ class Model():
if custom_verifier_models.get(mdl_name, False):
self.custom_verifier_models[mdl_name] = pickle.load(open(custom_verifier_models[mdl_name], 'rb'))
if len(self.custom_verifier_models.keys()) < len(custom_verifier_models.keys()):
raise ValueError(
"Custom verifier models were provided, but some were not matched with a base model!"
" Make sure that the keys provided in the `custom_verifier_models` dictionary argument"
" exactly match that of the `.models` attribute of an instantiated openWakeWord Model object"
" that has the same base models but doesn't have custom verifier models."
)
# Create buffer to store frame predictions
self.prediction_buffer: DefaultDict[str, deque] = defaultdict(partial(deque, maxlen=30))
@ -208,10 +216,11 @@ class Model():
for cls in predictions.keys():
if predictions[cls] >= self.custom_verifier_threshold:
parent_model = self.get_parent_model_from_label(cls)
verifier_prediction = self.custom_verifier_models[parent_model].predict_proba(
self.preprocessor.get_features(self.model_inputs[mdl])
)[0][-1]
predictions[cls] = verifier_prediction
if self.custom_verifier_models.get(parent_model, False):
verifier_prediction = self.custom_verifier_models[parent_model].predict_proba(
self.preprocessor.get_features(self.model_inputs[mdl])
)[0][-1]
predictions[cls] = verifier_prediction
# Update prediction buffer, and zero predictions for first 5 frames during model initialization
for cls in predictions.keys():

View file

@ -12,7 +12,7 @@ testpaths = [
[project]
name = "openwakeword"
version = "0.3.0"
version = "0.3.1"
authors = [
{ name="David Scripka", email="david.scripka@gmail.com" },
]

View file

@ -26,7 +26,7 @@ def build_additional_requires():
setuptools.setup(
name="openwakeword",
version="0.3.0",
version="0.3.1",
install_requires=['onnxruntime>=1.10.0,<2', 'tqdm>=4.0,<5.0', 'scipy>=1.3,<2', 'scikit-learn>=1,<2'],
extras_require={
'test': [

View file

@ -72,7 +72,15 @@ class TestModels:
model_name=os.path.join("openwakeword", "resources", "models", "hey_mycroft_v0.1.onnx")
)
# Load model with verifier model
with pytest.raises(ValueError):
# Load model with verifier model incorrectly to catch ValueError
owwModel = openwakeword.Model(
wakeword_model_paths=[os.path.join("openwakeword", "resources", "models", "hey_mycroft_v0.1.onnx")],
custom_verifier_models={"bad_key": os.path.join(tmp_dir, "verifier_model.pkl")},
custom_verifier_threshold=0.3,
)
# Load model with verifier model incorrectly to catch ValueError
owwModel = openwakeword.Model(
wakeword_model_paths=[os.path.join("openwakeword", "resources", "models", "hey_mycroft_v0.1.onnx")],
custom_verifier_models={"hey_mycroft_v0.1": os.path.join(tmp_dir, "verifier_model.pkl")},