Open Senantq opened 3 months ago
Hi @Senantq, could you try calling model directly via model(...) rather than model.call? Additionally I see that model.build
was called without an input shape.
Could you also supply the original source code of the model rather than just the .keras
file?
Hi @nkovela1 and thank you for your help. I have updated the reprex, added the model code into it, and made it a bit more explicit. Do not hesitate to ask if anything's still lacking. Have a nice day,
@Senantq
Isn't this the same issue as fixed here?
https://github.com/keras-team/keras/issues/20155#issue-2482970454
You may try using keras-nightly
I have just installed keras-nightly 3.5.0.dev2024090403 in a new conda environment, and it does'nt solve the problem unfortunately
model = keras.Sequential([
Input(shape=(10, 10, 3)),
Conv2D(filters=32, kernel_size=3),
])
model_2 = Model([model.inputs], [model.output]) # <--change
errors in a similar way.
output
is an output tensor, not a layer. One can use outputs
which returns a KerasTensor with a history (layer information.)
Specifically, model.outputs[-1]
or the get layer equivalent.
Hello ghsanti and thank you for answer. I must admit that it is still not totally clear to me. Particularly, I don't see why
from tensorflow import keras
model = keras.Sequential([
keras.Input(shape=(10, 10, 3)),
keras.layers.Conv2D(filters=32, kernel_size=3)])
model_2 = keras.Model([model.inputs], [model.output])
worked with keras 2 (installed via tensorflow==2.10) and not with the newer versions. Have I missed something during the change to keras 3? I don't see it listed as one of the new major releases features at https://github.com/keras-team/keras/issues/18467 Plus, it feels like it breaks a of not that old codes such as saliency methods or transfer learning codes.
@Senantq
This works
model = keras.Sequential([ Input(shape=(4, 4, 3)), Flatten(), Dense(units=5)])
model(keras.Input((4, 4, 3)))
model_2 = keras.Model([model.inputs], [model.output])
This too:
i= Input(shape=(4, 4, 3))
x= Flatten()(i)
x= Dense(units=5)(x)
model1 = keras.Model(i, x)
model2 = keras.Model([model1.inputs], [model1.output])
Sequential needs the extra call to build the layers.
I agree that the error message should be at least a bit more explicit if possible. In any case, thank for the time spent.
I am currently using tensorflow 2.17 with keras 3.4.1 under Ubuntu 24.04 LTS. I have also reproduced the issue with tf-nightly 2.18.0.dev20240731 (keras nightly 3.4.1.dev2024073103).
I encountered the issue when i downloaded a model I have ran on a cluster under tf 2.17/keras 3.4.1. I then tried to obtain some saliency maps on my computer after re-building the model without redifining all its layers from scratch.
See the following google drive for a reprex with the code, model and a data sample: https://drive.google.com/drive/folders/15J_ghWXWbs8EmSVXedH6sJRvJcPUSTIW?usp=sharing
But it raises the following traceback:
There is two workarounds where the value error is not raised: 1°) When using grad_model = keras.models.Model( [model.inputs], [model.get_layer(last_conv_layer_name).output, model.get_layer(Name_of_last_deep_layer).output]) but it results in none gradients in the rest of my code 2°) When redifining completely the model from scratch and loading only the weights, i.e., when using:
model.load_weights(...) -> this one doesn't raises any error
Thanks a lot!