-
Notifications
You must be signed in to change notification settings - Fork 118
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
Ensure backwards compatibility for legacy H5 saving format #682
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
2cbb158
Add saved_model_test
nkovela1 4a7a84e
Sync with main repo
nkovela1 f524725
Add extra saved model tests
nkovela1 8920368
Fix formatting
nkovela1 31013f5
Merge branch 'keras-team:main' into savedmodel
nkovela1 9842a2b
Merge remote-tracking branch 'refs/remotes/origin/h5' into h5
nkovela1 22a1d82
Add JSON utils for legacy saving
nkovela1 59e0426
Implement H5 saving for Keras core
nkovela1 e91f539
Change saving API routing
nkovela1 374fd33
Fix h5 format basic tests
nkovela1 3f60730
Ensure compile reload works with H5 format
nkovela1 7b49cfa
Remove useless comments
nkovela1 12eb4dc
Fix imports and formatting
nkovela1 690dc63
Move json_utils out of saved_model folder
nkovela1 a9929b1
Add test for set_weights in optimizer
nkovela1 ee19187
Fix formatting
nkovela1 e877c39
Add keras options scope to replace use_legacy_config attribute
nkovela1 2f809bc
Add options scope for legacy serialization, remove circular deps
nkovela1 1a61315
Add comments for legacy serialization code routing
nkovela1 3c670a7
Move saving/legacy to legacy/saving
nkovela1 c0cbe0a
Change keras options scope to use global state attr
nkovela1 0366717
Sync with main
nkovela1 079fa1e
Merge branch 'keras-team:main' into h5
nkovela1 a7f74b5
Add backwards compatibility for H5 saving
nkovela1 d416797
Fix formatting and add comments for helpers
nkovela1 46a1fab
Update helper comments
nkovela1 6f16fa7
Update helper comments
nkovela1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
"""Legacy serialization logic for Keras models.""" | ||
|
||
import contextlib | ||
import inspect | ||
import json | ||
import threading | ||
import weakref | ||
|
||
|
@@ -484,6 +484,12 @@ def deserialize(config, custom_objects=None): | |
arg_spec = inspect.getfullargspec(cls.from_config) | ||
custom_objects = custom_objects or {} | ||
|
||
# TODO(nkovela): Swap find and replace args during Keras 3.0 release | ||
# Replace keras refs with keras_core | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After the swap, the replacement should go the other way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, fixed the TODO comment. |
||
cls_config = _find_replace_nested_dict( | ||
cls_config, "keras.", "keras_core." | ||
) | ||
|
||
if "custom_objects" in arg_spec.args: | ||
deserialized_obj = cls.from_config( | ||
cls_config, | ||
|
@@ -558,3 +564,10 @@ def validate_config(config): | |
def is_default(method): | ||
"""Check if a method is decorated with the `default` wrapper.""" | ||
return getattr(method, "_is_default", False) | ||
|
||
|
||
def _find_replace_nested_dict(config, find, replace): | ||
dict_str = json.dumps(config) | ||
dict_str = dict_str.replace(find, replace) | ||
config = json.loads(dict_str) | ||
return config |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future
tf.keras
will be Keras Core. So can we avoid depending on it, and instead depend on a static artifact? Maybe either downloaded or checked into git?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, however, for now as I develop and expand this testing suite I will keep this dependency for debugging purposes/changes. After the full testing suite is done (in the next PR or so), I will generate them as h5 artifacts and add them to that PR.