Skip to content
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

keras-3: saving weights in legacy h5 format fails for custom layer containing two sub-layers of the same type. #19375

Closed
agreenbe0505 opened this issue Mar 25, 2024 · 8 comments

Comments

@agreenbe0505
Copy link

Keras-3 assigns short names to weights, which is handy, but when it saves weights in legacy h5 format it picks names as:
weight_names = [w.name.encode("utf8") for w in weights]

instead of:
weight_names = [w.value.name.encode("utf8") for w in weights] # old naming

The file is: /keras/src/legacy/saving/legacy_h5_format.py
function: save_subset_weights_to_hdf5_group()

See the stack trace and a sample for the failure reproduction.

stack.log

reproduction.py.txt

@sachinprasadhs
Copy link
Collaborator

Hi, As the warning suggests, HDF5 is a legacy format, it is suggested to use .keras for saving the model.

WARNING:absl:You are saving your model as an HDF5 file via model.save() or keras.saving.save_model(model). This file format is considered legacy. We recommend using instead the native Keras format, e.g. model.save('my_model.keras') or keras.saving.save_model(model, 'my_model.keras').

If you change the code model_file = "/tmp/model_weights.hdf5" to model_file = "/tmp/model_weights.keras" it will solve the issue. Thanks

@agreenbe0505
Copy link
Author

Do you say by this that the old keras2 saving format is unsupported in keras3?

@sachinprasadhs
Copy link
Collaborator

Do you say by this that the old keras2 saving format is unsupported in keras3?

It is supported, but it is suggested to use .keras format since it works seamlessly.

@agreenbe0505
Copy link
Author

agreenbe0505 commented Apr 9, 2024

OK, we could live with that, loading old models saved in legacy format and saving them in the new format.
But this also does not work when legacy custom layers exist in the model.
Attached 2 short snippets to demonstrate the problem.
One which stores the model is running in tf-2.8
The other which loads ther model is running in keras-3 tf-2.15
The load crashes with the error: AttributeError: 'CircleLayer' object has no attribute '_self_tracked_trackables'
load_legacy_model.py.txt
save_legacy_model.py.txt

@sachinprasadhs
Copy link
Collaborator

Saving in older version and loading it in Keras 3 may not work as expected, it is highly recommended to save the model in .keras and load it back in the same format, since it has all the model config details.
Note: From Keras 3, the only saving format is .keras

@fchollet
Copy link
Member

Thanks for the report. I have fixed the issue with the legacy h5 format at HEAD.

Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@sachinprasadhs
Copy link
Collaborator

Reference commit: 2c30d86

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants