text
stringlengths 0
4.99k
|
---|
Training a CTC-based model for automatic speech recognition. |
Introduction |
Speech recognition is an interdisciplinary subfield of computer science and computational linguistics that develops methodologies and technologies that enable the recognition and translation of spoken language into text by computers. It is also known as automatic speech recognition (ASR), computer speech recognition or speech to text (STT). It incorporates knowledge and research in the computer science, linguistics and computer engineering fields. |
This demonstration shows how to combine a 2D CNN, RNN and a Connectionist Temporal Classification (CTC) loss to build an ASR. CTC is an algorithm used to train deep neural networks in speech recognition, handwriting recognition and other sequence problems. CTC is used when we don鈥檛 know how the input aligns with the output (how the characters in the transcript align to the audio). The model we create is similar to DeepSpeech2. |
We will use the LJSpeech dataset from the LibriVox project. It consists of short audio clips of a single speaker reading passages from 7 non-fiction books. |
We will evaluate the quality of the model using Word Error Rate (WER). WER is obtained by adding up the substitutions, insertions, and deletions that occur in a sequence of recognized words. Divide that number by the total number of words originally spoken. The result is the WER. To get the WER score you need to install the jiwer package. You can use the following command line: |
pip install jiwer |
References: |
LJSpeech Dataset |
Speech recognition |
Sequence Modeling With CTC |
DeepSpeech2 |
Setup |
import pandas as pd |
import numpy as np |
import tensorflow as tf |
from tensorflow import keras |
from tensorflow.keras import layers |
import matplotlib.pyplot as plt |
from IPython import display |
from jiwer import wer |
Load the LJSpeech Dataset |
Let's download the LJSpeech Dataset. The dataset contains 13,100 audio files as wav files in the /wavs/ folder. The label (transcript) for each audio file is a string given in the metadata.csv file. The fields are: |
ID: this is the name of the corresponding .wav file |
Transcription: words spoken by the reader (UTF-8) |
Normalized transcription: transcription with numbers, ordinals, and monetary units expanded into full words (UTF-8). |
For this demo we will use on the \"Normalized transcription\" field. |
Each audio file is a single-channel 16-bit PCM WAV with a sample rate of 22,050 Hz. |
data_url = \"https://data.keithito.com/data/speech/LJSpeech-1.1.tar.bz2\" |
data_path = keras.utils.get_file(\"LJSpeech-1.1\", data_url, untar=True) |
wavs_path = data_path + \"/wavs/\" |
metadata_path = data_path + \"/metadata.csv\" |
# Read metadata file and parse it |
metadata_df = pd.read_csv(metadata_path, sep=\"|\", header=None, quoting=3) |
metadata_df.columns = [\"file_name\", \"transcription\", \"normalized_transcription\"] |
metadata_df = metadata_df[[\"file_name\", \"normalized_transcription\"]] |
metadata_df = metadata_df.sample(frac=1).reset_index(drop=True) |
metadata_df.head(3) |
file_name normalized_transcription |
0 LJ042-0218 to the entire land and complete foundations of... |
1 LJ004-0218 a week's allowance at a time, was abolished, a... |
2 LJ005-0151 in others women were very properly exempted fr... |
We now split the data into training and validation set. |
split = int(len(metadata_df) * 0.90) |
df_train = metadata_df[:split] |
df_val = metadata_df[split:] |
print(f\"Size of the training set: {len(df_train)}\") |
print(f\"Size of the training set: {len(df_val)}\") |
Size of the training set: 11790 |
Size of the training set: 1310 |
Preprocessing |
We first prepare the vocabulary to be used. |
# The set of characters accepted in the transcription. |
characters = [x for x in \"abcdefghijklmnopqrstuvwxyz'?! \"] |
# Mapping characters to integers |
char_to_num = keras.layers.StringLookup(vocabulary=characters, oov_token=\"\") |
# Mapping integers back to original characters |
num_to_char = keras.layers.StringLookup( |
vocabulary=char_to_num.get_vocabulary(), oov_token=\"\", invert=True |
) |
print( |
f\"The vocabulary is: {char_to_num.get_vocabulary()} \" |
f\"(size ={char_to_num.vocabulary_size()})\" |
) |
The vocabulary is: ['', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', \"'\", '?', '!', ' '] (size =31) |
2021-09-28 21:16:33.150832: 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 AVX512F FMA |
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. |
2021-09-28 21:16:33.692813: W tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:39] Overriding allow_growth setting because the TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original config value was 0. |
2021-09-28 21:16:33.692847: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 9124 MB memory: -> device: 0, name: GeForce RTX 2080 Ti, pci bus id: 0000:65:00.0, compute capability: 7.5 |
Next, we create the function that describes the transformation that we apply to each element of our dataset. |
# An integer scalar Tensor. The window length in samples. |
frame_length = 256 |
# An integer scalar Tensor. The number of samples to step. |
frame_step = 160 |
# An integer scalar Tensor. The size of the FFT to apply. |
# If not provided, uses the smallest power of 2 enclosing frame_length. |
fft_length = 384 |
def encode_single_sample(wav_file, label): |
########################################### |
## Process the Audio |
########################################## |