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

Failure loading a Keras model after conversion from Sequential to Functional #19304

Closed
pyhopp opened this issue Mar 13, 2024 · 6 comments
Closed
Assignees
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:Bug

Comments

@pyhopp
Copy link

pyhopp commented Mar 13, 2024

Hi, this issue is observed on Tensorflow version 2.16 (it works as expected on version 2.15 and earlier).

Problem Description:
The following sequence causes a load_model error:

  1. Create a Sequential model, and save it
  2. Load the Sequential model
  3. Convert the sequential model to a functional model
  4. Save the functional model
  5. Load the function model ---> error

Reproducible example
`

code

seq = keras.Sequential(
    [
        keras.Input(shape=(3,)),
        keras.layers.Dense(5),
        keras.layers.Softmax(),
    ],
)

seq.save("keras_model.keras")

# Load sequential model
seq = keras.models.load_model("keras_model.keras")

# Create functional model
func = keras.Model(inputs=seq.inputs, outputs=seq.outputs)

# Save functional model
func.save("functional_model.keras")

# Load functional model
func = keras.models.load_model("functional_model.keras")`

Error output

func = keras.models.load_model("functional_model.keras") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/keras/src/saving/saving_api.py", line 176, in load_model return saving_lib.load_model( ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/keras/src/saving/saving_lib.py", line 155, in load_model model = deserialize_keras_object( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/keras/src/saving/serialization_lib.py", line 711, in deserialize_keras_object instance = cls.from_config(inner_config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/keras/src/models/model.py", line 492, in from_config return functional_from_config( ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/keras/src/models/functional.py", line 522, in functional_from_config process_node(layer, node_data) File "/usr/local/lib/python3.11/dist-packages/keras/src/models/functional.py", line 466, in process_node args, kwargs = deserialize_node(node_data, created_layers) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/keras/src/models/functional.py", line 666, in deserialize_node args = tree.map_structure(convert_revived_tensor, args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/tree/__init__.py", line 435, in map_structure [func(*args) for args in zip(*map(flatten, structures))]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/tree/__init__.py", line 435, in <listcomp> [func(*args) for args in zip(*map(flatten, structures))]) ^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/keras/src/models/functional.py", line 656, in convert_revived_tensor raise ValueError( ValueError: Layer node index out of bounds. inbound_layer = <Dense name=dense, built=True> inbound_layer._inbound_nodes = [<Node operation=<Dense name=dense, built=True>, id=140157363310928>] inbound_node_index = 1

Useful (?) observation

If when I create the functional model i pass all the outputs of the sequential model instead of just model.outputs then I get no errors (however I end up with a model that outputs the result of every layer).
outputs=[layer.output for layer in seq.outputs]

Please note that it doesn't matter whether the models are built/compiled/trained/ we still experience the error as described above.

@SuryanarayanaY
Copy link
Contributor

Hi @pyhopp ,

Thanks for reporting. Replicated the reported issue with Keras and attached gist for reference.

@SuryanarayanaY SuryanarayanaY added the keras-team-review-pending Pending review by a Keras team member. label Mar 18, 2024
@sampathweb sampathweb removed the keras-team-review-pending Pending review by a Keras team member. label Mar 28, 2024
@SuryanarayanaY SuryanarayanaY added the stat:awaiting keras-eng Awaiting response from Keras engineer label Apr 1, 2024
@fchollet
Copy link
Member

fchollet commented Apr 1, 2024

Thanks for the report! I fixed the issue at HEAD.

@fchollet fchollet closed this as completed Apr 1, 2024
Copy link

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

@pyhopp
Copy link
Author

pyhopp commented Apr 2, 2024

Thanks for the help!

Will the fix get rolled out in the next major (minor?) version update?

@fchollet
Copy link
Member

fchollet commented Apr 2, 2024

In the next pip release. Within a few weeks.

@pyhopp
Copy link
Author

pyhopp commented Apr 2, 2024

Great. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:Bug
Projects
None yet
Development

No branches or pull requests

4 participants