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

IndexError when loading a model with shared sub-models #19326

Closed
torzdf opened this issue Mar 18, 2024 · 2 comments
Closed

IndexError when loading a model with shared sub-models #19326

torzdf opened this issue Mar 18, 2024 · 2 comments
Assignees
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:Bug

Comments

@torzdf
Copy link

torzdf commented Mar 18, 2024

Keras Version: 3.0.5 (PyPi latest)

When loading a model with 2 inputs and 3 sub-models (1 shared between 2 inputs, then 2 separate models to process each output from the shared model),, an IndexError is raised.

The model compiles, trains and saves fine, but the issue comes when trying to load the model back in.

The error occurs with both legacy .h5 format and new .keras format.

This kind of structure works in Keras 2.

Reproducible example (and gist):

from keras import Input, Model
from keras.models import load_model
from keras.layers import Dense

def shared():
    """ Shared model """
    inputs = Input(shape=(64, ))
    outputs = Dense(32)((inputs))
    return Model(inputs, outputs=outputs)


def split():
    """ Seperate models """
    inputs  = Input(shape=(32, ))
    outputs = Dense(32)(inputs)
    return Model(inputs, outputs=outputs)


# Bring them together
inputs = [Input((64, )), Input((64, ))]

shared_model = shared()  # Single instance of the shared model
shared_a = shared_model(inputs[0])
shared_b = shared_model(inputs[1])

out_a = split()(shared_a)  # instance 1 of the split model
out_b = split()(shared_b)  # instance 2 of the split model

# Full model
model = Model(inputs, outputs=[out_a, out_b])

print(model.summary())
model.save("test.keras")
test = load_model("test.keras")  # Fails with index out of range error

Model summary:

Model: "functional_7"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Layer (type)                  ┃ Output Shape              ┃         Param # ┃ Connected to               ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ input_layer (InputLayer)      │ (None, 64)                │               0 │ -                          │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ input_layer_1 (InputLayer)    │ (None, 64)                │               0 │ -                          │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ functional_1 (Functional)     │ (None, 32)                │           2,080 │ input_layer[0][0],         │
│                               │                           │                 │ input_layer_1[0][0]        │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ functional_3 (Functional)     │ (None, 32)                │           1,056 │ functional_1[0][0]         │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ functional_5 (Functional)     │ (None, 32)                │           1,056 │ functional_1[1][0]         │
└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘
 Total params: 4,192 (16.38 KB)
 Trainable params: 4,192 (16.38 KB)
 Non-trainable params: 0 (0.00 B)

Error output:

Traceback (most recent call last):
  File "/project/model_bug.py", line 34, in <module>
    test = load_model("test.keras")  # Fails with index out of range error
  File "/env/lib/python3.10/site-packages/keras/src/saving/saving_api.py", line 176, in load_model
    return saving_lib.load_model(
  File "/env/lib/python3.10/site-packages/keras/src/saving/saving_lib.py", line 155, in load_model
    model = deserialize_keras_object(
  File "/env/lib/python3.10/site-packages/keras/src/saving/serialization_lib.py", line 711, in deserialize_keras_object
    instance = cls.from_config(inner_config)
  File "/env/lib/python3.10/site-packages/keras/src/models/model.py", line 492, in from_config
    return functional_from_config(
  File "/env/lib/python3.10/site-packages/keras/src/models/functional.py", line 555, in functional_from_config
    output_tensors = map_tensors(config["output_layers"])
  File "/env/lib/python3.10/site-packages/keras/src/models/functional.py", line 552, in map_tensors
    return [get_tensor(*v) for v in tensors]
  File "/env/lib/python3.10/site-packages/keras/src/models/functional.py", line 552, in <listcomp>
    return [get_tensor(*v) for v in tensors]
  File "/env/lib/python3.10/site-packages/keras/src/models/functional.py", line 545, in get_tensor
    layer_output_tensors = layer._inbound_nodes[node_index].output_tensors
IndexError: list index out of range
@SuryanarayanaY
Copy link
Contributor

Hi @torzdf ,

I have replicated the issue with Keras3 and keras2 works fine. Attached reference gist.

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

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

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

5 participants