keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.06k stars 19.35k forks source link

TypeError when loading Keras 2 legacy models with nested outputs in Keras 3 #19670

Closed torzdf closed 1 week ago

torzdf commented 1 week ago

This issue relates to #19669

Nested outputs are not supported in Keras 3, but are in Keras 2. There is currently no code to support loading models with this structure in Keras 3, so the loading process errors out (see PR for more details).

The PR looks to address this, however in order to be able to test this fix, a model generated in Keras 2 needs to be used to ensure that the fix works as expected.

The attached model, with deliberately overly nested outputs, is built with the following definition in Keras 2 (gist):


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

inputs = Input((2, ))

dense1 = Dense(2, name="dense1")(inputs)
dense2 = Dense(2, name="dense2")(dense1)
dense3 = Dense(2, name="dense3")(dense2)
dense4 = Dense(2, name="dense4")(dense3)
dense5 = Dense(2, name="dense5")(dense4)

model = Model(inputs, outputs = [[dense1, [dense2], [dense3, dense4]], dense5])
print(model.summary())
model.save("keras2_nested_outputs.h5")

*Note, this model will not build in Keras 3

keras2_nested_outputs.zip

The following gist will load this saved model into Keras 3, then error when processing the model outputs:


from keras.src.utils import get_file
from keras.src.saving import load_model

legacy_model_file = get_file("keras2_nested_outputs.h5",
                             "https://github.com/keras-team/keras/files/15215057/keras2_nested_outputs.zip",
                             file_hash="f324c55586df3429f4ce4c725740714a2ffd4ce26fa1954643b89ba89e233c86",
                             extract=True)
model = load_model(legacy_model_file)  # Fails with TypeError
SuryanarayanaY commented 1 week ago

Hi @torzdf ,

Thanks for reporting the issue and raising a PR for fix. Will keep this issue till the outcome of PR #19669

torzdf commented 1 week ago

Resolved by https://github.com/keras-team/keras/pull/19669#issuecomment-2096770559