text
stringlengths 0
4.99k
|
---|
# 1. Read wav file |
file = tf.io.read_file(wavs_path + wav_file + \".wav\") |
# 2. Decode the wav file |
audio, _ = tf.audio.decode_wav(file) |
audio = tf.squeeze(audio, axis=-1) |
# 3. Change type to float |
audio = tf.cast(audio, tf.float32) |
# 4. Get the spectrogram |
spectrogram = tf.signal.stft( |
audio, frame_length=frame_length, frame_step=frame_step, fft_length=fft_length |
) |
# 5. We only need the magnitude, which can be derived by applying tf.abs |
spectrogram = tf.abs(spectrogram) |
spectrogram = tf.math.pow(spectrogram, 0.5) |
# 6. normalisation |
means = tf.math.reduce_mean(spectrogram, 1, keepdims=True) |
stddevs = tf.math.reduce_std(spectrogram, 1, keepdims=True) |
spectrogram = (spectrogram - means) / (stddevs + 1e-10) |
########################################### |
## Process the label |
########################################## |
# 7. Convert label to Lower case |
label = tf.strings.lower(label) |
# 8. Split the label |
label = tf.strings.unicode_split(label, input_encoding=\"UTF-8\") |
# 9. Map the characters in label to numbers |
label = char_to_num(label) |
# 10. Return a dict as our model is expecting two inputs |
return spectrogram, label |
Creating Dataset objects |
We create a tf.data.Dataset object that yields the transformed elements, in the same order as they appeared in the input. |
batch_size = 32 |
# Define the trainig dataset |
train_dataset = tf.data.Dataset.from_tensor_slices( |
(list(df_train[\"file_name\"]), list(df_train[\"normalized_transcription\"])) |
) |
train_dataset = ( |
train_dataset.map(encode_single_sample, num_parallel_calls=tf.data.AUTOTUNE) |
.padded_batch(batch_size) |
.prefetch(buffer_size=tf.data.AUTOTUNE) |
) |
# Define the validation dataset |
validation_dataset = tf.data.Dataset.from_tensor_slices( |
(list(df_val[\"file_name\"]), list(df_val[\"normalized_transcription\"])) |
) |
validation_dataset = ( |
validation_dataset.map(encode_single_sample, num_parallel_calls=tf.data.AUTOTUNE) |
.padded_batch(batch_size) |
.prefetch(buffer_size=tf.data.AUTOTUNE) |
) |
Visualize the data |
Let's visualize an example in our dataset, including the audio clip, the spectrogram and the corresponding label. |
fig = plt.figure(figsize=(8, 5)) |
for batch in train_dataset.take(1): |
spectrogram = batch[0][0].numpy() |
spectrogram = np.array([np.trim_zeros(x) for x in np.transpose(spectrogram)]) |
label = batch[1][0] |
# Spectrogram |
label = tf.strings.reduce_join(num_to_char(label)).numpy().decode(\"utf-8\") |
ax = plt.subplot(2, 1, 1) |
ax.imshow(spectrogram, vmax=1) |
ax.set_title(label) |
ax.axis(\"off\") |
# Wav |
file = tf.io.read_file(wavs_path + list(df_train[\"file_name\"])[0] + \".wav\") |
audio, _ = tf.audio.decode_wav(file) |
audio = audio.numpy() |
ax = plt.subplot(2, 1, 2) |
plt.plot(audio) |
ax.set_title(\"Signal Wave\") |
ax.set_xlim(0, len(audio)) |
display.display(display.Audio(np.transpose(audio), rate=16000)) |
plt.show() |
2021-09-28 21:16:34.014170: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2) |
png |
Model |
We first define the CTC Loss function. |
def CTCLoss(y_true, y_pred): |
# Compute the training-time loss value |
batch_len = tf.cast(tf.shape(y_true)[0], dtype=\"int64\") |
input_length = tf.cast(tf.shape(y_pred)[1], dtype=\"int64\") |
label_length = tf.cast(tf.shape(y_true)[1], dtype=\"int64\") |
input_length = input_length * tf.ones(shape=(batch_len, 1), dtype=\"int64\") |
label_length = label_length * tf.ones(shape=(batch_len, 1), dtype=\"int64\") |
loss = keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length) |
return loss |
We now define our model. We will define a model similar to DeepSpeech2. |
def build_model(input_dim, output_dim, rnn_layers=5, rnn_units=128): |
\"\"\"Model similar to DeepSpeech2.\"\"\" |
# Model's input |
input_spectrogram = layers.Input((None, input_dim), name=\"input\") |