implementing Loading Saved Model in TensorFlow 2.12: ValueError with Custom Objects
I've been struggling with this for a few days now and could really use some help. I'm having trouble loading a saved model in TensorFlow 2.12 that uses a custom layer. When I attempt to load the model using `tf.keras.models.load_model()`, I get a `ValueError` stating that the custom object is not defined. I have registered the custom layer using the following code: ```python class MyCustomLayer(tf.keras.layers.Layer): def __init__(self, units=32): super(MyCustomLayer, self).__init__() self.units = units def call(self, inputs): return tf.nn.relu(tf.matmul(inputs, self.kernel)) def build(self, input_shape): self.kernel = self.add_weight(shape=(input_shape[-1], self.units), initializer='random_normal', trainable=True) # Custom object dictionary custom_objects = {'MyCustomLayer': MyCustomLayer} ``` I saved the model with: ```python model.save('my_model.h5') ``` And when I attempt to load it, I use: ```python loaded_model = tf.keras.models.load_model('my_model.h5', custom_objects=custom_objects) ``` However, I encounter the following behavior: ``` ValueError: Unknown layer: MyCustomLayer ``` I confirmed that `MyCustomLayer` is defined in the same script where I perform the loading operation. I also tried passing the `custom_objects` argument directly in the `load_model` function, but that didn’t help either. Have I missed something in the way I’m defining or loading the custom layer, or is there an scenario with the TensorFlow version? Any insights would be greatly appreciated! Any ideas what could be causing this?