adding CaptchaReaderTest

This commit is contained in:
frankknoll
2023-03-15 14:47:07 +01:00
parent 7bdd20c650
commit 6330b7b724
1062 changed files with 617 additions and 369 deletions

27
src/captcha/CTCLayer.py Normal file
View File

@@ -0,0 +1,27 @@
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# see https://keras.io/guides/making_new_layers_and_models_via_subclassing/
class CTCLayer(layers.Layer):
def __init__(self, name=None):
super().__init__(name=name)
self.loss_fn = keras.backend.ctc_batch_cost
def call(self, y_true, y_pred):
# Compute the training-time loss value and add it
# to the layer using `self.add_loss()`.
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 = self.loss_fn(y_true, y_pred, input_length, label_length)
self.add_loss(loss)
# At test time, just return the computed predictions
return y_pred