diff --git a/README.md b/README.md index e0c6a94..dd71db6 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ model, utils = torch.hub.load(repo_or_dir='snakers4/silero-vad', force_reload=True) (get_speech_ts, + get_speech_ts_adaptive, _, read_audio, _, _, _) = utils @@ -122,9 +123,15 @@ files_dir = torch.hub.get_dir() + '/snakers4_silero-vad_master/files' wav = read_audio(f'{files_dir}/en.wav') # full audio # get speech timestamps from full audio file + +# classic way speech_timestamps = get_speech_ts(wav, model, num_steps=4) pprint(speech_timestamps) + +# adaptive way +speech_timestamps = get_speech_ts_adaptive(wav, model) +pprint(speech_timestamps) ``` #### Number Detector @@ -195,6 +202,7 @@ _, utils = torch.hub.load(repo_or_dir='snakers4/silero-vad', force_reload=True) (get_speech_ts, + get_speech_ts_adaptive, _, read_audio, _, _, _) = utils @@ -208,14 +216,20 @@ def validate_onnx(model, inputs): ort_inputs = {'input': inputs.cpu().numpy()} outs = model.run(None, ort_inputs) outs = [torch.Tensor(x) for x in outs] - return outs + return outs[0] model = init_onnx_model(f'{files_dir}/model.onnx') wav = read_audio(f'{files_dir}/en.wav') # get speech timestamps from full audio file + +# classic way speech_timestamps = get_speech_ts(wav, model, num_steps=4, run_function=validate_onnx) pprint(speech_timestamps) + +# adaptive way +speech_timestamps = get_speech_ts(wav, model, run_function=validate_onnx) +pprint(speech_timestamps) ``` #### Number Detector @@ -337,7 +351,7 @@ We use random 250 ms audio chunks for validation. Speech to non-speech ratio amo Since our VAD (only VAD, other networks are more flexible) was trained on chunks of the same length, model's output is just one float from 0 to 1 - **speech probability**. We use speech probabilities as thresholds for precision-recall curve. This can be extended to 100 - 150 ms. Less than 100 - 150 ms cannot be distinguished as speech with confidence. -[Webrtc](https://github.com/wiseman/py-webrtcvad) splits audio into frames, each frame has corresponding number (0 **or** 1). We use 30ms frames for webrtc, so each 250 ms chunk is split into 8 frames, their **mean** value is used as a treshold for plot. +[Webrtc](https://github.com/wiseman/py-webrtcvad) splits audio into frames, each frame has corresponding number (0 **or** 1). We use 30ms frames for webrtc, so each 250 ms chunk is split into 8 frames, their **mean** value is used as a threshold for plot. [Auditok](https://github.com/amsehili/auditok) - logic same as Webrtc, but we use 50ms frames. @@ -347,6 +361,9 @@ Since our VAD (only VAD, other networks are more flexible) was trained on chunks ### VAD Parameter Fine Tuning +#### **Classic way** + +**This is straightforward classic method `get_speech_ts` where thresholds (`trig_sum` and `neg_trig_sum`) are specified by users** - Among others, we provide several [utils](https://github.com/snakers4/silero-vad/blob/8b28767292b424e3e505c55f15cd3c4b91e4804b/utils.py#L52-L59) to simplify working with VAD; - We provide sensible basic hyper-parameters that work for us, but your case can be different; - `trig_sum` - overlapping windows are used for each audio chunk, trig sum defines average probability among those windows for switching into triggered state (speech state); @@ -365,6 +382,24 @@ speech_timestamps = get_speech_ts(wav, model, visualize_probs=True) ``` +#### **Adaptive way** + +**Adaptive algorithm (`get_speech_ts_adaptive`) automatically selects thresholds (`trig_sum` and `neg_trig_sum`) based on median speech probabilities over the whole audio, SOME ARGUMENTS VARY FROM THE CLASSIC WAY FUNCTION ARGUMENTS** +- `batch_size` - batch size to feed to silero VAD (default - `200`) +- `step` - step size in samples, (default - `500`) (`num_samples_per_window` / `num_steps` from classic method) +- `num_samples_per_window` - number of samples in each window, our models were trained using `4000` samples (250 ms) per window, so this is preferable value (lesser values reduce [quality](https://github.com/snakers4/silero-vad/issues/2#issuecomment-750840434)); +- `min_speech_samples` - minimum speech chunk duration in samples (default - `10000`) +- `min_silence_samples` - minimum silence duration in samples between to separate speech chunks (default - `4000`) +- `speech_pad_samples` - widen speech by this amount of samples each side (default - `2000`) + +``` +speech_timestamps = get_speech_ts_adaptive(wav, model, + num_samples_per_window=4000, + step=500, + visualize_probs=True) +``` + + The chart should looks something like this: ![image](https://user-images.githubusercontent.com/12515440/106242896-79142580-6219-11eb-9add-fa7195d6fd26.png) @@ -390,7 +425,7 @@ Please see [Quality Metrics](#quality-metrics) ### How Number Detector Works - It is recommended to split long audio into short ones (< 15s) and apply model on each of them; -- Number Detector can classify if whole audio contains a number, or if each audio frame contains a number; +- Number Detector can classify if the whole audio contains a number, or if each audio frame contains a number; - Audio is splitted into frames in a certain way, so, having a per-frame output, we can restore timing bounds for a numbers with an accuracy of about 0.2s; ### How Language Classifier Works diff --git a/silero-vad.ipynb b/silero-vad.ipynb old mode 100755 new mode 100644 index 0cb0a9d..f841184 --- a/silero-vad.ipynb +++ b/silero-vad.ipynb @@ -12,7 +12,7 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "id": "FpMplOCA2Fwp" }, "source": [ "## VAD" @@ -22,7 +22,7 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "id": "62A6F_072Fwq" }, "source": [ "### Install Dependencies" @@ -32,11 +32,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-30T17:35:43.397137Z", - "start_time": "2020-12-30T17:33:10.962078Z" - }, - "hidden": true + "hidden": true, + "id": "5w5AkskZ2Fwr" }, "outputs": [], "source": [ @@ -57,6 +54,7 @@ " force_reload=True)\n", "\n", "(get_speech_ts,\n", + " get_speech_ts_adaptive,\n", " save_audio,\n", " read_audio,\n", " state_generator,\n", @@ -69,23 +67,25 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true, - "hidden": true, "id": "fXbbaUO3jsrw" }, "source": [ "### Full Audio" ] }, + { + "cell_type": "markdown", + "metadata": { + "id": "dY2Us3_Q2Fws" + }, + "source": [ + "**Classic way of getting speech chunks, you may need to select the thresholds yourself**" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-30T17:35:44.362860Z", - "start_time": "2020-12-30T17:35:43.398441Z" - }, - "hidden": true, "id": "aI_eydBPjsrx" }, "outputs": [], @@ -101,11 +101,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-30T17:35:44.419280Z", - "start_time": "2020-12-30T17:35:44.364175Z" - }, - "hidden": true, "id": "OuEobLchjsry" }, "outputs": [], @@ -119,23 +114,62 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true, - "hidden": true, + "id": "n8plzbJU2Fws" + }, + "source": [ + "**Experimental Adaptive method, algorithm selects thresholds itself (see readme for more information)**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "SQOtu2Vl2Fwt" + }, + "outputs": [], + "source": [ + "wav = read_audio(f'{files_dir}/en.wav')\n", + "# get speech timestamps from full audio file\n", + "speech_timestamps = get_speech_ts_adaptive(wav, model, step=500, num_samples_per_window=4000)\n", + "pprint(speech_timestamps)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Lr6zCGXh2Fwt" + }, + "outputs": [], + "source": [ + "# merge all speech chunks to one audio\n", + "save_audio('only_speech.wav',\n", + " collect_chunks(speech_timestamps, wav), 16000) \n", + "Audio('only_speech.wav')" + ] + }, + { + "cell_type": "markdown", + "metadata": { "id": "iDKQbVr8jsry" }, "source": [ "### Single Audio Stream" ] }, + { + "cell_type": "markdown", + "metadata": { + "id": "xCM-HrUR2Fwu" + }, + "source": [ + "**Classic way of getting speech chunks, you may need to select the thresholds yourself**" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:09:59.199321Z", - "start_time": "2020-12-15T13:09:59.196823Z" - }, - "hidden": true, "id": "q-lql_2Wjsry" }, "outputs": [], @@ -147,11 +181,34 @@ " print(batch)" ] }, + { + "cell_type": "markdown", + "metadata": { + "id": "t8TXtnvk2Fwv" + }, + "source": [ + "**Experimental Adaptive method, algorithm selects thresholds itself (see readme for more information)**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "BX3UgwwB2Fwv" + }, + "outputs": [], + "source": [ + "wav = f'{files_dir}/en.wav'\n", + "\n", + "for batch in single_audio_stream(model, wav, iterator_type='adaptive'):\n", + " if batch:\n", + " print(batch)" + ] + }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true, "id": "KBDVybJCjsrz" }, "source": [ @@ -162,10 +219,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:10:03.590358Z", - "start_time": "2020-12-15T13:10:03.587071Z" - }, "hidden": true, "id": "BK4tGfWgjsrz" }, @@ -179,10 +232,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:10:15.762491Z", - "start_time": "2020-12-15T13:10:03.591388Z" - }, "hidden": true, "id": "v1l8sam1jsrz" }, @@ -196,7 +245,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "id": "36jY0niD2Fww" }, "source": [ "## Number detector" @@ -206,7 +256,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "id": "scd1DlS42Fwx" }, "source": [ "### Install Dependencies" @@ -216,7 +267,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "Kq5gQuYq2Fwx" }, "outputs": [], "source": [ @@ -249,7 +301,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "id": "qhPa30ij2Fwy" }, "source": [ "### Full audio" @@ -259,7 +312,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "EXpau6xq2Fwy" }, "outputs": [], "source": [ @@ -273,7 +327,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "u-KfXRhZ2Fwy" }, "outputs": [], "source": [ @@ -288,7 +343,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "iwYEC4aZ2Fwy" }, "outputs": [], "source": [ @@ -302,7 +358,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "fHaYejX12Fwy" }, "outputs": [], "source": [ @@ -315,7 +372,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "id": "PnKtJKbq2Fwz" }, "source": [ "## Language detector" @@ -325,7 +383,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "id": "F5cAmMbP2Fwz" }, "source": [ "### Install Dependencies" @@ -335,7 +394,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "Zu9D0t6n2Fwz" }, "outputs": [], "source": [ @@ -365,7 +425,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "id": "iC696eMX2Fwz" }, "source": [ "### Full audio" @@ -375,7 +436,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "c8UYnYBF2Fw0" }, "outputs": [], "source": [ @@ -396,7 +458,7 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "id": "hEhnfORV2Fw0" }, "source": [ "## VAD" @@ -406,7 +468,6 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true, "id": "bL4kn4KJrlyL" }, "source": [ @@ -439,6 +500,7 @@ " force_reload=True)\n", "\n", "(get_speech_ts,\n", + " get_speech_ts_adaptive,\n", " save_audio,\n", " read_audio,\n", " state_generator,\n", @@ -461,23 +523,25 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true, - "hidden": true, "id": "5JHErdB7jsr0" }, "source": [ "### Full Audio" ] }, + { + "cell_type": "markdown", + "metadata": { + "id": "TNEtK5zi2Fw2" + }, + "source": [ + "**Classic way of getting speech chunks, you may need to select the thresholds yourself**" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:09:06.643812Z", - "start_time": "2020-12-15T13:09:06.473386Z" - }, - "hidden": true, "id": "krnGoA6Kjsr0" }, "outputs": [], @@ -494,11 +558,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:09:08.862421Z", - "start_time": "2020-12-15T13:09:08.820014Z" - }, - "hidden": true, "id": "B176Lzfnjsr1" }, "outputs": [], @@ -511,23 +570,63 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true, - "hidden": true, + "id": "21RE8KEC2Fw2" + }, + "source": [ + "**Experimental Adaptive method, algorithm selects thresholds itself (see readme for more information)**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "uIVs56rb2Fw2" + }, + "outputs": [], + "source": [ + "model = init_onnx_model(f'{files_dir}/model.onnx')\n", + "wav = read_audio(f'{files_dir}/en.wav')\n", + "\n", + "# get speech timestamps from full audio file\n", + "speech_timestamps = get_speech_ts_adaptive(wav, model, run_function=validate_onnx) \n", + "pprint(speech_timestamps)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "cox6oumC2Fw3" + }, + "outputs": [], + "source": [ + "# merge all speech chunks to one audio\n", + "save_audio('only_speech.wav', collect_chunks(speech_timestamps, wav), 16000)\n", + "Audio('only_speech.wav')" + ] + }, + { + "cell_type": "markdown", + "metadata": { "id": "Rio9W50gjsr1" }, "source": [ "### Single Audio Stream" ] }, + { + "cell_type": "markdown", + "metadata": { + "id": "i8EZwtaA2Fw3" + }, + "source": [ + "**Classic way of getting speech chunks, you may need to select the thresholds yourself**" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:09:09.606031Z", - "start_time": "2020-12-15T13:09:09.504239Z" - }, - "hidden": true, "id": "IPkl8Yy1jsr1" }, "outputs": [], @@ -540,11 +639,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:09:11.453171Z", - "start_time": "2020-12-15T13:09:09.633435Z" - }, - "hidden": true, "id": "NC6Jim0hjsr1" }, "outputs": [], @@ -554,11 +648,44 @@ " pprint(batch)" ] }, + { + "cell_type": "markdown", + "metadata": { + "id": "0pSKslpz2Fw3" + }, + "source": [ + "**Experimental Adaptive method, algorithm selects thresholds itself (see readme for more information)**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "RZwc-Khk2Fw4" + }, + "outputs": [], + "source": [ + "model = init_onnx_model(f'{files_dir}/model.onnx')\n", + "wav = f'{files_dir}/en.wav'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Z4lzFPs02Fw4" + }, + "outputs": [], + "source": [ + "for batch in single_audio_stream(model, wav, iterator_type='adaptive', run_function=validate_onnx):\n", + " if batch:\n", + " pprint(batch)" + ] + }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true, "id": "WNZ42u0ajsr1" }, "source": [ @@ -569,10 +696,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:09:11.540423Z", - "start_time": "2020-12-15T13:09:11.455706Z" - }, "hidden": true, "id": "XjhGQGppjsr1" }, @@ -587,10 +710,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:09:19.565434Z", - "start_time": "2020-12-15T13:09:11.552097Z" - }, "hidden": true, "id": "QI7-arlqjsr2" }, @@ -604,7 +723,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "id": "7QMvUvpg2Fw4" }, "source": [ "## Number detector" @@ -615,7 +735,7 @@ "metadata": { "heading_collapsed": true, "hidden": true, - "id": "bL4kn4KJrlyL" + "id": "tBPDkpHr2Fw4" }, "source": [ "### Install Dependencies" @@ -625,13 +745,9 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-30T17:25:19.107534Z", - "start_time": "2020-12-30T17:24:51.853293Z" - }, "cellView": "form", "hidden": true, - "id": "Q4QIfSpprnkI" + "id": "PdjGd56R2Fw5" }, "outputs": [], "source": [ @@ -675,7 +791,7 @@ "metadata": { "heading_collapsed": true, "hidden": true, - "id": "5JHErdB7jsr0" + "id": "I9QWSFZh2Fw5" }, "source": [ "### Full Audio" @@ -685,12 +801,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:09:06.643812Z", - "start_time": "2020-12-15T13:09:06.473386Z" - }, "hidden": true, - "id": "krnGoA6Kjsr0" + "id": "_r6QZiwu2Fw5" }, "outputs": [], "source": [ @@ -706,7 +818,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "FN4aDwLV2Fw5" }, "outputs": [], "source": [ @@ -721,12 +834,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-15T13:09:08.862421Z", - "start_time": "2020-12-15T13:09:08.820014Z" - }, "hidden": true, - "id": "B176Lzfnjsr1" + "id": "JnvS6WTK2Fw5" }, "outputs": [], "source": [ @@ -740,7 +849,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "yUxOcOFG2Fw6" }, "outputs": [], "source": [ @@ -753,7 +863,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "id": "SR8Bgcd52Fw6" }, "source": [ "## Language detector" @@ -764,7 +875,7 @@ "metadata": { "heading_collapsed": true, "hidden": true, - "id": "bL4kn4KJrlyL" + "id": "PBnXPtKo2Fw6" }, "source": [ "### Install Dependencies" @@ -774,13 +885,9 @@ "cell_type": "code", "execution_count": null, "metadata": { - "ExecuteTime": { - "end_time": "2020-12-30T17:25:19.107534Z", - "start_time": "2020-12-30T17:24:51.853293Z" - }, "cellView": "form", "hidden": true, - "id": "Q4QIfSpprnkI" + "id": "iNkDWJ3H2Fw6" }, "outputs": [], "source": [ @@ -819,9 +926,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true, "hidden": true, - "id": "5JHErdB7jsr0" + "id": "G8N8oP4q2Fw6" }, "source": [ "### Full Audio" @@ -831,7 +937,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "hidden": true + "hidden": true, + "id": "WHXnh9IV2Fw6" }, "outputs": [], "source": [ @@ -863,7 +970,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.8.8" }, "toc": { "base_numbering": 1, @@ -880,5 +987,5 @@ } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 0 }