ValueError: Unknown layer: Custom>CTCLayer. Please ensure this object is passed to the `custom_objects` argument

Viewed 675

I would like to use the trained model described in Keras's Handwriting recognition Example, in another application and tried to load the model with the following;

from keras.models import load_model
from tensorflow import keras

model = keras.models.load_model("test4_20211113.h5", custom_objects={'CTCLayer': CTCLayer}) 

I received "ValueError: Unknown layer: Custom>CTCLayer. Please ensure this object is passed to the custom_objects argument."

I added the custom_objects argument and modified the CTCLayer class by adding **kwargs following this article, "ValueError: Unknown layer: CapsuleLayer".

class CTCLayer(keras.layers.Layer):
    def __init__(self, name=None, **kwargs):
        self.name = name
        super().__init__(**kwargs)
        self.loss_fn = keras.backend.ctc_batch_cost

    def call(self, y_true, y_pred):
        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

I'm a beginner in both Python and Keras, and greatly appreciate it if you let me know how to fix this.

0 Answers
Related