CTC丢失错误:找不到有效路径?Tf.keras中的OCR困难 [英] CTC Loss bug: no valid path found? OCR difficulties in Tf.keras
本文介绍了CTC丢失错误:找不到有效路径?Tf.keras中的OCR困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
全部。我正在尝试在这里获得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,
)
数据集已进行预处理。
如您所见,标签的尺寸小于日志。我知道,如果情况并非如此,则会出现";找不到有效路径的错误。
我做错了什么吗?请帮帮忙。 提前感谢您。
推荐答案
在ctc中,您需要比目标标签更多的隐藏状态。事实上,CTC学会了如何有效地使用特殊&空白&符号隔开目标标签,从而使标签与隐藏状态最匹配。但是,如果目标标签多于隐藏状态,则无法对齐它们。
在CNN中,您可能会对输入降维过多,隐藏状态序列太短。你应该重新考虑在CNN中如何填充和合并(这可能是更糟糕的想法),像CTC is used for machine translation那样进行一些状态拆分投影。
这篇关于CTC丢失错误:找不到有效路径?Tf.keras中的OCR困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文