keras-io icon indicating copy to clipboard operation
keras-io copied to clipboard

CTC Batch Cost error while migrating "handwriting_recognition" example to Keras 3

Open simeetnayan81 opened this issue 1 year ago • 2 comments

Issue Type

Bug

Source

source

Keras Version

Keras 3

Custom Code

Yes

OS Platform and Distribution

No response

Python version

No response

GPU model and memory

No response

Current Behavior?

I was trying to migrate "handwriting_recognition" example to keras 3. The example requires ctc endpoint layer for ctc batch loss calcultaion.

AttributeError: module 'keras.backend' has no attribute 'ctc_batch_cost'

This is the error I recieved.

Code for ctc layer class. This is same as what was present in original example.

Standalone code to reproduce the issue or tutorial link

class CTCLayer(keras.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):
        batch_len = keras.ops.cast(tf.shape(y_true)[0], dtype="int64")
        input_length = keras.ops.cast(tf.shape(y_pred)[1], dtype="int64")
        label_length = keras.ops.cast(tf.shape(y_true)[1], dtype="int64")

        input_length = input_length * keras.ops.ones(shape=(batch_len, 1), dtype="int64")
        label_length = label_length * keras.ops.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

Relevant log output

<ipython-input-26-0d4ad6c98297> in build_model()
     69 
     70     # Add CTC layer for calculating CTC loss at each step.
---> 71     output = CTCLayer(name="ctc_loss")(labels, x)
     72 
     73     # Define the model.

<ipython-input-26-0d4ad6c98297> in __init__(self, name)
      2     def __init__(self, name=None):
      3         super().__init__(name=name)
----> 4         self.loss_fn = keras.backend.ctc_batch_cost
      5 
      6     def call(self, y_true, y_pred):

AttributeError: module 'keras.backend' has no attribute 'ctc_batch_cost'

simeetnayan81 avatar Jan 09 '24 06:01 simeetnayan81