实施CTC loss keras

Implimenting CTC loss keras

考虑到您有一个与此类似的基本模型:

input_layer = layers.Input(shape=(50,20))
layer = layers.Dense(123, activation = 'relu')
layer = layers.LSTM(128, return_sequences = True)(layer)
outputs = layers.Dense(20, activation='softmax')(layer)
model = Model(input_layer,outputs)

您将如何实施 CTC 损失?我在 OCR 上尝试了一些来自 keras 代码教程的内容,如下所示:

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
labels = layers.Input(shape=(None,), dtype="float32")
input_layer = layers.Input(shape=(50,20))
layer = layers.Dense(123, activation = 'relu')
layer = layers.LSTM(128, return_sequences = True)(layer)
outputs = layers.Dense(20, activation='softmax')(layer)
output = CTCLayer()(labels,outputs)
model = Model(input_layer,outputs)

然而,当谈到 model.fit 部分时,由于我不知道如何为模型提供“标签”输入层的东西,它开始分崩离析。我认为本教程中的方法非常明确,那么实现 CTC 损失的更好、更有效的方法是什么?

你唯一做错的是模型创建 model = Model(input_layer,outputs) 它应该是 model = Model([input_layer,labels],output) 说你也可以编译模型 tf.nn.ctc_loss 作为损失,如果你不这样做想要 2 个输入

def my_loss_fn(y_true, y_pred):
  loss_value = tf.nn.ctc_loss(y_true, y_pred, y_true_length, y_pred_length, 
  logits_time_major = False)
  return tf.reduce_mean(loss_value, axis=-1)

model.compile(optimizer='adam', loss=my_loss_fn)

类似这样,请注意,上面的代码未经过测试,您需要找到 y_pred 和 y_true 长度,但您可以像在 ctc 层中那样做