CTC Loss 错误:找不到有效路径? Tf.keras 中的 OCR 困难

CTC Loss bug: no valid path found? OCR difficulties in Tf.keras

全部。我试图在这里获得 CTC 损失函数,但效果不是很好。我一直收到这个错误:

2020-11-04 07:28:53.647946: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
2020-11-04 07:28:53.647977: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
2020-11-04 07:28:53.648009: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
2020-11-04 07:28:53.647992: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
2020-11-04 07:28:53.648021: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
2020-11-04 07:28:53.648063: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
2020-11-04 07:28:53.648052: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
2020-11-04 07:28:53.648074: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
2020-11-04 07:28:53.648080: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
2020-11-04 07:28:53.648308: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.

我在互联网上搜索了这方面的信息,但一无所获。

这是它的代码:

    def loss_fn(self, y_true, y_pred):

        batch_len = tf.keras.backend.cast(tf.shape(y_true)[0], dtype="int64")
        input_length = tf.keras.backend.cast(tf.shape(y_pred)[1], dtype="int64") #Comes out to be 30
        label_length = tf.keras.backend.cast(tf.shape(y_true)[1], dtype="int64") #Comes out to be 25

        input_length = 30 * tf.ones(shape=(batch_len, 1), dtype="int64") #Just hardcoded 30 for now
        label_length = 25 * tf.ones(shape=(batch_len, 1), dtype="int64") #Just hardcoded 25 for now



        y_true = tf.keras.layers.Softmax()(y_true)
        y_pred = tf.keras.layers.Softmax()(y_pred)

        print("y_true shape %s" %y_true.shape) #Outputs y_true shape (32, 25)
        print(y_true) #outputs Tensor("loss_fn/softmax/Softmax:0", shape=(32, 25), dtype=float32)

        print("y_pred shape %s" %y_pred.shape) #Outputs y_pred shape (32, 30, 67)
        print(y_pred) #outputs Tensor("loss_fn/softmax_1/Softmax:0", shape=(32, 30, 67), dtype=float32)

        loss = tf.keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length)
        return tf.reduce_mean(loss)

这里正在调用损失函数:

...

    def ResNet:
        ...

        out = tf.keras.layers.Reshape((out.shape[2], out.shape[3]))(out)
        print("out %s" %out.shape) #Comes out to be: out (None, 30, 768)

        weight_initializer = tf.keras.initializers.he_uniform()
        bias_initializer = tf.keras.initializers.constant()

        logits = tf.keras.layers.Dense(67, kernel_initializer=weight_initializer, bias_initializer=bias_initializer, name="logits")(out)
        print("logits %s" %logits.shape) #Comes out to be: logits (None, 30, 67)

        print("________________________")
        print(logits)

        model = tf.keras.Model(inputs=[input, labels], outputs=logits, name="full_model")
        model.compile(optimizer="RMSprop", loss=self.loss_fn)
        print(model.summary())

调用这个的主函数:

...
...
    d = dataset.Dataset(confs)
    train_data = d.read_data(confs["trn_data_files"])
    valid_data = d.read_data(confs["val_data_files"])
    callbacks = [
        tf.keras.callbacks.ModelCheckpoint("./model_checkpoint", monitor="val_loss")
    ]

    for x,y in train_data:
        history = model.fit(
            x=x,
            y=y,
            validation_data=valid_data,
            epochs=50,
            callbacks=callbacks,
        )

数据集进行了预处理。

如您所见,标签的尺寸小于 logits。我知道如果不是这种情况,则会发生“找不到有效路径”错误。

我是不是做错了什么?请帮忙。 非常感谢您。

在 CTC 中,您需要拥有比目标标签更多的隐藏状态。事实上,CTC 学习了如何有效地插入带有特殊“空白”符号的目标标签,因此标签与隐藏状态最匹配。然而,当你的目标标签多于隐藏状态时,你就无法对齐它们。

在 CNN 中,您可能将输入的维度降低太多,隐藏状态序列太短。您应该重新考虑如何在 CNN 中进行填充和池化,或者(这可能是更糟糕的想法),在 CTC is used for machine translation.

时进行一些状态分裂投影