TypeError when loading Keras 2 legacy models with nested outputs in Keras 3
torzdf opened this issue · 2 comments
torzdf commented
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
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
torzdf commented
Resolved by #19669 (comment)