text
stringlengths
0
4.99k
dropout_3 (Dropout) (None, None, 1024) 0
______________________________________________________________________________________________________________
bidirectional_5 (Bidirectional) (None, None, 1024) 4724736
______________________________________________________________________________________________________________
dense_1 (Dense) (None, None, 1024) 1049600
______________________________________________________________________________________________________________
dense_1_relu (ReLU) (None, None, 1024) 0
______________________________________________________________________________________________________________
dropout_4 (Dropout) (None, None, 1024) 0
______________________________________________________________________________________________________________
dense (Dense) (None, None, 32) 32800
==============================================================================================================
Total params: 26,628,480
Trainable params: 26,628,352
Non-trainable params: 128
______________________________________________________________________________________________________________
Training and Evaluating
# A utility function to decode the output of the network
def decode_batch_predictions(pred):
input_len = np.ones(pred.shape[0]) * pred.shape[1]
# Use greedy search. For complex tasks, you can use beam search
results = keras.backend.ctc_decode(pred, input_length=input_len, greedy=True)[0][0]
# Iterate over the results and get back the text
output_text = []
for result in results:
result = tf.strings.reduce_join(num_to_char(result)).numpy().decode(\"utf-8\")
output_text.append(result)
return output_text
# A callback class to output a few transcriptions during training
class CallbackEval(keras.callbacks.Callback):
\"\"\"Displays a batch of outputs after every epoch.\"\"\"
def __init__(self, dataset):
super().__init__()
self.dataset = dataset
def on_epoch_end(self, epoch: int, logs=None):
predictions = []
targets = []
for batch in self.dataset:
X, y = batch
batch_predictions = model.predict(X)
batch_predictions = decode_batch_predictions(batch_predictions)
predictions.extend(batch_predictions)
for label in y:
label = (
tf.strings.reduce_join(num_to_char(label)).numpy().decode(\"utf-8\")
)
targets.append(label)
wer_score = wer(targets, predictions)
print(\"-\" * 100)
print(f\"Word Error Rate: {wer_score:.4f}\")
print(\"-\" * 100)
for i in np.random.randint(0, len(predictions), 2):
print(f\"Target : {targets[i]}\")
print(f\"Prediction: {predictions[i]}\")
print(\"-\" * 100)
Let's start the training process.
# Define the number of epochs.
epochs = 1
# Callback function to check transcription on the val set.
validation_callback = CallbackEval(validation_dataset)
# Train the model
history = model.fit(
train_dataset,
validation_data=validation_dataset,
epochs=epochs,
callbacks=[validation_callback],
)
2021-09-28 21:16:48.067448: I tensorflow/stream_executor/cuda/cuda_dnn.cc:369] Loaded cuDNN version 8100
369/369 [==============================] - 586s 2s/step - loss: 300.4624 - val_loss: 296.1459
----------------------------------------------------------------------------------------------------
Word Error Rate: 0.9998
----------------------------------------------------------------------------------------------------
Target : the procession traversed ratcliffe twice halting for a quarter of an hour in front of the victims' dwelling
Prediction: s
----------------------------------------------------------------------------------------------------
Target : some difficulty then arose as to gaining admission to the strong room and it was arranged that a man may another custom house clerk
Prediction: s
----------------------------------------------------------------------------------------------------
Inference
# Let's check results on more validation samples
predictions = []
targets = []
for batch in validation_dataset:
X, y = batch
batch_predictions = model.predict(X)
batch_predictions = decode_batch_predictions(batch_predictions)
predictions.extend(batch_predictions)
for label in y:
label = tf.strings.reduce_join(num_to_char(label)).numpy().decode(\"utf-8\")
targets.append(label)
wer_score = wer(targets, predictions)
print(\"-\" * 100)
print(f\"Word Error Rate: {wer_score:.4f}\")
print(\"-\" * 100)