{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Speech-to-Text RNNT Malay + Singlish" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Encoder model + RNNT loss for Malay + Singlish" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This tutorial is available as an IPython notebook at [malaya-speech/example/stt-transducer-model-2mixed](https://github.com/huseinzol05/malaya-speech/tree/master/example/stt-transducer-model-2mixed).\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This module is not language independent, so it not save to use on different languages. Pretrained models trained on hyperlocal languages.\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This is an application of malaya-speech Pipeline, read more about malaya-speech Pipeline at [malaya-speech/example/pipeline](https://github.com/huseinzol05/malaya-speech/tree/master/example/pipeline).\n", " \n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.environ['CUDA_VISIBLE_DEVICES'] = ''" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "`pyaudio` is not available, `malaya_speech.streaming.stream` is not able to use.\n" ] } ], "source": [ "import malaya_speech\n", "import numpy as np\n", "from malaya_speech import Pipeline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import logging\n", "\n", "logging.basicConfig(level=logging.INFO)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List available RNNT model" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:malaya_speech.stt:for `malay-fleur102` language, tested on FLEURS102 `ms_my` test set, https://github.com/huseinzol05/malaya-speech/tree/master/pretrained-model/prepare-stt\n", "INFO:malaya_speech.stt:for `malay-malaya` language, tested on malaya-speech test set, https://github.com/huseinzol05/malaya-speech/tree/master/pretrained-model/prepare-stt\n", "INFO:malaya_speech.stt:for `singlish` language, tested on IMDA malaya-speech test set, https://github.com/huseinzol05/malaya-speech/tree/master/pretrained-model/prepare-stt\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Size (MB)Quantized Size (MB)malay-malayamalay-fleur102Languagesinglish
tiny-conformer24.49.14{'WER': 0.2128108, 'CER': 0.08136871, 'WER-LM'...{'WER': 0.2682816, 'CER': 0.13052725, 'WER-LM'...[malay]NaN
small-conformer49.218.1{'WER': 0.19853302, 'CER': 0.07449528, 'WER-LM...{'WER': 0.23412149, 'CER': 0.1138314813, 'WER-...[malay]NaN
conformer12537.1{'WER': 0.16340855635999124, 'CER': 0.05897205...{'WER': 0.20090442596, 'CER': 0.09616901, 'WER...[malay]NaN
large-conformer404107{'WER': 0.1566839, 'CER': 0.0619715, 'WER-LM':...{'WER': 0.1711028238, 'CER': 0.077953559, 'WER...[malay]NaN
conformer-stack-2mixed13038.5{'WER': 0.1889883954, 'CER': 0.0726845531, 'WE...{'WER': 0.244836948, 'CER': 0.117409327, 'WER-...[malay, singlish]{'WER': 0.08535878149, 'CER': 0.0452357273822,...
small-conformer-singlish49.218.1NaNNaN[singlish]{'WER': 0.087831, 'CER': 0.0456859, 'WER-LM': ...
conformer-singlish12537.1NaNNaN[singlish]{'WER': 0.07779246, 'CER': 0.0403616, 'WER-LM'...
large-conformer-singlish404107NaNNaN[singlish]{'WER': 0.07014733, 'CER': 0.03587201, 'WER-LM...
\n", "
" ], "text/plain": [ " Size (MB) Quantized Size (MB) \\\n", "tiny-conformer 24.4 9.14 \n", "small-conformer 49.2 18.1 \n", "conformer 125 37.1 \n", "large-conformer 404 107 \n", "conformer-stack-2mixed 130 38.5 \n", "small-conformer-singlish 49.2 18.1 \n", "conformer-singlish 125 37.1 \n", "large-conformer-singlish 404 107 \n", "\n", " malay-malaya \\\n", "tiny-conformer {'WER': 0.2128108, 'CER': 0.08136871, 'WER-LM'... \n", "small-conformer {'WER': 0.19853302, 'CER': 0.07449528, 'WER-LM... \n", "conformer {'WER': 0.16340855635999124, 'CER': 0.05897205... \n", "large-conformer {'WER': 0.1566839, 'CER': 0.0619715, 'WER-LM':... \n", "conformer-stack-2mixed {'WER': 0.1889883954, 'CER': 0.0726845531, 'WE... \n", "small-conformer-singlish NaN \n", "conformer-singlish NaN \n", "large-conformer-singlish NaN \n", "\n", " malay-fleur102 \\\n", "tiny-conformer {'WER': 0.2682816, 'CER': 0.13052725, 'WER-LM'... \n", "small-conformer {'WER': 0.23412149, 'CER': 0.1138314813, 'WER-... \n", "conformer {'WER': 0.20090442596, 'CER': 0.09616901, 'WER... \n", "large-conformer {'WER': 0.1711028238, 'CER': 0.077953559, 'WER... \n", "conformer-stack-2mixed {'WER': 0.244836948, 'CER': 0.117409327, 'WER-... \n", "small-conformer-singlish NaN \n", "conformer-singlish NaN \n", "large-conformer-singlish NaN \n", "\n", " Language \\\n", "tiny-conformer [malay] \n", "small-conformer [malay] \n", "conformer [malay] \n", "large-conformer [malay] \n", "conformer-stack-2mixed [malay, singlish] \n", "small-conformer-singlish [singlish] \n", "conformer-singlish [singlish] \n", "large-conformer-singlish [singlish] \n", "\n", " singlish \n", "tiny-conformer NaN \n", "small-conformer NaN \n", "conformer NaN \n", "large-conformer NaN \n", "conformer-stack-2mixed {'WER': 0.08535878149, 'CER': 0.0452357273822,... \n", "small-conformer-singlish {'WER': 0.087831, 'CER': 0.0456859, 'WER-LM': ... \n", "conformer-singlish {'WER': 0.07779246, 'CER': 0.0403616, 'WER-LM'... \n", "large-conformer-singlish {'WER': 0.07014733, 'CER': 0.03587201, 'WER-LM... " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "malaya_speech.stt.transducer.available_transformer()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load RNNT model\n", "\n", "```python\n", "def transformer(\n", " model: str = 'conformer',\n", " quantized: bool = False,\n", " **kwargs,\n", "):\n", " \"\"\"\n", " Load Encoder-Transducer ASR model.\n", "\n", " Parameters\n", " ----------\n", " model : str, optional (default='conformer')\n", " Check available models at `malaya_speech.stt.transducer.available_transformer()`.\n", " quantized : bool, optional (default=False)\n", " if True, will load 8-bit quantized model.\n", " Quantized model not necessary faster, totally depends on the machine.\n", "\n", " Returns\n", " -------\n", " result : malaya_speech.model.transducer.Transducer class\n", " \"\"\"\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2023-02-01 11:47:45.718834: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", "2023-02-01 11:47:45.723521: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected\n", "2023-02-01 11:47:45.723540: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: husein-MS-7D31\n", "2023-02-01 11:47:45.723543: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: husein-MS-7D31\n", "2023-02-01 11:47:45.723621: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: Not found: was unable to find libcuda.so DSO loaded into this program\n", "2023-02-01 11:47:45.723640: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 470.161.3\n" ] } ], "source": [ "model = malaya_speech.stt.transducer.transformer(model = 'conformer-stack-2mixed')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load Quantized deep model\n", "\n", "To load 8-bit quantized model, simply pass `quantized = True`, default is `False`.\n", "\n", "We can expect slightly accuracy drop from quantized model, and not necessary faster than normal 32-bit float model, totally depends on machine." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "quantized_model = malaya_speech.stt.transducer.transformer(model = 'conformer-stack-2mixed', quantized = True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load sample" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "ceramah, sr = malaya_speech.load('speech/khutbah/wadi-annuar.wav')\n", "record1, sr = malaya_speech.load('speech/record/savewav_2020-11-26_22-36-06_294832.wav')\n", "record2, sr = malaya_speech.load('speech/record/savewav_2020-11-26_22-40-56_929661.wav')\n", "singlish0, sr = malaya_speech.load('speech/singlish/singlish0.wav')\n", "singlish1, sr = malaya_speech.load('speech/singlish/singlish1.wav')\n", "singlish2, sr = malaya_speech.load('speech/singlish/singlish2.wav')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import IPython.display as ipd\n", "\n", "ipd.Audio(ceramah, rate = sr)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ipd.Audio(record1, rate = sr)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ipd.Audio(record2, rate = sr)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ipd.Audio(singlish0, rate = sr)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ipd.Audio(singlish1, rate = sr)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ipd.Audio(singlish2, rate = sr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Predict using greedy decoder\n", "\n", "```python\n", "def greedy_decoder(self, inputs):\n", " \"\"\"\n", " Transcribe inputs using greedy decoder.\n", "\n", " Parameters\n", " ----------\n", " inputs: List[np.array]\n", " List[np.array] or List[malaya_speech.model.frame.Frame].\n", "\n", " Returns\n", " -------\n", " result: List[str]\n", " \"\"\"\n", "```" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 10.9 s, sys: 3.22 s, total: 14.2 s\n", "Wall time: 9.24 s\n" ] }, { "data": { "text/plain": [ "['jadi dalam perjalanan ini dunia yang susah ini ketika nabi mengajar muaz bin jabal tadi ni allahu',\n", " 'helo nama saya mesin saya tak suka mandi ke tak saya masak',\n", " 'helo nama saya husin saya suka mandi saya mandi titik hari',\n", " 'and then see how they roll it in film okay actually',\n", " 'then you talk to your eyes',\n", " 'surprise in ma']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "model.greedy_decoder([ceramah, record1, record2, singlish0, singlish1, singlish2])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 11 s, sys: 3.28 s, total: 14.3 s\n", "Wall time: 9.53 s\n" ] }, { "data": { "text/plain": [ "['jadi dalam perjalanan ini dunia yang susah ini ketika nabi mengajar muaz bin jabal tadi ni allah mah',\n", " 'hello nama saya usin saya tak suka mandi ketat saya masam',\n", " 'hello nama saya husin saya suka mandi saya mandi titik hari',\n", " 'and then see how they roll it in film okay actually',\n", " 'then you tell to your eyes',\n", " 'seven seven in mall']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "quantized_model.greedy_decoder([ceramah, record1, record2, singlish0, singlish1, singlish2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Predict using beam decoder\n", "\n", "```python\n", "def beam_decoder(self, inputs, beam_width: int = 5,\n", " temperature: float = 0.0,\n", " score_norm: bool = True):\n", " \"\"\"\n", " Transcribe inputs using beam decoder.\n", "\n", " Parameters\n", " ----------\n", " inputs: List[np.array]\n", " List[np.array] or List[malaya_speech.model.frame.Frame].\n", " beam_width: int, optional (default=5)\n", " beam size for beam decoder.\n", " temperature: float, optional (default=0.0)\n", " apply temperature function for logits, can help for certain case,\n", " logits += -np.log(-np.log(uniform_noise_shape_logits)) * temperature\n", " score_norm: bool, optional (default=True)\n", " descending sort beam based on score / length of decoded.\n", "\n", " Returns\n", " -------\n", " result: List[str]\n", " \"\"\"\n", "```" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 22.3 s, sys: 3.5 s, total: 25.8 s\n", "Wall time: 13.8 s\n" ] }, { "data": { "text/plain": [ "['jadi dalam perjalanan ini dunia yang susah ini ketika nabi mengajar muaz bin jabal tadi ni allah mah',\n", " 'helo nama saya usin saya tak suka mandi ketat saya masam',\n", " 'helo nama saya husin saya suka mandi saya mandi titik hari',\n", " 'and then see how they roll it in film okay actually',\n", " 'then you tell to your eyes',\n", " 'some person in mao']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "model.beam_decoder([ceramah, record1, record2, singlish0, singlish1, singlish2], beam_width = 5)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 21.2 s, sys: 3.14 s, total: 24.3 s\n", "Wall time: 12.6 s\n" ] }, { "data": { "text/plain": [ "['jadi dalam perjalanan ini dunia yang susah ini ketika nabi mengajar muaz bin jabal tadi ni allah mak',\n", " 'helo nama saya husin saya tak suka mandi ketat saya masam',\n", " 'helo nama saya husin saya suka mandi saya mandi titik hari',\n", " 'and then see how they roll it in film okay actually',\n", " 'then you tell to your eyes',\n", " 'some person in mao']" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "quantized_model.beam_decoder([ceramah, record1, record2, singlish0, singlish1, singlish2], beam_width = 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**RNNT model beam decoder not able to utilise batch programming, if feed a batch, it will process one by one**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Predict alignment\n", "\n", "We want to know when the speakers speak certain words, so we can use `predict_timestamp`,\n", "\n", "```python\n", "def predict_alignment(self, input, combined = True):\n", " \"\"\"\n", " Transcribe input and get timestamp, only support greedy decoder.\n", "\n", " Parameters\n", " ----------\n", " input: np.array\n", " np.array or malaya_speech.model.frame.Frame.\n", " combined: bool, optional (default=True)\n", " If True, will combined subwords to become a word.\n", "\n", " Returns\n", " -------\n", " result: List[Dict[text, start, end]]\n", " \"\"\"\n", "```" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 6.09 s, sys: 2.16 s, total: 8.25 s\n", "Wall time: 7.3 s\n" ] }, { "data": { "text/plain": [ "[{'text': 'and', 'start': 0.2, 'end': 0.21},\n", " {'text': 'then', 'start': 0.36, 'end': 0.45},\n", " {'text': 'see', 'start': 0.6, 'end': 0.77},\n", " {'text': 'how', 'start': 0.88, 'end': 1.09},\n", " {'text': 'they', 'start': 1.36, 'end': 1.49},\n", " {'text': 'roll', 'start': 1.92, 'end': 2.09},\n", " {'text': 'it', 'start': 2.2, 'end': 2.21},\n", " {'text': 'in', 'start': 2.4, 'end': 2.41},\n", " {'text': 'film', 'start': 2.6, 'end': 2.85},\n", " {'text': 'okay', 'start': 3.68, 'end': 3.85},\n", " {'text': 'actually', 'start': 3.92, 'end': 4.21}]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "model.predict_alignment(singlish0)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.07 s, sys: 165 ms, total: 1.24 s\n", "Wall time: 284 ms\n" ] }, { "data": { "text/plain": [ "[{'text': 'and', 'start': 0.2, 'end': 0.21},\n", " {'text': ' ', 'start': 0.28, 'end': 0.29},\n", " {'text': 'the', 'start': 0.36, 'end': 0.37},\n", " {'text': 'n_', 'start': 0.44, 'end': 0.45},\n", " {'text': 'se', 'start': 0.6, 'end': 0.61},\n", " {'text': 'e_', 'start': 0.76, 'end': 0.77},\n", " {'text': 'ho', 'start': 0.88, 'end': 0.89},\n", " {'text': 'w_', 'start': 1.08, 'end': 1.09},\n", " {'text': 'the', 'start': 1.36, 'end': 1.37},\n", " {'text': 'y_', 'start': 1.48, 'end': 1.49},\n", " {'text': 'ro', 'start': 1.92, 'end': 1.93},\n", " {'text': 'll_', 'start': 2.08, 'end': 2.09},\n", " {'text': 'it_', 'start': 2.2, 'end': 2.21},\n", " {'text': 'in_', 'start': 2.4, 'end': 2.41},\n", " {'text': 'fi', 'start': 2.6, 'end': 2.61},\n", " {'text': 'l', 'start': 2.76, 'end': 2.77},\n", " {'text': 'm_', 'start': 2.84, 'end': 2.85},\n", " {'text': 'oka', 'start': 3.68, 'end': 3.69},\n", " {'text': 'y_', 'start': 3.84, 'end': 3.85},\n", " {'text': 'ac', 'start': 3.92, 'end': 3.93},\n", " {'text': 'tu', 'start': 4.04, 'end': 4.05},\n", " {'text': 'all', 'start': 4.16, 'end': 4.17},\n", " {'text': 'y', 'start': 4.2, 'end': 4.21}]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "model.predict_alignment(singlish0, combined = False)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }