我正在尝试为我的手写文本识别模型构建一个用于损失计算的 CTC 层,但遇到这些错误
TypeError:调用CTC_Layer.call()时遇到异常。
传递给参数“indices”的值的数据类型 float32 不在允许值列表中:uint8、int8、int32、int64
class CTC_Layer(Layer):
def __init__(self, name=None):
super(CTC_Layer, self).__init__(name='ctc_loss')
self.loss_fn = tensorflow.nn.ctc_loss
def call(self, y_true, y_pred):
batch_length = tf.cast(tf.shape(y_true)[0], "int64")
input_length = tf.cast(tf.shape(y_pred)[1], "int64")
label_length = tf.cast(tf.shape(y_true)[1], "int64")
input_length = input_length * tf.ones(shape=(batch_length,), dtype="int64")
label_length = label_length * tf.ones(shape=(batch_length,), dtype="int64")
loss = self.loss_fn(y_true, y_pred, input_length, label_length)
self.add_loss(loss)
return y_pred
手写文本识别模型由 2-D CNN、双向 LSTM 组成
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[133], line 1
----> 1 model = build_model()
2 model.summary()
Cell In[132], line 31, in build_model()
24 x = layers.Bidirectional(
25 layers.LSTM(64, return_sequences=True, dropout=0.25)
26 )(x)
28 x = layers.Dense(
29 len((char_to_num.get_vocabulary()))+2, activation='softmax', name='dense2'
30 )(x)
---> 31 outputs = CTC_Layer(name="ctc_loss")(labels, x)
34 model = keras.models.Model(inputs=[input_image, labels], outputs=outputs)
36 model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001))
File /opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
119 filtered_tb = _process_traceback_frames(e.__traceback__)
120 # To get the full stack trace, call:
121 # `keras.config.disable_traceback_filtering()`
--> 122 raise e.with_traceback(filtered_tb) from None
123 finally:
124 del filtered_tb
Cell In[93], line 15, in CTC_Layer.call(self, y_true, y_pred)
12 input_length = input_length * tf.ones(shape=(batch_length,), dtype="int64")
13 label_length = label_length * tf.ones(shape=(batch_length,), dtype="int64")
---> 15 loss = self.loss_fn(y_true, y_pred, input_length, label_length)
16 self.add_loss(loss)
18 # At test time, just return the computed predictions.
TypeError: Exception encountered when calling CTC_Layer.call().
Value passed to parameter 'indices' has DataType float32 not in list of allowed values: uint8, int8, int32, int64
Arguments received by CTC_Layer.call():
• args=('<KerasTensor shape=(None, None), dtype=float32, sparse=None, name=label>', '<KerasTensor shape=(None, 32, 79), dtype=float32, sparse=False, name=keras_tensor_317>')
• kwargs=<class 'inspect._empty'>]
有人知道如何解决这个问题吗?预先感谢!
在您的
CTC_Layer.call
函数中,在将标签输入损失函数之前,将它们转换为适当的整数类型。这是更新:
def call(self, y_true, y_pred):
# ... (rest of your code)
# Cast labels to int32 (choose appropriate type if needed)
y_true = tf.cast(y_true, dtype="int32")
# ... (rest of your code)