-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error deserializing saved model after Keras bump 3.6 -> 3.7 #20806
Comments
Hi @cloudbopper , Please try the below steps: 1.If you have custom loss function code then you can copy the code before reloading the model.
This should work probably. I don't think its related to the version. For example if you have a model with custom classes or functions then save the model and restart the session and try to reload the model in same version without adding code to custom object like mentioned above. The problem will be replicated. The root cause seems Keras not saving complete custom object implementation except class/function name and signature. For the function body/implementation it depends on the storage ID of this object locally. When a session restarted it will loose this storage. Hence we need to add the custom objects before reloading the model from May be providing option for serializing @mehtamansi29 , Correct me If I am mistaken in any thing I said. |
Yes, it works by adding I can confirm that saving the model with the function registered using the decorator You can see the responsible merge commit 0c2bdff and the associated pull request #20406 in the changelog for Keras 3.7: v3.6.0...v3.7.0. |
Hi @cloudbopper , Actually I have tested a custom model with Keras 3.6v, saved it and restarted the session and tried to reload the session. Got error in reloading the model if not added custom object code in new session. Please refer to attached colab gist.Same is true for other version also like 3.5v and 3.8v. So what I observed is without adding custom code I can't reload a saved model with custom objects(even with serialization registry), can't be reloaded in a new session or with a new version without explicitly adding the code for custom objects before reloading. Could you please verify the colab gist that I have attached and confirm whether you have not observed similar behavior? |
Hi @Surya2k1 and @cloudbopper - Here while saving the model with serialization with custom object need to use get_config and from_config method. |
Ok, it turns out that the deserialization only fails for custom loss functions, not activation functions, when loading models saved in 3.6 in 3.8. I modified @Surya2k1's gist to show this behavior: https://colab.research.google.com/gist/cloudbopper/0efab83644d74000ebac5a5025f2c37e/20806_3-6v2.ipynb. The last cell output shows the error. The reason appears to be serialization differences between loss functions: keras/keras/src/losses/__init__.py Line 139 in 734cd03
and activation functions: keras/keras/src/activations/__init__.py Line 95 in 734cd03
In Keras 3.6, custom loss functions were saved using only the function name as key, ignoring the package in the decorator. The PR #20406 that was merged into 3.7 fixed this, but also removed fallback code that was allowing deserialization to work in 3.6, leading to the error in 3.7 and 3.8. The serialization code for activation functions adds the package to the key using |
Hi @mehtamansi29 , That's what we have been discussing. The question is, for custom objects to reload, Is it the only way to add custom object code in order to reload the model? or Is there any other way? Suppose I have a saved model with custom objects and don't have access to custom object code can I able to reload the model? Could you please confirm on it? |
@Surya2k1 - While working with custom layers, activations and losses in model saving and reloading, custom objects is preferable for model saving(serializing) and reloading(deserializing) with subclass layer.
@cloudbopper - Here in your gist, while reloading the model in keras3.8.0, you are not using custom object code properly at here. |
@mehtamansi29 it looks like you replaced
with
for loading the model in both Keras versions. However, I'm just using the recommended method (registering custom objects using the
You can see in their example that registering custom objects avoids having to pass in
|
Agreed. No need to pass When we pass
You can see without explicit passing of custom objects it will have only This can be a workaround for loading saved model in 3.6V with I also have a probable fix in hand, but eventually it will become redundant(In Future versions when 3.6 become too old to use), which can avoid loading of |
Thanks! Yes, I was able to work around it by passing
|
…= 3.6. Fixes keras-team#20806 Issue was introduced by keras-team#20406
Fixes keras-team#20806 This a workaround for an incompatibility between 3.6 and 3.7 introduced by serialization bug fix keras-team#20406
Fixes keras-team#20806 This a workaround for an incompatibility between 3.6 and 3.7 introduced by serialization bug fix keras-team#20406
I'm encountering this error while loading a model saved with Keras 3.6 in Keras 3.7 and 3.8:
TypeError: Could not locate function 'attention_loss'. Make sure custom classes are decorated with `@keras.saving.register_keras_serializable()`. Full object config: {'module': 'builtins', 'class_name': 'function', 'config': 'attention_loss', 'registered_name': 'function'}
The decorator for the custom function:
I narrowed down the changes causing the error to 795df4e.
The function was serialized without the package name prior to 795df4e, so the removal of the fallback code means that the serialized function key can no longer be used to retrieve the registered object (key 'attention_loss>attention_loss')
The text was updated successfully, but these errors were encountered: