8000 Unexpected file structure when serializing a model with layer called summary_layer · Issue #21166 · keras-team/keras · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Unexpected file structure when serializing a model with layer called summary_layer #21166

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

Open
niels-leif-bracher opened this issue Apr 15, 2025 · 2 comments
< 8000 div class="sticky-content">

Comments

@niels-leif-bracher
Copy link

Ordinarily, when serializing layers, we get a flat hierarchy in the resulting file. For example, the following code

@keras.saving.register_keras_serializable()
class TestModel(keras.Model):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.encoder = keras.layers.Dense(3)
        self.decoder = keras.layers.Dense(4)

    def build(self, input_shape):
        self.encoder.build(input_shape)
        self.decoder.build((None, 3))


model = TestModel()
model.build((None, 3))

keras.saving.save_model(model, "test_model.keras")
loaded = keras.saving.load_model("test_model.keras")

print(inspect_file("test_model.keras"))

when inspected with the tool described in #19705 results in the following file structure:

decoder

  • vars
    • ...

encoder

  • vars
    • ...

in the weights file. However, if a layer contains "summary" in its identifier:

@keras.saving.register_keras_serializable()
class TestModel(keras.Model):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.summary_layer = keras.layers.Dense(3)
        self.decoder = keras.layers.Dense(4)

    def build(self, input_shape):
        self.summary_layer.build(input_shape)
        self.decoder.build((None, 3))


model = TestModel()
model.build((None, 3))

keras.saving.save_model(model, "test_model.keras")
loaded = keras.saving.load_model("test_model.keras")

print(inspect_file("test_model.keras"))

an extra nesting level called "layers" is added:

decoder

  • vars
    • ...

layers

  • dense
    • vars
      • ...

Such a sudden change in file structure is unexpected for users like me who rely on a consistent layout.

@mehtamansi29
Copy link
Collaborator

Hi @niels-leif-bracher-

Thanks for reporting the issue. As we are not able to H5IOStore, here is the modified inspect_file function is there in this attached gist. Using that you can find the same result for saving and loading model.

Copy link
github-actions bot commented May 1, 2025

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label May 1, 2025
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

2 participants
0