Skip to main content

Keyword spotting decoder built with PyBind11

Project description

Keyword Spotting Decoder

This repository provides a high-performance implementation of a Keyword Spotting (KWS) decoder in C++, designed to accelerate decoding in Python via pybind11.

KWS is essentially a streamlined beam search algorithm that looks for specified keywords directly in the output of an acoustic model (AM). For example, consider the output from an acoustic model for the WAV file "tests/data/George_crop2.wav", where the speaker says: "AT ANY SECOND AND JUST GO AND THEN WATCH EVERYBODY'S MOUTH DROP I WILL NOT BE OVER"

Sample acoustic model output

The image above shows the first few timesteps of the acoustic model's output for the given text (for clarity).

There are two main approaches to searching for specific keywords in audio:

  1. Transcribe the entire audio: Convert all spoken words to text, then search for your keywords in the transcription.
  2. Direct keyword search in the acoustic model output: Search for keywords directly in the AM output.

The first approach is prone to errors due to the influence of the language model (LM) during ASR decoding. The LM may alter the output to form more probable sentences, potentially causing actual spoken keywords to be missed or replaced with other words.

KWS addresses this issue by searching for your desired keywords directly in the acoustic model output, bypassing the language model and reducing the chance of missing keywords.

Installation

The module is available on PyPI. You can install it with:

pip install kws-decoder

Usage example

the following code shows a simple example that this module could be used:

import numpy as np
from kws_decoder import KWSDecoder

labels = ["-", "|", "A", "B"]
blank_index = 0
decoder = KWSDecoder(labels, blank_index)

# you can set/get decoder parameters using setter/getter functions
decoder.set_beam_width(128)
decoder.set_beta(1.05)

# don't forget to add keywords to the decoder
keywords = ["AA", "AB"]
decoder.add_words(keywords)

# create a dummy am output
logits = np.random.randn(1000, 4).astype(np.float32)
exp_logits = np.exp(logits - logits.max(axis=1, keepdims=True))
probs = exp_logits / exp_logits.sum(axis=1, keepdims=True)

# then you can search through AM output
decoder.search(probs)

Algorithm hyper parameters

Here is the list of the most important and influential parameters of the decoder:

beam_width: Number of candidate sequences (beams) kept at each decoding step. Larger values can improve accuracy at the cost of more computation and time.

beta: Longer keywords naturally receive lower scores because more probabilities (each 0–1) are multiplied together. beta compensates for this length penalty: the score of a keyword is p(keyword) × beta^len(keyword).

min_keyword_score: Minimum score a keyword must reach to be accepted as detected. Any (partial or complete) keyword below this threshold is pruned from the beam.

max_gap: Maximum allowed gap, in timesteps, between successive detections of the same keyword. If two detections end within max_gap, they are merged into one occurrence whose score is the maximum of the two.

min_clip: Floor value applied to every character probability to prevent underflow and improve numerical stability, increasing algorithm robustness. Note that setting this value too high increases the likelihood of false alarms.

top_n: At each timestep only the top_n most-probable characters are expanded, limiting the branching factor of the beam search. It must be less than or equal to the number of characters.

(Optional) Run Sample Python Implementation Codes

The original code was developed in Python. To run the original implementation, install the package with:

pip install kws-decoder[ext]

This command installs pygtrie, torch, and tqdm, which are necessary to run the original implementation of the beam search algorithm. After installing the dependencies, you can run the script with the original Python implementation located in the test folder, specifically "beam_search.py".

Testing

After installing the module, you can run unit tests using pytest:

pytest

It runs two tests written in Persian and English to ensure everything works as expected.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kws_decoder-0.1.1.tar.gz (3.2 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

kws_decoder-0.1.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (275.4 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

kws_decoder-0.1.1-pp311-pypy311_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (289.4 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ i686

kws_decoder-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

kws_decoder-0.1.1-cp311-cp311-musllinux_1_2_i686.whl (1.3 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ i686

kws_decoder-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (140.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

kws_decoder-0.1.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (147.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

File details

Details for the file kws_decoder-0.1.1.tar.gz.

File metadata

  • Download URL: kws_decoder-0.1.1.tar.gz
  • Upload date:
  • Size: 3.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for kws_decoder-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d2a499acd405eef9962c1a215e8f52f728d448e02e69323b9dd03225541caa78
MD5 9715f8dd119b661dabf0a03cac687ea1
BLAKE2b-256 b982e35773b6914832c52efe96faabaee77f70c69f28289e5e700f9dded8519d

See more details on using hashes here.

File details

Details for the file kws_decoder-0.1.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for kws_decoder-0.1.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e00cd28749542a02064e7639193f1d16c3c6cd5d814c7ffe603fa3a6f37fd7a3
MD5 2a5fe1ea30c2899d762da70ac03a7a44
BLAKE2b-256 e91e70d1b9541e62b652d49b2cf287183103a0d9f332d1a86e6b858f1838cb13

See more details on using hashes here.

File details

Details for the file kws_decoder-0.1.1-pp311-pypy311_pp73-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for kws_decoder-0.1.1-pp311-pypy311_pp73-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 6fdf213334ec6429853617d4ab10d3e50e18ab066d1d714edb76b70407cfb630
MD5 0a187410ab8c391161f510a7d152aca3
BLAKE2b-256 b7f8984f41d95db1ccdc5865650279aba3348f629aa887832d907cf9e57db095

See more details on using hashes here.

File details

Details for the file kws_decoder-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for kws_decoder-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9f62ad7b0b6513ecce3e1a50259436fca4823959b81694b07fec06dbba636da3
MD5 1553b395ea38e172c2050b1b2faa1d9b
BLAKE2b-256 9c59519d7bb649d86075d048105bdf0ce9dd90e16aad3151809208c757b2bd0b

See more details on using hashes here.

File details

Details for the file kws_decoder-0.1.1-cp311-cp311-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for kws_decoder-0.1.1-cp311-cp311-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 aaa6ed90e605a3d698c9cfaf372dca3c9365f0e49ec3911b75392c338fa93839
MD5 139d47aab73c604a674ffd29803d60fd
BLAKE2b-256 08fb485cc0c7fb5866b21a222443f1b0dd81ce90455963d5a4a650ba3bbf43c3

See more details on using hashes here.

File details

Details for the file kws_decoder-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for kws_decoder-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 efabb3ce8c70c82788ed7dddbd8536e49e1d677f4f10ca86257a9faf9370337b
MD5 a101b591c11a040622780cc78ac4d5e6
BLAKE2b-256 3385466a9306685d36ba9e0a544dc044520d0ad4b67ac270e75fdcf37175e656

See more details on using hashes here.

File details

Details for the file kws_decoder-0.1.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for kws_decoder-0.1.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 72dcdde5c5b2d14f365d3ecd04166d583376433342db1d91a8bcfa05c74762d2
MD5 4ac06f0d5a900a0c6c738767f1c51e28
BLAKE2b-256 6007898f4f67b2b40b706cb5de6f6988af3167f1a1f698d64ffffbc58bad65d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page